Skip to content

Commit b27ee88

Browse files
committed
Expose a TOTAL_GMEM and FREE_GMEM property.
1 parent 6d28fbf commit b27ee88

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎pygpu/gpuarray.pxd‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ cdef extern from "gpuarray/buffer.h":
8080
int GA_CTX_PROP_NUMPROCS
8181
int GA_CTX_PROP_MAXGSIZE
8282
int GA_CTX_PROP_BIN_ID
83+
int GA_CTX_PROP_TOTAL_GMEM
84+
int GA_CTX_PROP_FREE_GMEM
8385
int GA_BUFFER_PROP_CTX
8486
int GA_KERNEL_PROP_CTX
8587
int GA_KERNEL_PROP_MAXLSIZE

‎pygpu/gpuarray.pyx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,21 @@ cdef class GpuContext:
10471047
ctx_property(self, GA_CTX_PROP_BIN_ID, &res)
10481048
return res;
10491049

1050+
property total_gmem:
1051+
"Total size of global memory on the device"
1052+
def __get__(self):
1053+
cdef size_t res
1054+
ctx_property(self, GA_CTX_PROP_TOTAL_GMEM, &res)
1055+
return res
1056+
1057+
property free_gmem:
1058+
"Size of free global memory on the device"
1059+
def __get__(self):
1060+
cdef size_t res
1061+
ctx_property(self, GA_CTX_PROP_FREE_GMEM, &res)
1062+
return res
1063+
1064+
10501065
cdef class flags(object):
10511066
cdef int fl
10521067

‎src/gpuarray/buffer.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,21 @@ typedef struct _gpuarray_buffer_ops {
554554
*/
555555
#define GA_CTX_PROP_ERRBUF 8
556556

557+
/**
558+
* Get the total size of global memory on the device.
559+
*
560+
* Type: `size_t`
561+
*/
562+
#define GA_CTX_PROP_TOTAL_GMEM 9
563+
564+
/**
565+
* Get the size of free global memory on the device.
566+
*
567+
* Type: `size_t`
568+
*/
569+
#define GA_CTX_PROP_FREE_GMEM 10
570+
571+
557572
/* Start at 512 for GA_BUFFER_PROP_ */
558573
/**
559574
* Get the context in which this buffer was allocated.

‎src/gpuarray_buffer_cuda.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,7 @@ static int cuda_property(void *c, gpudata *buf, gpukernel *k, int prop_id,
16721672
char *s;
16731673
CUdevice id;
16741674
int i;
1675+
size_t sz;
16751676

16761677
case GA_CTX_PROP_DEVNAME:
16771678
cuda_enter(ctx);
@@ -1781,6 +1782,18 @@ static int cuda_property(void *c, gpudata *buf, gpukernel *k, int prop_id,
17811782
*((gpudata **)res) = ctx->errbuf;
17821783
return GA_NO_ERROR;
17831784

1785+
case GA_CTX_PROP_TOTAL_GMEM:
1786+
cuda_enter(ctx);
1787+
ctx->err = cuMemGetInfo(&sz, (size_t *)res);
1788+
cuda_exit(ctx);
1789+
return ctx->err == CUDA_SUCCESS ? GA_NO_ERROR : GA_IMPL_ERROR;
1790+
1791+
case GA_CTX_PROP_FREE_GMEM:
1792+
cuda_enter(ctx);
1793+
ctx->err = cuMemGetInfo((size_t *)res, &sz);
1794+
cuda_exit(ctx);
1795+
return ctx->err == CUDA_SUCCESS ? GA_NO_ERROR : GA_IMPL_ERROR;
1796+
17841797
case GA_BUFFER_PROP_REFCNT:
17851798
*((unsigned int *)res) = buf->refcnt;
17861799
return GA_NO_ERROR;

0 commit comments

Comments
 (0)