Skip to content

Commit 7a82440

Browse files
committed
Update umath patching
* Add safety checks to patching * Add warnings related to multi-threaded programs * Implement _GlobalPatch wrapper class which implements patching globally * Rename _patch module to _patch_numpy * Rename patching functions
1 parent 402d639 commit 7a82440

11 files changed

Lines changed: 174 additions & 136 deletions

File tree

.github/workflows/conda-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ jobs:
133133
run: |
134134
source "$CONDA/etc/profile.d/conda.sh"
135135
conda activate test_mkl_umath
136-
python -c "import mkl_umath, numpy as np; mkl_umath.use_in_numpy(); np.sin(np.linspace(0, 1, num=10**6));"
136+
python -c "import mkl_umath, numpy as np; mkl_umath.patch_numpy_umath(); np.sin(np.linspace(0, 1, num=10**6));"
137137
138138
- name: Run tests
139139
run: |
@@ -328,7 +328,7 @@ jobs:
328328
run: |
329329
@ECHO ON
330330
conda activate mkl_umath_test
331-
python -c "import mkl_umath, numpy as np; mkl_umath.use_in_numpy(); np.sin(np.linspace(0, 1, num=10**6));"
331+
python -c "import mkl_umath, numpy as np; mkl_umath.patch_numpy_umath(); np.sin(np.linspace(0, 1, num=10**6));"
332332
333333
- name: Run tests
334334
shell: cmd /C CALL {0}

AGENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Entry point for agent context in this repo.
77

88
It provides:
99
- `mkl_umath._ufuncs` — OneMKL-backed NumPy ufunc loops
10-
- `mkl_umath._patch` — runtime patching interface (`use_in_numpy()`, `restore()`, `is_patched()`)
10+
- `mkl_umath._patch_numpy` — runtime patching interface (`patch_numpy_umath` `restore_numpy_umath`, `is_patched()`)
1111
- Performance-optimized math operations (sin, cos, exp, log, etc.) using Intel MKL VM
1212

1313
## Key components
1414
- **Python interface:** `mkl_umath/__init__.py`, `_init_helper.py`
1515
- **Core C implementation:** `mkl_umath/src/` (ufuncsmodule.c, mkl_umath_loops.c.src)
16-
- **Cython patch layer:** `mkl_umath/src/_patch.pyx`
16+
- **Cython patch layer:** `mkl_umath/src/_patch_numpy.pyx`
1717
- **Code generation:** `generate_umath.py`, `generate_umath_doc.py`
1818
- **Build system:** CMake (CMakeLists.txt) + scikit-build
1919

@@ -49,9 +49,9 @@ CC=${CC:-icx} pip install --no-build-isolation --no-deps . # clang is also supp
4949
## Usage
5050
```python
5151
import mkl_umath
52-
mkl_umath.use_in_numpy() # Patch NumPy to use MKL loops
52+
mkl_umath.patch_numpy_umath() # Patch NumPy to use MKL loops
5353
# ... perform NumPy operations (now accelerated) ...
54-
mkl_umath.restore() # Restore original NumPy loops
54+
mkl_umath.restore_numpy_umath() # Restore original NumPy loops
5555
```
5656

5757
## How to work in this repo

CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ if (UNIX)
134134
endif()
135135
install(TARGETS _ufuncs LIBRARY DESTINATION mkl_umath)
136136

