Skip to content

Fix a bug in is_patched function #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
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
6 changes: 5 additions & 1 deletion .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Conda package

on: push
on:
push:
branches:
- master
pull_request:

permissions: read-all

Expand Down
86 changes: 74 additions & 12 deletions mkl_umath/src/_patch.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# cython: language_level=3

import mkl_umath._ufuncs as mu
import numpy.core.umath as nu

cimport numpy as cnp
import numpy as np
Expand Down Expand Up @@ -59,15 +58,15 @@ cdef class patch:
self.functions_count = 0
for umath in umaths:
mkl_umath = getattr(mu, umath)
self.functions_count = self.functions_count + mkl_umath.ntypes
self.functions_count += mkl_umath.ntypes

self.functions = <function_info *> malloc(self.functions_count * sizeof(function_info))

func_number = 0
for umath in umaths:
patch_umath = getattr(mu, umath)
c_patch_umath = <cnp.ufunc>patch_umath
c_orig_umath = <cnp.ufunc>getattr(nu, umath)
c_orig_umath = <cnp.ufunc>getattr(np, umath)
nargs = c_patch_umath.nargs
for pi in range(c_patch_umath.ntypes):
oi = 0
Expand Down Expand Up @@ -103,7 +102,7 @@ cdef class patch:
cdef int* signature

for func in self.functions_dict:
np_umath = getattr(nu, func[0])
np_umath = getattr(np, func[0])
index = self.functions_dict[func]
function = self.functions[index].patch_function
signature = self.functions[index].signature
Expand All @@ -118,7 +117,7 @@ cdef class patch:
cdef int* signature

for func in self.functions_dict:
np_umath = getattr(nu, func[0])
np_umath = getattr(np, func[0])
index = self.functions_dict[func]
function = self.functions[index].original_function
signature = self.functions[index].signature
Expand All @@ -143,34 +142,97 @@ def _initialize_tls():


def use_in_numpy():
'''
"""
Enables using of mkl_umath in Numpy.
'''

Examples
--------
>>> import mkl_umath, numpy as np
>>> mkl_umath.is_patched()
# False

>>> mkl_umath.use_in_numpy() # Enable mkl_umath in Numpy
>>> mkl_umath.is_patched()
# True

>>> mkl_umath.restore() # Disable mkl_umath in Numpy
>>> mkl_umath.is_patched()
# False

"""
if not _is_tls_initialized():
_initialize_tls()
_tls.patch.do_patch()


def restore():
'''
"""
Disables using of mkl_umath in Numpy.
'''

Examples
--------
>>> import mkl_umath, numpy as np
>>> mkl_umath.is_patched()
# False

>>> mkl_umath.use_in_numpy() # Enable mkl_umath in Numpy
>>> mkl_umath.is_patched()
# True

>>> mkl_umath.restore() # Disable mkl_umath in Numpy
>>> mkl_umath.is_patched()
# False

"""
if not _is_tls_initialized():
_initialize_tls()
_tls.patch.do_unpatch()


def is_patched():
'''
"""
Returns whether Numpy has been patched with mkl_umath.
'''

Examples
--------
>>> import mkl_umath, numpy as np
>>> mkl_umath.is_patched()
# False

>>> mkl_umath.use_in_numpy() # Enable mkl_umath in Numpy
>>> mkl_umath.is_patched()
# True

>>> mkl_umath.restore() # Disable mkl_umath in Numpy
>>> mkl_umath.is_patched()
# False

"""
if not _is_tls_initialized():
_initialize_tls()
_tls.patch.is_patched()
return _tls.patch.is_patched()

from contextlib import ContextDecorator

class mkl_umath(ContextDecorator):
"""
Context manager and decorator to temporarily patch NumPy ufuncs
with MKL-based implementations.

Examples
--------
>>> import mkl_umath, numpy as np
>>> mkl_umath.is_patched()
# False

>>> with mkl_umath.mkl_umath(): # Enable mkl_umath in Numpy
>>> print(mkl_umath.is_patched())
# True

>>> mkl_umath.is_patched()
# False

"""
def __enter__(self):
use_in_numpy()
return self
Expand Down
Loading