Skip to content

Commit

Permalink
Merge pull request numpy#27392 from ngoldbaum/ufunc-cache-fix
Browse files Browse the repository at this point in the history
BUG: apply critical sections around populating the dispatch cache
  • Loading branch information
charris authored Sep 14, 2024
2 parents 72b35ee + 37ec084 commit b307a8c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 33 deletions.
20 changes: 0 additions & 20 deletions numpy/_core/src/common/npy_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@
#define _NpyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
#endif

#ifdef Py_GIL_DISABLED
#define LOCK_TABLE(tb) PyMutex_Lock(&tb->mutex)
#define UNLOCK_TABLE(tb) PyMutex_Unlock(&tb->mutex)
#define INITIALIZE_LOCK(tb) memset(&tb->mutex, 0, sizeof(PyMutex))
#else
// the GIL serializes access to the table so no need
// for locking if it is enabled
#define LOCK_TABLE(tb)
#define UNLOCK_TABLE(tb)
#define INITIALIZE_LOCK(tb)
#endif

/*
* This hashing function is basically the Python tuple hash with the type
* identity hash inlined. The tuple hash itself is a reduced version of xxHash.
Expand Down Expand Up @@ -112,8 +100,6 @@ PyArrayIdentityHash_New(int key_len)
res->size = 4; /* Start with a size of 4 */
res->nelem = 0;

INITIALIZE_LOCK(res);

res->buckets = PyMem_Calloc(4 * (key_len + 1), sizeof(PyObject *));
if (res->buckets == NULL) {
PyErr_NoMemory();
Expand Down Expand Up @@ -206,17 +192,14 @@ NPY_NO_EXPORT int
PyArrayIdentityHash_SetItem(PyArrayIdentityHash *tb,
PyObject *const *key, PyObject *value, int replace)
{
LOCK_TABLE(tb);
if (value != NULL && _resize_if_necessary(tb) < 0) {
/* Shrink, only if a new value is added. */
UNLOCK_TABLE(tb);
return -1;
}

PyObject **tb_item = find_item(tb, key);
if (value != NULL) {
if (tb_item[0] != NULL && tb_item[0] != value && !replace) {
UNLOCK_TABLE(tb);
PyErr_SetString(PyExc_RuntimeError,
"Identity cache already includes an item with this key.");
return -1;
Expand All @@ -230,16 +213,13 @@ PyArrayIdentityHash_SetItem(PyArrayIdentityHash *tb,
memset(tb_item, 0, (tb->key_len + 1) * sizeof(PyObject *));
}

UNLOCK_TABLE(tb);
return 0;
}


NPY_NO_EXPORT PyObject *
PyArrayIdentityHash_GetItem(PyArrayIdentityHash *tb, PyObject *const *key)
{
LOCK_TABLE(tb);
PyObject *res = find_item(tb, key)[0];
UNLOCK_TABLE(tb);
return res;
}
7 changes: 0 additions & 7 deletions numpy/_core/src/common/npy_hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ typedef struct {
PyObject **buckets;
npy_intp size; /* current size */
npy_intp nelem; /* number of elements */
#ifdef Py_GIL_DISABLED
#if PY_VERSION_HEX < 0x30d00b3
#error "GIL-disabled builds require Python 3.13.0b3 or newer"
#else
PyMutex mutex;
#endif
#endif
} PyArrayIdentityHash;


Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/src/common/pythoncapi-compat
5 changes: 1 addition & 4 deletions numpy/_core/src/multiarray/textreading/rows.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define _MULTIARRAYMODULE
#include "numpy/arrayobject.h"
#include "numpy/npy_3kcompat.h"
#include "npy_pycompat.h"
#include "alloc.h"

#include <string.h>
Expand Down Expand Up @@ -59,9 +60,7 @@ create_conv_funcs(
PyObject *key, *value;
Py_ssize_t pos = 0;
int error = 0;
#if Py_GIL_DISABLED
Py_BEGIN_CRITICAL_SECTION(converters);
#endif
while (PyDict_Next(converters, &pos, &key, &value)) {
Py_ssize_t column = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (column == -1 && PyErr_Occurred()) {
Expand Down Expand Up @@ -114,9 +113,7 @@ create_conv_funcs(
Py_INCREF(value);
conv_funcs[column] = value;
}
#if Py_GIL_DISABLED
Py_END_CRITICAL_SECTION();
#endif

if (error) {
goto error;
Expand Down
5 changes: 4 additions & 1 deletion numpy/_core/src/umath/dispatching.c
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,11 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
}
}

PyObject *info = promote_and_get_info_and_ufuncimpl(ufunc,
PyObject *info;
Py_BEGIN_CRITICAL_SECTION((PyObject *)ufunc);
info = promote_and_get_info_and_ufuncimpl(ufunc,
ops, signature, op_dtypes, legacy_promotion_is_possible);
Py_END_CRITICAL_SECTION();

if (info == NULL) {
goto handle_error;
Expand Down

0 comments on commit b307a8c

Please sign in to comment.