Skip to content

Commit 402a83c

Browse files
committed
MNT: move some thread-unsafe state in thread-unsafe state struct
1 parent 8f84875 commit 402a83c

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

numpy/_core/src/multiarray/alloc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "numpy/npy_common.h"
1212
#include "npy_config.h"
1313
#include "alloc.h"
14+
#include "multiarraymodule.h"
1415

1516
#include <assert.h>
1617
#ifdef NPY_OS_LINUX
@@ -35,21 +36,19 @@ typedef struct {
3536
static cache_bucket datacache[NBUCKETS];
3637
static cache_bucket dimcache[NBUCKETS_DIM];
3738

38-
static int _madvise_hugepage = 1;
39-
4039

4140
/*
4241
* This function tells whether NumPy attempts to call `madvise` with
4342
* `MADV_HUGEPAGE`. `madvise` is only ever used on linux, so the value
44-
* of `_madvise_hugepage` may be ignored.
43+
* of `madvise_hugepage` may be ignored.
4544
*
4645
* It is exposed to Python as `np._core.multiarray._get_madvise_hugepage`.
4746
*/
4847
NPY_NO_EXPORT PyObject *
4948
_get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
5049
{
5150
#ifdef NPY_OS_LINUX
52-
if (_madvise_hugepage) {
51+
if (npy_ma_thread_unsafe_state->madvise_hugepage) {
5352
Py_RETURN_TRUE;
5453
}
5554
#endif
@@ -59,20 +58,20 @@ _get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
5958

6059
/*
6160
* This function enables or disables the use of `MADV_HUGEPAGE` on Linux
62-
* by modifying the global static `_madvise_hugepage`.
63-
* It returns the previous value of `_madvise_hugepage`.
61+
* by modifying the global static `madvise_hugepage`.
62+
* It returns the previous value of `madvise_hugepage`.
6463
*
6564
* It is exposed to Python as `np._core.multiarray._set_madvise_hugepage`.
6665
*/
6766
NPY_NO_EXPORT PyObject *
6867
_set_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *enabled_obj)
6968
{
70-
int was_enabled = _madvise_hugepage;
69+
int was_enabled = npy_ma_thread_unsafe_state->madvise_hugepage;
7170
int enabled = PyObject_IsTrue(enabled_obj);
7271
if (enabled < 0) {
7372
return NULL;
7473
}
75-
_madvise_hugepage = enabled;
74+
npy_ma_thread_unsafe_state->madvise_hugepage = enabled;
7675
if (was_enabled) {
7776
Py_RETURN_TRUE;
7877
}
@@ -110,7 +109,8 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
110109
#endif
111110
#ifdef NPY_OS_LINUX
112111
/* allow kernel allocating huge pages for large arrays */
113-
if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) && _madvise_hugepage) {
112+
if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) &&
113+
npy_ma_thread_unsafe_state->madvise_hugepage) {
114114
npy_uintp offset = 4096u - (npy_uintp)p % (4096u);
115115
npy_uintp length = nelem * esz - offset;
116116
/**

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,8 +4345,6 @@ _set_numpy_warn_if_no_mem_policy(PyObject *NPY_UNUSED(self), PyObject *arg)
43454345

43464346
static PyObject *
43474347
_reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) {
4348-
static int initialized = 0;
4349-
43504348
#if !defined(PYPY_VERSION)
43514349
if (PyThreadState_Get()->interp != PyInterpreterState_Main()) {
43524350
if (PyErr_WarnEx(PyExc_UserWarning,
@@ -4362,19 +4360,19 @@ _reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) {
43624360
return NULL;
43634361
}
43644362
/* No need to give the other warning in a sub-interpreter as well... */
4365-
initialized = 1;
4363+
npy_ma_thread_unsafe_state->reload_guard_initialized = 1;
43664364
Py_RETURN_NONE;
43674365
}
43684366
#endif
4369-
if (initialized) {
4367+
if (npy_ma_thread_unsafe_state->reload_guard_initialized) {
43704368
if (PyErr_WarnEx(PyExc_UserWarning,
43714369
"The NumPy module was reloaded (imported a second time). "
43724370
"This can in some cases result in small but subtle issues "
43734371
"and is discouraged.", 2) < 0) {
43744372
return NULL;
43754373
}
43764374
}
4377-
initialized = 1;
4375+
npy_ma_thread_unsafe_state->reload_guard_initialized = 1;
43784376
Py_RETURN_NONE;
43794377
}
43804378

@@ -4901,6 +4899,7 @@ initialize_static_globals(void)
49014899
// this is module-level global heap allocation, it is currently
49024900
// never freed
49034901
npy_ma_static_data = PyMem_Calloc(1, sizeof(npy_ma_static_data_struct));
4902+
npy_ma_thread_unsafe_state = PyMem_Calloc(1, sizeof(npy_ma_thread_unsafe_state_struct));
49044903

49054904
// cached reference to objects defined in python
49064905

numpy/_core/src/multiarray/multiarraymodule.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,25 @@ typedef struct npy_ma_thread_unsafe_state_struct {
194194
PyObject *_var;
195195
PyObject *_view_is_safe;
196196
PyObject *_void_scalar_to_string;
197-
PyObject *array_function_errmsg_formatter;
198-
PyObject *array_ufunc_errmsg_formatter;
199-
PyObject *internal_gcd_func;
200-
PyObject *npy_ctypes_check;
201-
PyObject *numpy_matrix;
202-
PyObject *NO_NEP50_WARNING;
203-
} npy_ma_global_data_struct;
197+
198+
/*
199+
* Used to test the internal-only scaled float test dtype
200+
*/
201+
npy_bool get_sfloat_dtype_initialized;
202+
203+
/*
204+
* controls the global madvise hugepage setting
205+
*/
206+
int madvise_hugepage;
207+
208+
/*
209+
* used to detect module reloading in the reload guard
210+
*/
211+
int reload_guard_initialized;
212+
} npy_ma_thread_unsafe_state_struct;
213+
214+
NPY_VISIBILITY_HIDDEN extern npy_ma_str_struct *npy_ma_str;
215+
NPY_VISIBILITY_HIDDEN extern npy_ma_static_data_struct *npy_ma_static_data;
216+
NPY_VISIBILITY_HIDDEN extern npy_ma_thread_unsafe_state_struct *npy_ma_thread_unsafe_state;
204217

205218
#endif /* NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_ */

numpy/_core/src/umath/_scaled_float_dtype.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,7 @@ sfloat_init_ufuncs(void) {
867867
NPY_NO_EXPORT PyObject *
868868
get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args))
869869
{
870-
/* Allow calling the function multiple times. */
871-
static npy_bool initialized = NPY_FALSE;
872-
873-
if (initialized) {
870+
if (npy_ma_thread_unsafe_state->get_sfloat_dtype_initialized) {
874871
Py_INCREF(&PyArray_SFloatDType);
875872
return (PyObject *)&PyArray_SFloatDType;
876873
}
@@ -899,6 +896,6 @@ get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args))
899896
return NULL;
900897
}
901898

902-
initialized = NPY_TRUE;
899+
npy_ma_thread_unsafe_state->get_sfloat_dtype_initialized = NPY_TRUE;
903900
return (PyObject *)&PyArray_SFloatDType;
904901
}

0 commit comments

Comments
 (0)