Skip to content
Merged
Changes from all 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
Switch os.fork for mp fork ctx
As title
  • Loading branch information
stuartarchibald committed Aug 18, 2021
commit bde11b067dde52a579c7f1590c0055b3f214c129
71 changes: 41 additions & 30 deletions numba/tests/test_parallel_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,24 +956,24 @@ def forker():
print("ERR:", err)

@needs_external_compilers
@linux_only # os.fork required.
@linux_only # fork required.
def test_lifetime_of_task_scheduler_handle(self):
# See PR #7280 for context.
BROKEN_COMPILERS = 'SKIP: COMPILATION FAILED'
runme = """if 1:
import ctypes
import os
import sys
import multiprocessing as mp
from tempfile import TemporaryDirectory, NamedTemporaryFile
from numba.pycc.platform import Toolchain, _external_compiler_ok
from numba import njit, prange, threading_layer

import faulthandler
faulthandler.enable()
if not _external_compiler_ok:
raise AssertionError('External compilers are not found.')

try:
with TemporaryDirectory() as tmpdir:
with NamedTemporaryFile(dir=tmpdir) as tmpfile:
with TemporaryDirectory() as tmpdir:
with NamedTemporaryFile(dir=tmpdir) as tmpfile:
try:
src = \"\"\"
#define TBB_PREVIEW_WAITING_FOR_WORKERS 1
#include <tbb/tbb.h>
Expand All @@ -999,31 +999,42 @@ def test_lifetime_of_task_scheduler_handle(self):
# Load into the process, it doesn't matter whether the
# DSO exists on disk once it's loaded in.
DLL = ctypes.CDLL(dso_name)
except Exception as e:
# Something is broken in compilation, could be one of many
# things including, but not limited to: missing tbb headers,
# incorrect permissions, compilers that don't work for the above
print(e)
print('BROKEN_COMPILERS')
sys.exit(0)

# Do the test
DLL.launch()
except Exception as e:
# Something is broken in compilation, could be one of
# many things including, but not limited to: missing tbb
# headers, incorrect permissions, compilers that don't
# work for the above
print(e)
print('BROKEN_COMPILERS')
sys.exit(0)

# Do the test, launch this library and also execute a
# function with the TBB threading layer.

DLL.launch()

@njit(parallel=True)
def foo(n):
acc = 0
for i in prange(n):
acc += i
return acc

foo(1)

# Check the threading layer used was TBB
assert threading_layer() == 'tbb'

@njit(parallel=True)
def foo(n):
acc = 0
for i in prange(n):
acc += i
return acc
# Use mp context for a controlled version of fork, this triggers the
# reported bug.

foo(1)
assert threading_layer() == 'tbb'
pid = os.fork()
if pid != 0:
# parent prints success
print("SUCCESS")
sys.exit(0)
ctx = mp.get_context('fork')
def nowork():
pass
p = ctx.Process(target=nowork)
p.start()
p.join(10)
print("SUCCESS")
""".replace('BROKEN_COMPILERS', BROKEN_COMPILERS)

cmdline = [sys.executable, '-c', runme]
Expand Down