OpenMP in a shared library opened in Julia

A C++ (mostly C, really) library is dynamically loaded into Julia. That library uses
OpenMP for parallelism. I do not seem to be getting any speedups, which makes me wonder: Is there some interaction of Julia and the C++ OpenMP instrumentation of the library code? This is on Linux.

External libraries using OpenMP should just work, they’d link to either libgomp.so shipped with julia if already loaded in the current session, or whatever other system OpenMP runtime they find in the system. I seem to remember SparseArrays in Julia uses OpenMP. The default OpenBLAS builds use pthreads rather than OpenMP, but in the past I played with an OpenBLAS build using OpenMP and it was correctly using multiple threads without having to do anything special on the Julia side.

Did you check that multiple threads were actually used? Did you check OPENMP_NUM_THREADS is not set to 1? Or maybe the library is just scaling poorly for the current problem? Also, are you using this multithreaded library within multithreaded julia code? That’s going to play not nicely

Stupid question from me. If you run ‘htop’ while your code is running do you see threads being used?

I wish that was possible. The runtime of the graph calculation is about 0.6 seconds.

Yes, the library is called in the middle of a multithreaded computation. However, multiple threads are not running in the application when the library is called.

I tested the library by itself, starting Julia with multiple threads, and I get the expected speedups.

So, at the moment I am at a loss why by itself the library uses OpenMP to achieve parallel speedup, but as part of a larger application it fails to deliver.

Have you set the environment variable OMP_NUM_THREADS? I don’t think starting Julia with multiple threads means this environment variable will be set automatically.

I have not tried that yet, but when I test the library by itself, I just call it and provide
the number of threads as an argument. I believe the library does that with ECLGraphColor.jl/deps/ECL-GC_12-lib.cpp at 76d742db1dca1ec75460076bf32447a3234b9b26 · PetrKryslUCSD/ECLGraphColor.jl · GitHub

That has no effect whatsoever.

I turned off thread pinning. That fixed it.

Anyone familiar with thread pinning: why would it affect OpenMP in an external library?

Edit: issue filed.

As I said in the issue, this doesn’t seem to be a bug in ThreadPinning.jl but rather a usage/setup issue.