Skip to content

Commit 60193b4

Browse files
committed
Check and set MKL THREADS on the fly (fix issue pyscf#1102)
1 parent af54fd7 commit 60193b4

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

conda/build.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ set -x -e
1111
# find pyscf/lib/deps -name "*cint*" -exec rm {} \+
1212
# rm pyscf-2.0-depsa-openblas.tar.gz
1313

14+
# C extensions must be installed with sequential BLAS library
15+
# https://pyscf.org/install.html#using-optimized-blas
16+
export CMAKE_CONFIGURE_ARGS="-DWITH_F12=OFF -DBLA_VENDOR=Intel10_64lp_seq"
17+
1418
# env PYTHON not defined in certain conda-build version
1519
# $PYTHON -m pip install . -vv
16-
export CMAKE_CONFIGURE_ARGS="-DWITH_F12=OFF"
1720
pip install -v --prefix=$PREFIX .

pyscf/__init__.py

-6
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@
3939

4040
import os
4141
import sys
42-
# Avoid too many threads being created in OMP loops.
43-
# See issue https://github.com/pyscf/pyscf/issues/317
44-
if 'OPENBLAS_NUM_THREADS' not in os.environ:
45-
os.environ['OPENBLAS_NUM_THREADS'] = '1'
46-
if 'MKL_NUM_THREADS' not in os.environ:
47-
os.environ['MKL_NUM_THREADS'] = '1'
4842

4943
# Load modules which are developed as plugins of the namespace package
5044
PYSCF_EXT_PATH = os.getenv('PYSCF_EXT_PATH')

pyscf/lib/misc.py

+35
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@
3737
except ImportError:
3838
ThreadPoolExecutor = None
3939

40+
if sys.platform.startswith('linux'):
41+
# Avoid too many threads being created in OMP loops.
42+
# See issue https://github.com/pyscf/pyscf/issues/317
43+
try:
44+
from elftools.elf.elffile import ELFFile
45+
except ImportError:
46+
pass
47+
else:
48+
def _ldd(so_file):
49+
libs = []
50+
with open(so_file, 'rb') as f:
51+
elf = ELFFile(f)
52+
for seg in elf.iter_segments():
53+
if seg.header.p_type != 'PT_DYNAMIC':
54+
continue
55+
for t in seg.iter_tags():
56+
if t.entry.d_tag == 'DT_NEEDED':
57+
libs.append(t.needed)
58+
break
59+
return libs
60+
61+
so_file = os.path.abspath(os.path.join(__file__, '..', 'libnp_helper.so'))
62+
for p in _ldd(so_file):
63+
if 'mkl' in p and 'thread' in p:
64+
warnings.warn(f'PySCF C exteions are incompatible with {p}. '
65+
'MKL_NUM_THREADS is set to 1')
66+
os.environ['MKL_NUM_THREADS'] = '1'
67+
break
68+
elif 'openblasp' in p or 'openblaso' in p:
69+
warnings.warn(f'PySCF C exteions are incompatible with {p}. '
70+
'OPENBLAS_NUM_THREADS is set to 1')
71+
os.environ['OPENBLAS_NUM_THREADS'] = '1'
72+
break
73+
del p, so_file, _ldd
74+
4075
from pyscf.lib import param
4176
from pyscf import __config__
4277

0 commit comments

Comments
 (0)