Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 29 additions & 4 deletions numba/np/ufunc/workqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ platform_launch(void)
pthread_key_create(&tidkey, NULL);
}

#endif
#endif /* pthread threading */

/* Win Thread */
#ifdef NUMBA_WINTHREAD
Expand Down Expand Up @@ -262,7 +262,7 @@ platform_launch(void)
tidkey = TlsAlloc();
}

#endif
#endif /* Windows threading */

typedef struct Task
{
Expand Down Expand Up @@ -321,12 +321,20 @@ static THREAD_LOCAL(int) _TLS_num_threads = 0;
static void
set_num_threads(int count)
{
if (!queues)
{
launch_threads(NUM_THREADS);
}
_TLS_num_threads = count;
}

static int
get_num_threads(void)
{
if (!queues)
{
launch_threads(NUM_THREADS);
}
// This is purely to permit the implementation to survive to the point
// where it can exit cleanly as multiple threads cannot be used with this
// backend
Expand Down Expand Up @@ -506,6 +514,11 @@ add_task_internal(void *fn, void *args, void *dims, void *steps, void *data, int
{
void (*func)(void *args, void *dims, void *steps, void *data) = fn;

if (!queues)
{
launch_threads(NUM_THREADS);
}

Queue *queue = &queues[queue_pivot];

Task *task = &queue->task;
Expand Down Expand Up @@ -581,6 +594,11 @@ static void launch_threads(int count)

static void synchronize(void)
{
// This probably isn't needed, but is put in as a guard
if (!queues)
{
launch_threads(NUM_THREADS);
}
int i;
for (i = 0; i < queue_count; ++i)
{
Expand All @@ -590,6 +608,11 @@ static void synchronize(void)

static void ready(void)
{
// This probably isn't needed, but is put in as a guard
if (!queues)
{
launch_threads(NUM_THREADS);
}
int i;
for (i = 0; i < queue_count; ++i)
{
Expand All @@ -601,8 +624,10 @@ static void reset_after_fork(void)
{
free(queues);
queues = NULL;
NUM_THREADS = -1;
_INIT_NUM_THREADS = -1;
if (_INIT_NUM_THREADS != -1)
{
NUM_THREADS = _INIT_NUM_THREADS;
}
_nesting_level = 0;
}

Expand Down
36 changes: 36 additions & 0 deletions numba/tests/test_parallel_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,42 @@ def main():
self.assertIn("Terminating: Nested parallel kernel launch detected",
e_msg)

def test_workqueue_handles_fork_from_non_main_thread(self):
# For context see #7872, but essentially the multiprocessing pool
# implementation has a number of Python threads for handling the worker
# processes, one of which calls fork(2), this results in a fork from a
# non-main thread.

runme = """if 1:
from numba import njit, prange, threading_layer
import numpy as np
import multiprocessing

if __name__ == "__main__":

@njit(parallel=True)
def func(x):
return 10. * x

arr = np.arange(2.)

# run in single process to start Numba's thread pool
np.testing.assert_allclose(func(arr), func.py_func(arr))

# now run in a multiprocessing pool to get a fork from a
# non-main thread
with multiprocessing.Pool(10) as p:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

needs to force a fork context here

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.

Good point, OSX defaults to 'spawn'.

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.

b07d687 fixes, also adds a test skip for OS which don't have fork(2) and adds an assert to ensure the expected threading layer was used.

result = p.map(func, [arr])
np.testing.assert_allclose(result,
func.py_func(np.expand_dims(arr, 0)))
"""
cmdline = [sys.executable, '-c', runme]
env = os.environ.copy()
env['NUMBA_THREADING_LAYER'] = "workqueue"
env['NUMBA_NUM_THREADS'] = "4"

self.run_cmd(cmdline, env=env)


# 32bit or windows py27 (not that this runs on windows)
@skip_parfors_unsupported
Expand Down