numthreads 0.3.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.
@@ -0,0 +1,258 @@
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
+
40
+ <img src="https://media.githubusercontent.com/media/basnijholt/nijho.lt/3986f45bae9ea4e834486eab4f7f6963d980c7b6/content/project/numthreads/featured.png" align="right" style="width: 200px;" />
41
+
42
+ <h1 align="center">numthreads</h1>
43
+ <h3 align="center">Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and Accelerate</h3>
44
+
45
+ [![PyPI](https://img.shields.io/pypi/v/numthreads.svg)](https://pypi.python.org/pypi/numthreads)
46
+ [![Build Status](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml/badge.svg)](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
47
+ [![CodeCov](https://codecov.io/gh/basnijholt/numthreads/branch/main/graph/badge.svg)](https://codecov.io/gh/basnijholt/numthreads)
48
+ [![GitHub Repo stars](https://img.shields.io/github/stars/basnijholt/numthreads)](https://github.com/basnijholt/numthreads)
49
+ [![Documentation](https://readthedocs.org/projects/numthreads/badge/?version=latest)](https://numthreads.readthedocs.io/)
50
+
51
+ `numthreads` is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and Accelerate.
52
+ The number of threads can be set via the command line or in Python code.
53
+ The performance of many numerical algorithms varies significantly based on the number of threads employed.
54
+ While increasing the number of threads can often accelerate these algorithms, it's not always the case.
55
+ In some instances, using more threads may actually impede computational efficiency.
56
+ Therefore, it's important to be able to easily set the number of threads used by these libraries.
57
+
58
+ - Simple and straightforward command-line interface.
59
+ - Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
60
+ - Context manager support for temporary thread setting in Python code.
61
+ - Cross-platform compatibility (Linux, macOS, Windows).
62
+ - Tiny (≤7KB) and no dependencies.
63
+
64
+ <!-- toc-start -->
65
+
66
+ ## :books: Table of Contents
67
+
68
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
69
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
70
+
71
+ - [:package: Installation](#package-installation)
72
+ - [:rocket: Quick Start](#rocket-quick-start)
73
+ - [Using as a Python Module](#using-as-a-python-module)
74
+ - [Command Line Interface](#command-line-interface)
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)
79
+ - [:question: Getting Help](#question-getting-help)
80
+
81
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
82
+
83
+ <!-- toc-end -->
84
+
85
+ ## :package: Installation
86
+
87
+ To install `numthreads`, run the following command:
88
+
89
+ ```bash
90
+ pip install numthreads
91
+ ```
92
+
93
+ ## :rocket: Quick Start
94
+
95
+ Get started with `numthreads` in a few seconds.
96
+
97
+ ### Using as a Python Module
98
+
99
+ You can also use `numthreads` as a Python module:
100
+
101
+ ```python
102
+ from numthreads import set_num_threads
103
+
104
+ set_num_threads(4)
105
+ ```
106
+
107
+ This will set the number of threads using the following environment variables:
108
+
109
+ - OpenBLAS (via `OPENBLAS_NUM_THREADS`)
110
+ - MKL (via `MKL_NUM_THREADS`)
111
+ - OpenMP (via `OMP_NUM_THREADS`)
112
+ - NumExpr (via `NUMEXPR_NUM_THREADS`)
113
+ - Accelerate (via `VECLIB_MAXIMUM_THREADS`)
114
+
115
+ or use it as a context manager:
116
+
117
+ ```python
118
+ from numthreads import num_threads
119
+
120
+ with num_threads(4):
121
+ # Your code here will run with the specified number of threads
122
+ pass
123
+ ```
124
+
125
+ > [!WARNING]
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.
127
+
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:
129
+
130
+ ```python
131
+ from numthreads import omp_set_num_threads
132
+
133
+ omp_set_num_threads(4)
134
+ ```
135
+
136
+ or
137
+
138
+ ```python
139
+ from numthreads import omp_num_threads
140
+
141
+ with omp_num_threads(4):
142
+ # Your code here will run with the specified number of threads
143
+ pass
144
+ ```
145
+
146
+ ### Command Line Interface
147
+
148
+ 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:
149
+
150
+ ```bash
151
+ numthreads 4
152
+ ```
153
+
154
+ <!-- CODE:BASH:START -->
155
+ <!-- echo '```bash' -->
156
+ <!-- numthreads 4 -->
157
+ <!-- echo '```' -->
158
+ <!-- CODE:END -->
159
+
160
+ Which will print the following:
161
+
162
+ <!-- OUTPUT:START -->
163
+ <!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
164
+ ```bash
165
+ export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
166
+ ```
167
+
168
+ <!-- OUTPUT:END -->
169
+
170
+ #### Unix-like Systems (Linux, macOS, WSL)
171
+
172
+ To apply the settings in your shell:
173
+
174
+ ```bash
175
+ eval $(numthreads <number_of_threads>)
176
+ ```
177
+
178
+ #### Windows (PowerShell)
179
+
180
+ In PowerShell, use:
181
+
182
+ ```powershell
183
+ Invoke-Expression $(numthreads <number_of_threads>)
184
+ ```
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.
255
+
256
+ ## :question: Getting Help
257
+
258
+ For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).
@@ -0,0 +1,219 @@
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
+ [![PyPI](https://img.shields.io/pypi/v/numthreads.svg)](https://pypi.python.org/pypi/numthreads)
7
+ [![Build Status](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml/badge.svg)](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
8
+ [![CodeCov](https://codecov.io/gh/basnijholt/numthreads/branch/main/graph/badge.svg)](https://codecov.io/gh/basnijholt/numthreads)
9
+ [![GitHub Repo stars](https://img.shields.io/github/stars/basnijholt/numthreads)](https://github.com/basnijholt/numthreads)
10
+ [![Documentation](https://readthedocs.org/projects/numthreads/badge/?version=latest)](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
+ - Tiny (≤7KB) and 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
+ - [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)
40
+ - [:question: Getting Help](#question-getting-help)
41
+
42
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
43
+
44
+ <!-- toc-end -->
45
+
46
+ ## :package: Installation
47
+
48
+ To install `numthreads`, run the following command:
49
+
50
+ ```bash
51
+ pip install numthreads
52
+ ```
53
+
54
+ ## :rocket: Quick Start
55
+
56
+ Get started with `numthreads` in a few seconds.
57
+
58
+ ### Using as a Python Module
59
+
60
+ You can also use `numthreads` as a Python module:
61
+
62
+ ```python
63
+ from numthreads import set_num_threads
64
+
65
+ set_num_threads(4)
66
+ ```
67
+
68
+ This will set the number of threads using the following environment variables:
69
+
70
+ - OpenBLAS (via `OPENBLAS_NUM_THREADS`)
71
+ - MKL (via `MKL_NUM_THREADS`)
72
+ - OpenMP (via `OMP_NUM_THREADS`)
73
+ - NumExpr (via `NUMEXPR_NUM_THREADS`)
74
+ - Accelerate (via `VECLIB_MAXIMUM_THREADS`)
75
+
76
+ or use it as a context manager:
77
+
78
+ ```python
79
+ from numthreads import num_threads
80
+
81
+ with num_threads(4):
82
+ # Your code here will run with the specified number of threads
83
+ pass
84
+ ```
85
+
86
+ > [!WARNING]
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.
88
+
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:
90
+
91
+ ```python
92
+ from numthreads import omp_set_num_threads
93
+
94
+ omp_set_num_threads(4)
95
+ ```
96
+
97
+ or
98
+
99
+ ```python
100
+ from numthreads import omp_num_threads
101
+
102
+ with omp_num_threads(4):
103
+ # Your code here will run with the specified number of threads
104
+ pass
105
+ ```
106
+
107
+ ### Command Line Interface
108
+
109
+ 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:
110
+
111
+ ```bash
112
+ numthreads 4
113
+ ```
114
+
115
+ <!-- CODE:BASH:START -->
116
+ <!-- echo '```bash' -->
117
+ <!-- numthreads 4 -->
118
+ <!-- echo '```' -->
119
+ <!-- CODE:END -->
120
+
121
+ Which will print the following:
122
+
123
+ <!-- OUTPUT:START -->
124
+ <!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
125
+ ```bash
126
+ export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'
127
+ ```
128
+
129
+ <!-- OUTPUT:END -->
130
+
131
+ #### Unix-like Systems (Linux, macOS, WSL)
132
+
133
+ To apply the settings in your shell:
134
+
135
+ ```bash
136
+ eval $(numthreads <number_of_threads>)
137
+ ```
138
+
139
+ #### Windows (PowerShell)
140
+
141
+ In PowerShell, use:
142
+
143
+ ```powershell
144
+ Invoke-Expression $(numthreads <number_of_threads>)
145
+ ```
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.
216
+
217
+ ## :question: Getting Help
218
+
219
+ For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).