Skip to content

Commit 0a59156

Browse files
committed
MNT: fix threading regression
Signed-off-by: Iason Krommydas <[email protected]>
1 parent f1d2936 commit 0a59156

1 file changed

Lines changed: 101 additions & 25 deletions

File tree

‎numpy/_core/src/umath/searchsorted.cpp‎

Lines changed: 101 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,37 @@ searchsorted_descr_carrier(PyArray_Descr *descr)
8484
}
8585

8686

87+
/*
88+
* The carrier is built once per call in `searchsorted_get_loop` rather than
89+
* once per chunk in the loop, and travels to the loop as its auxiliary data.
90+
*/
91+
typedef struct {
92+
NpyAuxData base;
93+
PyArrayObject *carrier;
94+
} searchsorted_auxdata;
95+
96+
97+
static void
98+
searchsorted_auxdata_free(NpyAuxData *data)
99+
{
100+
Py_XDECREF(((searchsorted_auxdata *)data)->carrier);
101+
PyMem_Free(data);
102+
}
103+
104+
105+
static NpyAuxData *
106+
searchsorted_auxdata_clone(NpyAuxData *data)
107+
{
108+
searchsorted_auxdata *copy = PyMem_New(searchsorted_auxdata, 1);
109+
if (copy == NULL) {
110+
return NULL;
111+
}
112+
*copy = *(searchsorted_auxdata *)data;
113+
Py_XINCREF(copy->carrier);
114+
return (NpyAuxData *)copy;
115+
}
116+
117+
87118
/*
88119
* `generic` selects the legacy comparison function, used by the dtypes
89120
* without a dedicated kernel. It needs a descriptor carrier because the
@@ -94,7 +125,7 @@ template <bool generic, NPY_SEARCHSIDE side>
94125
static int
95126
searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
96127
npy_intp const dimensions[], npy_intp const strides[],
97-
NpyAuxData *NPY_UNUSED(auxdata))
128+
NpyAuxData *auxdata)
98129
{
99130
PyArray_Descr *descr = context->descriptors[0];
100131
PyArray_BinSearchFunc *bs = get_binsearch_func(descr, side);
@@ -104,11 +135,10 @@ searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
104135
return -1;
105136
}
106137
PyArrayObject *carrier = NULL;
138+
int needs_api = 0;
107139
if constexpr (generic) {
108-
carrier = searchsorted_descr_carrier(descr);
109-
if (carrier == NULL) {
110-
return -1;
111-
}
140+
carrier = ((searchsorted_auxdata *)auxdata)->carrier;
141+
needs_api = PyDataType_FLAGCHK(descr, NPY_NEEDS_PYAPI);
112142
}
113143

114144
npy_intp n_outer = dimensions[0];
@@ -128,16 +158,14 @@ searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
128158
bs(a_o, v_o, out_o, n, m, a_str, v_str, out_str, carrier, carrier,
129159
generic ? &searchsorted_compare : NULL);
130160
/* A comparison may have raised; do not keep searching on top of a
131-
* pending error (the GIL is held here, generic implies PYAPI). */
132-
if constexpr (generic) {
133-
if (PyErr_Occurred()) {
134-
ret = -1;
135-
break;
136-
}
161+
* pending error. Only the dtypes that need the Python API get here
162+
* holding the GIL, and only those can raise. */
163+
if (needs_api && PyErr_Occurred()) {
164+
ret = -1;
165+
break;
137166
}
138167
}
139168
searchsorted_restore_floatstatus((char *)&n_outer, saved);
140-
Py_XDECREF(carrier);
141169
return ret;
142170
}
143171

@@ -146,7 +174,7 @@ template <bool generic, NPY_SEARCHSIDE side>
146174
static int
147175
searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
148176
npy_intp const dimensions[], npy_intp const strides[],
149-
NpyAuxData *NPY_UNUSED(auxdata))
177+
NpyAuxData *auxdata)
150178
{
151179
PyArray_Descr *descr = context->descriptors[0];
152180
PyArray_ArgBinSearchFunc *bs = get_argbinsearch_func(descr, side);
@@ -156,11 +184,10 @@ searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
156184
return -1;
157185
}
158186
PyArrayObject *carrier = NULL;
187+
int needs_api = 0;
159188
if constexpr (generic) {
160-
carrier = searchsorted_descr_carrier(descr);
161-
if (carrier == NULL) {
162-
return -1;
163-
}
189+
carrier = ((searchsorted_auxdata *)auxdata)->carrier;
190+
needs_api = PyDataType_FLAGCHK(descr, NPY_NEEDS_PYAPI);
164191
}
165192

