Skip to content

macOS wheels: share the bundled libomp with other OpenMP users - #171

Open
eriknw wants to merge 7 commits into
GraphBLAS:mainfrom
eriknw:share-libomp
Open

eriknw wants to merge 7 commits into
GraphBLAS:mainfrom
eriknw:share-libomp

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

The macOS wheels bundle LLVM's OpenMP runtime the way delocate does by default: a copy in suitesparse_graphblas/.dylibs/libomp.dylib with an install name of its own (/DLC/suitesparse_graphblas/.dylibs/libomp.dylib), linked from libgraphblas by path. Any process that already has an LLVM OpenMP runtime then starts a second one, and libomp aborts:

OMP: Error #15: Initializing libomp.dylib, but found libomp.dylib already initialized.

Two common ways to get there (both reproduced on macOS arm64 with the 10.5.2.0 wheel):

  • conda-forge NumPy plus this wheel, with no other package involved: conda-forge's OpenBLAS uses llvm-openmp, import numpy starts that runtime, and initialize() (GrB_init) starts ours. It aborts in either order.
  • numba's omp threading layer in a venv made from a conda-forge Python (python-graphblas's compactify, scan and others are parallel numba functions).

Why not load the copy privately

That can't avoid it. libomp registers itself per process when it starts (on macOS, in /tmp/__KMP_REGISTERED_LIB_<pid>), whatever its file name, install name or dlopen mode. Renamed copies and copies loaded with RTLD_LOCAL abort the same way. KMP_DUPLICATE_LIB_OK=TRUE turns the abort into a deadlock, because dyld binds the weak symbols that libomp exports to whichever copy loaded first, so the two runtimes call into each other. For the same reason a second copy that is loaded but never started can crash the process.

What this does

conda-forge's libomp has the install name @rpath/libomp.dylib, which is also what numba's omp layer and conda-forge's OpenBLAS link. dyld resolves a request for @rpath/libomp.dylib to an already-loaded image with that install name before it searches any LC_RPATH (dyld 4, macOS 12 and later; dyld 3 has an equivalent rule). A new script, continuous_integration/share_libomp.py, runs after delocate in the macOS repair command and:

  • gives the bundled copy the install name @rpath/libomp.dylib;
  • links libgraphblas to @rpath/libomp.dylib, with an LC_RPATH of @loader_path that reaches the copy;
  • re-signs what it changed, then checks the result.

GraphBLAS then uses a libomp that is already loaded, or else loads its own copy, which libraries loaded later share. So there is one runtime whatever the order. PyTorch's macOS wheels did the same from 2.5 (pytorch/pytorch#129473). The file stays at suitesparse_graphblas/.dylibs/libomp.dylib, where python-graphblas looks for it.

Caveats

  • A libomp loaded before this package must be LLVM 19 or newer: libgraphblas uses __kmpc_dispatch_deinit. With an older one, dyld can't load libgraphblas, and import suitesparse_graphblas now raises an ImportError (chained to dyld's) that names the loaded libomp and says what to do: update it (in conda, llvm-openmp>=19) or use conda-forge's package. With the old layout, the same setup aborted or segfaulted.
  • One case that worked by accident before now fails with that ImportError: an LLVM 11 libomp under @rpath/libomp.dylib loaded first (old conda envs). Its runtime registered differently, so the two copies never noticed each other.
  • Libraries that load a libomp under another name still start a second runtime:
    • Homebrew's libomp, which the xgboost and lightgbm wheels use, shares only if this package loads first;
    • the private copies in the scikit-learn and recent PyTorch wheels never share.
  • Because later libraries share our copy, it should be the newest llvm-openmp at each release (a comment by the pin says so). The script fails if libgraphblas starts importing an OpenMP function beyond today's 23, so the "LLVM 19 or newer" floor can't go stale silently.
  • The README's "OpenMP on macOS" section says all of this briefly.

