Skip to content
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

[MRG] ENH: expose threading layer of mkl #48

Merged
merged 6 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .azure_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
VERSION_PYTHON: '*'
CC_OUTER_LOOP: 'clang-8'
CC_INNER_LOOP: 'gcc'
MKL_THREADING_LAYER: 'INTEL'
# Same but with numpy / scipy installed with pip from PyPI and switched
# compilers.
pylatest_pip_openblas_gcc_clang:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_threadpoolctl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import ctypes
import pytest
Expand Down Expand Up @@ -301,3 +302,19 @@ def test_get_original_num_threads(limit):
with pytest.warns(None, match='Multiple value possible'):
expected = min([module['num_threads'] for module in original_infos])
assert original_num_threads['blas'] == expected


def test_mkl_threading_layer():
# Check that threadpool_info correctly recovers the threading layer used
# by mkl
mkl_info = [module for module in threadpool_info()
if module['internal_api'] == 'mkl']

if not mkl_info:
pytest.skip("requires MKL")

expected_layer = os.getenv("MKL_THREADING_LAYER")
actual_layer = mkl_info[0]['threading_layer']

if expected_layer:
assert actual_layer == expected_layer.lower()
14 changes: 14 additions & 0 deletions threadpoolctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ def threadpool_info():
# we map it to 1 for consistency with other libraries.
if module['num_threads'] == -1 and module['internal_api'] == 'blis':
module['num_threads'] = 1
if module['internal_api'] == 'mkl':
layer = _get_mkl_threading_layer(module['dynlib'])
module['threading_layer'] = layer
# Remove the wrapper for the module and its function
del module['set_num_threads'], module['get_num_threads']
del module['dynlib']
Expand All @@ -251,6 +254,17 @@ def threadpool_info():
return infos


def _get_mkl_threading_layer(mkl_dynlib):
"""Return the threading layer of MKL"""
# The function mkl_set_threading_layer returns the current threading layer
# Calling it with an invalid threading layer allows us to safely get
# the threading layer
set_threading_layer = getattr(mkl_dynlib, "MKL_Set_Threading_Layer")
layer_map = {0: "intel", 1: "sequential", 2: "pgi",
3: "gnu", 4: "tbb", -1: "not specified"}
return layer_map[set_threading_layer(-1)]


def _get_version(dynlib, internal_api):
if internal_api == "mkl":
return _get_mkl_version(dynlib)
Expand Down