|
| 1 | +#******************************************************************************* |
| 2 | +# Copyright 2014-2020 Intel Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +#******************************************************************************/ |
| 16 | + |
| 17 | +# distutils: language = c |
| 18 | +# cython: language_level=2 |
| 19 | + |
| 20 | +import mkl_umath._ufuncs as mu |
| 21 | +import numpy.core.umath as nu |
| 22 | + |
| 23 | +cimport numpy as cnp |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +from libc.stdlib cimport malloc, free |
| 27 | + |
| 28 | +cdef extern from "loops_intel.h": |
| 29 | + cnp.PyUFuncGenericFunction get_func_by_name(char*) |
| 30 | + |
| 31 | +cnp.import_umath() |
| 32 | + |
| 33 | +def _get_func_name(name, type): |
| 34 | + if type.startswith('f'): |
| 35 | + type_str = 'FLOAT' |
| 36 | + elif type.startswith('d'): |
| 37 | + type_str = 'DOUBLE' |
| 38 | + elif type.startswith('F'): |
| 39 | + type_str = 'CFLOAT' |
| 40 | + elif type.startswith('D'): |
| 41 | + type_str = 'CDOUBLE' |
| 42 | + else: |
| 43 | + raise ValueError("_get_func_name: Unexpected type specified!") |
| 44 | + func_name = type_str + '_' + name |
| 45 | + if type.startswith('fl') or type.startswith('dl'): |
| 46 | + func_name = func_name + '_long' |
| 47 | + return func_name |
| 48 | + |
| 49 | + |
| 50 | +cdef void _fill_signature(signature_str, int* signature): |
| 51 | + for i in range(len(signature_str)): |
| 52 | + if signature_str[i] == 'f': |
| 53 | + signature[i] = cnp.NPY_FLOAT |
| 54 | + elif signature_str[i] == 'd': |
| 55 | + signature[i] = cnp.NPY_DOUBLE |
| 56 | + elif signature_str[i] == 'F': |
| 57 | + signature[i] = cnp.NPY_CFLOAT |
| 58 | + elif signature_str[i] == 'D': |
| 59 | + signature[i] = cnp.NPY_CDOUBLE |
| 60 | + elif signature_str[i] == 'i': |
| 61 | + signature[i] = cnp.NPY_INT |
| 62 | + elif signature_str[i] == 'l': |
| 63 | + signature[i] = cnp.NPY_LONG |
| 64 | + elif signature_str[i] == '?': |
| 65 | + signature[i] = cnp.NPY_BOOL |
| 66 | + else: |
| 67 | + raise ValueError("_fill_signature: Unexpected type specified!") |
| 68 | + |
| 69 | + |
| 70 | +cdef cnp.PyUFuncGenericFunction fooSaved |
| 71 | + |
| 72 | +funcs_dict = {} |
| 73 | +cdef cnp.PyUFuncGenericFunction* originalFuncs |
| 74 | + |
| 75 | + |
| 76 | +cdef c_do_patch(): |
| 77 | + cdef int res |
| 78 | + |
| 79 | + global originalFuncs |
| 80 | + |
| 81 | + umaths = [i for i in dir(mu) if isinstance(getattr(mu, i), np.ufunc)] |
| 82 | + func_number = 0 |
| 83 | + for umath in umaths: |
| 84 | + mkl_umath = getattr(mu, umath) |
| 85 | + types = mkl_umath.types |
| 86 | + for type in types: |
| 87 | + funcs_dict[(umath, type)] = func_number |
| 88 | + func_number = func_number + 1 |
| 89 | + originalFuncs = <cnp.PyUFuncGenericFunction *> malloc(len(funcs_dict) * sizeof(cnp.PyUFuncGenericFunction)) |
| 90 | + |
| 91 | + for func in funcs_dict: |
| 92 | + umath = func[0] |
| 93 | + type = func[1] |
| 94 | + np_umath = getattr(nu, umath) |
| 95 | + signature_str = type.replace('->', '') |
| 96 | + signature = <int *> malloc(len(signature_str) * sizeof(int)) |
| 97 | + _fill_signature(signature_str, signature) |
| 98 | + ufunc_name = _get_func_name(umath, type) |
| 99 | + ufunc = get_func_by_name(str.encode(ufunc_name)) |
| 100 | + res = cnp.PyUFunc_ReplaceLoopBySignature(np_umath, ufunc, signature, &(originalFuncs[funcs_dict[func]])) |
| 101 | + free(signature) |
| 102 | + |
| 103 | + |
| 104 | +cdef c_do_unpatch(): |
| 105 | + cdef int res |
| 106 | + cdef cnp.PyUFuncGenericFunction temp |
| 107 | + |
| 108 | + global originalFuncs |
| 109 | + |
| 110 | + for func in funcs_dict: |
| 111 | + umath = func[0] |
| 112 | + type = func[1] |
| 113 | + np_umath = getattr(nu, umath) |
| 114 | + signature_str = type.replace('->', '') |
| 115 | + signature = <int *> malloc(len(signature_str) * sizeof(int)) |
| 116 | + _fill_signature(signature_str, signature) |
| 117 | + res = cnp.PyUFunc_ReplaceLoopBySignature(np_umath, originalFuncs[funcs_dict[(umath, type)]], signature, &temp) |
| 118 | + free(signature) |
| 119 | + free(originalFuncs) |
| 120 | + |
| 121 | + |
| 122 | +def do_patch(): |
| 123 | + c_do_patch() |
| 124 | + |
| 125 | +def do_unpatch(): |
| 126 | + c_do_unpatch() |
0 commit comments