numthreads 0.6.1__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.6.1 → numthreads-0.7.0}/PKG-INFO +26 -16
- {numthreads-0.6.1 → numthreads-0.7.0}/README.md +15 -6
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.egg-info/PKG-INFO +26 -16
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.egg-info/requires.txt +1 -2
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.py +91 -69
- {numthreads-0.6.1 → numthreads-0.7.0}/pyproject.toml +29 -46
- numthreads-0.7.0/tests/test_numthreads.py +282 -0
- numthreads-0.6.1/tests/test_numthreads.py +0 -102
- {numthreads-0.6.1 → numthreads-0.7.0}/LICENSE +0 -0
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.egg-info/SOURCES.txt +0 -0
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.egg-info/dependency_links.txt +0 -0
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.egg-info/entry_points.txt +0 -0
- {numthreads-0.6.1 → numthreads-0.7.0}/numthreads.egg-info/top_level.txt +0 -0
- {numthreads-0.6.1 → numthreads-0.7.0}/setup.cfg +0 -0
|
@@ -1,27 +1,29 @@
|
|
|
1
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,15 +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
|
-
Requires-Dist: pytest-mock; extra == "test"
|
|
39
40
|
Dynamic: license-file
|
|
40
41
|
|
|
41
42
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
42
43
|
|
|
43
44
|
<h1 align="center">numthreads</h1>
|
|
44
|
-
<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>
|
|
45
46
|
|
|
46
47
|
[](https://pypi.python.org/pypi/numthreads)
|
|
47
48
|
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
@@ -57,11 +58,11 @@ In some instances, using more threads may actually impede computational efficien
|
|
|
57
58
|
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
58
59
|
|
|
59
60
|
- Simple and straightforward command-line interface.
|
|
60
|
-
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and
|
|
61
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, Accelerate, and Numba.
|
|
61
62
|
- Context manager support for temporary thread setting in Python code.
|
|
62
63
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
63
|
-
-
|
|
64
|
-
- Tiny (
|
|
64
|
+
- Includes a pytest plugin.
|
|
65
|
+
- Tiny (~7 KB) and dependency-free.
|
|
65
66
|
|
|
66
67
|
<!-- toc-start -->
|
|
67
68
|
|
|
@@ -78,6 +79,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
78
79
|
- [Windows (PowerShell)](#windows-powershell)
|
|
79
80
|
- [Get the number of threads](#get-the-number-of-threads)
|
|
80
81
|
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
82
|
+
- [:scales: Comparison with threadpoolctl](#scales-comparison-with-threadpoolctl)
|
|
81
83
|
- [:question: Getting Help](#question-getting-help)
|
|
82
84
|
|
|
83
85
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -146,6 +148,8 @@ with omp_num_threads(4):
|
|
|
146
148
|
pass
|
|
147
149
|
```
|
|
148
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
|
+
|
|
149
153
|
### Command Line Interface
|
|
150
154
|
|
|
151
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:
|
|
@@ -229,7 +233,7 @@ Check the `numthreads -h` message for more information:
|
|
|
229
233
|
```bash
|
|
230
234
|
usage: numthreads [-h] n
|
|
231
235
|
|
|
232
|
-
Set
|
|
236
|
+
Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
233
237
|
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
234
238
|
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
235
239
|
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
@@ -255,7 +259,13 @@ To use it, make sure `numthreads` is installed. Then, run pytest with the `--num
|
|
|
255
259
|
pytest --numthreads 1
|
|
256
260
|
```
|
|
257
261
|
|
|
258
|
-
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`.
|
|
259
269
|
|
|
260
270
|
## :question: Getting Help
|
|
261
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)
|
|
@@ -17,11 +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
|
-
-
|
|
24
|
-
- Tiny (
|
|
23
|
+
- Includes a pytest plugin.
|
|
24
|
+
- Tiny (~7 KB) and dependency-free.
|
|
25
25
|
|
|
26
26
|
<!-- toc-start -->
|
|
27
27
|
|
|
@@ -38,6 +38,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
38
38
|
- [Windows (PowerShell)](#windows-powershell)
|
|
39
39
|
- [Get the number of threads](#get-the-number-of-threads)
|
|
40
40
|
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
41
|
+
- [:scales: Comparison with threadpoolctl](#scales-comparison-with-threadpoolctl)
|
|
41
42
|
- [:question: Getting Help](#question-getting-help)
|
|
42
43
|
|
|
43
44
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -106,6 +107,8 @@ with omp_num_threads(4):
|
|
|
106
107
|
pass
|
|
107
108
|
```
|
|
108
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
|
+
|
|
109
112
|
### Command Line Interface
|
|
110
113
|
|
|
111
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:
|
|
@@ -189,7 +192,7 @@ Check the `numthreads -h` message for more information:
|
|
|
189
192
|
```bash
|
|
190
193
|
usage: numthreads [-h] n
|
|
191
194
|
|
|
192
|
-
Set
|
|
195
|
+
Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
193
196
|
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
194
197
|
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
195
198
|
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
@@ -215,7 +218,13 @@ To use it, make sure `numthreads` is installed. Then, run pytest with the `--num
|
|
|
215
218
|
pytest --numthreads 1
|
|
216
219
|
```
|
|
217
220
|
|
|
218
|
-
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`.
|
|
219
228
|
|
|
220
229
|
## :question: Getting Help
|
|
221
230
|
|
|
@@ -1,27 +1,29 @@
|
|
|
1
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,15 +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
|
-
Requires-Dist: pytest-mock; extra == "test"
|
|
39
40
|
Dynamic: license-file
|
|
40
41
|
|
|
41
42
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
42
43
|
|
|
43
44
|
<h1 align="center">numthreads</h1>
|
|
44
|
-
<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>
|
|
45
46
|
|
|
46
47
|
[](https://pypi.python.org/pypi/numthreads)
|
|
47
48
|
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
@@ -57,11 +58,11 @@ In some instances, using more threads may actually impede computational efficien
|
|
|
57
58
|
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
58
59
|
|
|
59
60
|
- Simple and straightforward command-line interface.
|
|
60
|
-
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and
|
|
61
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, Accelerate, and Numba.
|
|
61
62
|
- Context manager support for temporary thread setting in Python code.
|
|
62
63
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
63
|
-
-
|
|
64
|
-
- Tiny (
|
|
64
|
+
- Includes a pytest plugin.
|
|
65
|
+
- Tiny (~7 KB) and dependency-free.
|
|
65
66
|
|
|
66
67
|
<!-- toc-start -->
|
|
67
68
|
|
|
@@ -78,6 +79,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
78
79
|
- [Windows (PowerShell)](#windows-powershell)
|
|
79
80
|
- [Get the number of threads](#get-the-number-of-threads)
|
|
80
81
|
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
82
|
+
- [:scales: Comparison with threadpoolctl](#scales-comparison-with-threadpoolctl)
|
|
81
83
|
- [:question: Getting Help](#question-getting-help)
|
|
82
84
|
|
|
83
85
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -146,6 +148,8 @@ with omp_num_threads(4):
|
|
|
146
148
|
pass
|
|
147
149
|
```
|
|
148
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
|
+
|
|
149
153
|
### Command Line Interface
|
|
150
154
|
|
|
151
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:
|
|
@@ -229,7 +233,7 @@ Check the `numthreads -h` message for more information:
|
|
|
229
233
|
```bash
|
|
230
234
|
usage: numthreads [-h] n
|
|
231
235
|
|
|
232
|
-
Set
|
|
236
|
+
Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
|
|
233
237
|
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
234
238
|
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
235
239
|
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
@@ -255,7 +259,13 @@ To use it, make sure `numthreads` is installed. Then, run pytest with the `--num
|
|
|
255
259
|
pytest --numthreads 1
|
|
256
260
|
```
|
|
257
261
|
|
|
258
|
-
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`.
|
|
259
269
|
|
|
260
270
|
## :question: Getting Help
|
|
261
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,34 +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",
|
|
26
|
-
"NUMBA_NUM_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",
|
|
27
44
|
]
|
|
28
45
|
|
|
29
46
|
|
|
30
47
|
def set_num_threads(n: int = 1, *, overwrite: bool = True) -> None:
|
|
31
48
|
"""Set the number of threads via environment variables.
|
|
32
49
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
- ``"OPENBLAS_NUM_THREADS"`` for OpenBLAS (Open Basic Linear Algebra Subprograms)
|
|
50
|
+
- ``"OPENBLAS_NUM_THREADS"`` for OpenBLAS
|
|
36
51
|
- ``"MKL_NUM_THREADS"`` for MKL (Intel Math Kernel Library)
|
|
37
52
|
- ``"OMP_NUM_THREADS"`` for OMP (OpenMP)
|
|
38
|
-
- ``"NUMEXPR_NUM_THREADS"`` for NumExpr
|
|
53
|
+
- ``"NUMEXPR_NUM_THREADS"`` for NumExpr
|
|
39
54
|
- ``"VECLIB_MAXIMUM_THREADS"`` for Accelerate (macOS)
|
|
55
|
+
- ``"NUMBA_NUM_THREADS"`` for Numba
|
|
40
56
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
threads for NumPy, you should set the environment variables before importing
|
|
44
|
-
NumPy. When using OMP (OpenMP), you can use the ``omp_set_num_threads`` function
|
|
45
|
-
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.
|
|
46
59
|
|
|
47
60
|
Parameters
|
|
48
61
|
----------
|
|
@@ -57,7 +70,6 @@ def set_num_threads(n: int = 1, *, overwrite: bool = True) -> None:
|
|
|
57
70
|
os.environ[var] = str(n)
|
|
58
71
|
|
|
59
72
|
|
|
60
|
-
# Function alias
|
|
61
73
|
set = set_num_threads # noqa: A001
|
|
62
74
|
|
|
63
75
|
|
|
@@ -72,7 +84,7 @@ def num_threads(n: int = 1, *, overwrite: bool = True) -> Generator[None, None,
|
|
|
72
84
|
yield
|
|
73
85
|
finally:
|
|
74
86
|
for var, value in original_settings.items():
|
|
75
|
-
if value is None:
|
|
87
|
+
if value is None:
|
|
76
88
|
os.environ.pop(var, None)
|
|
77
89
|
else:
|
|
78
90
|
os.environ[var] = value
|
|
@@ -82,29 +94,27 @@ def _load_omp_library() -> ctypes.CDLL:
|
|
|
82
94
|
"""Loads the OpenMP library based on the operating system."""
|
|
83
95
|
system = platform.system()
|
|
84
96
|
|
|
85
|
-
if system == "Darwin":
|
|
97
|
+
if system == "Darwin":
|
|
86
98
|
lib_name = "libomp.dylib"
|
|
87
|
-
elif system == "Linux":
|
|
99
|
+
elif system == "Linux":
|
|
88
100
|
lib_name = "libgomp.so.1"
|
|
89
|
-
elif system == "Windows":
|
|
101
|
+
elif system == "Windows":
|
|
90
102
|
lib_name = "libiomp5md.dll"
|
|
91
|
-
else:
|
|
103
|
+
else:
|
|
92
104
|
msg = f"Unsupported operating system: {system}"
|
|
93
105
|
raise NotImplementedError(msg)
|
|
94
106
|
|
|
95
107
|
try:
|
|
96
108
|
return ctypes.CDLL(lib_name)
|
|
97
|
-
except OSError as e:
|
|
109
|
+
except OSError as e:
|
|
98
110
|
msg = f"Error loading {lib_name}. Make sure OpenMP is installed."
|
|
99
111
|
raise OSError(msg) from e
|
|
100
112
|
|
|
101
113
|
|
|
102
114
|
def omp_set_num_threads(num_threads: int, *, overwrite: bool = True) -> None:
|
|
103
|
-
"""
|
|
115
|
+
"""Set the thread limit for future OpenMP parallel regions.
|
|
104
116
|
|
|
105
|
-
|
|
106
|
-
*after* importing the library. This overrides whatever value is set in the
|
|
107
|
-
``"OMP_NUM_THREADS"`` environment variable.
|
|
117
|
+
This overrides ``OMP_NUM_THREADS`` even after the runtime is loaded.
|
|
108
118
|
|
|
109
119
|
Parameters
|
|
110
120
|
----------
|
|
@@ -112,18 +122,33 @@ def omp_set_num_threads(num_threads: int, *, overwrite: bool = True) -> None:
|
|
|
112
122
|
Number of threads to set.
|
|
113
123
|
overwrite
|
|
114
124
|
If true, overwrite the existing value.
|
|
125
|
+
|
|
115
126
|
"""
|
|
116
127
|
if overwrite:
|
|
117
128
|
omp_lib = _load_omp_library()
|
|
118
129
|
omp_lib.omp_set_num_threads.argtypes = [ctypes.c_int]
|
|
130
|
+
omp_lib.omp_set_num_threads.restype = None
|
|
119
131
|
omp_lib.omp_set_num_threads(num_threads)
|
|
120
132
|
|
|
121
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
|
+
|
|
122
144
|
def omp_get_num_threads() -> int:
|
|
123
|
-
"""
|
|
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
|
+
"""
|
|
124
149
|
omp_lib = _load_omp_library()
|
|
125
150
|
omp_lib.omp_get_num_threads.restype = ctypes.c_int
|
|
126
|
-
return omp_lib.omp_get_num_threads()
|
|
151
|
+
return int(omp_lib.omp_get_num_threads())
|
|
127
152
|
|
|
128
153
|
|
|
129
154
|
@contextlib.contextmanager
|
|
@@ -132,25 +157,16 @@ def omp_num_threads(
|
|
|
132
157
|
*,
|
|
133
158
|
overwrite: bool = True,
|
|
134
159
|
) -> Generator[None, None, None]:
|
|
135
|
-
"""
|
|
160
|
+
"""Temporarily set and then restore the OpenMP thread limit.
|
|
136
161
|
|
|
137
|
-
|
|
138
|
-
----------
|
|
139
|
-
num_threads
|
|
140
|
-
Number of threads to set in the OpenMP parallel regions.
|
|
141
|
-
overwrite
|
|
142
|
-
Whether to overwrite existing values.
|
|
143
|
-
|
|
144
|
-
Examples
|
|
145
|
-
--------
|
|
146
|
-
>>> import numpy as np
|
|
147
|
-
>>> from numthreads import omp_num_threads
|
|
148
|
-
>>> with omp_num_threads(2):
|
|
149
|
-
... np.ones(10)
|
|
150
|
-
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
|
|
162
|
+
With ``overwrite=False``, leave OpenMP untouched.
|
|
151
163
|
"""
|
|
152
|
-
|
|
153
|
-
|
|
164
|
+
if not overwrite:
|
|
165
|
+
yield
|
|
166
|
+
return
|
|
167
|
+
|
|
168
|
+
original_num_threads = omp_get_max_threads()
|
|
169
|
+
omp_set_num_threads(num_threads)
|
|
154
170
|
|
|
155
171
|
try:
|
|
156
172
|
yield
|
|
@@ -165,18 +181,16 @@ def print_current_thread_counts() -> None:
|
|
|
165
181
|
print(f"{var}: {value}")
|
|
166
182
|
|
|
167
183
|
|
|
168
|
-
def main() -> None:
|
|
184
|
+
def main() -> None:
|
|
169
185
|
"""Command-line interface."""
|
|
170
186
|
description = (
|
|
171
|
-
"Set
|
|
187
|
+
"Set thread counts for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba."
|
|
172
188
|
" Usage: Run `numthreads <number>` to print the export commands."
|
|
173
189
|
" On Unix-like systems (Linux, macOS, WSL), use `eval $(numthreads <number>)`"
|
|
174
190
|
" in your shell to apply these settings."
|
|
175
191
|
" On Windows, in PowerShell, use `Invoke-Expression $(numthreads <number>)`."
|
|
176
192
|
)
|
|
177
|
-
parser = argparse.ArgumentParser(
|
|
178
|
-
description=description,
|
|
179
|
-
)
|
|
193
|
+
parser = argparse.ArgumentParser(prog="numthreads", description=description)
|
|
180
194
|
parser.add_argument(
|
|
181
195
|
"n",
|
|
182
196
|
help="Number of threads to set or use 'get' to display current settings.",
|
|
@@ -187,38 +201,46 @@ def main() -> None: # pragma: no cover
|
|
|
187
201
|
print_current_thread_counts()
|
|
188
202
|
return
|
|
189
203
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
else:
|
|
198
|
-
export_commands = " ; ".join(
|
|
199
|
-
f"export {var}='{n}'" for var in THREAD_CONTROL_ENV_VARS
|
|
200
|
-
)
|
|
201
|
-
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)
|
|
202
211
|
|
|
212
|
+
prefix = "$env:" if platform.system() == "Windows" else "export "
|
|
213
|
+
print(" ; ".join(f"{prefix}{var}='{n}'" for var in THREAD_CONTROL_ENV_VARS))
|
|
203
214
|
|
|
204
|
-
|
|
205
|
-
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
215
|
+
|
|
216
|
+
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
206
217
|
"""Add the --numthreads option to the pytest command line."""
|
|
207
218
|
parser.addoption(
|
|
208
219
|
"--numthreads",
|
|
209
220
|
action="store",
|
|
210
|
-
default=None,
|
|
221
|
+
default=None,
|
|
211
222
|
type=int,
|
|
212
|
-
help=
|
|
223
|
+
help=(
|
|
224
|
+
"Number of threads to set for OpenBLAS, MKL, OMP, NumExpr, "
|
|
225
|
+
"Accelerate, and Numba"
|
|
226
|
+
),
|
|
213
227
|
)
|
|
214
228
|
|
|
215
229
|
|
|
216
|
-
def pytest_configure(config: pytest.Config) -> None:
|
|
230
|
+
def pytest_configure(config: pytest.Config) -> None:
|
|
217
231
|
"""Set the number of threads based on the --numthreads option."""
|
|
232
|
+
# Fallback when a conftest loads the plugin via pytest_plugins.
|
|
218
233
|
numthreads = config.getoption("--numthreads")
|
|
219
234
|
if numthreads is not None:
|
|
220
235
|
set_num_threads(numthreads)
|
|
221
236
|
|
|
222
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
|
+
|
|
223
245
|
if __name__ == "__main__":
|
|
224
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,71 +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
63
|
--cov-report xml
|
|
64
|
+
--cov-fail-under=100
|
|
64
65
|
-W error
|
|
65
66
|
-vvv
|
|
66
67
|
-p no:numthreads
|
|
67
68
|
"""
|
|
68
69
|
|
|
69
|
-
[tool.coverage.run]
|
|
70
|
-
omit = []
|
|
71
|
-
|
|
72
|
-
[tool.coverage.report]
|
|
73
|
-
exclude_lines = [
|
|
74
|
-
"pragma: no cover",
|
|
75
|
-
"raise NotImplementedError",
|
|
76
|
-
"if TYPE_CHECKING:",
|
|
77
|
-
"if __name__ == .__main__.:",
|
|
78
|
-
]
|
|
79
|
-
|
|
80
|
-
[tool.black]
|
|
81
|
-
line_length = 88
|
|
82
|
-
|
|
83
70
|
[tool.ruff]
|
|
84
71
|
line-length = 88
|
|
85
|
-
target-version = "
|
|
72
|
+
target-version = "py311"
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint]
|
|
86
75
|
select = ["ALL"]
|
|
87
76
|
ignore = [
|
|
88
77
|
"T20", # flake8-print
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"D401", # First line of docstring should be in imperative mood
|
|
96
|
-
"SLF001", # Private member accessed
|
|
97
|
-
"PLR0913", # Too many arguments in function definition
|
|
98
|
-
"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
|
|
99
84
|
]
|
|
100
85
|
|
|
101
|
-
[tool.ruff.per-file-ignores]
|
|
102
|
-
"tests/*" = ["
|
|
103
|
-
"tests/test_examples.py" = ["E501"]
|
|
104
|
-
".github/*" = ["INP001"]
|
|
105
|
-
"example/*" = ["INP001", "D100"]
|
|
86
|
+
[tool.ruff.lint.per-file-ignores]
|
|
87
|
+
"tests/*" = ["D103", "PLR2004", "S101", "SLF001"]
|
|
106
88
|
"docs/*" = ["INP001", "E501"]
|
|
107
89
|
|
|
108
|
-
[tool.ruff.mccabe]
|
|
90
|
+
[tool.ruff.lint.mccabe]
|
|
109
91
|
max-complexity = 18
|
|
110
92
|
|
|
111
93
|
[tool.mypy]
|
|
112
|
-
python_version = "3.
|
|
94
|
+
python_version = "3.11"
|
|
95
|
+
strict = true
|
|
113
96
|
|
|
114
97
|
# Use bump-my-version, e.g., call `bump-my-version bump minor`
|
|
115
98
|
[tool.bumpversion]
|
|
116
|
-
current_version = "0.
|
|
99
|
+
current_version = "0.7.0"
|
|
117
100
|
commit = true
|
|
118
101
|
commit_args = "--no-verify"
|
|
119
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
|