Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions docs/source/reference/envvars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,15 @@ Threading Control
* ``tbb`` - A threading layer backed by Intel TBB.
* ``omp`` - A threading layer backed by OpenMP.
* ``workqueue`` - A simple built-in work-sharing task scheduler.

.. envvar:: NUMBA_THREADING_LAYER_PRIORITY
Comment thread
ickc marked this conversation as resolved.
Resolved

This environment variable controls the priority of searching for
library used for concurrent execution
for the CPU parallel targets (``@vectorize(target='parallel')``,
``@guvectorize(target='parallel')`` and ``@njit(parallel=True)``). The
variable type is string and by default is ``tbb omp workqueue`` which
prioritize the order of searching
threading layer based on what is available in the runtime. The valid values
are any permutation of the 3 choices (for more information about these see
:ref:`the threading layer documentation <numba-threading-layer>`).
Comment thread
ickc marked this conversation as resolved.
Resolved
15 changes: 15 additions & 0 deletions docs/source/user/threading-layer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ choosing a threading layer, the first is by selecting a threading layer that is
safe under various forms of parallel execution, the second is through explicit
selection via the threading layer name (e.g. ``tbb``).

Setting the threading layer priority
------------------------------------
Comment thread
ickc marked this conversation as resolved.
Resolved

By default, The threading layers are searched in the order of
Comment thread
ickc marked this conversation as resolved.
Resolved
'tbb', 'omp', and 'workqueue'.
If you want to let Numba choice the threading layer
Comment thread
ickc marked this conversation as resolved.
Resolved
based on availability automatically but with a different priority,
it can be changed via the environment variable
``NUMBA_THREADING_LAYER_PRIORITY`` or through assignment to
``numba.config.THREADING_LAYER_PRIORITY``.

For example, if you want Numba to choose ``omp`` first if available,
then ``tbb`` and so on, you can set either
``NUMBA_THREADING_LAYER_PRIORITY="omp tbb workqueue"`` or
``numba.config.THREADING_LAYER_PRIORITY = ["omp", "tbb", "workqueue"]``.
Comment thread
ickc marked this conversation as resolved.
Resolved
Comment thread
stuartarchibald marked this conversation as resolved.
Resolved

Selecting a threading layer for safe parallel execution
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
5 changes: 5 additions & 0 deletions numba/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ def avx_default():
DISABLE_JIT = _readenv("NUMBA_DISABLE_JIT", int, 0)

# choose parallel backend to use

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment does not match this variable, it belongs to THREADING_LAYER.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But shouldn't both THREADING_LAYER_PRIORITY and THREADING_LAYER are part of the "choose parallel backend to use" process?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the THREADING_LAYER, if prescribed, will act as the choice for the parallel backend in use, whereas the THREADING_LAYER_PRIORITY sets preference for use of threading layers in the case that no explicit choice is prescribed?

THREADING_LAYER_PRIORITY = _readenv(
"NUMBA_THREADING_LAYER_PRIORITY",
lambda string: string.split(),
['tbb', 'omp', 'workqueue'],
)
Comment thread
ickc marked this conversation as resolved.
Resolved
THREADING_LAYER = _readenv("NUMBA_THREADING_LAYER", str, 'default')

# CUDA Configs
Expand Down
9 changes: 8 additions & 1 deletion numba/np/ufunc/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,14 @@ def select_from_backends(backends):
return lib, backend

t = str(config.THREADING_LAYER).lower()
namedbackends = ['tbb', 'omp', 'workqueue']
namedbackends = config.THREADING_LAYER_PRIORITY
if set(namedbackends) != {'tbb', 'omp', 'workqueue'}:
Comment thread
ickc marked this conversation as resolved.
Resolved
raise ValueError(
"THREADING_LAYER_PRIORITY invalid: %s."
Comment thread
ickc marked this conversation as resolved.
Resolved
"It must be a permutation of "
"'tbb', 'omp', 'workqueue'"
Comment thread
ickc marked this conversation as resolved.
Resolved
% namedbackends
)

lib = None
err_helpers = dict()
Expand Down
50 changes: 50 additions & 0 deletions numba/tests/test_parallel_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,56 @@ def generate(cls):
TestThreadingLayerSelection.generate()


@skip_parfors_unsupported
class TestThreadingLayerPriority(ThreadLayerTestHelper):

def each_env_var(self, env_var: str):
"""Test setting priority via env var NUMBA_THREADING_LAYER_PRIORITY."""
env = os.environ.copy()
env['NUMBA_THREADING_LAYER'] = 'default'
env['NUMBA_THREADING_LAYER_PRIORITY'] = env_var

code = """import numba

# trigger threading layer decision
# hence catching invalid THREADING_LAYER_PRIORITY
@numba.jit(
'float64[::1](float64[::1], float64[::1])',
nopython=True,
parallel=True,
)
def plus(x, y):
return x + y

print(' '.join(numba.config.THREADING_LAYER_PRIORITY))
"""
cmd = [
sys.executable,
'-c',
code,
]
out, err = self.run_cmd(cmd, env=env)
self.assertEqual(out.strip(), env_var)
Comment thread
ickc marked this conversation as resolved.
Resolved

def test_valid_env_var(self):
import itertools
Comment thread
ickc marked this conversation as resolved.
Resolved

default = ['tbb', 'omp', 'workqueue']
for p in itertools.permutations(default):
env_var = ' '.join(p)
self.each_env_var(env_var)
Comment thread
ickc marked this conversation as resolved.
Resolved

def test_invalid_env_var(self):
env_var = 'tbb omp workqueue notvalidhere'
try:
self.each_env_var(env_var)
except AssertionError:
return
raise RuntimeError(
'Not catching invalid NUMBA_THREADING_LAYER_PRIORITY: %s' % env_var
)
Comment thread
ickc marked this conversation as resolved.
Resolved


@skip_parfors_unsupported
class TestMiscBackendIssues(ThreadLayerTestHelper):
"""
Expand Down