numthreads 0.4.0__tar.gz → 0.6.1__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.4.0/numthreads.egg-info → numthreads-0.6.1}/PKG-INFO +109 -12
- numthreads-0.4.0/PKG-INFO → numthreads-0.6.1/README.md +89 -32
- numthreads-0.6.1/numthreads.egg-info/PKG-INFO +262 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/numthreads.egg-info/entry_points.txt +3 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/numthreads.py +68 -16
- {numthreads-0.4.0 → numthreads-0.6.1}/pyproject.toml +26 -15
- {numthreads-0.4.0 → numthreads-0.6.1}/tests/test_numthreads.py +34 -13
- numthreads-0.4.0/README.md +0 -143
- {numthreads-0.4.0 → numthreads-0.6.1}/LICENSE +0 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/numthreads.egg-info/SOURCES.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/numthreads.egg-info/dependency_links.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/numthreads.egg-info/requires.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/numthreads.egg-info/top_level.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.6.1}/setup.cfg +0 -0
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: numthreads
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.1
|
|
4
4
|
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
5
5
|
Author-email: Bas Nijholt <bas@nijho.lt>
|
|
6
6
|
Project-URL: Homepage, https://github.com/basnijholt/numthreads
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Framework :: Pytest
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
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
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: Software Development :: Testing
|
|
22
|
+
Classifier: Topic :: System :: Hardware :: Symmetric Multi-processing
|
|
23
|
+
Classifier: Topic :: Utilities
|
|
7
24
|
Requires-Python: >=3.7
|
|
8
25
|
Description-Content-Type: text/markdown
|
|
9
26
|
License-File: LICENSE
|
|
@@ -19,6 +36,7 @@ Requires-Dist: pre-commit; extra == "test"
|
|
|
19
36
|
Requires-Dist: coverage; extra == "test"
|
|
20
37
|
Requires-Dist: pytest-cov; extra == "test"
|
|
21
38
|
Requires-Dist: pytest-mock; extra == "test"
|
|
39
|
+
Dynamic: license-file
|
|
22
40
|
|
|
23
41
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
24
42
|
|
|
@@ -31,7 +49,7 @@ Requires-Dist: pytest-mock; extra == "test"
|
|
|
31
49
|
[](https://github.com/basnijholt/numthreads)
|
|
32
50
|
[](https://numthreads.readthedocs.io/)
|
|
33
51
|
|
|
34
|
-
`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
|
|
52
|
+
`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.
|
|
35
53
|
The number of threads can be set via the command line or in Python code.
|
|
36
54
|
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
37
55
|
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
@@ -42,7 +60,8 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
42
60
|
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
43
61
|
- Context manager support for temporary thread setting in Python code.
|
|
44
62
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
45
|
-
-
|
|
63
|
+
- Use as pytest plugin
|
|
64
|
+
- Tiny (≤7KB) and no dependencies.
|
|
46
65
|
|
|
47
66
|
<!-- toc-start -->
|
|
48
67
|
|
|
@@ -53,10 +72,12 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
53
72
|
|
|
54
73
|
- [:package: Installation](#package-installation)
|
|
55
74
|
- [:rocket: Quick Start](#rocket-quick-start)
|
|
56
|
-
- [Using as a Python Module](#using-as-a-python-module)
|
|
75
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
57
76
|
- [Command Line Interface](#command-line-interface)
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
78
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
79
|
+
- [Get the number of threads](#get-the-number-of-threads)
|
|
80
|
+
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
60
81
|
- [:question: Getting Help](#question-getting-help)
|
|
61
82
|
|
|
62
83
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -75,7 +96,7 @@ pip install numthreads
|
|
|
75
96
|
|
|
76
97
|
Get started with `numthreads` in a few seconds.
|
|
77
98
|
|
|
78
|
-
|
|
99
|
+
### Using as a Python Module
|
|
79
100
|
|
|
80
101
|
You can also use `numthreads` as a Python module:
|
|
81
102
|
|
|
@@ -86,11 +107,13 @@ set_num_threads(4)
|
|
|
86
107
|
```
|
|
87
108
|
|
|
88
109
|
This will set the number of threads using the following environment variables:
|
|
110
|
+
|
|
89
111
|
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
90
112
|
- MKL (via `MKL_NUM_THREADS`)
|
|
91
113
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
92
114
|
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
93
115
|
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
116
|
+
- Numba (via `NUMBA_NUM_THREADS`)
|
|
94
117
|
|
|
95
118
|
or use it as a context manager:
|
|
96
119
|
|
|
@@ -105,7 +128,7 @@ with num_threads(4):
|
|
|
105
128
|
> [!WARNING]
|
|
106
129
|
> Since environment variables are global and typically need to be set before importing any libraries, it's recommended to set the number of threads at the beginning of your Python script.
|
|
107
130
|
|
|
108
|
-
To set OMP (OpenMP) threads at any time, you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
131
|
+
To set OMP (OpenMP) threads at any time ([OpenMP docs](https://www.openmp.org/spec-html/5.0/openmpsu110.html)), you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
109
132
|
|
|
110
133
|
```python
|
|
111
134
|
from numthreads import omp_set_num_threads
|
|
@@ -130,20 +153,24 @@ After installing `numthreads`, you can easily set the number of threads used by
|
|
|
130
153
|
```bash
|
|
131
154
|
numthreads 4
|
|
132
155
|
```
|
|
156
|
+
|
|
133
157
|
<!-- CODE:BASH:START -->
|
|
134
158
|
<!-- echo '```bash' -->
|
|
135
159
|
<!-- numthreads 4 -->
|
|
136
160
|
<!-- echo '```' -->
|
|
137
161
|
<!-- CODE:END -->
|
|
162
|
+
|
|
163
|
+
Which will print the following:
|
|
164
|
+
|
|
138
165
|
<!-- OUTPUT:START -->
|
|
139
166
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
140
167
|
```bash
|
|
141
|
-
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
|
|
168
|
+
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'
|
|
142
169
|
```
|
|
143
170
|
|
|
144
171
|
<!-- OUTPUT:END -->
|
|
145
172
|
|
|
146
|
-
|
|
173
|
+
#### Unix-like Systems (Linux, macOS, WSL)
|
|
147
174
|
|
|
148
175
|
To apply the settings in your shell:
|
|
149
176
|
|
|
@@ -151,7 +178,7 @@ To apply the settings in your shell:
|
|
|
151
178
|
eval $(numthreads <number_of_threads>)
|
|
152
179
|
```
|
|
153
180
|
|
|
154
|
-
|
|
181
|
+
#### Windows (PowerShell)
|
|
155
182
|
|
|
156
183
|
In PowerShell, use:
|
|
157
184
|
|
|
@@ -159,6 +186,76 @@ In PowerShell, use:
|
|
|
159
186
|
Invoke-Expression $(numthreads <number_of_threads>)
|
|
160
187
|
```
|
|
161
188
|
|
|
189
|
+
### Get the number of threads
|
|
190
|
+
|
|
191
|
+
To get the number of threads currently set, run:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
eval $(numthreads 12) # first set something
|
|
195
|
+
numthreads get
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
<!-- CODE:BASH:START -->
|
|
199
|
+
<!-- echo '```bash' -->
|
|
200
|
+
<!-- eval $(numthreads 12) -->
|
|
201
|
+
<!-- numthreads get -->
|
|
202
|
+
<!-- echo '```' -->
|
|
203
|
+
<!-- CODE:END -->
|
|
204
|
+
|
|
205
|
+
Which will print the following:
|
|
206
|
+
|
|
207
|
+
<!-- OUTPUT:START -->
|
|
208
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
209
|
+
```bash
|
|
210
|
+
OPENBLAS_NUM_THREADS: 12
|
|
211
|
+
MKL_NUM_THREADS: 12
|
|
212
|
+
OMP_NUM_THREADS: 12
|
|
213
|
+
NUMEXPR_NUM_THREADS: 12
|
|
214
|
+
VECLIB_MAXIMUM_THREADS: 12
|
|
215
|
+
NUMBA_NUM_THREADS: 12
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
<!-- OUTPUT:END -->
|
|
219
|
+
|
|
220
|
+
Check the `numthreads -h` message for more information:
|
|
221
|
+
|
|
222
|
+
<!-- CODE:BASH:START -->
|
|
223
|
+
<!-- echo '```bash' -->
|
|
224
|
+
<!-- numthreads -h -->
|
|
225
|
+
<!-- echo '```' -->
|
|
226
|
+
<!-- CODE:END -->
|
|
227
|
+
<!-- OUTPUT:START -->
|
|
228
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
229
|
+
```bash
|
|
230
|
+
usage: numthreads [-h] n
|
|
231
|
+
|
|
232
|
+
Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
233
|
+
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
234
|
+
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
235
|
+
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
236
|
+
$(numthreads <number>)`.
|
|
237
|
+
|
|
238
|
+
positional arguments:
|
|
239
|
+
n Number of threads to set or use 'get' to display current
|
|
240
|
+
settings.
|
|
241
|
+
|
|
242
|
+
options:
|
|
243
|
+
-h, --help show this help message and exit
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
<!-- OUTPUT:END -->
|
|
247
|
+
|
|
248
|
+
### :electric_plug: Using as a pytest Plugin
|
|
249
|
+
|
|
250
|
+
`numthreads` can be used as a pytest plugin to automatically set the number of threads for your tests.
|
|
251
|
+
|
|
252
|
+
To use it, make sure `numthreads` is installed. Then, run pytest with the `--numthreads` option followed by the desired number of threads:
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
pytest --numthreads 1
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.
|
|
162
259
|
|
|
163
260
|
## :question: Getting Help
|
|
164
261
|
|
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: numthreads
|
|
3
|
-
Version: 0.4.0
|
|
4
|
-
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
5
|
-
Author-email: Bas Nijholt <bas@nijho.lt>
|
|
6
|
-
Project-URL: Homepage, https://github.com/basnijholt/numthreads
|
|
7
|
-
Requires-Python: >=3.7
|
|
8
|
-
Description-Content-Type: text/markdown
|
|
9
|
-
License-File: LICENSE
|
|
10
|
-
Provides-Extra: docs
|
|
11
|
-
Requires-Dist: myst-parser; extra == "docs"
|
|
12
|
-
Requires-Dist: sphinx; extra == "docs"
|
|
13
|
-
Requires-Dist: furo; extra == "docs"
|
|
14
|
-
Requires-Dist: emoji; extra == "docs"
|
|
15
|
-
Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
|
|
16
|
-
Provides-Extra: test
|
|
17
|
-
Requires-Dist: pytest; extra == "test"
|
|
18
|
-
Requires-Dist: pre-commit; extra == "test"
|
|
19
|
-
Requires-Dist: coverage; extra == "test"
|
|
20
|
-
Requires-Dist: pytest-cov; extra == "test"
|
|
21
|
-
Requires-Dist: pytest-mock; extra == "test"
|
|
22
|
-
|
|
23
1
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
24
2
|
|
|
25
3
|
<h1 align="center">numthreads</h1>
|
|
@@ -31,7 +9,7 @@ Requires-Dist: pytest-mock; extra == "test"
|
|
|
31
9
|
[](https://github.com/basnijholt/numthreads)
|
|
32
10
|
[](https://numthreads.readthedocs.io/)
|
|
33
11
|
|
|
34
|
-
`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.
|
|
35
13
|
The number of threads can be set via the command line or in Python code.
|
|
36
14
|
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
37
15
|
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
@@ -42,7 +20,8 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
42
20
|
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
43
21
|
- Context manager support for temporary thread setting in Python code.
|
|
44
22
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
45
|
-
-
|
|
23
|
+
- Use as pytest plugin
|
|
24
|
+
- Tiny (≤7KB) and no dependencies.
|
|
46
25
|
|
|
47
26
|
<!-- toc-start -->
|
|
48
27
|
|
|
@@ -53,10 +32,12 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
53
32
|
|
|
54
33
|
- [:package: Installation](#package-installation)
|
|
55
34
|
- [:rocket: Quick Start](#rocket-quick-start)
|
|
56
|
-
- [Using as a Python Module](#using-as-a-python-module)
|
|
35
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
57
36
|
- [Command Line Interface](#command-line-interface)
|
|
58
|
-
|
|
59
|
-
|
|
37
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
38
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
39
|
+
- [Get the number of threads](#get-the-number-of-threads)
|
|
40
|
+
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
60
41
|
- [:question: Getting Help](#question-getting-help)
|
|
61
42
|
|
|
62
43
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -75,7 +56,7 @@ pip install numthreads
|
|
|
75
56
|
|
|
76
57
|
Get started with `numthreads` in a few seconds.
|
|
77
58
|
|
|
78
|
-
|
|
59
|
+
### Using as a Python Module
|
|
79
60
|
|
|
80
61
|
You can also use `numthreads` as a Python module:
|
|
81
62
|
|
|
@@ -86,11 +67,13 @@ set_num_threads(4)
|
|
|
86
67
|
```
|
|
87
68
|
|
|
88
69
|
This will set the number of threads using the following environment variables:
|
|
70
|
+
|
|
89
71
|
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
90
72
|
- MKL (via `MKL_NUM_THREADS`)
|
|
91
73
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
92
74
|
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
93
75
|
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
76
|
+
- Numba (via `NUMBA_NUM_THREADS`)
|
|
94
77
|
|
|
95
78
|
or use it as a context manager:
|
|
96
79
|
|
|
@@ -105,7 +88,7 @@ with num_threads(4):
|
|
|
105
88
|
> [!WARNING]
|
|
106
89
|
> Since environment variables are global and typically need to be set before importing any libraries, it's recommended to set the number of threads at the beginning of your Python script.
|
|
107
90
|
|
|
108
|
-
To set OMP (OpenMP) threads at any time, you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
91
|
+
To set OMP (OpenMP) threads at any time ([OpenMP docs](https://www.openmp.org/spec-html/5.0/openmpsu110.html)), you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
109
92
|
|
|
110
93
|
```python
|
|
111
94
|
from numthreads import omp_set_num_threads
|
|
@@ -130,20 +113,24 @@ After installing `numthreads`, you can easily set the number of threads used by
|
|
|
130
113
|
```bash
|
|
131
114
|
numthreads 4
|
|
132
115
|
```
|
|
116
|
+
|
|
133
117
|
<!-- CODE:BASH:START -->
|
|
134
118
|
<!-- echo '```bash' -->
|
|
135
119
|
<!-- numthreads 4 -->
|
|
136
120
|
<!-- echo '```' -->
|
|
137
121
|
<!-- CODE:END -->
|
|
122
|
+
|
|
123
|
+
Which will print the following:
|
|
124
|
+
|
|
138
125
|
<!-- OUTPUT:START -->
|
|
139
126
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
140
127
|
```bash
|
|
141
|
-
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
|
|
128
|
+
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'
|
|
142
129
|
```
|
|
143
130
|
|
|
144
131
|
<!-- OUTPUT:END -->
|
|
145
132
|
|
|
146
|
-
|
|
133
|
+
#### Unix-like Systems (Linux, macOS, WSL)
|
|
147
134
|
|
|
148
135
|
To apply the settings in your shell:
|
|
149
136
|
|
|
@@ -151,7 +138,7 @@ To apply the settings in your shell:
|
|
|
151
138
|
eval $(numthreads <number_of_threads>)
|
|
152
139
|
```
|
|
153
140
|
|
|
154
|
-
|
|
141
|
+
#### Windows (PowerShell)
|
|
155
142
|
|
|
156
143
|
In PowerShell, use:
|
|
157
144
|
|
|
@@ -159,6 +146,76 @@ In PowerShell, use:
|
|
|
159
146
|
Invoke-Expression $(numthreads <number_of_threads>)
|
|
160
147
|
```
|
|
161
148
|
|
|
149
|
+
### Get the number of threads
|
|
150
|
+
|
|
151
|
+
To get the number of threads currently set, run:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
eval $(numthreads 12) # first set something
|
|
155
|
+
numthreads get
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
<!-- CODE:BASH:START -->
|
|
159
|
+
<!-- echo '```bash' -->
|
|
160
|
+
<!-- eval $(numthreads 12) -->
|
|
161
|
+
<!-- numthreads get -->
|
|
162
|
+
<!-- echo '```' -->
|
|
163
|
+
<!-- CODE:END -->
|
|
164
|
+
|
|
165
|
+
Which will print the following:
|
|
166
|
+
|
|
167
|
+
<!-- OUTPUT:START -->
|
|
168
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
169
|
+
```bash
|
|
170
|
+
OPENBLAS_NUM_THREADS: 12
|
|
171
|
+
MKL_NUM_THREADS: 12
|
|
172
|
+
OMP_NUM_THREADS: 12
|
|
173
|
+
NUMEXPR_NUM_THREADS: 12
|
|
174
|
+
VECLIB_MAXIMUM_THREADS: 12
|
|
175
|
+
NUMBA_NUM_THREADS: 12
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
<!-- OUTPUT:END -->
|
|
179
|
+
|
|
180
|
+
Check the `numthreads -h` message for more information:
|
|
181
|
+
|
|
182
|
+
<!-- CODE:BASH:START -->
|
|
183
|
+
<!-- echo '```bash' -->
|
|
184
|
+
<!-- numthreads -h -->
|
|
185
|
+
<!-- echo '```' -->
|
|
186
|
+
<!-- CODE:END -->
|
|
187
|
+
<!-- OUTPUT:START -->
|
|
188
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
189
|
+
```bash
|
|
190
|
+
usage: numthreads [-h] n
|
|
191
|
+
|
|
192
|
+
Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
193
|
+
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
194
|
+
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
195
|
+
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
196
|
+
$(numthreads <number>)`.
|
|
197
|
+
|
|
198
|
+
positional arguments:
|
|
199
|
+
n Number of threads to set or use 'get' to display current
|
|
200
|
+
settings.
|
|
201
|
+
|
|
202
|
+
options:
|
|
203
|
+
-h, --help show this help message and exit
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
<!-- OUTPUT:END -->
|
|
207
|
+
|
|
208
|
+
### :electric_plug: Using as a pytest Plugin
|
|
209
|
+
|
|
210
|
+
`numthreads` can be used as a pytest plugin to automatically set the number of threads for your tests.
|
|
211
|
+
|
|
212
|
+
To use it, make sure `numthreads` is installed. Then, run pytest with the `--numthreads` option followed by the desired number of threads:
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
pytest --numthreads 1
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.
|
|
162
219
|
|
|
163
220
|
## :question: Getting Help
|
|
164
221
|
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: numthreads
|
|
3
|
+
Version: 0.6.1
|
|
4
|
+
Summary: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
5
|
+
Author-email: Bas Nijholt <bas@nijho.lt>
|
|
6
|
+
Project-URL: Homepage, https://github.com/basnijholt/numthreads
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Framework :: Pytest
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
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
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: Software Development :: Testing
|
|
22
|
+
Classifier: Topic :: System :: Hardware :: Symmetric Multi-processing
|
|
23
|
+
Classifier: Topic :: Utilities
|
|
24
|
+
Requires-Python: >=3.7
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Provides-Extra: docs
|
|
28
|
+
Requires-Dist: myst-parser; extra == "docs"
|
|
29
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
30
|
+
Requires-Dist: furo; extra == "docs"
|
|
31
|
+
Requires-Dist: emoji; extra == "docs"
|
|
32
|
+
Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
|
|
33
|
+
Provides-Extra: test
|
|
34
|
+
Requires-Dist: pytest; extra == "test"
|
|
35
|
+
Requires-Dist: pre-commit; extra == "test"
|
|
36
|
+
Requires-Dist: coverage; extra == "test"
|
|
37
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
38
|
+
Requires-Dist: pytest-mock; extra == "test"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
42
|
+
|
|
43
|
+
<h1 align="center">numthreads</h1>
|
|
44
|
+
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and Accelerate</h3>
|
|
45
|
+
|
|
46
|
+
[](https://pypi.python.org/pypi/numthreads)
|
|
47
|
+
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
48
|
+
[](https://codecov.io/gh/basnijholt/numthreads)
|
|
49
|
+
[](https://github.com/basnijholt/numthreads)
|
|
50
|
+
[](https://numthreads.readthedocs.io/)
|
|
51
|
+
|
|
52
|
+
`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.
|
|
53
|
+
The number of threads can be set via the command line or in Python code.
|
|
54
|
+
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
55
|
+
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
56
|
+
In some instances, using more threads may actually impede computational efficiency.
|
|
57
|
+
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
58
|
+
|
|
59
|
+
- Simple and straightforward command-line interface.
|
|
60
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
61
|
+
- Context manager support for temporary thread setting in Python code.
|
|
62
|
+
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
63
|
+
- Use as pytest plugin
|
|
64
|
+
- Tiny (≤7KB) and no dependencies.
|
|
65
|
+
|
|
66
|
+
<!-- toc-start -->
|
|
67
|
+
|
|
68
|
+
## :books: Table of Contents
|
|
69
|
+
|
|
70
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
71
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
72
|
+
|
|
73
|
+
- [:package: Installation](#package-installation)
|
|
74
|
+
- [:rocket: Quick Start](#rocket-quick-start)
|
|
75
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
76
|
+
- [Command Line Interface](#command-line-interface)
|
|
77
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
78
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
79
|
+
- [Get the number of threads](#get-the-number-of-threads)
|
|
80
|
+
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
81
|
+
- [:question: Getting Help](#question-getting-help)
|
|
82
|
+
|
|
83
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
84
|
+
|
|
85
|
+
<!-- toc-end -->
|
|
86
|
+
|
|
87
|
+
## :package: Installation
|
|
88
|
+
|
|
89
|
+
To install `numthreads`, run the following command:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pip install numthreads
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## :rocket: Quick Start
|
|
96
|
+
|
|
97
|
+
Get started with `numthreads` in a few seconds.
|
|
98
|
+
|
|
99
|
+
### Using as a Python Module
|
|
100
|
+
|
|
101
|
+
You can also use `numthreads` as a Python module:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from numthreads import set_num_threads
|
|
105
|
+
|
|
106
|
+
set_num_threads(4)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
This will set the number of threads using the following environment variables:
|
|
110
|
+
|
|
111
|
+
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
112
|
+
- MKL (via `MKL_NUM_THREADS`)
|
|
113
|
+
- OpenMP (via `OMP_NUM_THREADS`)
|
|
114
|
+
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
115
|
+
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
116
|
+
- Numba (via `NUMBA_NUM_THREADS`)
|
|
117
|
+
|
|
118
|
+
or use it as a context manager:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from numthreads import num_threads
|
|
122
|
+
|
|
123
|
+
with num_threads(4):
|
|
124
|
+
# Your code here will run with the specified number of threads
|
|
125
|
+
pass
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
> [!WARNING]
|
|
129
|
+
> Since environment variables are global and typically need to be set before importing any libraries, it's recommended to set the number of threads at the beginning of your Python script.
|
|
130
|
+
|
|
131
|
+
To set OMP (OpenMP) threads at any time ([OpenMP docs](https://www.openmp.org/spec-html/5.0/openmpsu110.html)), you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from numthreads import omp_set_num_threads
|
|
135
|
+
|
|
136
|
+
omp_set_num_threads(4)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
or
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from numthreads import omp_num_threads
|
|
143
|
+
|
|
144
|
+
with omp_num_threads(4):
|
|
145
|
+
# Your code here will run with the specified number of threads
|
|
146
|
+
pass
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Command Line Interface
|
|
150
|
+
|
|
151
|
+
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:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
numthreads 4
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
<!-- CODE:BASH:START -->
|
|
158
|
+
<!-- echo '```bash' -->
|
|
159
|
+
<!-- numthreads 4 -->
|
|
160
|
+
<!-- echo '```' -->
|
|
161
|
+
<!-- CODE:END -->
|
|
162
|
+
|
|
163
|
+
Which will print the following:
|
|
164
|
+
|
|
165
|
+
<!-- OUTPUT:START -->
|
|
166
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
167
|
+
```bash
|
|
168
|
+
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'
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
<!-- OUTPUT:END -->
|
|
172
|
+
|
|
173
|
+
#### Unix-like Systems (Linux, macOS, WSL)
|
|
174
|
+
|
|
175
|
+
To apply the settings in your shell:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
eval $(numthreads <number_of_threads>)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### Windows (PowerShell)
|
|
182
|
+
|
|
183
|
+
In PowerShell, use:
|
|
184
|
+
|
|
185
|
+
```powershell
|
|
186
|
+
Invoke-Expression $(numthreads <number_of_threads>)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Get the number of threads
|
|
190
|
+
|
|
191
|
+
To get the number of threads currently set, run:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
eval $(numthreads 12) # first set something
|
|
195
|
+
numthreads get
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
<!-- CODE:BASH:START -->
|
|
199
|
+
<!-- echo '```bash' -->
|
|
200
|
+
<!-- eval $(numthreads 12) -->
|
|
201
|
+
<!-- numthreads get -->
|
|
202
|
+
<!-- echo '```' -->
|
|
203
|
+
<!-- CODE:END -->
|
|
204
|
+
|
|
205
|
+
Which will print the following:
|
|
206
|
+
|
|
207
|
+
<!-- OUTPUT:START -->
|
|
208
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
209
|
+
```bash
|
|
210
|
+
OPENBLAS_NUM_THREADS: 12
|
|
211
|
+
MKL_NUM_THREADS: 12
|
|
212
|
+
OMP_NUM_THREADS: 12
|
|
213
|
+
NUMEXPR_NUM_THREADS: 12
|
|
214
|
+
VECLIB_MAXIMUM_THREADS: 12
|
|
215
|
+
NUMBA_NUM_THREADS: 12
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
<!-- OUTPUT:END -->
|
|
219
|
+
|
|
220
|
+
Check the `numthreads -h` message for more information:
|
|
221
|
+
|
|
222
|
+
<!-- CODE:BASH:START -->
|
|
223
|
+
<!-- echo '```bash' -->
|
|
224
|
+
<!-- numthreads -h -->
|
|
225
|
+
<!-- echo '```' -->
|
|
226
|
+
<!-- CODE:END -->
|
|
227
|
+
<!-- OUTPUT:START -->
|
|
228
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
229
|
+
```bash
|
|
230
|
+
usage: numthreads [-h] n
|
|
231
|
+
|
|
232
|
+
Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
233
|
+
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
234
|
+
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
235
|
+
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
236
|
+
$(numthreads <number>)`.
|
|
237
|
+
|
|
238
|
+
positional arguments:
|
|
239
|
+
n Number of threads to set or use 'get' to display current
|
|
240
|
+
settings.
|
|
241
|
+
|
|
242
|
+
options:
|
|
243
|
+
-h, --help show this help message and exit
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
<!-- OUTPUT:END -->
|
|
247
|
+
|
|
248
|
+
### :electric_plug: Using as a pytest Plugin
|
|
249
|
+
|
|
250
|
+
`numthreads` can be used as a pytest plugin to automatically set the number of threads for your tests.
|
|
251
|
+
|
|
252
|
+
To use it, make sure `numthreads` is installed. Then, run pytest with the `--numthreads` option followed by the desired number of threads:
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
pytest --numthreads 1
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.
|
|
259
|
+
|
|
260
|
+
## :question: Getting Help
|
|
261
|
+
|
|
262
|
+
For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
"""numthreads: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and
|
|
1
|
+
"""numthreads: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.""" # noqa: E501
|
|
2
|
+
|
|
2
3
|
from __future__ import annotations
|
|
3
4
|
|
|
4
5
|
import argparse
|
|
@@ -12,7 +13,9 @@ from typing import TYPE_CHECKING
|
|
|
12
13
|
if TYPE_CHECKING:
|
|
13
14
|
from collections.abc import Generator
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
import pytest
|
|
17
|
+
|
|
18
|
+
__version__ = "0.6.1"
|
|
16
19
|
|
|
17
20
|
THREAD_CONTROL_ENV_VARS = [
|
|
18
21
|
"OPENBLAS_NUM_THREADS", # OpenBLAS
|
|
@@ -20,13 +23,15 @@ THREAD_CONTROL_ENV_VARS = [
|
|
|
20
23
|
"OMP_NUM_THREADS", # OMP
|
|
21
24
|
"NUMEXPR_NUM_THREADS", # NumExpr
|
|
22
25
|
"VECLIB_MAXIMUM_THREADS", # Accelerate
|
|
26
|
+
"NUMBA_NUM_THREADS", # Numba
|
|
23
27
|
]
|
|
24
28
|
|
|
25
29
|
|
|
26
|
-
def set_num_threads(n: int = 1) -> None:
|
|
30
|
+
def set_num_threads(n: int = 1, *, overwrite: bool = True) -> None:
|
|
27
31
|
"""Set the number of threads via environment variables.
|
|
28
32
|
|
|
29
|
-
Sets:
|
|
33
|
+
Sets the following environment variables:
|
|
34
|
+
|
|
30
35
|
- ``"OPENBLAS_NUM_THREADS"`` for OpenBLAS (Open Basic Linear Algebra Subprograms)
|
|
31
36
|
- ``"MKL_NUM_THREADS"`` for MKL (Intel Math Kernel Library)
|
|
32
37
|
- ``"OMP_NUM_THREADS"`` for OMP (OpenMP)
|
|
@@ -38,9 +43,18 @@ def set_num_threads(n: int = 1) -> None:
|
|
|
38
43
|
threads for NumPy, you should set the environment variables before importing
|
|
39
44
|
NumPy. When using OMP (OpenMP), you can use the ``omp_set_num_threads`` function
|
|
40
45
|
to change the number of threads *after* importing the library.
|
|
46
|
+
|
|
47
|
+
Parameters
|
|
48
|
+
----------
|
|
49
|
+
n
|
|
50
|
+
Number of threads to set.
|
|
51
|
+
overwrite
|
|
52
|
+
If true, overwrite the existing value.
|
|
53
|
+
|
|
41
54
|
"""
|
|
42
55
|
for var in THREAD_CONTROL_ENV_VARS:
|
|
43
|
-
os.environ
|
|
56
|
+
if overwrite or var not in os.environ:
|
|
57
|
+
os.environ[var] = str(n)
|
|
44
58
|
|
|
45
59
|
|
|
46
60
|
# Function alias
|
|
@@ -48,11 +62,11 @@ set = set_num_threads # noqa: A001
|
|
|
48
62
|
|
|
49
63
|
|
|
50
64
|
@contextlib.contextmanager
|
|
51
|
-
def num_threads(n: int = 1) -> Generator[None, None, None]:
|
|
65
|
+
def num_threads(n: int = 1, *, overwrite: bool = True) -> Generator[None, None, None]:
|
|
52
66
|
"""Context manager to set and then restore thread number settings."""
|
|
53
67
|
original_settings = {var: os.environ.get(var) for var in THREAD_CONTROL_ENV_VARS}
|
|
54
68
|
|
|
55
|
-
set_num_threads(n)
|
|
69
|
+
set_num_threads(n, overwrite=overwrite)
|
|
56
70
|
|
|
57
71
|
try:
|
|
58
72
|
yield
|
|
@@ -85,7 +99,7 @@ def _load_omp_library() -> ctypes.CDLL:
|
|
|
85
99
|
raise OSError(msg) from e
|
|
86
100
|
|
|
87
101
|
|
|
88
|
-
def omp_set_num_threads(num_threads: int) -> None:
|
|
102
|
+
def omp_set_num_threads(num_threads: int, *, overwrite: bool = True) -> None:
|
|
89
103
|
"""Sets the number of threads to be used by OpenMP parallel regions.
|
|
90
104
|
|
|
91
105
|
When using OMP (OpenMP), you can use this function to change the number of threads
|
|
@@ -96,10 +110,13 @@ def omp_set_num_threads(num_threads: int) -> None:
|
|
|
96
110
|
----------
|
|
97
111
|
num_threads
|
|
98
112
|
Number of threads to set.
|
|
113
|
+
overwrite
|
|
114
|
+
If true, overwrite the existing value.
|
|
99
115
|
"""
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
116
|
+
if overwrite:
|
|
117
|
+
omp_lib = _load_omp_library()
|
|
118
|
+
omp_lib.omp_set_num_threads.argtypes = [ctypes.c_int]
|
|
119
|
+
omp_lib.omp_set_num_threads(num_threads)
|
|
103
120
|
|
|
104
121
|
|
|
105
122
|
def omp_get_num_threads() -> int:
|
|
@@ -110,13 +127,19 @@ def omp_get_num_threads() -> int:
|
|
|
110
127
|
|
|
111
128
|
|
|
112
129
|
@contextlib.contextmanager
|
|
113
|
-
def omp_num_threads(
|
|
130
|
+
def omp_num_threads(
|
|
131
|
+
num_threads: int,
|
|
132
|
+
*,
|
|
133
|
+
overwrite: bool = True,
|
|
134
|
+
) -> Generator[None, None, None]:
|
|
114
135
|
"""Context manager to set and then restore OpenMP thread number settings.
|
|
115
136
|
|
|
116
137
|
Parameters
|
|
117
138
|
----------
|
|
118
139
|
num_threads
|
|
119
140
|
Number of threads to set in the OpenMP parallel regions.
|
|
141
|
+
overwrite
|
|
142
|
+
Whether to overwrite existing values.
|
|
120
143
|
|
|
121
144
|
Examples
|
|
122
145
|
--------
|
|
@@ -127,7 +150,7 @@ def omp_num_threads(num_threads: int) -> Generator[None, None, None]:
|
|
|
127
150
|
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
|
|
128
151
|
"""
|
|
129
152
|
original_num_threads = omp_get_num_threads()
|
|
130
|
-
omp_set_num_threads(num_threads)
|
|
153
|
+
omp_set_num_threads(num_threads, overwrite=overwrite)
|
|
131
154
|
|
|
132
155
|
try:
|
|
133
156
|
yield
|
|
@@ -135,6 +158,13 @@ def omp_num_threads(num_threads: int) -> Generator[None, None, None]:
|
|
|
135
158
|
omp_set_num_threads(original_num_threads)
|
|
136
159
|
|
|
137
160
|
|
|
161
|
+
def print_current_thread_counts() -> None:
|
|
162
|
+
"""Prints the current values of the thread control environment variables."""
|
|
163
|
+
for var in THREAD_CONTROL_ENV_VARS:
|
|
164
|
+
value = os.environ.get(var, "Not set")
|
|
165
|
+
print(f"{var}: {value}")
|
|
166
|
+
|
|
167
|
+
|
|
138
168
|
def main() -> None: # pragma: no cover
|
|
139
169
|
"""Command-line interface."""
|
|
140
170
|
description = (
|
|
@@ -149,12 +179,15 @@ def main() -> None: # pragma: no cover
|
|
|
149
179
|
)
|
|
150
180
|
parser.add_argument(
|
|
151
181
|
"n",
|
|
152
|
-
|
|
153
|
-
help="Number of threads to set.",
|
|
182
|
+
help="Number of threads to set or use 'get' to display current settings.",
|
|
154
183
|
)
|
|
155
184
|
args = parser.parse_args(args=None if sys.argv[1:] else ["--help"])
|
|
156
185
|
|
|
157
|
-
|
|
186
|
+
if args.n == "get":
|
|
187
|
+
print_current_thread_counts()
|
|
188
|
+
return
|
|
189
|
+
|
|
190
|
+
n = int(args.n)
|
|
158
191
|
system = platform.system()
|
|
159
192
|
if system == "Windows":
|
|
160
193
|
export_commands = " & ".join(
|
|
@@ -168,5 +201,24 @@ def main() -> None: # pragma: no cover
|
|
|
168
201
|
print(export_commands)
|
|
169
202
|
|
|
170
203
|
|
|
204
|
+
# Pytest Plugin Integration
|
|
205
|
+
def pytest_addoption(parser: pytest.Parser) -> None: # pragma: no cover
|
|
206
|
+
"""Add the --numthreads option to the pytest command line."""
|
|
207
|
+
parser.addoption(
|
|
208
|
+
"--numthreads",
|
|
209
|
+
action="store",
|
|
210
|
+
default=None, # Default to None, so we don't set unnecessarily
|
|
211
|
+
type=int,
|
|
212
|
+
help="Number of threads to set for OpenBLAS, MKL, OMP, NumExpr, and Accelerate",
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def pytest_configure(config: pytest.Config) -> None: # pragma: no cover
|
|
217
|
+
"""Set the number of threads based on the --numthreads option."""
|
|
218
|
+
numthreads = config.getoption("--numthreads")
|
|
219
|
+
if numthreads is not None:
|
|
220
|
+
set_num_threads(numthreads)
|
|
221
|
+
|
|
222
|
+
|
|
171
223
|
if __name__ == "__main__":
|
|
172
224
|
main()
|
|
@@ -9,6 +9,25 @@ dynamic = ["version"]
|
|
|
9
9
|
authors = [{ name = "Bas Nijholt", email = "bas@nijho.lt" }]
|
|
10
10
|
dependencies = []
|
|
11
11
|
requires-python = ">=3.7"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Framework :: Pytest",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"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
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Topic :: Software Development :: Testing",
|
|
28
|
+
"Topic :: System :: Hardware :: Symmetric Multi-processing",
|
|
29
|
+
"Topic :: Utilities",
|
|
30
|
+
]
|
|
12
31
|
|
|
13
32
|
[project.readme]
|
|
14
33
|
file = "README.md"
|
|
@@ -18,24 +37,15 @@ content-type = "text/markdown"
|
|
|
18
37
|
Homepage = "https://github.com/basnijholt/numthreads"
|
|
19
38
|
|
|
20
39
|
[project.optional-dependencies]
|
|
21
|
-
docs = [
|
|
22
|
-
|
|
23
|
-
"sphinx",
|
|
24
|
-
"furo",
|
|
25
|
-
"emoji",
|
|
26
|
-
"sphinx-autodoc-typehints",
|
|
27
|
-
]
|
|
28
|
-
test = [
|
|
29
|
-
"pytest",
|
|
30
|
-
"pre-commit",
|
|
31
|
-
"coverage",
|
|
32
|
-
"pytest-cov",
|
|
33
|
-
"pytest-mock",
|
|
34
|
-
]
|
|
40
|
+
docs = ["myst-parser", "sphinx", "furo", "emoji", "sphinx-autodoc-typehints"]
|
|
41
|
+
test = ["pytest", "pre-commit", "coverage", "pytest-cov", "pytest-mock"]
|
|
35
42
|
|
|
36
43
|
[project.scripts]
|
|
37
44
|
numthreads = "numthreads:main"
|
|
38
45
|
|
|
46
|
+
[project.entry-points.pytest11]
|
|
47
|
+
numthreads = "numthreads"
|
|
48
|
+
|
|
39
49
|
[tool.setuptools]
|
|
40
50
|
py-modules = ["numthreads"]
|
|
41
51
|
|
|
@@ -50,9 +60,10 @@ addopts = """
|
|
|
50
60
|
--cov=numthreads
|
|
51
61
|
--cov-report term
|
|
52
62
|
--cov-report html
|
|
53
|
-
--cov-
|
|
63
|
+
--cov-report xml
|
|
54
64
|
-W error
|
|
55
65
|
-vvv
|
|
66
|
+
-p no:numthreads
|
|
56
67
|
"""
|
|
57
68
|
|
|
58
69
|
[tool.coverage.run]
|
|
@@ -8,23 +8,31 @@ from numthreads import (
|
|
|
8
8
|
omp_get_num_threads,
|
|
9
9
|
omp_num_threads,
|
|
10
10
|
omp_set_num_threads,
|
|
11
|
+
print_current_thread_counts,
|
|
11
12
|
set_num_threads,
|
|
12
13
|
)
|
|
13
14
|
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
@pytest.mark.parametrize("overwrite", [True, False])
|
|
17
|
+
def test_set_num_threads(overwrite: bool) -> None: # noqa: FBT001
|
|
18
|
+
envvars = [
|
|
18
19
|
"OPENBLAS_NUM_THREADS",
|
|
19
20
|
"MKL_NUM_THREADS",
|
|
20
21
|
"OMP_NUM_THREADS",
|
|
21
22
|
"NUMEXPR_NUM_THREADS",
|
|
22
23
|
"VECLIB_MAXIMUM_THREADS",
|
|
23
|
-
]
|
|
24
|
-
|
|
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]
|
|
25
32
|
|
|
26
33
|
|
|
27
|
-
|
|
34
|
+
@pytest.mark.parametrize("overwrite", [True, False])
|
|
35
|
+
def test_num_threads_context_manager(overwrite: bool) -> None: # noqa: FBT001
|
|
28
36
|
original_values = {
|
|
29
37
|
var: os.environ.get(var)
|
|
30
38
|
for var in [
|
|
@@ -36,7 +44,7 @@ def test_num_threads_context_manager() -> None:
|
|
|
36
44
|
]
|
|
37
45
|
}
|
|
38
46
|
|
|
39
|
-
with num_threads(2):
|
|
47
|
+
with num_threads(2, overwrite=overwrite):
|
|
40
48
|
for var in [
|
|
41
49
|
"OPENBLAS_NUM_THREADS",
|
|
42
50
|
"MKL_NUM_THREADS",
|
|
@@ -44,7 +52,10 @@ def test_num_threads_context_manager() -> None:
|
|
|
44
52
|
"NUMEXPR_NUM_THREADS",
|
|
45
53
|
"VECLIB_MAXIMUM_THREADS",
|
|
46
54
|
]:
|
|
47
|
-
|
|
55
|
+
if overwrite:
|
|
56
|
+
assert os.environ.get(var) == "2"
|
|
57
|
+
else:
|
|
58
|
+
assert os.environ.get(var) == original_values[var]
|
|
48
59
|
|
|
49
60
|
for var in [
|
|
50
61
|
"OPENBLAS_NUM_THREADS",
|
|
@@ -60,14 +71,18 @@ def test_num_threads_context_manager() -> None:
|
|
|
60
71
|
os.name != "posix",
|
|
61
72
|
reason="OMP functions are tested only on POSIX systems",
|
|
62
73
|
)
|
|
63
|
-
|
|
64
|
-
|
|
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)
|
|
65
77
|
|
|
66
78
|
# Now this will fail: assert omp_get_num_threads() == 4
|
|
67
79
|
# because we need to call this in a parallel region.
|
|
68
|
-
|
|
69
|
-
omp_set_num_threads(1)
|
|
70
|
-
|
|
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
|
|
71
86
|
|
|
72
87
|
|
|
73
88
|
@pytest.mark.skipif(
|
|
@@ -79,3 +94,9 @@ def test_omp_num_threads() -> None:
|
|
|
79
94
|
# Now this will fail: assert omp_get_num_threads() == 4
|
|
80
95
|
# because we need to call this in a parallel region.
|
|
81
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
|
numthreads-0.4.0/README.md
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
2
|
-
|
|
3
|
-
<h1 align="center">numthreads</h1>
|
|
4
|
-
<h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and Accelerate</h3>
|
|
5
|
-
|
|
6
|
-
[](https://pypi.python.org/pypi/numthreads)
|
|
7
|
-
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
8
|
-
[](https://codecov.io/gh/basnijholt/numthreads)
|
|
9
|
-
[](https://github.com/basnijholt/numthreads)
|
|
10
|
-
[](https://numthreads.readthedocs.io/)
|
|
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 Accelerate.
|
|
13
|
-
The number of threads can be set via the command line or in Python code.
|
|
14
|
-
The performance of many numerical algorithms varies significantly based on the number of threads employed.
|
|
15
|
-
While increasing the number of threads can often accelerate these algorithms, it's not always the case.
|
|
16
|
-
In some instances, using more threads may actually impede computational efficiency.
|
|
17
|
-
Therefore, it's important to be able to easily set the number of threads used by these libraries.
|
|
18
|
-
|
|
19
|
-
- Simple and straightforward command-line interface.
|
|
20
|
-
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
21
|
-
- Context manager support for temporary thread setting in Python code.
|
|
22
|
-
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
23
|
-
- No dependencies.
|
|
24
|
-
|
|
25
|
-
<!-- toc-start -->
|
|
26
|
-
|
|
27
|
-
## :books: Table of Contents
|
|
28
|
-
|
|
29
|
-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
30
|
-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
31
|
-
|
|
32
|
-
- [:package: Installation](#package-installation)
|
|
33
|
-
- [:rocket: Quick Start](#rocket-quick-start)
|
|
34
|
-
- [Using as a Python Module](#using-as-a-python-module)
|
|
35
|
-
- [Command Line Interface](#command-line-interface)
|
|
36
|
-
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
37
|
-
- [Windows (PowerShell)](#windows-powershell)
|
|
38
|
-
- [:question: Getting Help](#question-getting-help)
|
|
39
|
-
|
|
40
|
-
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
41
|
-
|
|
42
|
-
<!-- toc-end -->
|
|
43
|
-
|
|
44
|
-
## :package: Installation
|
|
45
|
-
|
|
46
|
-
To install `numthreads`, run the following command:
|
|
47
|
-
|
|
48
|
-
```bash
|
|
49
|
-
pip install numthreads
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## :rocket: Quick Start
|
|
53
|
-
|
|
54
|
-
Get started with `numthreads` in a few seconds.
|
|
55
|
-
|
|
56
|
-
## Using as a Python Module
|
|
57
|
-
|
|
58
|
-
You can also use `numthreads` as a Python module:
|
|
59
|
-
|
|
60
|
-
```python
|
|
61
|
-
from numthreads import set_num_threads
|
|
62
|
-
|
|
63
|
-
set_num_threads(4)
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
This will set the number of threads using the following environment variables:
|
|
67
|
-
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
68
|
-
- MKL (via `MKL_NUM_THREADS`)
|
|
69
|
-
- OpenMP (via `OMP_NUM_THREADS`)
|
|
70
|
-
- NumExpr (via `NUMEXPR_NUM_THREADS`)
|
|
71
|
-
- Accelerate (via `VECLIB_MAXIMUM_THREADS`)
|
|
72
|
-
|
|
73
|
-
or use it as a context manager:
|
|
74
|
-
|
|
75
|
-
```python
|
|
76
|
-
from numthreads import num_threads
|
|
77
|
-
|
|
78
|
-
with num_threads(4):
|
|
79
|
-
# Your code here will run with the specified number of threads
|
|
80
|
-
pass
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
> [!WARNING]
|
|
84
|
-
> Since environment variables are global and typically need to be set before importing any libraries, it's recommended to set the number of threads at the beginning of your Python script.
|
|
85
|
-
|
|
86
|
-
To set OMP (OpenMP) threads at any time, you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
87
|
-
|
|
88
|
-
```python
|
|
89
|
-
from numthreads import omp_set_num_threads
|
|
90
|
-
|
|
91
|
-
omp_set_num_threads(4)
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
or
|
|
95
|
-
|
|
96
|
-
```python
|
|
97
|
-
from numthreads import omp_num_threads
|
|
98
|
-
|
|
99
|
-
with omp_num_threads(4):
|
|
100
|
-
# Your code here will run with the specified number of threads
|
|
101
|
-
pass
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
### Command Line Interface
|
|
105
|
-
|
|
106
|
-
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:
|
|
107
|
-
|
|
108
|
-
```bash
|
|
109
|
-
numthreads 4
|
|
110
|
-
```
|
|
111
|
-
<!-- CODE:BASH:START -->
|
|
112
|
-
<!-- echo '```bash' -->
|
|
113
|
-
<!-- numthreads 4 -->
|
|
114
|
-
<!-- echo '```' -->
|
|
115
|
-
<!-- CODE:END -->
|
|
116
|
-
<!-- OUTPUT:START -->
|
|
117
|
-
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
118
|
-
```bash
|
|
119
|
-
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
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
<!-- OUTPUT:END -->
|
|
123
|
-
|
|
124
|
-
### Unix-like Systems (Linux, macOS, WSL)
|
|
125
|
-
|
|
126
|
-
To apply the settings in your shell:
|
|
127
|
-
|
|
128
|
-
```bash
|
|
129
|
-
eval $(numthreads <number_of_threads>)
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### Windows (PowerShell)
|
|
133
|
-
|
|
134
|
-
In PowerShell, use:
|
|
135
|
-
|
|
136
|
-
```powershell
|
|
137
|
-
Invoke-Expression $(numthreads <number_of_threads>)
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
## :question: Getting Help
|
|
142
|
-
|
|
143
|
-
For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|