Skip to content

Commit

Permalink
Merge pull request #526 from notoraptor/fix-memory-leak-libgpuarray
Browse files Browse the repository at this point in the history
Fix some memory leaks in libgpuarray
  • Loading branch information
abergeron authored Sep 13, 2017
2 parents 39d49b2 + 06b2d4e commit fdf790b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
21 changes: 12 additions & 9 deletions pygpu/gpuarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1431,17 +1431,20 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims,
raise MemoryError, "could not allocate cdims"

cdef size_t d
for i in range(nd):
d = newdims[i]
if i == caxis:
d = a.size // tot
try:
for i in range(nd):
d = newdims[i]
if i == caxis:
d = a.size // tot

if d * tot != a.size:
raise GpuArrayException, "..."
cdims[i] = d
if d * tot != a.size:
raise GpuArrayException, "..."
cdims[i] = d

array_reshape(res, a, nd, cdims, ord, nocopy)
return res
array_reshape(res, a, nd, cdims, ord, nocopy)
return res
finally:
free(cdims)


cdef GpuArray pygpu_transpose(GpuArray a, const unsigned int *newaxes):
Expand Down
11 changes: 9 additions & 2 deletions src/cache/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,15 @@ static int key_path(disk_cache *c, const cache_key_t key, char *out) {
unsigned char hash[64];
int i;

if (c->kwrite(&kb, key)) return -1;
if (Skein_512((unsigned char *)kb.s, kb.l, hash)) return -1;
if (c->kwrite(&kb, key)) {
strb_clear(&kb);
return -1;
}
if (Skein_512((unsigned char *)kb.s, kb.l, hash)) {
strb_clear(&kb);
return -1;
}
strb_clear(&kb);
if (snprintf(out, 10, "%02x%02x/%02x%02x",
hash[0], hash[1], hash[2], hash[3]) != 9)
return -1;
Expand Down
8 changes: 5 additions & 3 deletions src/gpuarray_buffer_cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,8 @@ static int compile(cuda_context *ctx, strb *src, strb* bin, strb *log) {

GA_CHECK(make_bin(ctx, &ptx, bin, log));

strb_clear(&ptx);

if (ctx->disk_cache) {
pk = calloc(sizeof(disk_key), 1);
if (pk == NULL) {
Expand All @@ -1234,23 +1236,23 @@ static int compile(cuda_context *ctx, strb *src, strb* bin, strb *log) {
memcpy(pk, &k, DISK_KEY_MM);
strb_appendb(&pk->src, src);
if (strb_error(&pk->src)) {
error_sys(ctx->err, "strb_appendb");
error_sys(ctx->err, "strb_appendb");
fprintf(stderr, "Error adding kernel to disk cache %s\n",
ctx->err->msg);
disk_free((cache_key_t)pk);
return GA_NO_ERROR;
}
cbin = strb_alloc(bin->l);
if (cbin == NULL) {
error_sys(ctx->err, "strb_alloc");
error_sys(ctx->err, "strb_alloc");
fprintf(stderr, "Error adding kernel to disk cache: %s\n",
ctx->err->msg);
disk_free((cache_key_t)pk);
return GA_NO_ERROR;
}
strb_appendb(cbin, bin);
if (strb_error(cbin)) {
error_sys(ctx->err, "strb_appendb");
error_sys(ctx->err, "strb_appendb");
fprintf(stderr, "Error adding kernel to disk cache %s\n",
ctx->err->msg);
disk_free((cache_key_t)pk);
Expand Down
18 changes: 12 additions & 6 deletions src/gpuarray_elemwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,18 +738,24 @@ GpuElemwise *GpuElemwise_new(gpucontext *ctx,

void GpuElemwise_free(GpuElemwise *ge) {
unsigned int i;
for (i = 0; i < ge->nd; i++) {
if (k_initialized(&ge->k_basic_32[i]))
GpuKernel_clear(&ge->k_basic_32[i]);
if (k_initialized(&ge->k_basic[i]))
GpuKernel_clear(&ge->k_basic[i]);
}
if (ge->k_basic_32 != NULL)
for (i = 0; i < ge->nd; i++) {
if (k_initialized(&ge->k_basic_32[i]))
GpuKernel_clear(&ge->k_basic_32[i]);
}
if (ge->k_basic != NULL)
for (i = 0; i < ge->nd; i++) {
if (k_initialized(&ge->k_basic[i]))
GpuKernel_clear(&ge->k_basic[i]);
}
if (ge->strides != NULL)
for (i = 0; i < ge->narray; i++) {
free(ge->strides[i]);
}
if (k_initialized(&ge->k_contig))
GpuKernel_clear(&ge->k_contig);
free(ge->k_basic_32);
free(ge->k_basic);
free_args(ge->n, ge->args);
free((void *)ge->preamble);
free((void *)ge->expr);
Expand Down

0 comments on commit fdf790b

Please sign in to comment.