0

I am building a shared library on a linux system which is intended to be used in a project that will be deployed in a Docker on a server. This shared library uses openmp. I am therefore wondering if it is better (or more portable) to have

  1. openmp linked statically to my shared library
  2. I should install gcc properly in the Docker in order to have openmp found
  3. Distribute the .so of openmp together with my library

If option 1 is the best, can someone provide the right way to do this with cmake?

I add below some documentation on the subject:

Yvus
  • 338
  • 1
  • 20

2 Answers2

3

You generally cannot link a static library into a shared one.

Because static libraries (lib*.a) don't contain position-independent code (PIC), but shared libraries need PIC (in practice).

(in theory, non PIC shared libraries could be possible; but they would contain so much relocation that the "shared" aspect is lost and the dynamic linker would have a lot of work. So in practice, every shared library needs to be PIC to permit its code segment[s] to be mmap(2)-ed at different addresses in various processes and still stay shared)

Read Drepper's How To Write Shared Libraries paper.

However, you can link a shared library (e.g. libopenmp.so) into another one (your shared library, see this). Then the program using your shared library will need that libopenmp.so.

So you could do 2 or 3, or even package your library as a proper .deb package (which would depend on libopenmpi2 Debian package).

You may want to understand package management.

You should understand more the virtual address space of your process. For that, use proc(5) and pmap(1). For first examples, try cat /proc/self/maps and cat /proc/$$/maps. Then, if your process has pid 1234, try cat /proc/1234/maps and/or pmap 1234. You'll then understand how shared libraries are mmap(2)-ed.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

Linking the OpenMP runtime statically into another shared library is a bad idea even if you could achieve it. If some other component of the final code also uses OpenMP you will end up with two different OpenMP runtimes in the process. That rapidly leads to over-subscription when each runtime creates its own thread pool and thus poor performance. (As well as potential correctness failures if your code assumes that OpenMP critical sections will protect it against the other parallel code...)

The easiest answer is probably your number 3: ship a copy of the relevant OpenMP runtime shared library with your code.

Jim Cownie
  • 2,409
  • 1
  • 11
  • 20