Testing

  • test_bundled_libomp_is_shared (macOS wheels only; skipped where there is no bundled libomp) runs both directions in a subprocess:
    • other: a copy of the bundled libomp is loaded first; GraphBLAS must use it.
    • graphblas: GraphBLAS loads first; a later dlopen("@rpath/libomp.dylib"), the lookup numba's omppool makes, must get the same image.
      Both cases also run a parallel GraphBLAS operation and require exactly one libomp in the process. Both fail on the 10.5.2.0 wheels (arm64 cp313 and cp314t, x86_64 cp311) and pass once those wheels are repaired, with PyPI's NumPy and with conda-forge's (whose OpenBLAS then provides the shared libomp).
  • test_old_libomp_error checks the ImportError rewrite against dyld's message for conda-forge's llvm-openmp 18.1.8. With 18.1.8 or 11.1.0 really loaded first, the import raises it; with 19.1.7 it imports fine.
  • Repaired 10.5.2.0 wheels, installed with pip, in each of these orders. Every run had exactly one runtime and no abort:
    • in a clean Python, psg first or numba first;
    • in a venv from a conda-forge Python, with numba in either order;
    • with conda-forge NumPy, with that env's libomp, or with our copy loaded first;
    • with python-graphblas's compactify.
  • The script is idempotent, refuses a wheel that isn't laid out as it expects, and leaves the wheel untouched when it fails.
  • CI: the first push's macOS jobs ran the repair on every arm64 wheel (cp311 to cp315, cp314t, cp315t, pp311), and its test passed on each.
  • Still to check: an older macOS (12 to 14) at runtime. Apple's dyld source has the same lookup from dyld-940 (macOS 12) to dyld-1378.

delocate copies libomp into suitesparse_graphblas/.dylibs/, gives the
copy the install name /DLC/suitesparse_graphblas/.dylibs/libomp.dylib,
and links libgraphblas to it by path.  A process that already has an
LLVM OpenMP runtime, such as conda-forge's NumPy (whose OpenBLAS uses
llvm-openmp) or numba's omp threading layer, then loads a second one,
and libomp aborts with "OMP: Error GraphBLAS#15" when GraphBLAS starts it.  With
KMP_DUPLICATE_LIB_OK=TRUE the two runtimes deadlock instead.

Loading the bundled copy privately can't avoid this.  libomp registers
itself per process, whatever its file name, install name or dlopen
mode, and dyld binds the weak symbols of both copies to whichever was
loaded first, so a second copy can crash the process even if it is
never started.

conda-forge's libomp has the install name @rpath/libomp.dylib, which is
also what numba and conda-forge's OpenBLAS link, and dyld resolves
@rpath/libomp.dylib to an already-loaded image with that install name
before it searches any LC_RPATH.  So the new script
continuous_integration/share_libomp.py, run after delocate in the macOS
repair command, gives the bundled copy that install name and links
libgraphblas to @rpath/libomp.dylib, with an LC_RPATH of @loader_path.
GraphBLAS then uses a libomp that is already loaded, or loads its own,
which libraries loaded later share: one runtime, whatever the order.
The file stays at suitesparse_graphblas/.dylibs/libomp.dylib, where
python-graphblas looks for it.  The script fails if the wheel is not
laid out as it expects, and changes nothing on a second run.

A libomp loaded before GraphBLAS must be LLVM 19 or newer, which has
__kmpc_dispatch_deinit; with an older one, importing
suitesparse_graphblas now fails with "Symbol not found:
___kmpc_dispatch_deinit" instead of aborting or crashing later.  Wheels
that load their own libomp by path still start a second runtime, as
before.

test_bundled_libomp_is_shared loads a copy of the bundled libomp before
GraphBLAS, runs a parallel GraphBLAS operation, and checks that the copy
is the only libomp in the process.  It fails with the 10.5.2.0 wheel
(two libomp images) and passes once that wheel is repaired.  The README
says what to expect.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.62069% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.66%. Comparing base (1d8c5df) to head (e748427).

Files with missing lines Patch % Lines
suitesparse_graphblas/tests/test_package.py 58.62% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #171      +/-   ##
==========================================
- Coverage   98.91%   98.66%   -0.25%     
==========================================
  Files          42       42              
  Lines        4706     4735      +29     
  Branches      395      396       +1     
