numthreads 0.5.0__tar.gz → 0.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {numthreads-0.5.0 → numthreads-0.7.0}/PKG-INFO +32 -18
- {numthreads-0.5.0 → numthreads-0.7.0}/README.md +19 -7
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.egg-info/PKG-INFO +32 -18
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.egg-info/requires.txt +1 -2
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.py +91 -68
- {numthreads-0.5.0 → numthreads-0.7.0}/pyproject.toml +31 -46
- numthreads-0.7.0/tests/test_numthreads.py +282 -0
- numthreads-0.5.0/tests/test_numthreads.py +0 -102
- {numthreads-0.5.0 → numthreads-0.7.0}/LICENSE +0 -0
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.egg-info/SOURCES.txt +0 -0
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.egg-info/dependency_links.txt +0 -0
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.egg-info/entry_points.txt +0 -0
- {numthreads-0.5.0 → numthreads-0.7.0}/numthreads.egg-info/top_level.txt +0 -0
- {numthreads-0.5.0 → numthreads-0.7.0}/setup.cfg +0 -0
|
@@ -1,27 +1,29 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: numthreads
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
5
5
|
Author-email: Bas Nijholt <bas@nijho.lt>
|
|
6
|
+
License-Expression: MIT
|
|
6
7
|
Project-URL: Homepage, https://github.com/basnijholt/numthreads
|
|
8
|
+
Project-URL: Repository, https://github.com/basnijholt/numthreads
|
|
9
|
+
Project-URL: Documentation, https://numthreads.readthedocs.io/
|
|
10
|
+
Project-URL: Issues, https://github.com/basnijholt/numthreads/issues
|
|
7
11
|
Classifier: Development Status :: 4 - Beta
|
|
8
12
|
Classifier: Framework :: Pytest
|
|
9
13
|
Classifier: Intended Audience :: Developers
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
14
|
Classifier: Operating System :: OS Independent
|
|
12
15
|
Classifier: Programming Language :: Python :: 3
|
|
13
16
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
18
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
20
22
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
23
|
Classifier: Topic :: Software Development :: Testing
|
|
22
24
|
Classifier: Topic :: System :: Hardware :: Symmetric Multi-processing
|
|
23
25
|
Classifier: Topic :: Utilities
|
|
24
|
-
Requires-Python: >=3.
|
|
26
|
+
Requires-Python: >=3.11
|
|
25
27
|
Description-Content-Type: text/markdown
|
|
26
28
|
License-File: LICENSE
|
|
27
29
|
Provides-Extra: docs
|
|
@@ -33,14 +35,14 @@ Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
|
|
|
33
35
|
Provides-Extra: test
|
|
34
36
|
Requires-Dist: pytest; extra == "test"
|
|
35
37
|
Requires-Dist: pre-commit; extra == "test"
|
|
36
|
-
Requires-Dist: coverage; extra == "test"
|
|
38
|
+
Requires-Dist: coverage>=7.10; extra == "test"
|
|
37
39
|
Requires-Dist: pytest-cov; extra == "test"
|
|
38
|
-
|
|
40
|
+
Dynamic: license-file
|
|
39
41
|
|
|
40
42
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
41
43
|
|
|
42
44
|
<h1 align="center">numthreads</h1>
|
|
43
|
-
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and
|
|
45
|
+
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba</h3>
|
|
44
46
|
|
|
45
47
|
[](https://pypi.python.org/pypi/numthreads)
|
|
46
48
|
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
@@ -48,7 +50,7 @@ Requires-Dist: pytest-mock; extra == "test"
|
|
|
48
50
|
[](https://github.com/basnijholt/numthreads)
|
|
49
51
|
[](https://numthreads.readthedocs.io/)
|
|
50
52
|
|
|
51
|
-
`numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and
|
|
53
|
+
`numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, Accelerate, and Numba.
|
|
52
54
|
The number of threads can be set via the command line or in Python code.
|
|
53
55
|
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
54
56
|
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
@@ -56,10 +58,11 @@ In some instances, using more threads may actually impede computational efficien
|
|
|
56
58
|
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
57
59
|
|
|
58
60
|
- Simple and straightforward command-line interface.
|
|
59
|
-
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and
|
|
61
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, Accelerate, and Numba.
|
|
60
62
|
- Context manager support for temporary thread setting in Python code.
|
|
61
63
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
62
|
-
-
|
|
64
|
+
- Includes a pytest plugin.
|
|
65
|
+
- Tiny (~7 KB) and dependency-free.
|
|
63
66
|
|
|
64
67
|
<!-- toc-start -->
|
|
65
68
|
|
|
@@ -76,6 +79,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
76
79
|
- [Windows (PowerShell)](#windows-powershell)
|
|
77
80
|
- [Get the number of threads](#get-the-number-of-threads)
|
|
78
81
|
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
82
|
+
- [:scales: Comparison with threadpoolctl](#scales-comparison-with-threadpoolctl)
|
|
79
83
|
- [:question: Getting Help](#question-getting-help)
|
|
80
84
|
|
|
81
85
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -111,6 +115,7 @@ This will set the number of threads using the following environment variables:
|
|
|
111
115
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
112
116
|
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
113
117
|
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
118
|
+
- Numba (via `NUMBA_NUM_THREADS`)
|
|
114
119
|
|
|
115
120
|
or use it as a context manager:
|
|
116
121
|
|
|
@@ -143,6 +148,8 @@ with omp_num_threads(4):
|
|
|
143
148
|
pass
|
|
144
149
|
```
|
|
145
150
|
|
|
151
|
+
Use `omp_get_max_threads()` to see the configured upper bound for the next OpenMP parallel region. `omp_get_num_threads()` reports the current region's thread count (1 outside a parallel region).
|
|
152
|
+
|
|
146
153
|
### Command Line Interface
|
|
147
154
|
|
|
148
155
|
After installing `numthreads`, you can easily set the number of threads used by supported libraries via the command line. For example, to print the command to set the number of threads to 4, run:
|
|
@@ -162,7 +169,7 @@ Which will print the following:
|
|
|
162
169
|
<!-- OUTPUT:START -->
|
|
163
170
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
164
171
|
```bash
|
|
165
|
-
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
|
|
172
|
+
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4' ; export NUMBA_NUM_THREADS='4'
|
|
166
173
|
```
|
|
167
174
|
|
|
168
175
|
<!-- OUTPUT:END -->
|
|
@@ -209,6 +216,7 @@ MKL_NUM_THREADS: 12
|
|
|
209
216
|
OMP_NUM_THREADS: 12
|
|
210
217
|
NUMEXPR_NUM_THREADS: 12
|
|
211
218
|
VECLIB_MAXIMUM_THREADS: 12
|
|
219
|
+
NUMBA_NUM_THREADS: 12
|
|
212
220
|
```
|
|
213
221
|
|
|
214
222
|
<!-- OUTPUT:END -->
|
|
@@ -225,7 +233,7 @@ Check the `numthreads -h` message for more information:
|
|
|
225
233
|
```bash
|
|
226
234
|
usage: numthreads [-h] n
|
|
227
235
|
|
|
228
|
-
Set
|
|
236
|
+
Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
229
237
|
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
230
238
|
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
231
239
|
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
@@ -251,7 +259,13 @@ To use it, make sure `numthreads` is installed. Then, run pytest with the `--num
|
|
|
251
259
|
pytest --numthreads 1
|
|
252
260
|
```
|
|
253
261
|
|
|
254
|
-
This
|
|
262
|
+
This sets the thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba before pytest imports its initial `conftest.py` files. You can also put `--numthreads 1` in pytest's `addopts` configuration.
|
|
263
|
+
|
|
264
|
+
## :scales: Comparison with threadpoolctl
|
|
265
|
+
|
|
266
|
+
`numthreads` sets thread-control environment variables before importing numerical libraries. It also provides a CLI, a pytest plugin, and lightweight OpenMP helpers via `ctypes`, all in a single ~7 KB file with no dependencies. Use it when you control how a script or test suite starts.
|
|
267
|
+
|
|
268
|
+
[`threadpoolctl`](https://github.com/joblib/threadpoolctl) discovers already-loaded BLAS and OpenMP shared libraries and inspects or limits their thread pools at runtime through their C APIs. Use it when the libraries are already loaded or you need to inspect their thread pools. The tools complement each other: set defaults at startup with `numthreads`, then inspect or adjust loaded pools with `threadpoolctl`.
|
|
255
269
|
|
|
256
270
|
## :question: Getting Help
|
|
257
271
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
2
2
|
|
|
3
3
|
<h1 align="center">numthreads</h1>
|
|
4
|
-
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and
|
|
4
|
+
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba</h3>
|
|
5
5
|
|
|
6
6
|
[](https://pypi.python.org/pypi/numthreads)
|
|
7
7
|
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
[](https://github.com/basnijholt/numthreads)
|
|
10
10
|
[](https://numthreads.readthedocs.io/)
|
|
11
11
|
|
|
12
|
-
`numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and
|
|
12
|
+
`numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, Accelerate, and Numba.
|
|
13
13
|
The number of threads can be set via the command line or in Python code.
|
|
14
14
|
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
15
15
|
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
@@ -17,10 +17,11 @@ In some instances, using more threads may actually impede computational efficien
|
|
|
17
17
|
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
18
18
|
|
|
19
19
|
- Simple and straightforward command-line interface.
|
|
20
|
-
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and
|
|
20
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, Accelerate, and Numba.
|
|
21
21
|
- Context manager support for temporary thread setting in Python code.
|
|
22
22
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
23
|
-
-
|
|
23
|
+
- Includes a pytest plugin.
|
|
24
|
+
- Tiny (~7 KB) and dependency-free.
|
|
24
25
|
|
|
25
26
|
<!-- toc-start -->
|
|
26
27
|
|
|
@@ -37,6 +38,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
37
38
|
- [Windows (PowerShell)](#windows-powershell)
|
|
38
39
|
- [Get the number of threads](#get-the-number-of-threads)
|
|
39
40
|
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
41
|
+
- [:scales: Comparison with threadpoolctl](#scales-comparison-with-threadpoolctl)
|
|
40
42
|
- [:question: Getting Help](#question-getting-help)
|
|
41
43
|
|
|
42
44
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -72,6 +74,7 @@ This will set the number of threads using the following environment variables:
|
|
|
72
74
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
73
75
|
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
74
76
|
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
77
|
+
- Numba (via `NUMBA_NUM_THREADS`)
|
|
75
78
|
|
|
76
79
|
or use it as a context manager:
|
|
77
80
|
|
|
@@ -104,6 +107,8 @@ with omp_num_threads(4):
|
|
|
104
107
|
pass
|
|
105
108
|
```
|
|
106
109
|
|
|
110
|
+
Use `omp_get_max_threads()` to see the configured upper bound for the next OpenMP parallel region. `omp_get_num_threads()` reports the current region's thread count (1 outside a parallel region).
|
|
111
|
+
|
|
107
112
|
### Command Line Interface
|
|
108
113
|
|
|
109
114
|
After installing `numthreads`, you can easily set the number of threads used by supported libraries via the command line. For example, to print the command to set the number of threads to 4, run:
|
|
@@ -123,7 +128,7 @@ Which will print the following:
|
|
|
123
128
|
<!-- OUTPUT:START -->
|
|
124
129
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
125
130
|
```bash
|
|
126
|
-
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
|
|
131
|
+
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4' ; export NUMBA_NUM_THREADS='4'
|
|
127
132
|
```
|
|
128
133
|
|
|
129
134
|
<!-- OUTPUT:END -->
|
|
@@ -170,6 +175,7 @@ MKL_NUM_THREADS: 12
|
|
|
170
175
|
OMP_NUM_THREADS: 12
|
|
171
176
|
NUMEXPR_NUM_THREADS: 12
|
|
172
177
|
VECLIB_MAXIMUM_THREADS: 12
|
|
178
|
+
NUMBA_NUM_THREADS: 12
|
|
173
179
|
```
|
|
174
180
|
|
|
175
181
|
<!-- OUTPUT:END -->
|
|
@@ -186,7 +192,7 @@ Check the `numthreads -h` message for more information:
|
|
|
186
192
|
```bash
|
|
187
193
|
usage: numthreads [-h] n
|
|
188
194
|
|
|
189
|
-
Set
|
|
195
|
+
Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
190
196
|
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
191
197
|
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
192
198
|
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
@@ -212,7 +218,13 @@ To use it, make sure `numthreads` is installed. Then, run pytest with the `--num
|
|
|
212
218
|
pytest --numthreads 1
|
|
213
219
|
```
|
|
214
220
|
|
|
215
|
-
This
|
|
221
|
+
This sets the thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba before pytest imports its initial `conftest.py` files. You can also put `--numthreads 1` in pytest's `addopts` configuration.
|
|
222
|
+
|
|
223
|
+
## :scales: Comparison with threadpoolctl
|
|
224
|
+
|
|
225
|
+
`numthreads` sets thread-control environment variables before importing numerical libraries. It also provides a CLI, a pytest plugin, and lightweight OpenMP helpers via `ctypes`, all in a single ~7 KB file with no dependencies. Use it when you control how a script or test suite starts.
|
|
226
|
+
|
|
227
|
+
[`threadpoolctl`](https://github.com/joblib/threadpoolctl) discovers already-loaded BLAS and OpenMP shared libraries and inspects or limits their thread pools at runtime through their C APIs. Use it when the libraries are already loaded or you need to inspect their thread pools. The tools complement each other: set defaults at startup with `numthreads`, then inspect or adjust loaded pools with `threadpoolctl`.
|
|
216
228
|
|
|
217
229
|
## :question: Getting Help
|
|
218
230
|
|
|
@@ -1,27 +1,29 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: numthreads
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
5
5
|
Author-email: Bas Nijholt <bas@nijho.lt>
|
|
6
|
+
License-Expression: MIT
|
|
6
7
|
Project-URL: Homepage, https://github.com/basnijholt/numthreads
|
|
8
|
+
Project-URL: Repository, https://github.com/basnijholt/numthreads
|
|
9
|
+
Project-URL: Documentation, https://numthreads.readthedocs.io/
|
|
10
|
+
Project-URL: Issues, https://github.com/basnijholt/numthreads/issues
|
|
7
11
|
Classifier: Development Status :: 4 - Beta
|
|
8
12
|
Classifier: Framework :: Pytest
|
|
9
13
|
Classifier: Intended Audience :: Developers
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
14
|
Classifier: Operating System :: OS Independent
|
|
12
15
|
Classifier: Programming Language :: Python :: 3
|
|
13
16
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
18
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
20
22
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
23
|
Classifier: Topic :: Software Development :: Testing
|
|
22
24
|
Classifier: Topic :: System :: Hardware :: Symmetric Multi-processing
|
|
23
25
|
Classifier: Topic :: Utilities
|
|
24
|
-
Requires-Python: >=3.
|
|
26
|
+
Requires-Python: >=3.11
|
|
25
27
|
Description-Content-Type: text/markdown
|
|
26
28
|
License-File: LICENSE
|
|
27
29
|
Provides-Extra: docs
|
|
@@ -33,14 +35,14 @@ Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
|
|
|
33
35
|
Provides-Extra: test
|
|
34
36
|
Requires-Dist: pytest; extra == "test"
|
|
35
37
|
Requires-Dist: pre-commit; extra == "test"
|
|
36
|
-
Requires-Dist: coverage; extra == "test"
|
|
38
|
+
Requires-Dist: coverage>=7.10; extra == "test"
|
|
37
39
|
Requires-Dist: pytest-cov; extra == "test"
|
|
38
|
-
|
|
40
|
+
Dynamic: license-file
|
|
39
41
|
|
|
40
42
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
41
43
|
|
|
42
44
|
<h1 align="center">numthreads</h1>
|
|
43
|
-
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and
|
|
45
|
+
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba</h3>
|
|
44
46
|
|
|
45
47
|
[](https://pypi.python.org/pypi/numthreads)
|
|
46
48
|
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
@@ -48,7 +50,7 @@ Requires-Dist: pytest-mock; extra == "test"
|
|
|
48
50
|
[](https://github.com/basnijholt/numthreads)
|
|
49
51
|
[](https://numthreads.readthedocs.io/)
|
|
50
52
|
|
|
51
|
-
`numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and
|
|
53
|
+
`numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, Accelerate, and Numba.
|
|
52
54
|
The number of threads can be set via the command line or in Python code.
|
|
53
55
|
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
54
56
|
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
@@ -56,10 +58,11 @@ In some instances, using more threads may actually impede computational efficien
|
|
|
56
58
|
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
57
59
|
|
|
58
60
|
- Simple and straightforward command-line interface.
|
|
59
|
-
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and
|
|
61
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, Accelerate, and Numba.
|
|
60
62
|
- Context manager support for temporary thread setting in Python code.
|
|
61
63
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
62
|
-
-
|
|
64
|
+
- Includes a pytest plugin.
|
|
65
|
+
- Tiny (~7 KB) and dependency-free.
|
|
63
66
|
|
|
64
67
|
<!-- toc-start -->
|
|
65
68
|
|
|
@@ -76,6 +79,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
76
79
|
- [Windows (PowerShell)](#windows-powershell)
|
|
77
80
|
- [Get the number of threads](#get-the-number-of-threads)
|
|
78
81
|
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
82
|
+
- [:scales: Comparison with threadpoolctl](#scales-comparison-with-threadpoolctl)
|
|
79
83
|
- [:question: Getting Help](#question-getting-help)
|
|
80
84
|
|
|
81
85
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -111,6 +115,7 @@ This will set the number of threads using the following environment variables:
|
|
|
111
115
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
112
116
|
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
113
117
|
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
118
|
+
- Numba (via `NUMBA_NUM_THREADS`)
|
|
114
119
|
|
|
115
120
|
or use it as a context manager:
|
|
116
121
|
|
|
@@ -143,6 +148,8 @@ with omp_num_threads(4):
|
|
|
143
148
|
pass
|
|
144
149
|
```
|
|
145
150
|
|
|
151
|
+
Use `omp_get_max_threads()` to see the configured upper bound for the next OpenMP parallel region. `omp_get_num_threads()` reports the current region's thread count (1 outside a parallel region).
|
|
152
|
+
|
|
146
153
|
### Command Line Interface
|
|
147
154
|
|
|
148
155
|
After installing `numthreads`, you can easily set the number of threads used by supported libraries via the command line. For example, to print the command to set the number of threads to 4, run:
|
|
@@ -162,7 +169,7 @@ Which will print the following:
|
|
|
162
169
|
<!-- OUTPUT:START -->
|
|
163
170
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
164
171
|
```bash
|
|
165
|
-
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
|
|
172
|
+
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4' ; export NUMBA_NUM_THREADS='4'
|
|
166
173
|
```
|
|
167
174
|
|
|
168
175
|
<!-- OUTPUT:END -->
|
|
@@ -209,6 +216,7 @@ MKL_NUM_THREADS: 12
|
|
|
209
216
|
OMP_NUM_THREADS: 12
|
|
210
217
|
NUMEXPR_NUM_THREADS: 12
|
|
211
218
|
VECLIB_MAXIMUM_THREADS: 12
|
|
219
|
+
NUMBA_NUM_THREADS: 12
|
|
212
220
|
```
|
|
213
221
|
|
|
214
222
|
<!-- OUTPUT:END -->
|
|
@@ -225,7 +233,7 @@ Check the `numthreads -h` message for more information:
|
|
|
225
233
|
```bash
|
|
226
234
|
usage: numthreads [-h] n
|
|
227
235
|
|
|
228
|
-
Set
|
|
236
|
+
Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
229
237
|
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
230
238
|
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
231
239
|
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
@@ -251,7 +259,13 @@ To use it, make sure `numthreads` is installed. Then, run pytest with the `--num
|
|
|
251
259
|
pytest --numthreads 1
|
|
252
260
|
```
|
|
253
261
|
|
|
254
|
-
This
|
|
262
|
+
This sets the thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba before pytest imports its initial `conftest.py` files. You can also put `--numthreads 1` in pytest's `addopts` configuration.
|
|
263
|
+
|
|
264
|
+
## :scales: Comparison with threadpoolctl
|
|
265
|
+
|
|
266
|
+
`numthreads` sets thread-control environment variables before importing numerical libraries. It also provides a CLI, a pytest plugin, and lightweight OpenMP helpers via `ctypes`, all in a single ~7 KB file with no dependencies. Use it when you control how a script or test suite starts.
|
|
267
|
+
|
|
268
|
+
[`threadpoolctl`](https://github.com/joblib/threadpoolctl) discovers already-loaded BLAS and OpenMP shared libraries and inspects or limits their thread pools at runtime through their C APIs. Use it when the libraries are already loaded or you need to inspect their thread pools. The tools complement each other: set defaults at startup with `numthreads`, then inspect or adjust loaded pools with `threadpoolctl`.
|
|
255
269
|
|
|
256
270
|
## :question: Getting Help
|
|
257
271
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Set thread counts for OpenBLAS, MKL, OpenMP, NumExpr, Accelerate, and Numba."""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
@@ -15,33 +15,47 @@ if TYPE_CHECKING:
|
|
|
15
15
|
|
|
16
16
|
import pytest
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
__all__ = [
|
|
19
|
+
"THREAD_CONTROL_ENV_VARS",
|
|
20
|
+
"__version__",
|
|
21
|
+
"main",
|
|
22
|
+
"num_threads",
|
|
23
|
+
"omp_get_max_threads",
|
|
24
|
+
"omp_get_num_threads",
|
|
25
|
+
"omp_num_threads",
|
|
26
|
+
"omp_set_num_threads",
|
|
27
|
+
"print_current_thread_counts",
|
|
28
|
+
"pytest_addoption",
|
|
29
|
+
"pytest_configure",
|
|
30
|
+
"pytest_load_initial_conftests",
|
|
31
|
+
"set",
|
|
32
|
+
"set_num_threads",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
__version__ = "0.7.0"
|
|
19
36
|
|
|
20
37
|
THREAD_CONTROL_ENV_VARS = [
|
|
21
|
-
"OPENBLAS_NUM_THREADS",
|
|
22
|
-
"MKL_NUM_THREADS",
|
|
23
|
-
"OMP_NUM_THREADS",
|
|
24
|
-
"NUMEXPR_NUM_THREADS",
|
|
25
|
-
"VECLIB_MAXIMUM_THREADS",
|
|
38
|
+
"OPENBLAS_NUM_THREADS",
|
|
39
|
+
"MKL_NUM_THREADS",
|
|
40
|
+
"OMP_NUM_THREADS",
|
|
41
|
+
"NUMEXPR_NUM_THREADS",
|
|
42
|
+
"VECLIB_MAXIMUM_THREADS",
|
|
43
|
+
"NUMBA_NUM_THREADS",
|
|
26
44
|
]
|
|
27
45
|
|
|
28
46
|
|
|
29
47
|
def set_num_threads(n: int = 1, *, overwrite: bool = True) -> None:
|
|
30
48
|
"""Set the number of threads via environment variables.
|
|
31
49
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- ``"OPENBLAS_NUM_THREADS"`` for OpenBLAS (Open Basic Linear Algebra Subprograms)
|
|
50
|
+
- ``"OPENBLAS_NUM_THREADS"`` for OpenBLAS
|
|
35
51
|
- ``"MKL_NUM_THREADS"`` for MKL (Intel Math Kernel Library)
|
|
36
52
|
- ``"OMP_NUM_THREADS"`` for OMP (OpenMP)
|
|
37
|
-
- ``"NUMEXPR_NUM_THREADS"`` for NumExpr
|
|
53
|
+
- ``"NUMEXPR_NUM_THREADS"`` for NumExpr
|
|
38
54
|
- ``"VECLIB_MAXIMUM_THREADS"`` for Accelerate (macOS)
|
|
55
|
+
- ``"NUMBA_NUM_THREADS"`` for Numba
|
|
39
56
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
threads for NumPy, you should set the environment variables before importing
|
|
43
|
-
NumPy. When using OMP (OpenMP), you can use the ``omp_set_num_threads`` function
|
|
44
|
-
to change the number of threads *after* importing the library.
|
|
57
|
+
Set these variables before importing the relevant libraries, such as NumPy.
|
|
58
|
+
For OpenMP, use ``omp_set_num_threads`` after import.
|
|
45
59
|
|
|
46
60
|
Parameters
|
|
47
61
|
----------
|
|
@@ -56,7 +70,6 @@ def set_num_threads(n: int = 1, *, overwrite: bool = True) -> None:
|
|
|
56
70
|
os.environ[var] = str(n)
|
|
57
71
|
|
|
58
72
|
|
|
59
|
-
# Function alias
|
|
60
73
|
set = set_num_threads # noqa: A001
|
|
61
74
|
|
|
62
75
|
|
|
@@ -71,7 +84,7 @@ def num_threads(n: int = 1, *, overwrite: bool = True) -> Generator[None, None,
|
|
|
71
84
|
yield
|
|
72
85
|
finally:
|
|
73
86
|
for var, value in original_settings.items():
|
|
74
|
-
if value is None:
|
|
87
|
+
if value is None:
|
|
75
88
|
os.environ.pop(var, None)
|
|
76
89
|
else:
|
|
77
90
|
os.environ[var] = value
|
|
@@ -81,29 +94,27 @@ def _load_omp_library() -> ctypes.CDLL:
|
|
|
81
94
|
"""Loads the OpenMP library based on the operating system."""
|
|
82
95
|
system = platform.system()
|
|
83
96
|
|
|
84
|
-
if system == "Darwin":
|
|
97
|
+
if system == "Darwin":
|
|
85
98
|
lib_name = "libomp.dylib"
|
|
86
|
-
elif system == "Linux":
|
|
99
|
+
elif system == "Linux":
|
|
87
100
|
lib_name = "libgomp.so.1"
|
|
88
|
-
elif system == "Windows":
|
|
101
|
+
elif system == "Windows":
|
|
89
102
|
lib_name = "libiomp5md.dll"
|
|
90
|
-
else:
|
|
103
|
+
else:
|
|
91
104
|
msg = f"Unsupported operating system: {system}"
|
|
92
105
|
raise NotImplementedError(msg)
|
|
93
106
|
|
|
94
107
|
try:
|
|
95
108
|
return ctypes.CDLL(lib_name)
|
|
96
|
-
except OSError as e:
|
|
109
|
+
except OSError as e:
|
|
97
110
|
msg = f"Error loading {lib_name}. Make sure OpenMP is installed."
|
|
98
111
|
raise OSError(msg) from e
|
|
99
112
|
|
|
100
113
|
|
|
101
114
|
def omp_set_num_threads(num_threads: int, *, overwrite: bool = True) -> None:
|
|
102
|
-
"""
|
|
115
|
+
"""Set the thread limit for future OpenMP parallel regions.
|
|
103
116
|
|
|
104
|
-
|
|
105
|
-
*after* importing the library. This overrides whatever value is set in the
|
|
106
|
-
``"OMP_NUM_THREADS"`` environment variable.
|
|
117
|
+
This overrides ``OMP_NUM_THREADS`` even after the runtime is loaded.
|
|
107
118
|
|
|
108
119
|
Parameters
|
|
109
120
|
----------
|
|
@@ -111,18 +122,33 @@ def omp_set_num_threads(num_threads: int, *, overwrite: bool = True) -> None:
|
|
|
111
122
|
Number of threads to set.
|
|
112
123
|
overwrite
|
|
113
124
|
If true, overwrite the existing value.
|
|
125
|
+
|
|
114
126
|
"""
|
|
115
127
|
if overwrite:
|
|
116
128
|
omp_lib = _load_omp_library()
|
|
117
129
|
omp_lib.omp_set_num_threads.argtypes = [ctypes.c_int]
|
|
130
|
+
omp_lib.omp_set_num_threads.restype = None
|
|
118
131
|
omp_lib.omp_set_num_threads(num_threads)
|
|
119
132
|
|
|
120
133
|
|
|
134
|
+
def omp_get_max_threads() -> int:
|
|
135
|
+
"""Return the configured upper bound for the next OpenMP parallel region.
|
|
136
|
+
|
|
137
|
+
This value is controlled by ``omp_set_num_threads`` or ``OMP_NUM_THREADS``.
|
|
138
|
+
"""
|
|
139
|
+
omp_lib = _load_omp_library()
|
|
140
|
+
omp_lib.omp_get_max_threads.restype = ctypes.c_int
|
|
141
|
+
return int(omp_lib.omp_get_max_threads())
|
|
142
|
+
|
|
143
|
+
|
|
121
144
|
def omp_get_num_threads() -> int:
|
|
122
|
-
"""
|
|
145
|
+
"""Return the current OpenMP region's thread count (1 outside a region).
|
|
146
|
+
|
|
147
|
+
Use ``omp_get_max_threads`` for the configured limit instead.
|
|
148
|
+
"""
|
|
123
149
|
omp_lib = _load_omp_library()
|
|
124
150
|
omp_lib.omp_get_num_threads.restype = ctypes.c_int
|
|
125
|
-
return omp_lib.omp_get_num_threads()
|
|
151
|
+
return int(omp_lib.omp_get_num_threads())
|
|
126
152
|
|
|
127
153
|
|
|
128
154
|
@contextlib.contextmanager
|
|
@@ -131,25 +157,16 @@ def omp_num_threads(
|
|
|
131
157
|
*,
|
|
132
158
|
overwrite: bool = True,
|
|
133
159
|
) -> Generator[None, None, None]:
|
|
134
|
-
"""
|
|
160
|
+
"""Temporarily set and then restore the OpenMP thread limit.
|
|
135
161
|
|
|
136
|
-
|
|
137
|
-
----------
|
|
138
|
-
num_threads
|
|
139
|
-
Number of threads to set in the OpenMP parallel regions.
|
|
140
|
-
overwrite
|
|
141
|
-
Whether to overwrite existing values.
|
|
142
|
-
|
|
143
|
-
Examples
|
|
144
|
-
--------
|
|
145
|
-
>>> import numpy as np
|
|
146
|
-
>>> from numthreads import omp_num_threads
|
|
147
|
-
>>> with omp_num_threads(2):
|
|
148
|
-
... np.ones(10)
|
|
149
|
-
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
|
|
162
|
+
With ``overwrite=False``, leave OpenMP untouched.
|
|
150
163
|
"""
|
|
151
|
-
|
|
152
|
-
|
|
164
|
+
if not overwrite:
|
|
165
|
+
yield
|
|
166
|
+
return
|
|
167
|
+
|
|
168
|
+
original_num_threads = omp_get_max_threads()
|
|
169
|
+
omp_set_num_threads(num_threads)
|
|
153
170
|
|
|
154
171
|
try:
|
|
155
172
|
yield
|
|
@@ -164,18 +181,16 @@ def print_current_thread_counts() -> None:
|
|
|
164
181
|
print(f"{var}: {value}")
|
|
165
182
|
|
|
166
183
|
|
|
167
|
-
def main() -> None:
|
|
184
|
+
def main() -> None:
|
|
168
185
|
"""Command-line interface."""
|
|
169
186
|
description = (
|
|
170
|
-
"Set
|
|
187
|
+
"Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba."
|
|
171
188
|
" Usage: Run `numthreads <number>` to print the export commands."
|
|
172
189
|
" On Unix-like systems (Linux, macOS, WSL), use `eval $(numthreads <number>)`"
|
|
173
190
|
" in your shell to apply these settings."
|
|
174
191
|
" On Windows, in PowerShell, use `Invoke-Expression $(numthreads <number>)`."
|
|
175
192
|
)
|
|
176
|
-
parser = argparse.ArgumentParser(
|
|
177
|
-
description=description,
|
|
178
|
-
)
|
|
193
|
+
parser = argparse.ArgumentParser(prog="numthreads", description=description)
|
|
179
194
|
parser.add_argument(
|
|
180
195
|
"n",
|
|
181
196
|
help="Number of threads to set or use 'get' to display current settings.",
|
|
@@ -186,38 +201,46 @@ def main() -> None: # pragma: no cover
|
|
|
186
201
|
print_current_thread_counts()
|
|
187
202
|
return
|
|
188
203
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
else:
|
|
197
|
-
export_commands = " ; ".join(
|
|
198
|
-
f"export {var}='{n}'" for var in THREAD_CONTROL_ENV_VARS
|
|
199
|
-
)
|
|
200
|
-
print(export_commands)
|
|
204
|
+
invalid_n = "n must be a positive integer or 'get'"
|
|
205
|
+
try:
|
|
206
|
+
n = int(args.n)
|
|
207
|
+
except ValueError:
|
|
208
|
+
parser.error(invalid_n)
|
|
209
|
+
if n < 1:
|
|
210
|
+
parser.error(invalid_n)
|
|
201
211
|
|
|
212
|
+
prefix = "$env:" if platform.system() == "Windows" else "export "
|
|
213
|
+
print(" ; ".join(f"{prefix}{var}='{n}'" for var in THREAD_CONTROL_ENV_VARS))
|
|
202
214
|
|
|
203
|
-
|
|
204
|
-
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
215
|
+
|
|
216
|
+
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
205
217
|
"""Add the --numthreads option to the pytest command line."""
|
|
206
218
|
parser.addoption(
|
|
207
219
|
"--numthreads",
|
|
208
220
|
action="store",
|
|
209
|
-
default=None,
|
|
221
|
+
default=None,
|
|
210
222
|
type=int,
|
|
211
|
-
help=
|
|
223
|
+
help=(
|
|
224
|
+
"Number of threads to set for OpenBLAS, MKL, OMP, NumExpr, "
|
|
225
|
+
"Accelerate, and Numba"
|
|
226
|
+
),
|
|
212
227
|
)
|
|
213
228
|
|
|
214
229
|
|
|
215
|
-
def pytest_configure(config: pytest.Config) -> None:
|
|
230
|
+
def pytest_configure(config: pytest.Config) -> None:
|
|
216
231
|
"""Set the number of threads based on the --numthreads option."""
|
|
232
|
+
# Fallback when a conftest loads the plugin via pytest_plugins.
|
|
217
233
|
numthreads = config.getoption("--numthreads")
|
|
218
234
|
if numthreads is not None:
|
|
219
235
|
set_num_threads(numthreads)
|
|
220
236
|
|
|
221
237
|
|
|
238
|
+
def pytest_load_initial_conftests(early_config: pytest.Config) -> None:
|
|
239
|
+
"""Set thread counts before pytest imports the initial conftest files."""
|
|
240
|
+
numthreads = early_config.known_args_namespace.numthreads
|
|
241
|
+
if numthreads is not None:
|
|
242
|
+
set_num_threads(numthreads)
|
|
243
|
+
|
|
244
|
+
|
|
222
245
|
if __name__ == "__main__":
|
|
223
246
|
main()
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=77.0.3"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "numthreads"
|
|
7
|
-
description = "Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and
|
|
7
|
+
description = "Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba."
|
|
8
8
|
dynamic = ["version"]
|
|
9
9
|
authors = [{ name = "Bas Nijholt", email = "bas@nijho.lt" }]
|
|
10
10
|
dependencies = []
|
|
11
|
-
requires-python = ">=3.
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
license = "MIT"
|
|
13
|
+
license-files = ["LICENSE"]
|
|
12
14
|
classifiers = [
|
|
13
15
|
"Development Status :: 4 - Beta",
|
|
14
16
|
"Framework :: Pytest",
|
|
15
17
|
"Intended Audience :: Developers",
|
|
16
|
-
"License :: OSI Approved :: MIT License",
|
|
17
18
|
"Operating System :: OS Independent",
|
|
18
19
|
"Programming Language :: Python :: 3",
|
|
19
20
|
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
-
"Programming Language :: Python :: 3.7",
|
|
21
|
-
"Programming Language :: Python :: 3.8",
|
|
22
|
-
"Programming Language :: Python :: 3.9",
|
|
23
|
-
"Programming Language :: Python :: 3.10",
|
|
24
21
|
"Programming Language :: Python :: 3.11",
|
|
25
22
|
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Programming Language :: Python :: 3.15",
|
|
26
26
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
27
|
"Topic :: Software Development :: Testing",
|
|
28
28
|
"Topic :: System :: Hardware :: Symmetric Multi-processing",
|
|
@@ -35,10 +35,13 @@ content-type = "text/markdown"
|
|
|
35
35
|
|
|
36
36
|
[project.urls]
|
|
37
37
|
Homepage = "https://github.com/basnijholt/numthreads"
|
|
38
|
+
Repository = "https://github.com/basnijholt/numthreads"
|
|
39
|
+
Documentation = "https://numthreads.readthedocs.io/"
|
|
40
|
+
Issues = "https://github.com/basnijholt/numthreads/issues"
|
|
38
41
|
|
|
39
42
|
[project.optional-dependencies]
|
|
40
43
|
docs = ["myst-parser", "sphinx", "furo", "emoji", "sphinx-autodoc-typehints"]
|
|
41
|
-
test = ["pytest", "pre-commit", "coverage", "pytest-cov"
|
|
44
|
+
test = ["pytest", "pre-commit", "coverage>=7.10", "pytest-cov"]
|
|
42
45
|
|
|
43
46
|
[project.scripts]
|
|
44
47
|
numthreads = "numthreads:main"
|
|
@@ -49,69 +52,51 @@ numthreads = "numthreads"
|
|
|
49
52
|
[tool.setuptools]
|
|
50
53
|
py-modules = ["numthreads"]
|
|
51
54
|
|
|
52
|
-
[tool.setuptools.packages.find]
|
|
53
|
-
include = ["numthreads.*", "numthreads"]
|
|
54
|
-
|
|
55
55
|
[tool.setuptools.dynamic]
|
|
56
56
|
version = { attr = "numthreads.__version__" }
|
|
57
57
|
|
|
58
58
|
[tool.pytest.ini_options]
|
|
59
59
|
addopts = """
|
|
60
60
|
--cov=numthreads
|
|
61
|
-
--cov-report term
|
|
61
|
+
--cov-report term-missing
|
|
62
62
|
--cov-report html
|
|
63
|
+
--cov-report xml
|
|
64
|
+
--cov-fail-under=100
|
|
63
65
|
-W error
|
|
64
66
|
-vvv
|
|
67
|
+
-p no:numthreads
|
|
65
68
|
"""
|
|
66
69
|
|
|
67
|
-
[tool.coverage.run]
|
|
68
|
-
omit = []
|
|
69
|
-
|
|
70
|
-
[tool.coverage.report]
|
|
71
|
-
exclude_lines = [
|
|
72
|
-
"pragma: no cover",
|
|
73
|
-
"raise NotImplementedError",
|
|
74
|
-
"if TYPE_CHECKING:",
|
|
75
|
-
"if __name__ == .__main__.:",
|
|
76
|
-
]
|
|
77
|
-
|
|
78
|
-
[tool.black]
|
|
79
|
-
line_length = 88
|
|
80
|
-
|
|
81
70
|
[tool.ruff]
|
|
82
71
|
line-length = 88
|
|
83
|
-
target-version = "
|
|
72
|
+
target-version = "py311"
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint]
|
|
84
75
|
select = ["ALL"]
|
|
85
76
|
ignore = [
|
|
86
77
|
"T20", # flake8-print
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"D401", # First line of docstring should be in imperative mood
|
|
94
|
-
"SLF001", # Private member accessed
|
|
95
|
-
"PLR0913", # Too many arguments in function definition
|
|
96
|
-
"TD002", # Missing author in TODO
|
|
78
|
+
"COM812", # trailing commas are handled by ruff format
|
|
79
|
+
"CPY001", # licensing is covered by LICENSE
|
|
80
|
+
"D203", # use no blank line before class docstrings
|
|
81
|
+
"D213", # start multiline docstrings on the first line
|
|
82
|
+
"D401", # first line of docstring should be imperative
|
|
83
|
+
"D402", # first line should not be the function's signature
|
|
97
84
|
]
|
|
98
85
|
|
|
99
|
-
[tool.ruff.per-file-ignores]
|
|
100
|
-
"tests/*" = ["
|
|
101
|
-
"tests/test_examples.py" = ["E501"]
|
|
102
|
-
".github/*" = ["INP001"]
|
|
103
|
-
"example/*" = ["INP001", "D100"]
|
|
86
|
+
[tool.ruff.lint.per-file-ignores]
|
|
87
|
+
"tests/*" = ["D103", "PLR2004", "S101", "SLF001"]
|
|
104
88
|
"docs/*" = ["INP001", "E501"]
|
|
105
89
|
|
|
106
|
-
[tool.ruff.mccabe]
|
|
90
|
+
[tool.ruff.lint.mccabe]
|
|
107
91
|
max-complexity = 18
|
|
108
92
|
|
|
109
93
|
[tool.mypy]
|
|
110
|
-
python_version = "3.
|
|
94
|
+
python_version = "3.11"
|
|
95
|
+
strict = true
|
|
111
96
|
|
|
112
97
|
# Use bump-my-version, e.g., call `bump-my-version bump minor`
|
|
113
98
|
[tool.bumpversion]
|
|
114
|
-
current_version = "0.
|
|
99
|
+
current_version = "0.7.0"
|
|
115
100
|
commit = true
|
|
116
101
|
commit_args = "--no-verify"
|
|
117
102
|
tag = true
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"""Tests for numthreads."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ctypes
|
|
6
|
+
import importlib.machinery
|
|
7
|
+
import os
|
|
8
|
+
import platform
|
|
9
|
+
import runpy
|
|
10
|
+
import sys
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
from unittest.mock import Mock, call
|
|
13
|
+
|
|
14
|
+
import pytest
|
|
15
|
+
|
|
16
|
+
import numthreads
|
|
17
|
+
from numthreads import THREAD_CONTROL_ENV_VARS
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from collections.abc import Callable, Iterator
|
|
21
|
+
|
|
22
|
+
pytest_plugins = ["pytester"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@pytest.fixture(autouse=True)
|
|
26
|
+
def _clean_thread_environment(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
27
|
+
for var in THREAD_CONTROL_ENV_VARS:
|
|
28
|
+
# Record the original state even if the variable was already absent.
|
|
29
|
+
monkeypatch.setenv(var, "1")
|
|
30
|
+
monkeypatch.delenv(var)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _thread_settings() -> list[str | None]:
|
|
34
|
+
return [os.environ.get(var) for var in THREAD_CONTROL_ENV_VARS]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_thread_control_env_vars() -> None:
|
|
38
|
+
assert THREAD_CONTROL_ENV_VARS == [
|
|
39
|
+
"OPENBLAS_NUM_THREADS",
|
|
40
|
+
"MKL_NUM_THREADS",
|
|
41
|
+
"OMP_NUM_THREADS",
|
|
42
|
+
"NUMEXPR_NUM_THREADS",
|
|
43
|
+
"VECLIB_MAXIMUM_THREADS",
|
|
44
|
+
"NUMBA_NUM_THREADS",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@pytest.mark.parametrize(
|
|
49
|
+
"setter", [numthreads.set_num_threads, numthreads.set], ids=["function", "alias"]
|
|
50
|
+
)
|
|
51
|
+
@pytest.mark.parametrize("overwrite", [True, False])
|
|
52
|
+
def test_set_num_threads(setter: Callable[..., None], *, overwrite: bool) -> None:
|
|
53
|
+
setter(2, overwrite=overwrite)
|
|
54
|
+
assert _thread_settings() == ["2"] * len(THREAD_CONTROL_ENV_VARS)
|
|
55
|
+
|
|
56
|
+
setter(4, overwrite=overwrite)
|
|
57
|
+
assert _thread_settings() == ["4" if overwrite else "2"] * len(
|
|
58
|
+
THREAD_CONTROL_ENV_VARS,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@pytest.mark.parametrize("overwrite", [True, False])
|
|
63
|
+
def test_num_threads(monkeypatch: pytest.MonkeyPatch, *, overwrite: bool) -> None:
|
|
64
|
+
monkeypatch.setenv(THREAD_CONTROL_ENV_VARS[0], "7")
|
|
65
|
+
original = _thread_settings()
|
|
66
|
+
|
|
67
|
+
with numthreads.num_threads(2, overwrite=overwrite):
|
|
68
|
+
assert _thread_settings() == [
|
|
69
|
+
"2" if overwrite or value is None else value for value in original
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
assert _thread_settings() == original
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_num_threads_restores_on_exception(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
76
|
+
monkeypatch.setenv(THREAD_CONTROL_ENV_VARS[0], "7")
|
|
77
|
+
original = _thread_settings()
|
|
78
|
+
msg = "boom"
|
|
79
|
+
with pytest.raises(RuntimeError, match=msg), numthreads.num_threads(2):
|
|
80
|
+
raise RuntimeError(msg)
|
|
81
|
+
assert _thread_settings() == original
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@pytest.fixture
|
|
85
|
+
def _real_openmp() -> Iterator[None]:
|
|
86
|
+
try:
|
|
87
|
+
numthreads._load_omp_library()
|
|
88
|
+
except OSError as exc:
|
|
89
|
+
if platform.system() == "Linux":
|
|
90
|
+
raise
|
|
91
|
+
pytest.skip(str(exc))
|
|
92
|
+
|
|
93
|
+
original = numthreads.omp_get_max_threads()
|
|
94
|
+
try:
|
|
95
|
+
yield
|
|
96
|
+
finally:
|
|
97
|
+
numthreads.omp_set_num_threads(original)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@pytest.mark.usefixtures("_real_openmp")
|
|
101
|
+
def test_real_openmp() -> None:
|
|
102
|
+
numthreads.omp_set_num_threads(4)
|
|
103
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
104
|
+
assert numthreads.omp_get_num_threads() == 1
|
|
105
|
+
|
|
106
|
+
numthreads.omp_set_num_threads(2, overwrite=False)
|
|
107
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
108
|
+
|
|
109
|
+
with numthreads.omp_num_threads(2):
|
|
110
|
+
assert numthreads.omp_get_max_threads() == 2
|
|
111
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
112
|
+
|
|
113
|
+
with numthreads.omp_num_threads(2, overwrite=False):
|
|
114
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
115
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
116
|
+
|
|
117
|
+
msg = "boom"
|
|
118
|
+
with pytest.raises(RuntimeError, match=msg), numthreads.omp_num_threads(2):
|
|
119
|
+
raise RuntimeError(msg)
|
|
120
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@pytest.mark.parametrize(
|
|
124
|
+
("system", "lib_name"),
|
|
125
|
+
[
|
|
126
|
+
("Linux", "libgomp.so.1"),
|
|
127
|
+
("Darwin", "libomp.dylib"),
|
|
128
|
+
("Windows", "libiomp5md.dll"),
|
|
129
|
+
],
|
|
130
|
+
)
|
|
131
|
+
def test_load_omp_library(
|
|
132
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
133
|
+
system: str,
|
|
134
|
+
lib_name: str,
|
|
135
|
+
) -> None:
|
|
136
|
+
cdll = Mock()
|
|
137
|
+
monkeypatch.setattr(platform, "system", lambda: system)
|
|
138
|
+
monkeypatch.setattr(ctypes, "CDLL", cdll)
|
|
139
|
+
assert numthreads._load_omp_library() is cdll.return_value
|
|
140
|
+
cdll.assert_called_once_with(lib_name)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def test_load_omp_library_unsupported_os(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
144
|
+
monkeypatch.setattr(platform, "system", lambda: "FreeBSD")
|
|
145
|
+
with pytest.raises(
|
|
146
|
+
NotImplementedError, match="Unsupported operating system: FreeBSD"
|
|
147
|
+
):
|
|
148
|
+
numthreads._load_omp_library()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_load_omp_library_missing_library(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
152
|
+
error = OSError("missing library")
|
|
153
|
+
monkeypatch.setattr(platform, "system", lambda: "Linux")
|
|
154
|
+
monkeypatch.setattr(ctypes, "CDLL", Mock(side_effect=error))
|
|
155
|
+
with pytest.raises(OSError, match=r"Error loading libgomp\.so\.1") as exc:
|
|
156
|
+
numthreads._load_omp_library()
|
|
157
|
+
assert exc.value.__cause__ is error
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_omp_bindings(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
161
|
+
library = Mock()
|
|
162
|
+
library.omp_get_num_threads.return_value = 1
|
|
163
|
+
library.omp_get_max_threads.return_value = 4
|
|
164
|
+
load_library = Mock(return_value=library)
|
|
165
|
+
monkeypatch.setattr(numthreads, "_load_omp_library", load_library)
|
|
166
|
+
|
|
167
|
+
numthreads.omp_set_num_threads(2, overwrite=False)
|
|
168
|
+
with numthreads.omp_num_threads(2, overwrite=False):
|
|
169
|
+
pass
|
|
170
|
+
load_library.assert_not_called()
|
|
171
|
+
|
|
172
|
+
assert numthreads.omp_get_num_threads() == 1
|
|
173
|
+
assert library.omp_get_num_threads.restype is ctypes.c_int
|
|
174
|
+
assert numthreads.omp_get_max_threads() == 4
|
|
175
|
+
assert library.omp_get_max_threads.restype is ctypes.c_int
|
|
176
|
+
|
|
177
|
+
msg = "boom"
|
|
178
|
+
with pytest.raises(RuntimeError, match=msg), numthreads.omp_num_threads(2):
|
|
179
|
+
raise RuntimeError(msg)
|
|
180
|
+
assert library.omp_set_num_threads.argtypes == [ctypes.c_int]
|
|
181
|
+
assert library.omp_set_num_threads.restype is None
|
|
182
|
+
assert library.omp_set_num_threads.call_args_list == [call(2), call(4)]
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def test_print_current_thread_counts(
|
|
186
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
187
|
+
capsys: pytest.CaptureFixture[str],
|
|
188
|
+
) -> None:
|
|
189
|
+
first, *rest = THREAD_CONTROL_ENV_VARS
|
|
190
|
+
monkeypatch.setenv(first, "4")
|
|
191
|
+
numthreads.print_current_thread_counts()
|
|
192
|
+
assert capsys.readouterr().out.splitlines() == [
|
|
193
|
+
f"{first}: 4",
|
|
194
|
+
*(f"{var}: Not set" for var in rest),
|
|
195
|
+
]
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@pytest.mark.parametrize(
|
|
199
|
+
("system", "prefix"), [("Linux", "export "), ("Windows", "$env:")]
|
|
200
|
+
)
|
|
201
|
+
def test_cli_set(
|
|
202
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
203
|
+
capsys: pytest.CaptureFixture[str],
|
|
204
|
+
system: str,
|
|
205
|
+
prefix: str,
|
|
206
|
+
) -> None:
|
|
207
|
+
monkeypatch.setattr(sys, "argv", ["numthreads", "4"])
|
|
208
|
+
monkeypatch.setattr(platform, "system", lambda: system)
|
|
209
|
+
numthreads.main()
|
|
210
|
+
assert capsys.readouterr().out.strip().split(" ; ") == [
|
|
211
|
+
f"{prefix}{var}='4'" for var in THREAD_CONTROL_ENV_VARS
|
|
212
|
+
]
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_cli_get(
|
|
216
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
217
|
+
) -> None:
|
|
218
|
+
monkeypatch.setattr(sys, "argv", ["numthreads", "get"])
|
|
219
|
+
runpy.run_path(numthreads.__file__, run_name="__main__")
|
|
220
|
+
assert capsys.readouterr().out.splitlines() == [
|
|
221
|
+
f"{var}: Not set" for var in THREAD_CONTROL_ENV_VARS
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def test_cli_help(
|
|
226
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
227
|
+
) -> None:
|
|
228
|
+
monkeypatch.setattr(sys, "argv", ["numthreads"])
|
|
229
|
+
# Simulate a ZIP launcher, whose default argparse prog includes Python.
|
|
230
|
+
monkeypatch.setattr(
|
|
231
|
+
sys.modules["__main__"],
|
|
232
|
+
"__spec__",
|
|
233
|
+
importlib.machinery.ModuleSpec("__main__", None),
|
|
234
|
+
)
|
|
235
|
+
with pytest.raises(SystemExit) as exc:
|
|
236
|
+
numthreads.main()
|
|
237
|
+
assert exc.value.code == 0
|
|
238
|
+
assert "usage: numthreads" in capsys.readouterr().out
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@pytest.mark.parametrize("arg", ["invalid", "0", "-2"])
|
|
242
|
+
def test_cli_rejects_invalid_counts(
|
|
243
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
244
|
+
capsys: pytest.CaptureFixture[str],
|
|
245
|
+
arg: str,
|
|
246
|
+
) -> None:
|
|
247
|
+
monkeypatch.setattr(sys, "argv", ["numthreads", arg])
|
|
248
|
+
with pytest.raises(SystemExit) as exc:
|
|
249
|
+
numthreads.main()
|
|
250
|
+
assert exc.value.code == 2
|
|
251
|
+
assert "n must be a positive integer or 'get'" in capsys.readouterr().err
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@pytest.mark.parametrize(
|
|
255
|
+
("cli_args", "addopts", "expected"),
|
|
256
|
+
[
|
|
257
|
+
(("--numthreads", "2"), "--numthreads 3", "2"),
|
|
258
|
+
((), "--numthreads 2", "2"),
|
|
259
|
+
((), "", None),
|
|
260
|
+
],
|
|
261
|
+
ids=["cli-overrides-addopts", "addopts", "unset"],
|
|
262
|
+
)
|
|
263
|
+
def test_pytest_sets_threads_before_conftest(
|
|
264
|
+
pytester: pytest.Pytester,
|
|
265
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
266
|
+
cli_args: tuple[str, ...],
|
|
267
|
+
addopts: str,
|
|
268
|
+
expected: str | None,
|
|
269
|
+
) -> None:
|
|
270
|
+
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
|
271
|
+
pytester.makeini(f"[pytest]\naddopts = {addopts}")
|
|
272
|
+
pytester.makeconftest(f"""
|
|
273
|
+
import os
|
|
274
|
+
from numthreads import THREAD_CONTROL_ENV_VARS
|
|
275
|
+
|
|
276
|
+
counts = {{os.environ.get(var) for var in THREAD_CONTROL_ENV_VARS}}
|
|
277
|
+
assert counts == {{{expected!r}}}
|
|
278
|
+
""")
|
|
279
|
+
pytester.makepyfile("def test_loaded(): pass")
|
|
280
|
+
|
|
281
|
+
result = pytester.runpytest_inprocess("-p", "numthreads", *cli_args)
|
|
282
|
+
assert result.ret == pytest.ExitCode.OK
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"""Tests for ``numthreads`` package."""
|
|
2
|
-
import os
|
|
3
|
-
|
|
4
|
-
import pytest
|
|
5
|
-
|
|
6
|
-
from numthreads import (
|
|
7
|
-
num_threads,
|
|
8
|
-
omp_get_num_threads,
|
|
9
|
-
omp_num_threads,
|
|
10
|
-
omp_set_num_threads,
|
|
11
|
-
print_current_thread_counts,
|
|
12
|
-
set_num_threads,
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@pytest.mark.parametrize("overwrite", [True, False])
|
|
17
|
-
def test_set_num_threads(overwrite: bool) -> None: # noqa: FBT001
|
|
18
|
-
envvars = [
|
|
19
|
-
"OPENBLAS_NUM_THREADS",
|
|
20
|
-
"MKL_NUM_THREADS",
|
|
21
|
-
"OMP_NUM_THREADS",
|
|
22
|
-
"NUMEXPR_NUM_THREADS",
|
|
23
|
-
"VECLIB_MAXIMUM_THREADS",
|
|
24
|
-
]
|
|
25
|
-
prev_values = {var: os.environ.get(var) for var in envvars}
|
|
26
|
-
set_num_threads(4, overwrite=overwrite)
|
|
27
|
-
for var in envvars:
|
|
28
|
-
if overwrite:
|
|
29
|
-
assert os.environ.get(var) == "4"
|
|
30
|
-
else:
|
|
31
|
-
assert os.environ.get(var) == prev_values[var]
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
@pytest.mark.parametrize("overwrite", [True, False])
|
|
35
|
-
def test_num_threads_context_manager(overwrite: bool) -> None: # noqa: FBT001
|
|
36
|
-
original_values = {
|
|
37
|
-
var: os.environ.get(var)
|
|
38
|
-
for var in [
|
|
39
|
-
"OPENBLAS_NUM_THREADS",
|
|
40
|
-
"MKL_NUM_THREADS",
|
|
41
|
-
"OMP_NUM_THREADS",
|
|
42
|
-
"NUMEXPR_NUM_THREADS",
|
|
43
|
-
"VECLIB_MAXIMUM_THREADS",
|
|
44
|
-
]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
with num_threads(2, overwrite=overwrite):
|
|
48
|
-
for var in [
|
|
49
|
-
"OPENBLAS_NUM_THREADS",
|
|
50
|
-
"MKL_NUM_THREADS",
|
|
51
|
-
"OMP_NUM_THREADS",
|
|
52
|
-
"NUMEXPR_NUM_THREADS",
|
|
53
|
-
"VECLIB_MAXIMUM_THREADS",
|
|
54
|
-
]:
|
|
55
|
-
if overwrite:
|
|
56
|
-
assert os.environ.get(var) == "2"
|
|
57
|
-
else:
|
|
58
|
-
assert os.environ.get(var) == original_values[var]
|
|
59
|
-
|
|
60
|
-
for var in [
|
|
61
|
-
"OPENBLAS_NUM_THREADS",
|
|
62
|
-
"MKL_NUM_THREADS",
|
|
63
|
-
"OMP_NUM_THREADS",
|
|
64
|
-
"NUMEXPR_NUM_THREADS",
|
|
65
|
-
"VECLIB_MAXIMUM_THREADS",
|
|
66
|
-
]:
|
|
67
|
-
assert os.environ.get(var) == original_values[var]
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
@pytest.mark.skipif(
|
|
71
|
-
os.name != "posix",
|
|
72
|
-
reason="OMP functions are tested only on POSIX systems",
|
|
73
|
-
)
|
|
74
|
-
@pytest.mark.parametrize("overwrite", [True, False])
|
|
75
|
-
def test_omp_set_get_num_threads(overwrite: bool) -> None: # noqa: FBT001
|
|
76
|
-
omp_set_num_threads(4, overwrite=overwrite)
|
|
77
|
-
|
|
78
|
-
# Now this will fail: assert omp_get_num_threads() == 4
|
|
79
|
-
# because we need to call this in a parallel region.
|
|
80
|
-
prev_value = omp_get_num_threads()
|
|
81
|
-
omp_set_num_threads(1, overwrite=overwrite)
|
|
82
|
-
if overwrite:
|
|
83
|
-
assert omp_get_num_threads() == 1
|
|
84
|
-
else:
|
|
85
|
-
assert omp_get_num_threads() == prev_value
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
@pytest.mark.skipif(
|
|
89
|
-
os.name != "posix",
|
|
90
|
-
reason="OMP functions are tested only on POSIX systems",
|
|
91
|
-
)
|
|
92
|
-
def test_omp_num_threads() -> None:
|
|
93
|
-
with omp_num_threads(4):
|
|
94
|
-
# Now this will fail: assert omp_get_num_threads() == 4
|
|
95
|
-
# because we need to call this in a parallel region.
|
|
96
|
-
pass
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def test_print_current_thread_counts(capsys: pytest.CaptureFixture) -> None:
|
|
100
|
-
set_num_threads(4)
|
|
101
|
-
print_current_thread_counts()
|
|
102
|
-
assert "OPENBLAS_NUM_THREADS: 4" in capsys.readouterr().out
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|