Skip to content

Commit 6f64750

Browse files
Merge pull request #17 from IntelPython/fixed-issue-14
Fixed issue 14
2 parents a712dd3 + 0c3f7e0 commit 6f64750

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

mkl/_mkl_service.pyx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2424
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525

26+
# distutils: language = c
27+
# cython: language_level=3
2628

27-
import six
29+
import numbers
2830
import warnings
29-
cimport _mkl_service as mkl
31+
cimport mkl._mkl_service as mkl
3032

3133

3234
ctypedef struct MemStatData:
@@ -80,9 +82,9 @@ cdef int __domain_to_mkl_domain(domain):
8082
'pardiso': mkl.MKL_DOMAIN_PARDISO,
8183
'all': mkl.MKL_DOMAIN_ALL }
8284

83-
if isinstance(domain, six.integer_types):
84-
c_mkl_domain = domain
85-
elif isinstance(domain, six.string_types):
85+
if isinstance(domain, numbers.Integral):
86+
c_mkl_domain = <int>domain
87+
elif isinstance(domain, str):
8688
if domain not in _mapping:
8789
c_mkl_domain = __warn_and_fallback_on_default_domain(domain)
8890
else:
@@ -113,7 +115,7 @@ cpdef set_num_threads_local(num_threads):
113115
https://software.intel.com/en-us/mkl-developer-reference-c-mkl-set-num-threads-local
114116
"""
115117
cdef c_num_threads = 0
116-
if isinstance(num_threads, six.string_types):
118+
if isinstance(num_threads, str):
117119
if num_threads is not 'global_num_threads':
118120
raise ValueError("The argument of set_num_threads_local is expected "
119121
"to be a non-negative integer or a string 'global_num_threads'")
@@ -382,9 +384,9 @@ cdef __mkl_status_to_string(int mkl_status):
382384

383385

384386
cdef int __python_obj_to_int(obj, func_name):
385-
if not isinstance(obj, six.integer_types):
387+
if not isinstance(obj, numbers.Integral):
386388
raise ValueError("The argument of " + func_name + " is expected to be a positive integer")
387-
cdef c_int = obj
389+
cdef int c_int = <int>obj
388390
return c_int
389391

390392

@@ -772,7 +774,7 @@ cdef object __enable_instructions(isa=None):
772774
cdef int c_mkl_isa = __mkl_str_to_int(isa, __variables['input'])
773775

774776
cdef int c_mkl_status = mkl.mkl_enable_instructions(c_mkl_isa)
775-
777+
776778
return __mkl_status_to_string(c_mkl_status)
777779

778780

mkl/_mklinitmodule.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define _GNU_SOURCE 1
1111
#include <dlfcn.h>
1212
#include <string.h>
13+
#include <pthread.h>
1314
#undef _GNU_SOURCE
1415
#endif
1516

@@ -29,30 +30,47 @@ static struct PyMethodDef methods[] = {
2930
#define MKL_SERVICE_INLINE inline
3031
#endif
3132

32-
static MKL_SERVICE_INLINE void _set_mkl_ilp64();
33-
static MKL_SERVICE_INLINE void _set_mkl_lp64();
34-
static MKL_SERVICE_INLINE void _set_mkl_interface();
33+
static MKL_SERVICE_INLINE void _set_mkl_ilp64(void);
34+
static MKL_SERVICE_INLINE void _set_mkl_lp64(void);
35+
static MKL_SERVICE_INLINE void _set_mkl_interface(void);
36+
37+
static const char* mtlayer;
38+
static const char* verbose;
3539

36-
static void _preload_threading_layer() {
3740
#if FORCE_PRELOADING
3841
#define VERBOSE(...) if(verbose) printf("mkl-service + Intel(R) MKL: " __VA_ARGS__)
39-
#define SET_MTLAYER(L) do { \
42+
43+
static void restore_mtlayer(void) {
44+
if (mtlayer) {
45+
VERBOSE("Re-setting Intel(R) MKL_THREADING_LAYER=%s for the forked process\n", mtlayer);
46+
setenv("MKL_THREADING_LAYER", mtlayer, 1);
47+
} else {
48+
VERBOSE("Unsetting Intel(R) MKL_THREADING_LAYER variable for the forked process \n");
49+
unsetenv("MKL_THREADING_LAYER");
50+
}
51+
}
52+
#endif
53+
54+
static void _preload_threading_layer(void) {
55+
#if FORCE_PRELOADING
56+
#define SET_MTLAYER(L) do { \
4057
VERBOSE("setting Intel(R) MKL to use " #L " OpenMP runtime\n"); \
41-
mkl_set_threading_layer(MKL_THREADING_##L); \
42-
setenv("MKL_THREADING_LAYER", #L, 0); \
58+
mkl_set_threading_layer(MKL_THREADING_##L); \
59+
setenv("MKL_THREADING_LAYER", #L, 0); \
60+
pthread_atfork(NULL, NULL, &restore_mtlayer); \
4361
} while(0)
44-
#define PRELOAD(lib) do { \
45-
VERBOSE("preloading %s runtime\n", lib); \
46-
dlopen(lib, RTLD_LAZY|RTLD_GLOBAL); \
62+
#define PRELOAD(lib) do { \
63+
VERBOSE("preloading %s runtime\n", lib); \
64+
dlopen(lib, RTLD_LAZY|RTLD_GLOBAL); \
4765
} while(0)
4866
/*
4967
* The following is the pseudo-code skeleton for reinterpreting unset MKL_THREADING_LAYER
50-
*
68+
*
5169
* if MKL_THREADING_LAYER is empty
5270
* if kmp_calloc (or a suitable symbol identified by Terry) is loaded,
5371
* we are using Intel(R) OpenMP, i.e. reinterpret as implicit value of INTEL
5472
* otherwise check if other Open MP is loaded by checking get_omp_num_threads symbol
55-
* if not loaded:
73+
* if not loaded:
5674
* assume INTEL, and force loading of IOMP5
5775
* if loaded:
5876
* if Gnu OMP, set MKL_THREADING_LAYER=GNU, and call set_mkl_threading_layer(MKL_THREADING_GNU)
@@ -65,8 +83,14 @@ static void _preload_threading_layer() {
6583
*/
6684

6785
const char *libiomp = "libiomp5.so";
68-
const char *verbose = getenv("MKL_VERBOSE");
69-
const char *mtlayer = getenv("MKL_THREADING_LAYER");
86+
verbose = getenv("MKL_VERBOSE");
87+
mtlayer = getenv("MKL_THREADING_LAYER");
88+
89+
/* Use of RTLD_DEFAULT handler is to indicate that symbol is being lookup-up among symbols
90+
* presently known to this process.
91+
*
92+
* See: https://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html
93+
*/
7094
void *omp = dlsym(RTLD_DEFAULT, "omp_get_num_threads");
7195
const char *omp_name = "(unidentified)";
7296
const char *iomp = NULL; /* non-zero indicates Intel(R) OpenMP is loaded */
@@ -108,21 +132,21 @@ static void _preload_threading_layer() {
108132
return;
109133
}
110134

111-
static MKL_SERVICE_INLINE void _set_mkl_ilp64() {
135+
static MKL_SERVICE_INLINE void _set_mkl_ilp64(void) {
112136
#ifdef USING_MKL_RT
113-
int i = mkl_set_interface_layer(MKL_INTERFACE_ILP64);
137+
mkl_set_interface_layer(MKL_INTERFACE_ILP64);
114138
#endif
115139
return;
116140
}
117141

118-
static MKL_SERVICE_INLINE void _set_mkl_lp64() {
142+
static MKL_SERVICE_INLINE void _set_mkl_lp64(void) {
119143
#ifdef USING_MKL_RT
120-
int i = mkl_set_interface_layer(MKL_INTERFACE_LP64);
144+
mkl_set_interface_layer(MKL_INTERFACE_LP64);
121145
#endif
122146
return;
123147
}
124148

125-
static MKL_SERVICE_INLINE void _set_mkl_interface() {
149+
static MKL_SERVICE_INLINE void _set_mkl_interface(void) {
126150
_set_mkl_lp64();
127151
_preload_threading_layer();
128152
}

setup.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,12 @@
6060

6161

6262
def get_extensions():
63-
try:
64-
from numpy.distutils.system_info import get_info
65-
mkl_info = get_info('mkl')
66-
except ImportError:
67-
mkl_root = os.environ['MKLROOT']
68-
mkl_info = {
69-
'include_dirs': [join(mkl_root, 'include')],
70-
'library_dirs': [join(mkl_root, 'lib'), join(mkl_root, 'lib', 'intel64')],
71-
'libraries': ['mkl_rt']
72-
}
63+
mkl_root = os.environ['MKLROOT']
64+
mkl_info = {
65+
'include_dirs': [join(mkl_root, 'include')],
66+
'library_dirs': [join(mkl_root, 'lib'), join(mkl_root, 'lib', 'intel64')],
67+
'libraries': ['mkl_rt']
68+
}
7369

7470
mkl_include_dirs = mkl_info.get('include_dirs', [])
7571
mkl_library_dirs = mkl_info.get('library_dirs', [])
@@ -99,7 +95,7 @@ def get_extensions():
9995
sources=['mkl/_mklinitmodule.c'],
10096
define_macros=defs,
10197
include_dirs=mkl_include_dirs,
102-
libraries=mkl_libraries,
98+
libraries=mkl_libraries + ["pthread"],
10399
library_dirs=mkl_library_dirs,
104100
extra_compile_args=[
105101
'-DNDEBUG'
@@ -155,7 +151,7 @@ def setup_package():
155151
test_suite='nose.collector',
156152
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
157153
setup_requires=['setuptools', 'cython'],
158-
install_requires=['six'],
154+
install_requires=[],
159155
packages=setuptools.find_packages(),
160156
ext_modules=get_extensions()
161157
)

0 commit comments

Comments
 (0)