numthreads 0.1.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.1.0/LICENSE +7 -0
- numthreads-0.1.0/PKG-INFO +112 -0
- numthreads-0.1.0/README.md +90 -0
- numthreads-0.1.0/numthreads.egg-info/PKG-INFO +112 -0
- numthreads-0.1.0/numthreads.egg-info/SOURCES.txt +11 -0
- numthreads-0.1.0/numthreads.egg-info/dependency_links.txt +1 -0
- numthreads-0.1.0/numthreads.egg-info/entry_points.txt +2 -0
- numthreads-0.1.0/numthreads.egg-info/requires.txt +14 -0
- numthreads-0.1.0/numthreads.egg-info/top_level.txt +1 -0
- numthreads-0.1.0/numthreads.py +123 -0
- numthreads-0.1.0/pyproject.toml +114 -0
- numthreads-0.1.0/setup.cfg +4 -0
- numthreads-0.1.0/tests/test_numthreads.py +69 -0
numthreads-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2023, Bas Nijholt
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: numthreads
|
|
3
|
+
Version: 0.1.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
|
+
[](https://pypi.python.org/pypi/numthreads)
|
|
24
|
+
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
25
|
+
[](https://codecov.io/gh/basnijholt/numthreads)
|
|
26
|
+
[](https://github.com/basnijholt/numthreads)
|
|
27
|
+
[](https://numthreads.readthedocs.io/)
|
|
28
|
+
|
|
29
|
+
`numthreads` is a concise, easy-to-use tool designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and Accelerate.
|
|
30
|
+
This Python-based utility aids in optimizing the performance of numerical and scientific computing applications by allowing users to efficiently control the threading behavior of these key libraries.
|
|
31
|
+
|
|
32
|
+
<!-- toc-start -->
|
|
33
|
+
|
|
34
|
+
## :books: Table of Contents
|
|
35
|
+
|
|
36
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
37
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
38
|
+
|
|
39
|
+
- [:sparkles: Features](#sparkles-features)
|
|
40
|
+
- [:package: Installation](#package-installation)
|
|
41
|
+
- [:rocket: Quick Start](#rocket-quick-start)
|
|
42
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
43
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
44
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
45
|
+
- [:question: Getting Help](#question-getting-help)
|
|
46
|
+
|
|
47
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
48
|
+
|
|
49
|
+
<!-- toc-end -->
|
|
50
|
+
|
|
51
|
+
## :sparkles: Features
|
|
52
|
+
|
|
53
|
+
- Simple and straightforward command-line interface.
|
|
54
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
55
|
+
- Context manager support for temporary thread setting in Python code.
|
|
56
|
+
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
57
|
+
|
|
58
|
+
## :package: Installation
|
|
59
|
+
|
|
60
|
+
To install `numthreads`, run the following command:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install "numthreads"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## :rocket: Quick Start
|
|
67
|
+
|
|
68
|
+
After installing `numthreads`, you can easily set the number of threads used by supported libraries via the command line. For example, to set the number of threads to 4, run:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
numthreads 4
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Unix-like Systems (Linux, macOS, WSL)
|
|
75
|
+
|
|
76
|
+
To apply the settings in your shell:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
eval $(numthreads <number_of_threads>)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Windows (PowerShell)
|
|
83
|
+
|
|
84
|
+
In PowerShell, use:
|
|
85
|
+
|
|
86
|
+
```powershell
|
|
87
|
+
Invoke-Expression $(numthreads <number_of_threads>)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Using as a Python Module
|
|
91
|
+
|
|
92
|
+
You can also use `numthreads` as a Python module:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from numthreads import set_num_threads
|
|
96
|
+
|
|
97
|
+
set_num_threads(4)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
or
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from numthreads import num_threads
|
|
104
|
+
|
|
105
|
+
with num_threads(4):
|
|
106
|
+
# Your code here will run with the specified number of threads
|
|
107
|
+
pass
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## :question: Getting Help
|
|
111
|
+
|
|
112
|
+
For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
[](https://pypi.python.org/pypi/numthreads)
|
|
2
|
+
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
3
|
+
[](https://codecov.io/gh/basnijholt/numthreads)
|
|
4
|
+
[](https://github.com/basnijholt/numthreads)
|
|
5
|
+
[](https://numthreads.readthedocs.io/)
|
|
6
|
+
|
|
7
|
+
`numthreads` is a concise, easy-to-use tool designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and Accelerate.
|
|
8
|
+
This Python-based utility aids in optimizing the performance of numerical and scientific computing applications by allowing users to efficiently control the threading behavior of these key libraries.
|
|
9
|
+
|
|
10
|
+
<!-- toc-start -->
|
|
11
|
+
|
|
12
|
+
## :books: Table of Contents
|
|
13
|
+
|
|
14
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
15
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
16
|
+
|
|
17
|
+
- [:sparkles: Features](#sparkles-features)
|
|
18
|
+
- [:package: Installation](#package-installation)
|
|
19
|
+
- [:rocket: Quick Start](#rocket-quick-start)
|
|
20
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
21
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
22
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
23
|
+
- [:question: Getting Help](#question-getting-help)
|
|
24
|
+
|
|
25
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
26
|
+
|
|
27
|
+
<!-- toc-end -->
|
|
28
|
+
|
|
29
|
+
## :sparkles: Features
|
|
30
|
+
|
|
31
|
+
- Simple and straightforward command-line interface.
|
|
32
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
33
|
+
- Context manager support for temporary thread setting in Python code.
|
|
34
|
+
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
35
|
+
|
|
36
|
+
## :package: Installation
|
|
37
|
+
|
|
38
|
+
To install `numthreads`, run the following command:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install "numthreads"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## :rocket: Quick Start
|
|
45
|
+
|
|
46
|
+
After installing `numthreads`, you can easily set the number of threads used by supported libraries via the command line. For example, to set the number of threads to 4, run:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
numthreads 4
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Unix-like Systems (Linux, macOS, WSL)
|
|
53
|
+
|
|
54
|
+
To apply the settings in your shell:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
eval $(numthreads <number_of_threads>)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Windows (PowerShell)
|
|
61
|
+
|
|
62
|
+
In PowerShell, use:
|
|
63
|
+
|
|
64
|
+
```powershell
|
|
65
|
+
Invoke-Expression $(numthreads <number_of_threads>)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Using as a Python Module
|
|
69
|
+
|
|
70
|
+
You can also use `numthreads` as a Python module:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from numthreads import set_num_threads
|
|
74
|
+
|
|
75
|
+
set_num_threads(4)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
or
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from numthreads import num_threads
|
|
82
|
+
|
|
83
|
+
with num_threads(4):
|
|
84
|
+
# Your code here will run with the specified number of threads
|
|
85
|
+
pass
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## :question: Getting Help
|
|
89
|
+
|
|
90
|
+
For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: numthreads
|
|
3
|
+
Version: 0.1.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
|
+
[](https://pypi.python.org/pypi/numthreads)
|
|
24
|
+
[](https://github.com/basnijholt/numthreads/actions/workflows/pytest.yml)
|
|
25
|
+
[](https://codecov.io/gh/basnijholt/numthreads)
|
|
26
|
+
[](https://github.com/basnijholt/numthreads)
|
|
27
|
+
[](https://numthreads.readthedocs.io/)
|
|
28
|
+
|
|
29
|
+
`numthreads` is a concise, easy-to-use tool designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and Accelerate.
|
|
30
|
+
This Python-based utility aids in optimizing the performance of numerical and scientific computing applications by allowing users to efficiently control the threading behavior of these key libraries.
|
|
31
|
+
|
|
32
|
+
<!-- toc-start -->
|
|
33
|
+
|
|
34
|
+
## :books: Table of Contents
|
|
35
|
+
|
|
36
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
37
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
38
|
+
|
|
39
|
+
- [:sparkles: Features](#sparkles-features)
|
|
40
|
+
- [:package: Installation](#package-installation)
|
|
41
|
+
- [:rocket: Quick Start](#rocket-quick-start)
|
|
42
|
+
- [Unix-like Systems (Linux, macOS, WSL)](#unix-like-systems-linux-macos-wsl)
|
|
43
|
+
- [Windows (PowerShell)](#windows-powershell)
|
|
44
|
+
- [Using as a Python Module](#using-as-a-python-module)
|
|
45
|
+
- [:question: Getting Help](#question-getting-help)
|
|
46
|
+
|
|
47
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
48
|
+
|
|
49
|
+
<!-- toc-end -->
|
|
50
|
+
|
|
51
|
+
## :sparkles: Features
|
|
52
|
+
|
|
53
|
+
- Simple and straightforward command-line interface.
|
|
54
|
+
- Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
|
|
55
|
+
- Context manager support for temporary thread setting in Python code.
|
|
56
|
+
- Cross-platform compatibility (Linux, macOS, Windows).
|
|
57
|
+
|
|
58
|
+
## :package: Installation
|
|
59
|
+
|
|
60
|
+
To install `numthreads`, run the following command:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install "numthreads"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## :rocket: Quick Start
|
|
67
|
+
|
|
68
|
+
After installing `numthreads`, you can easily set the number of threads used by supported libraries via the command line. For example, to set the number of threads to 4, run:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
numthreads 4
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Unix-like Systems (Linux, macOS, WSL)
|
|
75
|
+
|
|
76
|
+
To apply the settings in your shell:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
eval $(numthreads <number_of_threads>)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Windows (PowerShell)
|
|
83
|
+
|
|
84
|
+
In PowerShell, use:
|
|
85
|
+
|
|
86
|
+
```powershell
|
|
87
|
+
Invoke-Expression $(numthreads <number_of_threads>)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Using as a Python Module
|
|
91
|
+
|
|
92
|
+
You can also use `numthreads` as a Python module:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from numthreads import set_num_threads
|
|
96
|
+
|
|
97
|
+
set_num_threads(4)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
or
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from numthreads import num_threads
|
|
104
|
+
|
|
105
|
+
with num_threads(4):
|
|
106
|
+
# Your code here will run with the specified number of threads
|
|
107
|
+
pass
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## :question: Getting Help
|
|
111
|
+
|
|
112
|
+
For more information, or to report issues, please visit [numthreads GitHub repository](https://github.com/basnijholt/numthreads).
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
numthreads.py
|
|
4
|
+
pyproject.toml
|
|
5
|
+
numthreads.egg-info/PKG-INFO
|
|
6
|
+
numthreads.egg-info/SOURCES.txt
|
|
7
|
+
numthreads.egg-info/dependency_links.txt
|
|
8
|
+
numthreads.egg-info/entry_points.txt
|
|
9
|
+
numthreads.egg-info/requires.txt
|
|
10
|
+
numthreads.egg-info/top_level.txt
|
|
11
|
+
tests/test_numthreads.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
numthreads
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""numthreads: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.""" # noqa: E501
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import contextlib
|
|
6
|
+
import ctypes
|
|
7
|
+
import os
|
|
8
|
+
import platform
|
|
9
|
+
import sys
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from collections.abc import Generator
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
16
|
+
|
|
17
|
+
THREAD_CONTROL_ENV_VARS = [
|
|
18
|
+
"OPENBLAS_NUM_THREADS", # OpenBLAS
|
|
19
|
+
"MKL_NUM_THREADS", # MKL
|
|
20
|
+
"OMP_NUM_THREADS", # OMP
|
|
21
|
+
"NUMEXPR_NUM_THREADS", # NumExpr
|
|
22
|
+
"VECLIB_MAXIMUM_THREADS", # Accelerate
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def set_num_threads(n: int = 1) -> None:
|
|
27
|
+
"""Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate."""
|
|
28
|
+
for var in THREAD_CONTROL_ENV_VARS:
|
|
29
|
+
os.environ[var] = str(n)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@contextlib.contextmanager
|
|
33
|
+
def num_threads(n: int = 1) -> Generator[None, None, None]:
|
|
34
|
+
"""Context manager to set and then restore thread number settings."""
|
|
35
|
+
original_settings = {var: os.environ.get(var) for var in THREAD_CONTROL_ENV_VARS}
|
|
36
|
+
|
|
37
|
+
set_num_threads(n)
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
yield
|
|
41
|
+
finally:
|
|
42
|
+
for var, value in original_settings.items():
|
|
43
|
+
if value is None: # pragma: no cover
|
|
44
|
+
os.environ.pop(var, None)
|
|
45
|
+
else:
|
|
46
|
+
os.environ[var] = value
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _load_omp_library() -> ctypes.CDLL:
|
|
50
|
+
"""Loads the OpenMP library based on the operating system."""
|
|
51
|
+
system = platform.system()
|
|
52
|
+
|
|
53
|
+
if system == "Darwin": # pragma: no cover
|
|
54
|
+
lib_name = "libomp.dylib"
|
|
55
|
+
elif system == "Linux": # pragma: no cover
|
|
56
|
+
lib_name = "libgomp.so.1"
|
|
57
|
+
elif system == "Windows": # pragma: no cover
|
|
58
|
+
lib_name = "libiomp5md.dll"
|
|
59
|
+
else: # pragma: no cover
|
|
60
|
+
msg = f"Unsupported operating system: {system}"
|
|
61
|
+
raise NotImplementedError(msg)
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
return ctypes.CDLL(lib_name)
|
|
65
|
+
except OSError as e: # pragma: no cover
|
|
66
|
+
msg = f"Error loading {lib_name}. Make sure OpenMP is installed."
|
|
67
|
+
raise OSError(msg) from e
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def omp_set_num_threads(num_threads: int) -> None:
|
|
71
|
+
"""Sets the number of threads to be used by OpenMP parallel regions.
|
|
72
|
+
|
|
73
|
+
Parameters
|
|
74
|
+
----------
|
|
75
|
+
num_threads
|
|
76
|
+
Number of threads to set.
|
|
77
|
+
"""
|
|
78
|
+
omp_lib = _load_omp_library()
|
|
79
|
+
omp_lib.omp_set_num_threads(num_threads)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def omp_get_num_threads() -> int:
|
|
83
|
+
"""Returns the number of threads in the current OpenMP parallel region."""
|
|
84
|
+
omp_lib = _load_omp_library()
|
|
85
|
+
omp_lib.omp_get_num_threads.restype = ctypes.c_int
|
|
86
|
+
return omp_lib.omp_get_num_threads()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def main() -> None: # pragma: no cover
|
|
90
|
+
"""Command-line interface."""
|
|
91
|
+
description = (
|
|
92
|
+
"Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate."
|
|
93
|
+
" Usage: Run `numthreads <number>` to print the export commands."
|
|
94
|
+
" On Unix-like systems (Linux, macOS, WSL), use `eval $(numthreads <number>)`"
|
|
95
|
+
" in your shell to apply these settings."
|
|
96
|
+
" On Windows, in PowerShell, use `Invoke-Expression $(numthreads <number>)`."
|
|
97
|
+
)
|
|
98
|
+
parser = argparse.ArgumentParser(
|
|
99
|
+
description=description,
|
|
100
|
+
)
|
|
101
|
+
parser.add_argument(
|
|
102
|
+
"n",
|
|
103
|
+
type=int,
|
|
104
|
+
help="Number of threads to set.",
|
|
105
|
+
)
|
|
106
|
+
args = parser.parse_args(args=None if sys.argv[1:] else ["--help"])
|
|
107
|
+
|
|
108
|
+
n = args.n
|
|
109
|
+
system = platform.system()
|
|
110
|
+
if system == "Windows":
|
|
111
|
+
export_commands = " & ".join(
|
|
112
|
+
f"$env:{var}={n}" for var in THREAD_CONTROL_ENV_VARS
|
|
113
|
+
)
|
|
114
|
+
print(f'powershell.exe -Command "{export_commands}"')
|
|
115
|
+
else:
|
|
116
|
+
export_commands = " ; ".join(
|
|
117
|
+
f"export {var}={n}" for var in THREAD_CONTROL_ENV_VARS
|
|
118
|
+
)
|
|
119
|
+
print(export_commands)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if __name__ == "__main__":
|
|
123
|
+
main()
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=42", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "numthreads"
|
|
7
|
+
description = "Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate."
|
|
8
|
+
dynamic = ["version"]
|
|
9
|
+
authors = [{ name = "Bas Nijholt", email = "bas@nijho.lt" }]
|
|
10
|
+
dependencies = []
|
|
11
|
+
requires-python = ">=3.7"
|
|
12
|
+
|
|
13
|
+
[project.readme]
|
|
14
|
+
file = "README.md"
|
|
15
|
+
content-type = "text/markdown"
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/basnijholt/numthreads"
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
docs = [
|
|
22
|
+
"myst-parser",
|
|
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
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.scripts]
|
|
37
|
+
numthreads = "numthreads:main"
|
|
38
|
+
|
|
39
|
+
[tool.setuptools]
|
|
40
|
+
py-modules = ["numthreads"]
|
|
41
|
+
|
|
42
|
+
[tool.setuptools.packages.find]
|
|
43
|
+
include = ["numthreads.*", "numthreads"]
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.dynamic]
|
|
46
|
+
version = { attr = "numthreads.__version__" }
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
addopts = """
|
|
50
|
+
--cov=numthreads
|
|
51
|
+
--cov-report term
|
|
52
|
+
--cov-report html
|
|
53
|
+
--cov-fail-under=95
|
|
54
|
+
-W error
|
|
55
|
+
-vvv
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
[tool.coverage.run]
|
|
59
|
+
omit = []
|
|
60
|
+
|
|
61
|
+
[tool.coverage.report]
|
|
62
|
+
exclude_lines = [
|
|
63
|
+
"pragma: no cover",
|
|
64
|
+
"raise NotImplementedError",
|
|
65
|
+
"if TYPE_CHECKING:",
|
|
66
|
+
"if __name__ == .__main__.:",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[tool.black]
|
|
70
|
+
line_length = 88
|
|
71
|
+
|
|
72
|
+
[tool.ruff]
|
|
73
|
+
line-length = 88
|
|
74
|
+
target-version = "py37"
|
|
75
|
+
select = ["ALL"]
|
|
76
|
+
ignore = [
|
|
77
|
+
"T20", # flake8-print
|
|
78
|
+
"ANN101", # Missing type annotation for {name} in method
|
|
79
|
+
"S101", # Use of assert detected
|
|
80
|
+
"PD901", # df is a bad variable name. Be kinder to your future self.
|
|
81
|
+
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed in {name}
|
|
82
|
+
"D402", # First line should not be the function's signature
|
|
83
|
+
"PLW0603", # Using the global statement to update `X` is discouraged
|
|
84
|
+
"D401", # First line of docstring should be in imperative mood
|
|
85
|
+
"SLF001", # Private member accessed
|
|
86
|
+
"PLR0913", # Too many arguments in function definition
|
|
87
|
+
"TD002", # Missing author in TODO
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
[tool.ruff.per-file-ignores]
|
|
91
|
+
"tests/*" = ["SLF001", "D103", "E501", "PLR2004"]
|
|
92
|
+
"tests/test_examples.py" = ["E501"]
|
|
93
|
+
".github/*" = ["INP001"]
|
|
94
|
+
"example/*" = ["INP001", "D100"]
|
|
95
|
+
"docs/*" = ["INP001", "E501"]
|
|
96
|
+
|
|
97
|
+
[tool.ruff.mccabe]
|
|
98
|
+
max-complexity = 18
|
|
99
|
+
|
|
100
|
+
[tool.mypy]
|
|
101
|
+
python_version = "3.7"
|
|
102
|
+
|
|
103
|
+
# Use bump-my-version, e.g., call `bump-my-version bump minor`
|
|
104
|
+
[tool.bumpversion]
|
|
105
|
+
current_version = "0.2.0"
|
|
106
|
+
commit = true
|
|
107
|
+
commit_args = "--no-verify"
|
|
108
|
+
tag = true
|
|
109
|
+
tag_name = "v{new_version}"
|
|
110
|
+
|
|
111
|
+
[[tool.bumpversion.files]]
|
|
112
|
+
filename = "numthreads.py"
|
|
113
|
+
replace = '__version__ = "{new_version}"'
|
|
114
|
+
search = '__version__ = "{current_version}"'
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Tests for ``numthreads`` package."""
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from numthreads import (
|
|
7
|
+
num_threads,
|
|
8
|
+
omp_get_num_threads,
|
|
9
|
+
omp_set_num_threads,
|
|
10
|
+
set_num_threads,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_set_num_threads() -> None:
|
|
15
|
+
set_num_threads(4)
|
|
16
|
+
for var in [
|
|
17
|
+
"OPENBLAS_NUM_THREADS",
|
|
18
|
+
"MKL_NUM_THREADS",
|
|
19
|
+
"OMP_NUM_THREADS",
|
|
20
|
+
"NUMEXPR_NUM_THREADS",
|
|
21
|
+
"VECLIB_MAXIMUM_THREADS",
|
|
22
|
+
]:
|
|
23
|
+
assert os.environ.get(var) == "4"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_num_threads_context_manager() -> None:
|
|
27
|
+
original_values = {
|
|
28
|
+
var: os.environ.get(var)
|
|
29
|
+
for var in [
|
|
30
|
+
"OPENBLAS_NUM_THREADS",
|
|
31
|
+
"MKL_NUM_THREADS",
|
|
32
|
+
"OMP_NUM_THREADS",
|
|
33
|
+
"NUMEXPR_NUM_THREADS",
|
|
34
|
+
"VECLIB_MAXIMUM_THREADS",
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
with num_threads(2):
|
|
39
|
+
for var in [
|
|
40
|
+
"OPENBLAS_NUM_THREADS",
|
|
41
|
+
"MKL_NUM_THREADS",
|
|
42
|
+
"OMP_NUM_THREADS",
|
|
43
|
+
"NUMEXPR_NUM_THREADS",
|
|
44
|
+
"VECLIB_MAXIMUM_THREADS",
|
|
45
|
+
]:
|
|
46
|
+
assert os.environ.get(var) == "2"
|
|
47
|
+
|
|
48
|
+
for var in [
|
|
49
|
+
"OPENBLAS_NUM_THREADS",
|
|
50
|
+
"MKL_NUM_THREADS",
|
|
51
|
+
"OMP_NUM_THREADS",
|
|
52
|
+
"NUMEXPR_NUM_THREADS",
|
|
53
|
+
"VECLIB_MAXIMUM_THREADS",
|
|
54
|
+
]:
|
|
55
|
+
assert os.environ.get(var) == original_values[var]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@pytest.mark.skipif(
|
|
59
|
+
os.name != "posix",
|
|
60
|
+
reason="OMP functions are tested only on POSIX systems",
|
|
61
|
+
)
|
|
62
|
+
def test_omp_set_get_num_threads() -> None:
|
|
63
|
+
omp_set_num_threads(4)
|
|
64
|
+
|
|
65
|
+
# Now this will fail: assert omp_get_num_threads() == 4
|
|
66
|
+
# because we need to call this in a parallel region.
|
|
67
|
+
|
|
68
|
+
omp_set_num_threads(1)
|
|
69
|
+
assert omp_get_num_threads() == 1
|