Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extend-ignore =
D102,
# missing docstring in public function:
D103,
# missing docstring in public package:
D104,
# missing docstring in __init__:
D107,
# 1 blank line required between summary line and description:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-with-clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:
# mkl_umath cannot be installed in editable mode, we need
# to change directory before importing it and running tests
cd ..
python -m pytest -sv --pyargs mkl_umath/mkl_umath/tests
python -m pytest -sv --pyargs mkl_umath
2 changes: 1 addition & 1 deletion .github/workflows/build_pip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ jobs:
# mkl_umath cannot be installed in editable mode, we need
# to change directory before importing it and running tests
cd ..
python -m pytest -v mkl_umath/mkl_umath/tests
python -m pytest -v --pyargs mkl_umath
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 @@ -49,9 +49,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 @@ -134,13 +134,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 @@ -43,7 +43,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 @@ -47,7 +47,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 (
Comment thread
ndgrigorian marked this conversation as resolved.
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