Skip to content

Commit 82558da

Browse files
authored
Merge pull request #312 from abergeron/mem_if
Stop returning allocated memory from the library to avoid problems on windows
2 parents dd9593e + 29c8e1e commit 82558da

File tree

5 files changed

+18
-67
lines changed

5 files changed

+18
-67
lines changed

pygpu/gpuarray.pyx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,28 +1036,18 @@ cdef class GpuContext:
10361036
property devname:
10371037
"Device name for this context"
10381038
def __get__(self):
1039-
cdef char *tmp
1040-
cdef unicode res
1039+
cdef char tmp[256]
10411040

1042-
ctx_property(self, GA_CTX_PROP_DEVNAME, &tmp)
1043-
try:
1044-
res = tmp.decode('ascii')
1045-
finally:
1046-
free(tmp)
1047-
return res
1041+
ctx_property(self, GA_CTX_PROP_DEVNAME, tmp)
1042+
return tmp.decode('ascii')
10481043

10491044
property pcibusid:
10501045
"Device PCI Bus ID for this context"
10511046
def __get__(self):
1052-
cdef char *tmp
1053-
cdef unicode res
1047+
cdef char tmp[16]
10541048

1055-
ctx_property(self, GA_CTX_PROP_PCIBUSID, &tmp)
1056-
try:
1057-
res = tmp.decode('ascii')
1058-
finally:
1059-
free(tmp)
1060-
return res
1049+
ctx_property(self, GA_CTX_PROP_PCIBUSID, tmp)
1050+
return tmp.decode('ascii')
10611051

10621052
property maxlsize:
10631053
"Maximum size of thread block (local size) for this context"

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ set_target_properties(gpuarray PROPERTIES
8888
INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib
8989
MACOSX_RPATH OFF
9090
# This is the shared library version
91-
VERSION 0.1
91+
VERSION 1.0
9292
)
9393

9494
add_library(gpuarray-static STATIC ${GPUARRAY_SRC})

src/gpuarray/buffer.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,9 @@ GPUARRAY_PUBLIC int gpukernel_call(gpukernel *k, unsigned int n,
494494
size_t shared, void **args);
495495

496496
/**
497-
* Get the kernel binary.
497+
* (Deprecated) Get the kernel binary.
498+
*
499+
* This function is deprecated and will be removed in the next release.
498500
*
499501
* This can be use to cache kernel binaries after compilation of a
500502
* specific device. The kernel can be recreated by calling
@@ -537,9 +539,7 @@ GPUARRAY_PUBLIC gpucontext *gpukernel_context(gpukernel *k);
537539
/**
538540
* Get the device name for the context.
539541
*
540-
* \note The returned string is allocated and must be freed by the caller.
541-
*
542-
* Type: `char *`
542+
* Type: `char [256]`
543543
*/
544544
#define GA_CTX_PROP_DEVNAME 1
545545

@@ -683,9 +683,7 @@ GPUARRAY_PUBLIC gpucontext *gpukernel_context(gpukernel *k);
683683
/**
684684
* Get the device PCI Bus ID for the context.
685685
*
686-
* \note The returned string is allocated and must be freed by the caller.
687-
*
688-
* Type: `char *`
686+
* Type: `char [16]`
689687
*/
690688
#define GA_CTX_PROP_PCIBUSID 19
691689

src/gpuarray_buffer_cuda.c

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,6 @@ static int cuda_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
13891389
}
13901390

13911391
switch (prop_id) {
1392-
char *s;
13931392
CUdevice id;
13941393
int i;
13951394
size_t sz;
@@ -1401,21 +1400,9 @@ static int cuda_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
14011400
cuda_exit(ctx);
14021401
return GA_IMPL_ERROR;
14031402
}
1404-
/* 256 is what the CUDA API uses so it's good enough for me */
1405-
s = malloc(256);
1406-
if (s == NULL) {
1407-
cuda_exit(ctx);
1408-
return GA_MEMORY_ERROR;
1409-
}
1410-
ctx->err = cuDeviceGetName(s, 256, id);
1411-
if (ctx->err != CUDA_SUCCESS) {
1412-
free(s);
1413-
cuda_exit(ctx);
1414-
return GA_IMPL_ERROR;
1415-
}
1416-
*((char **)res) = s;
1403+
ctx->err = cuDeviceGetName((char *)res, 256, id);
14171404
cuda_exit(ctx);
1418-
return GA_NO_ERROR;
1405+
return (ctx->err != CUDA_SUCCESS) ? GA_IMPL_ERROR : GA_NO_ERROR;
14191406

14201407
case GA_CTX_PROP_PCIBUSID:
14211408
cuda_enter(ctx);
@@ -1424,20 +1411,9 @@ static int cuda_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
14241411
cuda_exit(ctx);
14251412
return GA_IMPL_ERROR;
14261413
}
1427-
s = malloc(13);
1428-
if (s == NULL) {
1429-
cuda_exit(ctx);
1430-
return GA_MEMORY_ERROR;
1431-
}
1432-
ctx->err = cuDeviceGetPCIBusId(s, 13, id);
1433-
if (ctx->err != CUDA_SUCCESS) {
1434-
free(s);
1435-
cuda_exit(ctx);
1436-
return GA_IMPL_ERROR;
1437-
}
1438-
*((char **)res) = s;
1414+
ctx->err = cuDeviceGetPCIBusId((char *)res, 13, id);
14391415
cuda_exit(ctx);
1440-
return GA_NO_ERROR;
1416+
return (ctx->err != CUDA_SUCCESS) ? GA_IMPL_ERROR : GA_NO_ERROR;
14411417

14421418
case GA_CTX_PROP_LARGEST_MEMBLOCK:
14431419
*((size_t *)res) = largest_size(ctx);

src/gpuarray_buffer_opencl.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,6 @@ static int cl_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
11571157
}
11581158

11591159
switch (prop_id) {
1160-
char *s;
11611160
size_t sz;
11621161
size_t *psz;
11631162
cl_device_id id;
@@ -1168,23 +1167,11 @@ static int cl_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
11681167
&id, NULL);
11691168
if (ctx->err != CL_SUCCESS)
11701169
return GA_IMPL_ERROR;
1171-
ctx->err = clGetDeviceInfo(id, CL_DEVICE_NAME, 0, NULL, &sz);
1172-
if (ctx->err != CL_SUCCESS)
1173-
return GA_IMPL_ERROR;
1174-
s = malloc(sz);
1175-
if (s == NULL)
1176-
return GA_MEMORY_ERROR;
1177-
ctx->err = clGetDeviceInfo(id, CL_DEVICE_NAME, sz, s, NULL);
1178-
if (ctx->err != CL_SUCCESS) {
1179-
free(s);
1180-
return GA_IMPL_ERROR;
1181-
}
1182-
*((char **)res) = s;
1183-
return GA_NO_ERROR;
1170+
ctx->err = clGetDeviceInfo(id, CL_DEVICE_NAME, 256, (char *)res, NULL);
1171+
return (ctx->err != CL_SUCCESS) ? GA_IMPL_ERROR : GA_NO_ERROR;
11841172

11851173
case GA_CTX_PROP_PCIBUSID:
11861174
/* For the moment, PCI Bus ID is not supported for OpenCL. */
1187-
*((void **)res) = NULL;
11881175
return GA_DEVSUP_ERROR;
11891176

11901177
case GA_CTX_PROP_MAXLSIZE:

0 commit comments

Comments
 (0)