-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy path_mklinitmodule.c
More file actions
220 lines (199 loc) · 6.69 KB
/
_mklinitmodule.c
File metadata and controls
220 lines (199 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- c -*- */
/*
* This is a dummy module whose purpose is to get distutils to generate the
* configuration files before the libraries are made.
*/
#if (defined(USING_MKL_RT) && defined(__linux__))
#define FORCE_PRELOADING 1
#define _GNU_SOURCE 1
#include <dlfcn.h>
#include <pthread.h>
#include <string.h>
#undef _GNU_SOURCE
#endif
#include <Python.h>
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif
#include "mkl.h"
static struct PyMethodDef methods[] = {{NULL, NULL, 0, NULL}};
#if defined(_MSC_VER) && (_MSC_VER <= 1500)
#define MKL_SERVICE_INLINE
#else
#define MKL_SERVICE_INLINE inline
#endif
#ifdef MKL_ILP64
static MKL_SERVICE_INLINE void _set_mkl_ilp64(void);
#else
static MKL_SERVICE_INLINE void _set_mkl_lp64(void);
#endif
static MKL_SERVICE_INLINE void _set_mkl_interface(void);
static const char *mtlayer;
static const char *verbose;
#if FORCE_PRELOADING
#define VERBOSE(...) \
if (verbose) \
printf("mkl-service + Intel(R) MKL: " __VA_ARGS__)
static void restore_mtlayer(void)
{
if (mtlayer) {
VERBOSE("Re-setting Intel(R) MKL_THREADING_LAYER=%s for the forked "
"process\n",
mtlayer);
setenv("MKL_THREADING_LAYER", mtlayer, 1);
}
else {
VERBOSE(
"Unsetting Intel(R) MKL_THREADING_LAYER variable for the forked "
"process \n");
unsetenv("MKL_THREADING_LAYER");
}
}
#endif
static void _preload_threading_layer(void)
{
#if FORCE_PRELOADING
#define SET_MTLAYER(L) \
do { \
VERBOSE("setting Intel(R) MKL to use " #L " OpenMP runtime\n"); \
mkl_set_threading_layer(MKL_THREADING_##L); \
setenv("MKL_THREADING_LAYER", #L, 0); \
pthread_atfork(NULL, NULL, &restore_mtlayer); \
} while (0)
#define PRELOAD(lib) \
do { \
VERBOSE("preloading %s runtime\n", lib); \
dlopen(lib, RTLD_LAZY | RTLD_GLOBAL); \
} while (0)
/*
* The following is the pseudo-code skeleton for reinterpreting unset
* MKL_THREADING_LAYER
*
* if MKL_THREADING_LAYER is empty
* if kmp_calloc (or a suitable symbol identified by Terry) is
* loaded, we are using Intel(R) OpenMP, i.e. reinterpret as implicit value
* of INTEL otherwise check if other Open MP is loaded by checking
* get_omp_num_threads symbol if not loaded: assume INTEL, and force loading
* of IOMP5 if loaded: if Gnu OMP, set MKL_THREADING_LAYER=GNU, and call
* set_mkl_threading_layer(MKL_THREADING_GNU) if other vendors? if
* MKL_THREADING_LAYER is INTEL force loading of iomp, to preempt
* possibility of other modules loading other OMP library before MKL is
* actually used
*
* should we treat other possible values of MKL_THREADING_LAYER
* specially?
*
*/
const char *libiomp = "libiomp5.so";
verbose = getenv("MKL_VERBOSE");
mtlayer = getenv("MKL_THREADING_LAYER");
/* Use of RTLD_DEFAULT handler is to indicate that symbol is being lookup-up
* among symbols presently known to this process.
*
* See:
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html
*/
void *omp = dlsym(RTLD_DEFAULT, "omp_get_num_threads");
const char *omp_name = "(unidentified)";
const char *iomp = NULL; /* non-zero indicates Intel(R) OpenMP is loaded */
Dl_info omp_info;
if (verbose && (verbose[0] == 0 || atoi(verbose) == 0))
verbose = NULL;
VERBOSE("THREADING LAYER: %s\n", mtlayer);
if (omp) {
if (dladdr(omp, &omp_info)) {
omp_name = basename(
omp_info.dli_fname); /* GNU version doesn't modify argument */
iomp = strstr(omp_name, libiomp);
}
VERBOSE("%s OpenMP runtime %s is already loaded\n",
iomp ? "Intel(R)" : "Other vendor", omp_name);
}
if (!mtlayer || mtlayer[0] == 0) { /* unset or empty */
if (omp) { /* if OpenMP runtime is loaded */
if (iomp) /* if Intel runtime is loaded */
SET_MTLAYER(INTEL);
else /* otherwise, assume it is GNU OpenMP */
SET_MTLAYER(GNU);
}
else { /* nothing is loaded */
SET_MTLAYER(INTEL);
PRELOAD(libiomp);
}
}
else if (strcasecmp(mtlayer, "intel") ==
0) { /* Intel runtime is requested */
if (omp && !iomp) {
fprintf(stderr,
"Error: mkl-service + Intel(R) MKL: "
"MKL_THREADING_LAYER=INTEL is "
"incompatible with %s library."
"\n\tTry to import numpy first or set the threading layer "
"accordingly. "
"Set MKL_SERVICE_FORCE_INTEL to force it.\n",
omp_name);
if (!getenv("MKL_SERVICE_FORCE_INTEL"))
exit(1);
}
else
PRELOAD(libiomp);
}
#endif
return;
}
#ifdef MKL_ILP64
static MKL_SERVICE_INLINE void _set_mkl_ilp64(void)
{
#ifdef USING_MKL_RT
mkl_set_interface_layer(MKL_INTERFACE_ILP64);
#endif
return;
}
#else
static MKL_SERVICE_INLINE void _set_mkl_lp64(void)
{
#ifdef USING_MKL_RT
mkl_set_interface_layer(MKL_INTERFACE_LP64);
#endif
return;
}
#endif
static MKL_SERVICE_INLINE void _set_mkl_interface(void)
{
#ifdef MKL_ILP64
_set_mkl_ilp64();
#else
_set_mkl_lp64();
#endif
_preload_threading_layer();
}
#if defined(IS_PY3K)
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
"mklinit",
NULL,
-1,
methods,
NULL,
NULL,
NULL,
NULL};
#endif
/* Initialization function for the module */
#if defined(IS_PY3K)
PyMODINIT_FUNC PyInit__mklinit(void)
{
PyObject *m;
_set_mkl_interface();
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
}
return m;
}
#else
PyMODINIT_FUNC init_mklinit(void)
{
_set_mkl_interface();
Py_InitModule("_mklinit", methods);
}
#endif