From edf31c8aaed054c4a5fc278e8efe70b6484b417f Mon Sep 17 00:00:00 2001 From: notoraptor Date: Tue, 12 Sep 2017 15:42:27 -0400 Subject: [PATCH 1/5] Try Try to fix some memory leaks. --- pygpu/gpuarray.pyx | 3 +++ src/cache/disk.c | 11 +++++++++-- src/gpuarray_elemwise.c | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pygpu/gpuarray.pyx b/pygpu/gpuarray.pyx index 44608e1e4f..f582e05c4a 100644 --- a/pygpu/gpuarray.pyx +++ b/pygpu/gpuarray.pyx @@ -1428,6 +1428,7 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims, tot *= newdims[i] cdims = calloc(nd, sizeof(size_t)) if cdims == NULL: + free(cdims) raise MemoryError, "could not allocate cdims" cdef size_t d @@ -1437,10 +1438,12 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims, d = a.size // tot if d * tot != a.size: + free(cdims) raise GpuArrayException, "..." cdims[i] = d array_reshape(res, a, nd, cdims, ord, nocopy) + free(cdims) return res diff --git a/src/cache/disk.c b/src/cache/disk.c index 6fda751b30..0f9de82e0a 100644 --- a/src/cache/disk.c +++ b/src/cache/disk.c @@ -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; diff --git a/src/gpuarray_elemwise.c b/src/gpuarray_elemwise.c index 3ba31d30fe..fa5b0efa2c 100644 --- a/src/gpuarray_elemwise.c +++ b/src/gpuarray_elemwise.c @@ -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); From 613ab5997c7b25895ed38e27c9bc7293fa05d318 Mon Sep 17 00:00:00 2001 From: notoraptor Date: Wed, 13 Sep 2017 08:40:14 -0400 Subject: [PATCH 2/5] Try to fix another memory leak. --- src/gpuarray_buffer_cuda.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/gpuarray_buffer_cuda.c b/src/gpuarray_buffer_cuda.c index 9a99d749c9..99b9bbee31 100644 --- a/src/gpuarray_buffer_cuda.c +++ b/src/gpuarray_buffer_cuda.c @@ -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) { @@ -1234,7 +1236,7 @@ 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); @@ -1242,7 +1244,7 @@ static int compile(cuda_context *ctx, strb *src, strb* bin, strb *log) { } 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); @@ -1250,7 +1252,7 @@ static int compile(cuda_context *ctx, strb *src, strb* bin, strb *log) { } 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); From e3d4aa0b31dacfa62e0531d69782b439f7c68640 Mon Sep 17 00:00:00 2001 From: notoraptor Date: Wed, 13 Sep 2017 14:59:10 -0400 Subject: [PATCH 3/5] Remove useless call to free. --- pygpu/gpuarray.pyx | 1 - 1 file changed, 1 deletion(-) diff --git a/pygpu/gpuarray.pyx b/pygpu/gpuarray.pyx index f582e05c4a..293803771d 100644 --- a/pygpu/gpuarray.pyx +++ b/pygpu/gpuarray.pyx @@ -1428,7 +1428,6 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims, tot *= newdims[i] cdims = calloc(nd, sizeof(size_t)) if cdims == NULL: - free(cdims) raise MemoryError, "could not allocate cdims" cdef size_t d From 714f3a54e69e86ad7e71e22439bf0b06a328a2e9 Mon Sep 17 00:00:00 2001 From: notoraptor Date: Wed, 13 Sep 2017 15:13:14 -0400 Subject: [PATCH 4/5] Manage free() with a try-catch block. --- pygpu/gpuarray.pyx | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pygpu/gpuarray.pyx b/pygpu/gpuarray.pyx index 293803771d..94f9b36181 100644 --- a/pygpu/gpuarray.pyx +++ b/pygpu/gpuarray.pyx @@ -1430,20 +1430,21 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims, if cdims == NULL: 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 - - if d * tot != a.size: - free(cdims) - raise GpuArrayException, "..." - cdims[i] = d + try: + cdef size_t d + 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 - array_reshape(res, a, nd, cdims, ord, nocopy) - free(cdims) - 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): From 06b2d4e1f2dcaf7438713284f64b4dee546efc6e Mon Sep 17 00:00:00 2001 From: notoraptor Date: Wed, 13 Sep 2017 15:30:12 -0400 Subject: [PATCH 5/5] Fix cython syntax --- pygpu/gpuarray.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygpu/gpuarray.pyx b/pygpu/gpuarray.pyx index 94f9b36181..a1e38bc4cf 100644 --- a/pygpu/gpuarray.pyx +++ b/pygpu/gpuarray.pyx @@ -1430,8 +1430,8 @@ cdef GpuArray pygpu_reshape(GpuArray a, unsigned int nd, const size_t *newdims, if cdims == NULL: raise MemoryError, "could not allocate cdims" + cdef size_t d try: - cdef size_t d for i in range(nd): d = newdims[i] if i == caxis: