Skip to content

Commit fabb535

Browse files
bibikaroleksandr-pavlyk
authored andcommitted
Add fallback to os.cpu_count() if os.sched_getaffinity() is unavailable (e.g. on Darwin)
1 parent 0974ad9 commit fabb535

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

fft_bench.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@ def valid_dtype(dtype_str):
4040
return dtype
4141

4242

43+
if hasattr(os, 'sched_getaffinity'):
44+
default_num_threads = len(os.sched_getaffinity(0))
45+
else:
46+
default_num_threads = os.cpu_count()
47+
48+
4349
# Parse args
4450
parser = argparse.ArgumentParser(description='Benchmark FFT using NumPy and '
4551
'SciPy.')
4652

4753
fft_group = parser.add_argument_group(title='FFT problem arguments')
4854
fft_group.add_argument('-t', '--threads', '--num-threads', '--core-number',
49-
type=int, default=len(os.sched_getaffinity(0)),
55+
type=int, default=default_num_threads,
5056
help='Number of threads to use for FFT computation. '
5157
'%(prog)s will attempt to use mkl-service to get/set '
5258
'number of threads globally, and will also try to '

perf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ def set_threads(num_threads=None, verbose=False):
6363
print(f'TAG: WARNING: np.__mkl_version__ = {np.__mkl_version__}, '
6464
f'but mkl-service module was not found. Number of threads '
6565
f'is likely inaccurate!')
66-
return len(os.sched_getaffinity(0)), 'len(os.sched_getaffinity(0))'
66+
if hasattr(os, 'sched_getaffinity'):
67+
return len(os.sched_getaffinity(0)), 'len(os.sched_getaffinity(0))'
68+
else:
69+
return os.cpu_count(), 'os.cpu_count()'
6770
else:
6871
# no MKL, so assume not threaded
6972
return 1, 'guessing'

0 commit comments

Comments
 (0)