137-
add_cython_target(_patch "mkl_umath/src/_patch.pyx" C OUTPUT_VAR _generated_src)
138-
Python_add_library(_patch MODULE WITH_SOABI ${_generated_src})
139-
target_include_directories(_patch PRIVATE "mkl_umath/src/" ${Python_NumPy_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
140-
target_compile_definitions(_patch PUBLIC NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
141-
target_link_libraries(_patch PRIVATE mkl_umath_loops)
142-
set_target_properties(_patch PROPERTIES C_STANDARD 99)
137+
add_cython_target(_patch_numpy "mkl_umath/src/_patch_numpy.pyx" C OUTPUT_VAR _generated_src)
138+
Python_add_library(_patch_numpy MODULE WITH_SOABI ${_generated_src})
139+
target_include_directories(_patch_numpy PRIVATE "mkl_umath/src/" ${Python_NumPy_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
140+
target_compile_definitions(_patch_numpy PUBLIC NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
141+
target_link_libraries(_patch_numpy PRIVATE mkl_umath_loops)
142+
set_target_properties(_patch_numpy PROPERTIES C_STANDARD 99)
143143
if (UNIX)
144-
set_target_properties(_patch PROPERTIES INSTALL_RPATH "$ORIGIN/../..;$ORIGIN/../../..;$ORIGIN")
144+
set_target_properties(_patch_numpy PROPERTIES INSTALL_RPATH "$ORIGIN/../..;$ORIGIN/../../..;$ORIGIN")
145145
endif()
146-
install(TARGETS _patch LIBRARY DESTINATION mkl_umath)
146+
install(TARGETS _patch_numpy LIBRARY DESTINATION mkl_umath)

conda-recipe-cf/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test:
4343
imports:
4444
- mkl_umath
4545
- mkl_umath._ufuncs
46-
- mkl_umath._patch
46+
- mkl_umath._patch_numpy
4747

4848
about:
4949
home: http://github.com/IntelPython/mkl_umath

conda-recipe/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test:
4747
imports:
4848
- mkl_umath
4949
- mkl_umath._ufuncs
50-
- mkl_umath._patch
50+
- mkl_umath._patch_numpy
5151

5252
about:
5353
home: http://github.com/IntelPython/mkl_umath

mkl_umath/AGENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Core MKL-backed ufunc implementation: Python interface, Cython patching, and C/M
1414

1515
## Patching API
1616
```python
17-
mkl_umath.use_in_numpy() # Replace NumPy loops with MKL
18-
mkl_umath.restore() # Restore original NumPy loops
19-
mkl_umath.is_patched() # Check patch status
17+
mkl_umath.patch_numpy_umath() # Replace NumPy loops with MKL
18+
mkl_umath.restore_numpy_umath() # Restore original NumPy loops
19+
mkl_umath.is_patched() # Check patch status
2020
```
2121

2222
## Development guardrails
@@ -31,6 +31,6 @@ mkl_umath.is_patched() # Check patch status
3131
- Docstrings: dual NumPy 1.x/2.x support via separate docstring modules
3232

3333
## Notes
34-
- `_patch.pyx` is Cython; changes require Cython rebuild
34+
- `_patch_numpy.pyx` is Cython; changes require Cython rebuild
3535
- MKL VM loops in `src/mkl_umath_loops.c.src`
3636
- `src/ufuncsmodule.c` — NumPy ufunc registration and dispatch

mkl_umath/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
"""
3030

3131
from . import _init_helper
32-
from ._patch import is_patched, mkl_umath, restore, use_in_numpy
32+
from ._patch_numpy import (
33+
is_patched,
34+
mkl_umath,
35+
patch_numpy_umath,
36+
restore_numpy_umath,
37+
)
3338
from ._ufuncs import *
3439
from ._version import __version__
3540

36-
# TODO: add __all__ with public API and remove star imports
37-
3841
del _init_helper

mkl_umath/src/AGENTS.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ C/Cython implementation layer: MKL VM integration, ufunc loops, and NumPy patchi
77
- **ufuncsmodule.h** — ufunc module public headers
88
- **mkl_umath_loops.c.src** — MKL VM loop implementations (template, ~60k LOC)
99
- **mkl_umath_loops.h.src** — loop function declarations (template)
10-
- **_patch.pyx** — Cython patching layer (runtime NumPy loop replacement)
10+
- **_patch_numpy.pyx** — Cython patching layer (runtime NumPy loop replacement)
1111
- **fast_loop_macros.h** — loop generation macros
1212
- **blocking_utils.h** — blocking/chunking utilities for large arrays
1313

@@ -21,15 +21,16 @@ C/Cython implementation layer: MKL VM integration, ufunc loops, and NumPy patchi
2121
- Blocking strategy: chunk large arrays for cache efficiency
2222
- Error handling: MKL VM status → NumPy error state
2323

24-
## Patching mechanism (_patch.pyx)
25-
- Cython extension exposing `use_in_numpy()`, `restore()`, `is_patched()`
24+
## Patching mechanism (_patch_numpy.pyx)
25+
- Cython extension exposing `patch_numpy_umath()`, `restore_numpy_umath()`,
26+
`is_patched()`
2627
- Replaces function pointers in NumPy's ufunc loop tables
2728
- Thread-safe: guards against concurrent patching
2829
- Reversible: stores original pointers for restoration
2930

3031
## Build output
3132
- `mkl_umath_loops.c` → shared library (libmkl_umath_loops.so/.dll)
32-
- `_patch.pyx` → Python extension (_patch.*.so)
33+
- `_patch_numpy.pyx` → Python extension (_patch.*.so)
3334
- `ufuncsmodule.c` + `__umath_generated.c``_ufuncs` extension
3435

3536
## Development notes

0 commit comments

Comments
 (0)