Skip to content

Commit

Permalink
v0.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
asistradition committed Apr 28, 2024
1 parent 4a4b038 commit 2f0354d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ jobs:
python-version: ["3.9", "3.10"]
scipy-version: ['1.8.1', '1.11.3']
os: [macos-latest, ubuntu-latest]
interface: ['LP64', 'ILP64']

steps:
- uses: actions/checkout@v3
- name: Install conda python & MKL
run: |
sudo chown -R $(whoami) $CONDA
echo $CONDA/bin >> $GITHUB_PATH
$CONDA/bin/conda install mkl python=${{ matrix.python-version }}
$CONDA/bin/conda install mkl python=${{ matrix.python-version }}
export MKL_INTERFACE_LAYER=${{ matrix.interface }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version 0.9.2

* Explicit check for interface env MKL_INTERFACE_LAYER and interface selection in python.

### Version 0.9.1

* Support for `out` parameter with sparse-sparse multiplication when `dense=True`
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages

DISTNAME = 'sparse_dot_mkl'
VERSION = '0.9.1'
VERSION = '0.9.2'
DESCRIPTION = "Intel MKL wrapper for sparse matrix multiplication"
MAINTAINER = 'Chris Jackson'
MAINTAINER_EMAIL = '[email protected]'
Expand Down
3 changes: 3 additions & 0 deletions sparse_dot_mkl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__version__ = '0.9.2'


from sparse_dot_mkl.sparse_dot import (
dot_product_mkl,
dot_product_transpose_mkl,
Expand Down
46 changes: 39 additions & 7 deletions sparse_dot_mkl/_mkl_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
from scipy import sparse as _spsparse
import numpy as _np
import ctypes as _ctypes
import warnings as _warnings
from ._constants import *
from ._structs import (
MKL_Complex8,
MKL_Complex16,
matrix_descr,
sparse_matrix_t
)
from ._cfunctions import MKL
from ._cfunctions import (
MKL,
mkl_set_interface_layer
)
from ._common import (
_create_mkl_sparse,
_export_mkl,
Expand Down Expand Up @@ -63,11 +67,15 @@ def _validate_dtype():

try:
csr_ref = _convert_to_csr(csc_ref)
final_array = _export_mkl(
csr_ref,
precision_flag,
output_type='csr_matrix'
)

with _warnings.catch_warnings():
_warnings.filterwarnings("ignore", category=RuntimeWarning)
final_array = _export_mkl(
csr_ref,
precision_flag,
output_type='csr_matrix'
)

if not _np.allclose(test_comparison, final_array.A):
raise ValueError("Match failed after matrix conversion")
_destroy_mkl_handle(csr_ref)
Expand Down Expand Up @@ -95,8 +103,32 @@ def _empirical_set_dtype():
raise ImportError("Unable to set MKL numeric type")


if MKL.MKL_INT is None:
_mkl_interface_env = os.getenv('MKL_INTERFACE_LAYER')


# Check to make sure that the MKL_Set_Interface_Layer call was correct
# And fail back to 32bit if it wasn't
if MKL.MKL_INT is None and _mkl_interface_env == 'ILP64':
try:
_validate_dtype()
except (ValueError, RuntimeError):
_warnings.warn(
"MKL_INTERFACE_LAYER=ILP64 failed to set MKL interface; "
"64-bit integer support unavailable"
)
MKL._set_int_type(_ctypes.c_int, _np.int32)
_validate_dtype()
elif MKL.MKL_INT is None and _mkl_interface_env == 'LP64':
_validate_dtype()
elif MKL.MKL_INT is None and _mkl_interface_env is not None:
_warnings.warn(
f"MKL_INTERFACE_LAYER value {_mkl_interface_env} invalid; "
"set 'ILP64' or 'LP64'"
)
_empirical_set_dtype()
elif MKL.MKL_INT is None:
_empirical_set_dtype()


if _sklearn_env is not None:
os.environ["KMP_INIT_AT_FORK"] = _sklearn_env
29 changes: 29 additions & 0 deletions sparse_dot_mkl/_mkl_interface/_cfunctions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import ctypes as _ctypes

from sparse_dot_mkl._mkl_interface._structs import (
Expand All @@ -14,6 +15,12 @@
import numpy as _np
from numpy.ctypeslib import ndpointer

# MKL_SET_INTERFACE_LAYER flags
MKL_INTERFACE_LP64 = 0
MKL_INTERFACE_ILP64 = 1
MKL_INTERFACE_GNU = 2


_libmkl = mkl_library()


Expand Down Expand Up @@ -129,6 +136,9 @@ class MKL:
_mkl_sparse_d_qr_solve = _libmkl.mkl_sparse_d_qr_solve
_mkl_sparse_s_qr_solve = _libmkl.mkl_sparse_s_qr_solve

# Set interface function
_mkl_set_interface_layer = _libmkl.MKL_Set_Interface_Layer

@classmethod
def _set_int_type(cls, c_type, _np_type):
cls.MKL_INT = c_type
Expand Down Expand Up @@ -561,3 +571,22 @@ def _qr_solve(prec_type):
ndpointer(dtype=prec_type, ndim=2),
MKL.MKL_INT,
]


MKL._mkl_set_interface_layer.argtypes = [_ctypes.c_int]
MKL._mkl_set_interface_layer.restypes = [_ctypes.c_int]


def mkl_set_interface_layer(layer_code):
return MKL._mkl_set_interface_layer(layer_code)


_mkl_interface_env = os.getenv('MKL_INTERFACE_LAYER')


if MKL.MKL_INT is None and _mkl_interface_env == 'ILP64':
mkl_set_interface_layer(MKL_INTERFACE_ILP64)
MKL._set_int_type(_ctypes.c_longlong, _np.int64)
elif MKL.MKL_INT is None and _mkl_interface_env == 'LP64':
mkl_set_interface_layer(MKL_INTERFACE_LP64)
MKL._set_int_type(_ctypes.c_int, _np.int32)
5 changes: 5 additions & 0 deletions sparse_dot_mkl/_mkl_interface/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@

# ILP64 message
ILP64_MSG = " Try changing MKL to int64 with the environment variable MKL_INTERFACE_LAYER=ILP64"

# MKL_SET_INTERFACE_LAYER flags
MKL_INTERFACE_LP64 = 0
MKL_INTERFACE_ILP64 = 1
MKL_INTERFACE_GNU = 2
2 changes: 1 addition & 1 deletion sparse_dot_mkl/_mkl_interface/_load_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def mkl_library():
"Unable to load the MKL libraries through "
"libmkl_rt. Try setting $LD_LIBRARY_PATH to the "
"LD library path or $MKL_RT to the libmkl_rt.so library "
" file directly. " + str(err)
"file directly. " + str(err)
)

return _libmkl

0 comments on commit 2f0354d

Please sign in to comment.