Skip to content

Commit

Permalink
Check and set MKL THREADS on the fly (fix issue pyscf#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Mar 23, 2023
1 parent af54fd7 commit 60193b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
5 changes: 4 additions & 1 deletion conda/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ set -x -e
# find pyscf/lib/deps -name "*cint*" -exec rm {} \+
# rm pyscf-2.0-depsa-openblas.tar.gz

# C extensions must be installed with sequential BLAS library
# https://pyscf.org/install.html#using-optimized-blas
export CMAKE_CONFIGURE_ARGS="-DWITH_F12=OFF -DBLA_VENDOR=Intel10_64lp_seq"

# env PYTHON not defined in certain conda-build version
# $PYTHON -m pip install . -vv
export CMAKE_CONFIGURE_ARGS="-DWITH_F12=OFF"
pip install -v --prefix=$PREFIX .
6 changes: 0 additions & 6 deletions pyscf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@

import os
import sys
# Avoid too many threads being created in OMP loops.
# See issue https://github.com/pyscf/pyscf/issues/317
if 'OPENBLAS_NUM_THREADS' not in os.environ:
os.environ['OPENBLAS_NUM_THREADS'] = '1'
if 'MKL_NUM_THREADS' not in os.environ:
os.environ['MKL_NUM_THREADS'] = '1'

# Load modules which are developed as plugins of the namespace package
PYSCF_EXT_PATH = os.getenv('PYSCF_EXT_PATH')
Expand Down
35 changes: 35 additions & 0 deletions pyscf/lib/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@
except ImportError:
ThreadPoolExecutor = None

if sys.platform.startswith('linux'):
# Avoid too many threads being created in OMP loops.
# See issue https://github.com/pyscf/pyscf/issues/317
try:
from elftools.elf.elffile import ELFFile
except ImportError:
pass
else:
def _ldd(so_file):
libs = []
with open(so_file, 'rb') as f:
elf = ELFFile(f)
for seg in elf.iter_segments():
if seg.header.p_type != 'PT_DYNAMIC':
continue
for t in seg.iter_tags():
if t.entry.d_tag == 'DT_NEEDED':
libs.append(t.needed)
break
return libs

so_file = os.path.abspath(os.path.join(__file__, '..', 'libnp_helper.so'))
for p in _ldd(so_file):
if 'mkl' in p and 'thread' in p:
warnings.warn(f'PySCF C exteions are incompatible with {p}. '
'MKL_NUM_THREADS is set to 1')
os.environ['MKL_NUM_THREADS'] = '1'
break
elif 'openblasp' in p or 'openblaso' in p:
warnings.warn(f'PySCF C exteions are incompatible with {p}. '
'OPENBLAS_NUM_THREADS is set to 1')
os.environ['OPENBLAS_NUM_THREADS'] = '1'
break
del p, so_file, _ldd

from pyscf.lib import param
from pyscf import __config__

Expand Down

0 comments on commit 60193b4

Please sign in to comment.