Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:
run: |
source "$CONDA/etc/profile.d/conda.sh"
conda activate test_mkl_umath
python -c "import mkl_umath, numpy as np; mkl_umath.use_in_numpy(); np.sin(np.linspace(0, 1, num=10**6));"
python -c "import mkl_umath, numpy as np; mkl_umath.patch_numpy_umath(); np.sin(np.linspace(0, 1, num=10**6));"

- name: Run tests
run: |
Expand Down Expand Up @@ -328,7 +328,7 @@ jobs:
run: |
@ECHO ON
conda activate mkl_umath_test
python -c "import mkl_umath, numpy as np; mkl_umath.use_in_numpy(); np.sin(np.linspace(0, 1, num=10**6));"
python -c "import mkl_umath, numpy as np; mkl_umath.patch_numpy_umath(); np.sin(np.linspace(0, 1, num=10**6));"

- name: Run tests
shell: cmd /C CALL {0}
Expand Down
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Entry point for agent context in this repo.

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

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

Expand Down Expand Up @@ -50,9 +50,9 @@ CC=${CC:-icx} pip install --no-build-isolation --no-deps . # clang is also supp
## Usage
```python
import mkl_umath
mkl_umath.use_in_numpy() # Patch NumPy to use MKL loops
mkl_umath.patch_numpy_umath() # Patch NumPy to use MKL loops
# ... perform NumPy operations (now accelerated) ...
mkl_umath.restore() # Restore original NumPy loops
mkl_umath.restore_numpy_umath() # Restore original NumPy loops
```

## How to work in this repo
Expand Down
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ if (UNIX)
endif()
install(TARGETS _ufuncs LIBRARY DESTINATION mkl_umath)

add_cython_target(_patch "mkl_umath/src/_patch.pyx" C OUTPUT_VAR _generated_src)
Python_add_library(_patch MODULE WITH_SOABI ${_generated_src})
target_include_directories(_patch PRIVATE "mkl_umath/src/" ${Python_NumPy_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
target_compile_definitions(_patch PUBLIC NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
target_link_libraries(_patch PRIVATE mkl_umath_loops)
set_target_properties(_patch PROPERTIES C_STANDARD 99)
add_cython_target(_patch_numpy "mkl_umath/src/_patch_numpy.pyx" C OUTPUT_VAR _generated_src)
Python_add_library(_patch_numpy MODULE WITH_SOABI ${_generated_src})
target_include_directories(_patch_numpy PRIVATE "mkl_umath/src/" ${Python_NumPy_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
target_compile_definitions(_patch_numpy PUBLIC NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
target_link_libraries(_patch_numpy PRIVATE mkl_umath_loops)
set_target_properties(_patch_numpy PROPERTIES C_STANDARD 99)
if (UNIX)
set_target_properties(_patch PROPERTIES INSTALL_RPATH "$ORIGIN/../..;$ORIGIN/../../..;$ORIGIN")
set_target_properties(_patch_numpy PROPERTIES INSTALL_RPATH "$ORIGIN/../..;$ORIGIN/../../..;$ORIGIN")
endif()
install(TARGETS _patch LIBRARY DESTINATION mkl_umath)
install(TARGETS _patch_numpy LIBRARY DESTINATION mkl_umath)
2 changes: 1 addition & 1 deletion conda-recipe-cf/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test:
imports:
- mkl_umath
- mkl_umath._ufuncs
- mkl_umath._patch
- mkl_umath._patch_numpy

about:
home: http://github.com/IntelPython/mkl_umath
Expand Down
2 changes: 1 addition & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test:
imports:
- mkl_umath
- mkl_umath._ufuncs
- mkl_umath._patch
- mkl_umath._patch_numpy

about:
home: http://github.com/IntelPython/mkl_umath
Expand Down
8 changes: 4 additions & 4 deletions mkl_umath/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Core MKL-backed ufunc implementation: Python interface, Cython patching, and C/M

## Patching API
```python
mkl_umath.use_in_numpy() # Replace NumPy loops with MKL
mkl_umath.restore() # Restore original NumPy loops
mkl_umath.is_patched() # Check patch status
mkl_umath.patch_numpy_umath() # Replace NumPy loops with MKL
mkl_umath.restore_numpy_umath() # Restore original NumPy loops
mkl_umath.is_patched() # Check patch status
```

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

## Notes
- `_patch.pyx` is Cython; changes require Cython rebuild
- `_patch_numpy.pyx` is Cython; changes require Cython rebuild
- MKL VM loops in `src/mkl_umath_loops.c.src`
- `src/ufuncsmodule.c` — NumPy ufunc registration and dispatch
9 changes: 8 additions & 1 deletion mkl_umath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
"""

from . import _init_helper
from ._patch import is_patched, mkl_umath, restore, use_in_numpy
from ._patch_numpy import (
is_patched,
mkl_umath,
patch_numpy_umath,
restore,
restore_numpy_umath,
use_in_numpy,
)
from ._ufuncs import *
from ._version import __version__

Expand Down
9 changes: 5 additions & 4 deletions mkl_umath/src/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ C/Cython implementation layer: MKL VM integration, ufunc loops, and NumPy patchi
- **ufuncsmodule.h** — ufunc module public headers
- **mkl_umath_loops.c.src** — MKL VM loop implementations (template, ~60k LOC)
- **mkl_umath_loops.h.src** — loop function declarations (template)
- **_patch.pyx** — Cython patching layer (runtime NumPy loop replacement)
- **_patch_numpy.pyx** — Cython patching layer (runtime NumPy loop replacement)
- **fast_loop_macros.h** — loop generation macros
- **blocking_utils.h** — blocking/chunking utilities for large arrays

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

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

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

## Development notes
Expand Down
Loading
Loading