numthreads 0.4.0__tar.gz → 0.5.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.4.0/numthreads.egg-info → numthreads-0.5.0}/PKG-INFO +104 -11
- numthreads-0.4.0/PKG-INFO → numthreads-0.5.0/README.md +85 -31
- numthreads-0.4.0/README.md → numthreads-0.5.0/numthreads.egg-info/PKG-INFO +124 -9
- {numthreads-0.4.0 → numthreads-0.5.0}/numthreads.egg-info/entry_points.txt +3 -0
- {numthreads-0.4.0 → numthreads-0.5.0}/numthreads.py +66 -15
- {numthreads-0.4.0 → numthreads-0.5.0}/pyproject.toml +24 -15
- {numthreads-0.4.0 → numthreads-0.5.0}/tests/test_numthreads.py +34 -13
- {numthreads-0.4.0 → numthreads-0.5.0}/LICENSE +0 -0
- {numthreads-0.4.0 → numthreads-0.5.0}/numthreads.egg-info/SOURCES.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.5.0}/numthreads.egg-info/dependency_links.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.5.0}/numthreads.egg-info/requires.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.5.0}/numthreads.egg-info/top_level.txt +0 -0
- {numthreads-0.4.0 → numthreads-0.5.0}/setup.cfg +0 -0
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: numthreads
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
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
|
|
@@ -42,7 +59,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
42
59
|
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
43
60
|
- Context manager support for temporary thread setting in Python code.
|
|
44
61
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
45
|
-
-
|
|
62
|
+
- Tiny (≤7KB) and no dependencies.
|
|
46
63
|
|
|
47
64
|
<!-- toc-start -->
|
|
48
65
|
|
|
@@ -53,10 +70,12 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
53
70
|
|
|
54
71
|
- [:package: Installation](#package-installation)
|
|
55
72
|
- [:rocket: Quick Start](#rocket-quick-start)
|
|
56
|
-
- [Using as a Python Module](#using-as-a-python-module)
|
|
73
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
57
74
|
- [Command Line Interface](#command-line-interface)
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
76
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
77
|
+
- [Get the number of threads](#get-the-number-of-threads)
|
|
78
|
+
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
60
79
|
- [:question: Getting Help](#question-getting-help)
|
|
61
80
|
|
|
62
81
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -75,7 +94,7 @@ pip install numthreads
|
|
|
75
94
|
|
|
76
95
|
Get started with `numthreads` in a few seconds.
|
|
77
96
|
|
|
78
|
-
|
|
97
|
+
### Using as a Python Module
|
|
79
98
|
|
|
80
99
|
You can also use `numthreads` as a Python module:
|
|
81
100
|
|
|
@@ -86,6 +105,7 @@ set_num_threads(4)
|
|
|
86
105
|
```
|
|
87
106
|
|
|
88
107
|
This will set the number of threads using the following environment variables:
|
|
108
|
+
|
|
89
109
|
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
90
110
|
- MKL (via `MKL_NUM_THREADS`)
|
|
91
111
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
@@ -105,7 +125,7 @@ with num_threads(4):
|
|
|
105
125
|
> [!WARNING]
|
|
106
126
|
> 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
127
|
|
|
108
|
-
To set OMP (OpenMP) threads at any time, you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
128
|
+
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
129
|
|
|
110
130
|
```python
|
|
111
131
|
from numthreads import omp_set_num_threads
|
|
@@ -130,20 +150,24 @@ After installing `numthreads`, you can easily set the number of threads used by
|
|
|
130
150
|
```bash
|
|
131
151
|
numthreads 4
|
|
132
152
|
```
|
|
153
|
+
|
|
133
154
|
<!-- CODE:BASH:START -->
|
|
134
155
|
<!-- echo '```bash' -->
|
|
135
156
|
<!-- numthreads 4 -->
|
|
136
157
|
<!-- echo '```' -->
|
|
137
158
|
<!-- CODE:END -->
|
|
159
|
+
|
|
160
|
+
Which will print the following:
|
|
161
|
+
|
|
138
162
|
<!-- OUTPUT:START -->
|
|
139
163
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
140
164
|
```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
|
|
165
|
+
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
|
|
142
166
|
```
|
|
143
167
|
|
|
144
168
|
<!-- OUTPUT:END -->
|
|
145
169
|
|
|
146
|
-
|
|
170
|
+
#### Unix-like Systems (Linux, macOS, WSL)
|
|
147
171
|
|
|
148
172
|
To apply the settings in your shell:
|
|
149
173
|
|
|
@@ -151,7 +175,7 @@ To apply the settings in your shell:
|
|
|
151
175
|
eval $(numthreads <number_of_threads>)
|
|
152
176
|
```
|
|
153
177
|
|
|
154
|
-
|
|
178
|
+
#### Windows (PowerShell)
|
|
155
179
|
|
|
156
180
|
In PowerShell, use:
|
|
157
181
|
|
|
@@ -159,6 +183,75 @@ In PowerShell, use:
|
|
|
159
183
|
Invoke-Expression $(numthreads <number_of_threads>)
|
|
160
184
|
```
|
|
161
185
|
|
|
186
|
+
### Get the number of threads
|
|
187
|
+
|
|
188
|
+
To get the number of threads currently set, run:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
eval $(numthreads 12) # first set something
|
|
192
|
+
numthreads get
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
<!-- CODE:BASH:START -->
|
|
196
|
+
<!-- echo '```bash' -->
|
|
197
|
+
<!-- eval $(numthreads 12) -->
|
|
198
|
+
<!-- numthreads get -->
|
|
199
|
+
<!-- echo '```' -->
|
|
200
|
+
<!-- CODE:END -->
|
|
201
|
+
|
|
202
|
+
Which will print the following:
|
|
203
|
+
|
|
204
|
+
<!-- OUTPUT:START -->
|
|
205
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
206
|
+
```bash
|
|
207
|
+
OPENBLAS_NUM_THREADS: 12
|
|
208
|
+
MKL_NUM_THREADS: 12
|
|
209
|
+
OMP_NUM_THREADS: 12
|
|
210
|
+
NUMEXPR_NUM_THREADS: 12
|
|
211
|
+
VECLIB_MAXIMUM_THREADS: 12
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
<!-- OUTPUT:END -->
|
|
215
|
+
|
|
216
|
+
Check the `numthreads -h` message for more information:
|
|
217
|
+
|
|
218
|
+
<!-- CODE:BASH:START -->
|
|
219
|
+
<!-- echo '```bash' -->
|
|
220
|
+
<!-- numthreads -h -->
|
|
221
|
+
<!-- echo '```' -->
|
|
222
|
+
<!-- CODE:END -->
|
|
223
|
+
<!-- OUTPUT:START -->
|
|
224
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
225
|
+
```bash
|
|
226
|
+
usage: numthreads [-h] n
|
|
227
|
+
|
|
228
|
+
Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
229
|
+
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
230
|
+
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
231
|
+
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
232
|
+
$(numthreads <number>)`.
|
|
233
|
+
|
|
234
|
+
positional arguments:
|
|
235
|
+
n Number of threads to set or use 'get' to display current
|
|
236
|
+
settings.
|
|
237
|
+
|
|
238
|
+
options:
|
|
239
|
+
-h, --help show this help message and exit
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
<!-- OUTPUT:END -->
|
|
243
|
+
|
|
244
|
+
### :electric_plug: Using as a pytest Plugin
|
|
245
|
+
|
|
246
|
+
`numthreads` can be used as a pytest plugin to automatically set the number of threads for your tests.
|
|
247
|
+
|
|
248
|
+
To use it, make sure `numthreads` is installed. Then, run pytest with the `--numthreads` option followed by the desired number of threads:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
pytest --numthreads 1
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.
|
|
162
255
|
|
|
163
256
|
## :question: Getting Help
|
|
164
257
|
|
|
@@ -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>
|
|
@@ -42,7 +20,7 @@ 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
|
+
- Tiny (≤7KB) and no dependencies.
|
|
46
24
|
|
|
47
25
|
<!-- toc-start -->
|
|
48
26
|
|
|
@@ -53,10 +31,12 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
53
31
|
|
|
54
32
|
- [:package: Installation](#package-installation)
|
|
55
33
|
- [:rocket: Quick Start](#rocket-quick-start)
|
|
56
|
-
- [Using as a Python Module](#using-as-a-python-module)
|
|
34
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
57
35
|
- [Command Line Interface](#command-line-interface)
|
|
58
|
-
|
|
59
|
-
|
|
36
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
37
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
38
|
+
- [Get the number of threads](#get-the-number-of-threads)
|
|
39
|
+
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
60
40
|
- [:question: Getting Help](#question-getting-help)
|
|
61
41
|
|
|
62
42
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -75,7 +55,7 @@ pip install numthreads
|
|
|
75
55
|
|
|
76
56
|
Get started with `numthreads` in a few seconds.
|
|
77
57
|
|
|
78
|
-
|
|
58
|
+
### Using as a Python Module
|
|
79
59
|
|
|
80
60
|
You can also use `numthreads` as a Python module:
|
|
81
61
|
|
|
@@ -86,6 +66,7 @@ set_num_threads(4)
|
|
|
86
66
|
```
|
|
87
67
|
|
|
88
68
|
This will set the number of threads using the following environment variables:
|
|
69
|
+
|
|
89
70
|
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
90
71
|
- MKL (via `MKL_NUM_THREADS`)
|
|
91
72
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
@@ -105,7 +86,7 @@ with num_threads(4):
|
|
|
105
86
|
> [!WARNING]
|
|
106
87
|
> 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
88
|
|
|
108
|
-
To set OMP (OpenMP) threads at any time, you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
89
|
+
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
90
|
|
|
110
91
|
```python
|
|
111
92
|
from numthreads import omp_set_num_threads
|
|
@@ -130,20 +111,24 @@ After installing `numthreads`, you can easily set the number of threads used by
|
|
|
130
111
|
```bash
|
|
131
112
|
numthreads 4
|
|
132
113
|
```
|
|
114
|
+
|
|
133
115
|
<!-- CODE:BASH:START -->
|
|
134
116
|
<!-- echo '```bash' -->
|
|
135
117
|
<!-- numthreads 4 -->
|
|
136
118
|
<!-- echo '```' -->
|
|
137
119
|
<!-- CODE:END -->
|
|
120
|
+
|
|
121
|
+
Which will print the following:
|
|
122
|
+
|
|
138
123
|
<!-- OUTPUT:START -->
|
|
139
124
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
140
125
|
```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
|
|
126
|
+
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
|
|
142
127
|
```
|
|
143
128
|
|
|
144
129
|
<!-- OUTPUT:END -->
|
|
145
130
|
|
|
146
|
-
|
|
131
|
+
#### Unix-like Systems (Linux, macOS, WSL)
|
|
147
132
|
|
|
148
133
|
To apply the settings in your shell:
|
|
149
134
|
|
|
@@ -151,7 +136,7 @@ To apply the settings in your shell:
|
|
|
151
136
|
eval $(numthreads <number_of_threads>)
|
|
152
137
|
```
|
|
153
138
|
|
|
154
|
-
|
|
139
|
+
#### Windows (PowerShell)
|
|
155
140
|
|
|
156
141
|
In PowerShell, use:
|
|
157
142
|
|
|
@@ -159,6 +144,75 @@ In PowerShell, use:
|
|
|
159
144
|
Invoke-Expression $(numthreads <number_of_threads>)
|
|
160
145
|
```
|
|
161
146
|
|
|
147
|
+
### Get the number of threads
|
|
148
|
+
|
|
149
|
+
To get the number of threads currently set, run:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
eval $(numthreads 12) # first set something
|
|
153
|
+
numthreads get
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
<!-- CODE:BASH:START -->
|
|
157
|
+
<!-- echo '```bash' -->
|
|
158
|
+
<!-- eval $(numthreads 12) -->
|
|
159
|
+
<!-- numthreads get -->
|
|
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
|
+
OPENBLAS_NUM_THREADS: 12
|
|
169
|
+
MKL_NUM_THREADS: 12
|
|
170
|
+
OMP_NUM_THREADS: 12
|
|
171
|
+
NUMEXPR_NUM_THREADS: 12
|
|
172
|
+
VECLIB_MAXIMUM_THREADS: 12
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
<!-- OUTPUT:END -->
|
|
176
|
+
|
|
177
|
+
Check the `numthreads -h` message for more information:
|
|
178
|
+
|
|
179
|
+
<!-- CODE:BASH:START -->
|
|
180
|
+
<!-- echo '```bash' -->
|
|
181
|
+
<!-- numthreads -h -->
|
|
182
|
+
<!-- echo '```' -->
|
|
183
|
+
<!-- CODE:END -->
|
|
184
|
+
<!-- OUTPUT:START -->
|
|
185
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
186
|
+
```bash
|
|
187
|
+
usage: numthreads [-h] n
|
|
188
|
+
|
|
189
|
+
Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
190
|
+
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
191
|
+
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
192
|
+
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
193
|
+
$(numthreads <number>)`.
|
|
194
|
+
|
|
195
|
+
positional arguments:
|
|
196
|
+
n Number of threads to set or use 'get' to display current
|
|
197
|
+
settings.
|
|
198
|
+
|
|
199
|
+
options:
|
|
200
|
+
-h, --help show this help message and exit
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
<!-- OUTPUT:END -->
|
|
204
|
+
|
|
205
|
+
### :electric_plug: Using as a pytest Plugin
|
|
206
|
+
|
|
207
|
+
`numthreads` can be used as a pytest plugin to automatically set the number of threads for your tests.
|
|
208
|
+
|
|
209
|
+
To use it, make sure `numthreads` is installed. Then, run pytest with the `--numthreads` option followed by the desired number of threads:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
pytest --numthreads 1
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.
|
|
162
216
|
|
|
163
217
|
## :question: Getting Help
|
|
164
218
|
|
|
@@ -1,3 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: numthreads
|
|
3
|
+
Version: 0.5.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
|
+
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
|
+
|
|
1
40
|
<img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
|
|
2
41
|
|
|
3
42
|
<h1 align="center">numthreads</h1>
|
|
@@ -20,7 +59,7 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
20
59
|
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
21
60
|
- Context manager support for temporary thread setting in Python code.
|
|
22
61
|
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
23
|
-
-
|
|
62
|
+
- Tiny (≤7KB) and no dependencies.
|
|
24
63
|
|
|
25
64
|
<!-- toc-start -->
|
|
26
65
|
|
|
@@ -31,10 +70,12 @@ Therefore, it's important to be able to easily set the number of threads used by
|
|
|
31
70
|
|
|
32
71
|
- [:package: Installation](#package-installation)
|
|
33
72
|
- [:rocket: Quick Start](#rocket-quick-start)
|
|
34
|
-
- [Using as a Python Module](#using-as-a-python-module)
|
|
73
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
35
74
|
- [Command Line Interface](#command-line-interface)
|
|
36
|
-
|
|
37
|
-
|
|
75
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
76
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
77
|
+
- [Get the number of threads](#get-the-number-of-threads)
|
|
78
|
+
- [:electric_plug: Using as a pytest Plugin](#electric_plug-using-as-a-pytest-plugin)
|
|
38
79
|
- [:question: Getting Help](#question-getting-help)
|
|
39
80
|
|
|
40
81
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -53,7 +94,7 @@ pip install numthreads
|
|
|
53
94
|
|
|
54
95
|
Get started with `numthreads` in a few seconds.
|
|
55
96
|
|
|
56
|
-
|
|
97
|
+
### Using as a Python Module
|
|
57
98
|
|
|
58
99
|
You can also use `numthreads` as a Python module:
|
|
59
100
|
|
|
@@ -64,6 +105,7 @@ set_num_threads(4)
|
|
|
64
105
|
```
|
|
65
106
|
|
|
66
107
|
This will set the number of threads using the following environment variables:
|
|
108
|
+
|
|
67
109
|
- OpenBLAS (via `OPENBLAS_NUM_THREADS`)
|
|
68
110
|
- MKL (via `MKL_NUM_THREADS`)
|
|
69
111
|
- OpenMP (via `OMP_NUM_THREADS`)
|
|
@@ -83,7 +125,7 @@ with num_threads(4):
|
|
|
83
125
|
> [!WARNING]
|
|
84
126
|
> 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
127
|
|
|
86
|
-
To set OMP (OpenMP) threads at any time, you can use `omp_set_num_threads` or the `omp_num_threads` context manager:
|
|
128
|
+
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:
|
|
87
129
|
|
|
88
130
|
```python
|
|
89
131
|
from numthreads import omp_set_num_threads
|
|
@@ -108,20 +150,24 @@ After installing `numthreads`, you can easily set the number of threads used by
|
|
|
108
150
|
```bash
|
|
109
151
|
numthreads 4
|
|
110
152
|
```
|
|
153
|
+
|
|
111
154
|
<!-- CODE:BASH:START -->
|
|
112
155
|
<!-- echo '```bash' -->
|
|
113
156
|
<!-- numthreads 4 -->
|
|
114
157
|
<!-- echo '```' -->
|
|
115
158
|
<!-- CODE:END -->
|
|
159
|
+
|
|
160
|
+
Which will print the following:
|
|
161
|
+
|
|
116
162
|
<!-- OUTPUT:START -->
|
|
117
163
|
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
118
164
|
```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
|
|
165
|
+
export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
|
|
120
166
|
```
|
|
121
167
|
|
|
122
168
|
<!-- OUTPUT:END -->
|
|
123
169
|
|
|
124
|
-
|
|
170
|
+
#### Unix-like Systems (Linux, macOS, WSL)
|
|
125
171
|
|
|
126
172
|
To apply the settings in your shell:
|
|
127
173
|
|
|
@@ -129,7 +175,7 @@ To apply the settings in your shell:
|
|
|
129
175
|
eval $(numthreads <number_of_threads>)
|
|
130
176
|
```
|
|
131
177
|
|
|
132
|
-
|
|
178
|
+
#### Windows (PowerShell)
|
|
133
179
|
|
|
134
180
|
In PowerShell, use:
|
|
135
181
|
|
|
@@ -137,6 +183,75 @@ In PowerShell, use:
|
|
|
137
183
|
Invoke-Expression $(numthreads <number_of_threads>)
|
|
138
184
|
```
|
|
139
185
|
|
|
186
|
+
### Get the number of threads
|
|
187
|
+
|
|
188
|
+
To get the number of threads currently set, run:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
eval $(numthreads 12) # first set something
|
|
192
|
+
numthreads get
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
<!-- CODE:BASH:START -->
|
|
196
|
+
<!-- echo '```bash' -->
|
|
197
|
+
<!-- eval $(numthreads 12) -->
|
|
198
|
+
<!-- numthreads get -->
|
|
199
|
+
<!-- echo '```' -->
|
|
200
|
+
<!-- CODE:END -->
|
|
201
|
+
|
|
202
|
+
Which will print the following:
|
|
203
|
+
|
|
204
|
+
<!-- OUTPUT:START -->
|
|
205
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
206
|
+
```bash
|
|
207
|
+
OPENBLAS_NUM_THREADS: 12
|
|
208
|
+
MKL_NUM_THREADS: 12
|
|
209
|
+
OMP_NUM_THREADS: 12
|
|
210
|
+
NUMEXPR_NUM_THREADS: 12
|
|
211
|
+
VECLIB_MAXIMUM_THREADS: 12
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
<!-- OUTPUT:END -->
|
|
215
|
+
|
|
216
|
+
Check the `numthreads -h` message for more information:
|
|
217
|
+
|
|
218
|
+
<!-- CODE:BASH:START -->
|
|
219
|
+
<!-- echo '```bash' -->
|
|
220
|
+
<!-- numthreads -h -->
|
|
221
|
+
<!-- echo '```' -->
|
|
222
|
+
<!-- CODE:END -->
|
|
223
|
+
<!-- OUTPUT:START -->
|
|
224
|
+
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
|
|
225
|
+
```bash
|
|
226
|
+
usage: numthreads [-h] n
|
|
227
|
+
|
|
228
|
+
Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
|
|
229
|
+
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
|
|
230
|
+
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
|
|
231
|
+
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
|
|
232
|
+
$(numthreads <number>)`.
|
|
233
|
+
|
|
234
|
+
positional arguments:
|
|
235
|
+
n Number of threads to set or use 'get' to display current
|
|
236
|
+
settings.
|
|
237
|
+
|
|
238
|
+
options:
|
|
239
|
+
-h, --help show this help message and exit
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
<!-- OUTPUT:END -->
|
|
243
|
+
|
|
244
|
+
### :electric_plug: Using as a pytest Plugin
|
|
245
|
+
|
|
246
|
+
`numthreads` can be used as a pytest plugin to automatically set the number of threads for your tests.
|
|
247
|
+
|
|
248
|
+
To use it, make sure `numthreads` is installed. Then, run pytest with the `--numthreads` option followed by the desired number of threads:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
pytest --numthreads 1
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.
|
|
140
255
|
|
|
141
256
|
## :question: Getting Help
|
|
142
257
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"""numthreads: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.""" # 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.5.0"
|
|
16
19
|
|
|
17
20
|
THREAD_CONTROL_ENV_VARS = [
|
|
18
21
|
"OPENBLAS_NUM_THREADS", # OpenBLAS
|
|
@@ -23,10 +26,11 @@ THREAD_CONTROL_ENV_VARS = [
|
|
|
23
26
|
]
|
|
24
27
|
|
|
25
28
|
|
|
26
|
-
def set_num_threads(n: int = 1) -> None:
|
|
29
|
+
def set_num_threads(n: int = 1, *, overwrite: bool = True) -> None:
|
|
27
30
|
"""Set the number of threads via environment variables.
|
|
28
31
|
|
|
29
|
-
Sets:
|
|
32
|
+
Sets the following environment variables:
|
|
33
|
+
|
|
30
34
|
- ``"OPENBLAS_NUM_THREADS"`` for OpenBLAS (Open Basic Linear Algebra Subprograms)
|
|
31
35
|
- ``"MKL_NUM_THREADS"`` for MKL (Intel Math Kernel Library)
|
|
32
36
|
- ``"OMP_NUM_THREADS"`` for OMP (OpenMP)
|
|
@@ -38,9 +42,18 @@ def set_num_threads(n: int = 1) -> None:
|
|
|
38
42
|
threads for NumPy, you should set the environment variables before importing
|
|
39
43
|
NumPy. When using OMP (OpenMP), you can use the ``omp_set_num_threads`` function
|
|
40
44
|
to change the number of threads *after* importing the library.
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
n
|
|
49
|
+
Number of threads to set.
|
|
50
|
+
overwrite
|
|
51
|
+
If true, overwrite the existing value.
|
|
52
|
+
|
|
41
53
|
"""
|
|
42
54
|
for var in THREAD_CONTROL_ENV_VARS:
|
|
43
|
-
os.environ
|
|
55
|
+
if overwrite or var not in os.environ:
|
|
56
|
+
os.environ[var] = str(n)
|
|
44
57
|
|
|
45
58
|
|
|
46
59
|
# Function alias
|
|
@@ -48,11 +61,11 @@ set = set_num_threads # noqa: A001
|
|
|
48
61
|
|
|
49
62
|
|
|
50
63
|
@contextlib.contextmanager
|
|
51
|
-
def num_threads(n: int = 1) -> Generator[None, None, None]:
|
|
64
|
+
def num_threads(n: int = 1, *, overwrite: bool = True) -> Generator[None, None, None]:
|
|
52
65
|
"""Context manager to set and then restore thread number settings."""
|
|
53
66
|
original_settings = {var: os.environ.get(var) for var in THREAD_CONTROL_ENV_VARS}
|
|
54
67
|
|
|
55
|
-
set_num_threads(n)
|
|
68
|
+
set_num_threads(n, overwrite=overwrite)
|
|
56
69
|
|
|
57
70
|
try:
|
|
58
71
|
yield
|
|
@@ -85,7 +98,7 @@ def _load_omp_library() -> ctypes.CDLL:
|
|
|
85
98
|
raise OSError(msg) from e
|
|
86
99
|
|
|
87
100
|
|
|
88
|
-
def omp_set_num_threads(num_threads: int) -> None:
|
|
101
|
+
def omp_set_num_threads(num_threads: int, *, overwrite: bool = True) -> None:
|
|
89
102
|
"""Sets the number of threads to be used by OpenMP parallel regions.
|
|
90
103
|
|
|
91
104
|
When using OMP (OpenMP), you can use this function to change the number of threads
|
|
@@ -96,10 +109,13 @@ def omp_set_num_threads(num_threads: int) -> None:
|
|
|
96
109
|
----------
|
|
97
110
|
num_threads
|
|
98
111
|
Number of threads to set.
|
|
112
|
+
overwrite
|
|
113
|
+
If true, overwrite the existing value.
|
|
99
114
|
"""
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
115
|
+
if overwrite:
|
|
116
|
+
omp_lib = _load_omp_library()
|
|
117
|
+
omp_lib.omp_set_num_threads.argtypes = [ctypes.c_int]
|
|
118
|
+
omp_lib.omp_set_num_threads(num_threads)
|
|
103
119
|
|
|
104
120
|
|
|
105
121
|
def omp_get_num_threads() -> int:
|
|
@@ -110,13 +126,19 @@ def omp_get_num_threads() -> int:
|
|
|
110
126
|
|
|
111
127
|
|
|
112
128
|
@contextlib.contextmanager
|
|
113
|
-
def omp_num_threads(
|
|
129
|
+
def omp_num_threads(
|
|
130
|
+
num_threads: int,
|
|
131
|
+
*,
|
|
132
|
+
overwrite: bool = True,
|
|
133
|
+
) -> Generator[None, None, None]:
|
|
114
134
|
"""Context manager to set and then restore OpenMP thread number settings.
|
|
115
135
|
|
|
116
136
|
Parameters
|
|
117
137
|
----------
|
|
118
138
|
num_threads
|
|
119
139
|
Number of threads to set in the OpenMP parallel regions.
|
|
140
|
+
overwrite
|
|
141
|
+
Whether to overwrite existing values.
|
|
120
142
|
|
|
121
143
|
Examples
|
|
122
144
|
--------
|
|
@@ -127,7 +149,7 @@ def omp_num_threads(num_threads: int) -> Generator[None, None, None]:
|
|
|
127
149
|
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
|
|
128
150
|
"""
|
|
129
151
|
original_num_threads = omp_get_num_threads()
|
|
130
|
-
omp_set_num_threads(num_threads)
|
|
152
|
+
omp_set_num_threads(num_threads, overwrite=overwrite)
|
|
131
153
|
|
|
132
154
|
try:
|
|
133
155
|
yield
|
|
@@ -135,6 +157,13 @@ def omp_num_threads(num_threads: int) -> Generator[None, None, None]:
|
|
|
135
157
|
omp_set_num_threads(original_num_threads)
|
|
136
158
|
|
|
137
159
|
|
|
160
|
+
def print_current_thread_counts() -> None:
|
|
161
|
+
"""Prints the current values of the thread control environment variables."""
|
|
162
|
+
for var in THREAD_CONTROL_ENV_VARS:
|
|
163
|
+
value = os.environ.get(var, "Not set")
|
|
164
|
+
print(f"{var}: {value}")
|
|
165
|
+
|
|
166
|
+
|
|
138
167
|
def main() -> None: # pragma: no cover
|
|
139
168
|
"""Command-line interface."""
|
|
140
169
|
description = (
|
|
@@ -149,12 +178,15 @@ def main() -> None: # pragma: no cover
|
|
|
149
178
|
)
|
|
150
179
|
parser.add_argument(
|
|
151
180
|
"n",
|
|
152
|
-
|
|
153
|
-
help="Number of threads to set.",
|
|
181
|
+
help="Number of threads to set or use 'get' to display current settings.",
|
|
154
182
|
)
|
|
155
183
|
args = parser.parse_args(args=None if sys.argv[1:] else ["--help"])
|
|
156
184
|
|
|
157
|
-
|
|
185
|
+
if args.n == "get":
|
|
186
|
+
print_current_thread_counts()
|
|
187
|
+
return
|
|
188
|
+
|
|
189
|
+
n = int(args.n)
|
|
158
190
|
system = platform.system()
|
|
159
191
|
if system == "Windows":
|
|
160
192
|
export_commands = " & ".join(
|
|
@@ -168,5 +200,24 @@ def main() -> None: # pragma: no cover
|
|
|
168
200
|
print(export_commands)
|
|
169
201
|
|
|
170
202
|
|
|
203
|
+
# Pytest Plugin Integration
|
|
204
|
+
def pytest_addoption(parser: pytest.Parser) -> None: # pragma: no cover
|
|
205
|
+
"""Add the --numthreads option to the pytest command line."""
|
|
206
|
+
parser.addoption(
|
|
207
|
+
"--numthreads",
|
|
208
|
+
action="store",
|
|
209
|
+
default=None, # Default to None, so we don't set unnecessarily
|
|
210
|
+
type=int,
|
|
211
|
+
help="Number of threads to set for OpenBLAS, MKL, OMP, NumExpr, and Accelerate",
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def pytest_configure(config: pytest.Config) -> None: # pragma: no cover
|
|
216
|
+
"""Set the number of threads based on the --numthreads option."""
|
|
217
|
+
numthreads = config.getoption("--numthreads")
|
|
218
|
+
if numthreads is not None:
|
|
219
|
+
set_num_threads(numthreads)
|
|
220
|
+
|
|
221
|
+
|
|
171
222
|
if __name__ == "__main__":
|
|
172
223
|
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,7 +60,6 @@ addopts = """
|
|
|
50
60
|
--cov=numthreads
|
|
51
61
|
--cov-report term
|
|
52
62
|
--cov-report html
|
|
53
|
-
--cov-fail-under=95
|
|
54
63
|
-W error
|
|
55
64
|
-vvv
|
|
56
65
|
"""
|
|
@@ -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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|