166193
npy_intp n_outer = dimensions[0];
@@ -189,16 +216,14 @@ searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
189216
break;
190217
}
191218
/* A comparison may have raised; do not keep searching on top of a
192-
* pending error (the GIL is held here, generic implies PYAPI). */
193-
if constexpr (generic) {
194-
if (PyErr_Occurred()) {
195-
ret = -1;
196-
break;
197-
}
219+
* pending error. Only the dtypes that need the Python API get here
220+
* holding the GIL, and only those can raise. */
221+
if (needs_api && PyErr_Occurred()) {
222+
ret = -1;
223+
break;
198224
}
199225
}
200226
searchsorted_restore_floatstatus((char *)&n_outer, saved);
201-
Py_XDECREF(carrier);
202227
return ret;
203228
}
204229

@@ -238,6 +263,49 @@ searchsorted_resolve_descriptors(
238263
}
239264

240265

266+
/*
267+
* Build the descriptor carrier the legacy comparison needs, and release the
268+
* GIL for the dtypes whose comparison cannot reach Python. This is the test
269+
* PyArray_SearchSorted makes through NPY_BEGIN_THREADS_DESCR. VOID_compare
270+
* calls into Python even with no object fields, so the dtype flag rather
271+
* than a reference check decides it, and it depends on the descriptor rather
272+
* than the DType, which is why it happens here.
273+
*/
274+
static int
275+
searchsorted_get_loop(PyArrayMethod_Context *context, int aligned,
276+
int move_references, const npy_intp *strides,
277+
PyArrayMethod_StridedLoop **out_loop,
278+
NpyAuxData **out_transferdata,
279+
NPY_ARRAYMETHOD_FLAGS *flags)
280+
{
281+
if (npy_default_get_strided_loop(context, aligned, move_references,
282+
strides, out_loop, out_transferdata,
283+
flags) < 0) {
284+
return -1;
285+
}
286+
searchsorted_auxdata *auxdata = PyMem_New(searchsorted_auxdata, 1);
287+
if (auxdata == NULL) {
288+
PyErr_NoMemory();
289+
return -1;
290+
}
291+
memset(auxdata, 0, sizeof(*auxdata));
292+
auxdata->base.free = &searchsorted_auxdata_free;
293+
auxdata->base.clone = &searchsorted_auxdata_clone;
294+
295+
PyArray_Descr *descr = context->descriptors[0];
296+
auxdata->carrier = searchsorted_descr_carrier(descr);
297+
if (auxdata->carrier == NULL) {
298+
PyMem_Free(auxdata);
299+
return -1;
300+
}
301+
*out_transferdata = (NpyAuxData *)auxdata;
302+
if (!PyDataType_FLAGCHK(descr, NPY_NEEDS_PYAPI)) {
303+
*flags = (NPY_ARRAYMETHOD_FLAGS)(*flags & ~NPY_METH_REQUIRES_PYAPI);
304+
}
305+
return 0;
306+
}
307+
308+
241309
/*
242310
* `generic` picks the legacy comparison function over the dedicated kernels
243311
* from binsearch.cpp.
@@ -283,8 +351,15 @@ add_searchsorted_loop(PyObject *ufunc, PyArray_DTypeMeta *dt, int with_sorter,
283351
{NPY_METH_strided_loop, loop},
284352
{NPY_METH_resolve_descriptors,
285353
(void *)&searchsorted_resolve_descriptors},
354+
{0, NULL},
286355
{0, NULL}
287356
};
357+
if (generic) {
358+
/* Only the legacy comparison needs a carrier and, for some dtypes,
359+
* the GIL. */
360+
slots[2].slot = NPY_METH_get_loop;
361+
slots[2].pfunc = (void *)&searchsorted_get_loop;
362+
}
288363

289364
PyArrayMethod_Spec spec = {};
290365
spec.name = with_sorter ? "searchsorted_sorter_loop" : "searchsorted_loop";
@@ -295,7 +370,8 @@ add_searchsorted_loop(PyObject *ufunc, PyArray_DTypeMeta *dt, int with_sorter,
295370
/* Discarded by the gufunc machinery, see searchsorted_restore_floatstatus */
296371
spec.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS;
297372
if (generic) {
298-
/* the legacy comparison function may call into Python */
373+
/* Cleared again in searchsorted_get_loop for the dtypes whose
374+
* comparison cannot call into Python. */
299375
spec.flags = (NPY_ARRAYMETHOD_FLAGS)(spec.flags
300376
| NPY_METH_REQUIRES_PYAPI);
301377
}

0 commit comments

Comments
 (0)