8000 BUG: fix cached allocations without the GIL by charris · Pull Request #11985 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fix cached allocations without the GIL #11985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions numpy/core/src/multiarray/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ typedef struct {
static cache_bucket datacache[NBUCKETS];
static cache_bucket dimcache[NBUCKETS_DIM];

/* as the cache is managed in global variables verify the GIL is held */
#if defined(NPY_PY3K)
#define NPY_CHECK_GIL_HELD() PyGILState_Check()
#else
#define NPY_CHECK_GIL_HELD() 1
#endif

/*
* very simplistic small memory block cache to avoid more expensive libc
* allocations
Expand All @@ -47,6 +54,7 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
{
assert((esz == 1 && cache == datacache) ||
(esz == sizeof(npy_intp) && cache == dimcache));
assert(NPY_CHECK_GIL_HELD());
if (nelem < msz) {
if (cache[nelem].available > 0) {
return cache[nelem].ptrs[--(cache[nelem].available)];
Expand Down Expand Up @@ -75,6 +83,7 @@ static NPY_INLINE void
_npy_free_cache(void * p, npy_uintp nelem, npy_uint msz,
cache_bucket * cache, void (*dealloc)(void *))
{
assert(NPY_CHECK_GIL_HELD());
if (p != NULL && nelem < msz) {
if (cache[nelem].available < NCACHE) {
cache[nelem].ptrs[cache[nelem].available++] = p;
Expand Down
22 changes: 11 additions & 11 deletions numpy/core/src/multiarray/item_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,6 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort,
}
size = it->size;

NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(op));

if (needcopy) {
buffer = npy_alloc_cache(N * elsize);
if (buffer == NULL) {
Expand All @@ -842,6 +840,8 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort,
}
}

NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(op));

while (size--) {
char *bufptr = it->dataptr;

Expand Down Expand Up @@ -916,8 +916,8 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort,
}

fail:
npy_free_cache(buffer, N * elsize);
NPY_END_THREADS_DESCR(PyArray_DESCR(op));
npy_free_cache(buffer, N * elsize);
if (ret < 0 && !PyErr_Occurred()) {
/* Out of memory during sorting or buffer creation */
PyErr_NoMemory();
Expand Down Expand Up @@ -977,8 +977,6 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort,
}
size = it->size;

NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(op));

if (needcopy) {
valbuffer = npy_alloc_cache(N * elsize);
if (valbuffer == NULL) {
Expand All @@ -995,6 +993,8 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort,
}
}

NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(op));

while (size--) {
char *valptr = it->dataptr;
npy_intp *idxptr = (npy_intp *)rit->dataptr;
Expand Down Expand Up @@ -1078,9 +1078,9 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort,
}

fail:
NPY_END_THREADS_DESCR(PyArray_DESCR(op));
npy_free_cache(valbuffer, N * elsize);
npy_free_cache(idxbuffer, N * sizeof(npy_intp));
NPY_END_THREADS_DESCR(PyArray_DESCR(op));
if (ret < 0) {
if (!PyErr_Occurred()) {
/* Out of memory during sorting or buffer creation */
Expand Down Expand Up @@ -1495,13 +1495,13 @@ PyArray_LexSort(PyObject *sort_keys, int axis)
char *valbuffer, *indbuffer;
int *swaps;

valbuffer = npy_alloc_cache(N * maxelsize);
valbuffer = PyDataMem_NEW(N * maxelsize);
if (valbuffer == NULL) {
goto fail;
}
indbuffer = npy_alloc_cache(N * sizeof(npy_intp));
indbuffer = PyDataMem_NEW(N * sizeof(npy_intp));
if (indbuffer == NULL) {
npy_free_cache(indbuffer, N * sizeof(npy_intp));
PyDataMem_FREE(indbuffer);
goto fail;
}
swaps = malloc(n*sizeof(int));
Expand Down Expand Up @@ -1544,8 +1544,8 @@ PyArray_LexSort(PyObject *sort_keys, int axis)
sizeof(npy_intp), N, sizeof(npy_intp));
PyArray_ITER_NEXT(rit);
}
npy_free_cache(valbuffer, N * maxelsize);
npy_free_cache(indbuffer, N * sizeof(npy_intp));
PyDataMem_FREE(valbuffer);
PyDataMem_FREE(indbuffer);
free(swaps);
}
else {
Expand Down
0