/
omp.pyx
113 lines (81 loc) · 3.06 KB
/
omp.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!python
import os
cimport safe_openmp as openmp
have_openmp = <int> openmp.have_openmp
__all__ = ['have_openmp', 'default_threads', 'cpu_count', 'thread_count']
def cpu_count():
"""Return number of cpus as determined by omp_get_num_procs."""
if have_openmp:
return openmp.omp_get_num_procs()
else:
return 1
def thread_count():
"""Return number of threads as determined by omp_get_max_threads."""
if have_openmp:
return openmp.omp_get_max_threads()
else:
return 1
def _get_default_threads():
"""Default number of threads for OpenMP.
This function prioritizes the OMP_NUM_THREADS environment variable.
"""
if have_openmp:
try:
default_threads = int(os.environ.get('OMP_NUM_THREADS', None))
if default_threads < 1:
raise ValueError("invalid number of threads")
except (ValueError, TypeError):
default_threads = openmp.omp_get_num_procs()
return default_threads
else:
return 1
default_threads = _get_default_threads()
def determine_num_threads(num_threads):
"""Determine the effective number of threads to be used for OpenMP calls
- For ``num_threads = None``,
- if the ``OMP_NUM_THREADS`` environment variable is set, return that
value
- otherwise, return the maximum number of cores retrieved by
``openmp.opm_get_num_procs()``.
- For ``num_threads > 0``, return this value.
- For ``num_threads < 0``, return the maximal number of threads minus
``|num_threads + 1|``. In particular ``num_threads = -1`` will use as
many threads as there are available cores on the machine.
- For ``num_threads = 0`` a ValueError is raised.
Parameters
----------
num_threads : int or None
Desired number of threads to be used.
"""
if not isinstance(num_threads, int) and num_threads is not None:
raise TypeError("num_threads must be an int or None")
if num_threads == 0:
raise ValueError("num_threads cannot be 0")
if num_threads is None:
return default_threads
if num_threads < 0:
return max(1, cpu_count() + num_threads + 1)
return num_threads
cdef void set_num_threads(num_threads):
"""Set the number of threads to be used by OpenMP
This function does nothing if OpenMP is not available.
Parameters
----------
num_threads : int
Effective number of threads for OpenMP accelerated code.
"""
if openmp.have_openmp:
openmp.omp_set_dynamic(0)
openmp.omp_set_num_threads(num_threads)
cdef void restore_default_num_threads():
"""Restore OpenMP to using the default number of threads.
This function does nothing if OpenMP is not available
"""
if openmp.have_openmp:
openmp.omp_set_num_threads(<int> default_threads)
def _set_omp_threads(num_threads):
"""Function for testing set_num_threads."""
set_num_threads(num_threads)
def _restore_omp_threads():
"""Function for testing restore_default_num_threads."""
restore_default_num_threads()