# OpenMP in a shared library opened in Julia

**URL:** https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880
**Category:** General Usage
**Created:** [May 6, 2024, 3:58am UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880 "2024-05-06T03:58:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 6, 2024, 3:58am UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/1 "2024-05-06T03:58:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [May 6, 2024, 10:00am UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/2 "2024-05-06T10:00:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [May 6, 2024, 11:06am UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/3 "2024-05-06T11:06:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 6, 2024, 3:48pm UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/4 "2024-05-06T15:48:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 6, 2024, 3:51pm UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/5 "2024-05-06T15:51:31Z")

</div>

> [@giordano](#):
>
> Also, are you using this multithreaded library within multithreaded julia code? That’s going to play not nicely

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.

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [May 6, 2024, 6:24pm UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/6 "2024-05-06T18:24:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 6, 2024, 7:05pm UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/7 "2024-05-06T19:05:59Z")

</div>

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](https://github.com/PetrKryslUCSD/ECLGraphColor.jl/blob/76d742db1dca1ec75460076bf32447a3234b9b26/deps/ECL-GC_12-lib.cpp#L124)

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 6, 2024, 7:38pm UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/8 "2024-05-06T19:38:48Z")

</div>

> [@greatpet](#):
>
> et the environment variable `OMP_NUM_THREADS`

That has no effect whatsoever.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 7, 2024, 2:22pm UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/9 "2024-05-07T14:22:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [May 8, 2024, 8:00am UTC](https://discourse.julialang.org/t/openmp-in-a-shared-library-opened-in-julia/113880/10 "2024-05-08T08:00:36Z")

</div>

As I said in the [issue](https://github.com/carstenbauer/ThreadPinning.jl/issues/94), this doesn’t seem to be a bug in ThreadPinning.jl but rather a usage/setup issue.
