Skip to content

Commit

Permalink
ENH: For smaller array, added macro NPY_BEGIN_THREADS_THRESHOLDED in …
Browse files Browse the repository at this point in the history
…ndarraytypes.h

Avoiding NPY_BEGIN_THREADS for small arrays, can speed-up trivial_three_operand_loop by 5%.
As releases of GIL, then quickly restoring just after small operation doesn't benefit.
  • Loading branch information
arinkverma committed Aug 5, 2013
1 parent 6815f86 commit 5a5e86f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions numpy/core/include/numpy/ndarraytypes.h
Expand Up @@ -921,6 +921,8 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
#define NPY_BEGIN_THREADS_DEF PyThreadState *_save=NULL;
#define NPY_BEGIN_THREADS do {_save = PyEval_SaveThread();} while (0);
#define NPY_END_THREADS do {if (_save) PyEval_RestoreThread(_save);} while (0);
#define NPY_BEGIN_THREADS_THRESHOLDED(loop_size) do { if (loop_size > 500) \
{ _save = PyEval_SaveThread();} } while (0);

#define NPY_BEGIN_THREADS_DESCR(dtype) \
do {if (!(PyDataType_FLAGCHK(dtype, NPY_NEEDS_PYAPI))) \
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/src/umath/ufunc_object.c
Expand Up @@ -1098,7 +1098,7 @@ trivial_two_operand_loop(PyArrayObject **op,
NPY_UF_DBG_PRINT1("two operand loop count %d\n", (int)count[0]);

if (!needs_api) {
NPY_BEGIN_THREADS;
NPY_BEGIN_THREADS_THRESHOLDED(count[0]);
}

innerloop(data, count, stride, innerloopdata);
Expand Down Expand Up @@ -1131,7 +1131,7 @@ trivial_three_operand_loop(PyArrayObject **op,
NPY_UF_DBG_PRINT1("three operand loop count %d\n", (int)count[0]);

if (!needs_api) {
NPY_BEGIN_THREADS;
NPY_BEGIN_THREADS_THRESHOLDED(count[0]);
}

innerloop(data, count, stride, innerloopdata);
Expand Down

0 comments on commit 5a5e86f

Please sign in to comment.