Skip to content
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
20 changes: 11 additions & 9 deletions mkl/_mkl_service.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# distutils: language = c
# cython: language_level=3

import six
import numbers
import warnings
cimport _mkl_service as mkl
cimport mkl._mkl_service as mkl


ctypedef struct MemStatData:
Expand Down Expand Up @@ -80,9 +82,9 @@ cdef int __domain_to_mkl_domain(domain):
'pardiso': mkl.MKL_DOMAIN_PARDISO,
'all': mkl.MKL_DOMAIN_ALL }

if isinstance(domain, six.integer_types):
c_mkl_domain = domain
elif isinstance(domain, six.string_types):
if isinstance(domain, numbers.Integral):
c_mkl_domain = <int>domain
elif isinstance(domain, str):
if domain not in _mapping:
c_mkl_domain = __warn_and_fallback_on_default_domain(domain)
else:
Expand Down Expand Up @@ -113,7 +115,7 @@ cpdef set_num_threads_local(num_threads):
https://software.intel.com/en-us/mkl-developer-reference-c-mkl-set-num-threads-local
"""
cdef c_num_threads = 0
if isinstance(num_threads, six.string_types):
if isinstance(num_threads, str):
if num_threads is not 'global_num_threads':
raise ValueError("The argument of set_num_threads_local is expected "
"to be a non-negative integer or a string 'global_num_threads'")
Expand Down Expand Up @@ -382,9 +384,9 @@ cdef __mkl_status_to_string(int mkl_status):


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


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

cdef int c_mkl_status = mkl.mkl_enable_instructions(c_mkl_isa)

return __mkl_status_to_string(c_mkl_status)


Expand Down
62 changes: 43 additions & 19 deletions mkl/_mklinitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define _GNU_SOURCE 1
#include <dlfcn.h>
#include <string.h>
#include <pthread.h>
#undef _GNU_SOURCE
#endif

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

static MKL_SERVICE_INLINE void _set_mkl_ilp64();
static MKL_SERVICE_INLINE void _set_mkl_lp64();
static MKL_SERVICE_INLINE void _set_mkl_interface();
static MKL_SERVICE_INLINE void _set_mkl_ilp64(void);
static MKL_SERVICE_INLINE void _set_mkl_lp64(void);
static MKL_SERVICE_INLINE void _set_mkl_interface(void);

static const char* mtlayer;
static const char* verbose;

static void _preload_threading_layer() {
#if FORCE_PRELOADING
#define VERBOSE(...) if(verbose) printf("mkl-service + Intel(R) MKL: " __VA_ARGS__)
#define SET_MTLAYER(L) do { \

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); \
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); \
#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:
* 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)
Expand All @@ -65,8 +83,14 @@ static void _preload_threading_layer() {
*/

const char *libiomp = "libiomp5.so";
const char *verbose = getenv("MKL_VERBOSE");
const char *mtlayer = getenv("MKL_THREADING_LAYER");
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 */
Expand Down Expand Up @@ -108,21 +132,21 @@ static void _preload_threading_layer() {
return;
}

static MKL_SERVICE_INLINE void _set_mkl_ilp64() {
static MKL_SERVICE_INLINE void _set_mkl_ilp64(void) {
#ifdef USING_MKL_RT
int i = mkl_set_interface_layer(MKL_INTERFACE_ILP64);
mkl_set_interface_layer(MKL_INTERFACE_ILP64);
#endif
return;
}

static MKL_SERVICE_INLINE void _set_mkl_lp64() {
static MKL_SERVICE_INLINE void _set_mkl_lp64(void) {
#ifdef USING_MKL_RT
int i = mkl_set_interface_layer(MKL_INTERFACE_LP64);
mkl_set_interface_layer(MKL_INTERFACE_LP64);
#endif
return;
}

static MKL_SERVICE_INLINE void _set_mkl_interface() {
static MKL_SERVICE_INLINE void _set_mkl_interface(void) {
_set_mkl_lp64();
_preload_threading_layer();
}
Expand Down
20 changes: 8 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,12 @@


def get_extensions():
try:
from numpy.distutils.system_info import get_info
mkl_info = get_info('mkl')
except ImportError:
mkl_root = os.environ['MKLROOT']
mkl_info = {
'include_dirs': [join(mkl_root, 'include')],
'library_dirs': [join(mkl_root, 'lib'), join(mkl_root, 'lib', 'intel64')],
'libraries': ['mkl_rt']
}
mkl_root = os.environ['MKLROOT']
mkl_info = {
'include_dirs': [join(mkl_root, 'include')],
'library_dirs': [join(mkl_root, 'lib'), join(mkl_root, 'lib', 'intel64')],
'libraries': ['mkl_rt']
}

mkl_include_dirs = mkl_info.get('include_dirs', [])
mkl_library_dirs = mkl_info.get('library_dirs', [])
Expand Down Expand Up @@ -99,7 +95,7 @@ def get_extensions():
sources=['mkl/_mklinitmodule.c'],
define_macros=defs,
include_dirs=mkl_include_dirs,
libraries=mkl_libraries,
libraries=mkl_libraries + ["pthread"],
library_dirs=mkl_library_dirs,
extra_compile_args=[
'-DNDEBUG'
Expand Down Expand Up @@ -155,7 +151,7 @@ def setup_package():
test_suite='nose.collector',
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
setup_requires=['setuptools', 'cython'],
install_requires=['six'],
install_requires=[],
packages=setuptools.find_packages(),
ext_modules=get_extensions()
)
Expand Down