==========================================
+ Hits         4655     4672      +17     
- Misses         27       39      +12     
  Partials       24       24              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The macOS wheels now use an LLVM OpenMP runtime that another package
already loaded as @rpath/libomp.dylib.  If that runtime is older than
LLVM 19, it lacks __kmpc_dispatch_deinit, which libgraphblas calls, and
dyld refuses to load libgraphblas ("Symbol not found:
___kmpc_dispatch_deinit").  The same setups used to abort or crash.

When the import fails with dyld's "Symbol not found" for an OpenMP
function, raise an ImportError, chained to dyld's, that says what
happened and what to do: update that libomp (in conda,
llvm-openmp>=19), or in a conda environment use conda-forge's
python-suitesparse-graphblas.  Importing suitesparse_graphblas first
helps only if the package that loaded the old libomp is not NumPy:
suitesparse_graphblas imports NumPy (through utils) before _graphblas,
and conda-forge's NumPy loads conda's libomp.

dyld's error comes out of `from . import utils`, which imports
_graphblas, so the try covers both imports.  Their order stays, so
NumPy's libomp, if it has one, is still loaded first.

test_old_libomp_error checks the message against what dyld printed with
conda-forge's llvm-openmp 18.1.8 loaded first.  A test with a real old
libomp would have to download one.
- Run /usr/bin/otool, install_name_tool, codesign and nm, not whichever
  comes first on PATH: a conda environment can put cctools-port's first.
- Require each file that links libomp to have exactly one LC_RPATH, the
  one that reaches the bundled copy.  When no libomp is loaded yet, dyld
  tries a file's LC_RPATHs in order, so another one could load some
  other libomp.  delocate removes absolute ones today; this keeps it so.
- Fail if libgraphblas imports an OpenMP function other than the 23
  that 10.5.2 and 10.5.3 import.  A libomp that is already loaded must
  export all of them, and the newest, __kmpc_dispatch_deinit, is what
  makes the minimum LLVM 19.  A new one may raise that minimum, so it
  needs a look, and then the README and the ImportError message may
  need an update.
- Write the repaired wheel to a temporary file and replace the original
  only once it is complete, so that a failure while zipping can't leave
  a truncated wheel behind.
test_bundled_libomp_is_shared covered a libomp loaded before
GraphBLAS.  Run it also with nothing loaded first: after GraphBLAS has
loaded its own copy and run in parallel, a dlopen of
@rpath/libomp.dylib, as a library that links it would do, must get
GraphBLAS's copy (dladdr on omp_get_max_threads says which file), and
the process must still have one libomp.  dyld finds it as
"already-loaded-by-rpath", the same lookup it does for a library's
@rpath dependency.  With 10.5.2.0's layout the dlopen finds no libomp,
or the interpreter's own (conda), and the test fails.

Use ctypes.CDLL(None) to reach dyld's functions instead of naming
libSystem's path.
Libraries loaded after GraphBLAS that link @rpath/libomp.dylib now get
the libomp the wheel bundles.  One that needs a function this libomp
lacks fails to load, so take the newest llvm-openmp at each release.
Say what to do when an older libomp loaded first makes the import
fail: update it, or import suitesparse_graphblas first, which cannot
help when it came with NumPy because suitesparse_graphblas imports
NumPy first.  Say which cases still give two runtimes: Homebrew's
libomp (the xgboost and lightgbm wheels) shares only if
suitesparse_graphblas loads first, and the copies in the scikit-learn
wheel and recent PyTorch wheels, linked by path, never do.
With nothing preloaded, test_bundled_libomp_is_shared[graphblas] required
the bundled libomp to be the process's only one.  But with conda-forge's
NumPy, OpenBLAS loads conda's libomp before GraphBLAS does, GraphBLAS
then shares it, and the test failed although that is the intended
behavior.  Check instead that there is one libomp and that a later
request for @rpath/libomp.dylib gets it.

The original 10.5.2.0 wheel still fails both cases; a repaired one
passes both, with PyPI's NumPy and with conda-forge's.
@eriknw
eriknw marked this pull request as ready for review December 2, 2026 06:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant