Skip to content

Commit

Permalink
Use RTLD_GLOBAL for libgomp (gwastro#4353)
Browse files Browse the repository at this point in the history
* Use RTLD_GLOBAL for libgomp

In conda-forge/pycbc-feedstock#74 it was suggested to use RTLD_GLOBAL for libgomp. Let's see if this works fine with the test suite (which should answer @josh-willis 's concerns).

* Move import of ctypes/gomp into __enter__

* Try this
  • Loading branch information
spxiwh authored and titodalcanton committed Jan 19, 2024
1 parent f69dd4b commit fdece5f
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pycbc/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
import logging
from .libutils import get_ctypes_library

try:
_libgomp = get_ctypes_library("gomp", ['gomp'])
except:
# Should we fail or give a warning if we cannot import
# libgomp? Seems to work even for MKL scheme, but
# not entirely sure why...
_libgomp = None

class _SchemeManager(object):
_single = None
Expand Down Expand Up @@ -129,17 +122,27 @@ def __init__(self, num_threads=1):
else:
import multiprocessing
self.num_threads = multiprocessing.cpu_count()
self._libgomp = None

def __enter__(self):
Scheme.__enter__(self)
try:
self._libgomp = get_ctypes_library("gomp", ['gomp'],
mode=ctypes.RTLD_GLOBAL)
except:
# Should we fail or give a warning if we cannot import
# libgomp? Seems to work even for MKL scheme, but
# not entirely sure why...
pass

os.environ["OMP_NUM_THREADS"] = str(self.num_threads)
if _libgomp is not None:
_libgomp.omp_set_num_threads( int(self.num_threads) )
if self._libgomp is not None:
self._libgomp.omp_set_num_threads( int(self.num_threads) )

def __exit__(self, type, value, traceback):
os.environ["OMP_NUM_THREADS"] = "1"
if _libgomp is not None:
_libgomp.omp_set_num_threads(1)
if self._libgomp is not None:
self._libgomp.omp_set_num_threads(1)
Scheme.__exit__(self, type, value, traceback)

class MKLScheme(CPUScheme):
Expand Down

0 comments on commit fdece5f

Please sign in to comment.