Search code examples
c++callbackclangopenmp

Enabling OpenMP tool with callbacks in a C++ code


I'm developing a small OpenMP tool library that will allow me to utilise OpenMP callbacks. This library is written entirely in C. I can compile and link it into my user code. To compile the library I use the following commands in a Makefile:

CC     = clang
CFLAGS =-O3 -fopenmp
LDLIBS =-lomp

libompt.so: counter.c ompt.c
        $(CC) $(CFLAGS) -shared -fPIC $^ -o $@

To compile my little example statically or dynamically I have these targets in the Makefile:

test_daxpy_sta: counter.c ompt.c daxpy.c test_daxpy.c
        $(CC) $(CFLAGS) $(LDLIBS) $^ -o $@

test_daxpy_dyn: daxpy.c test_daxpy.c libompt.so
        $(CC) $(CFLAGS) -L. -lompt $(LDLIBS) $^ -o $@

All works nice and well. Now I would like to use a C++ object and its methods inside OpenMP on_ompt_callback_thread_begin() and on_ompt_callback_thread_end() callbacks. To achieve that I need to compile my little OpenMP tool library with a C++ compiler. The only change to the commands above is:

CC     = clang++

Compiled with clang++ the OpenMP tool no longer works. It does not register callbacks and does not do any "profiling" work behind the scenes. I do not know why. What is a correct way to enable an OpenMP tool with callbacks in a C++ code?

I compiled my library and static and dynamic executables with clang and clang++. Then I used ldd on my library and the executables. The only difference is that test_daxpy_sta compiled with clang++ uses libgcc_s.so.1 => /usr/lib/libgcc_s.so.1. Is this right? Shouldn't clang++ rely on an LLVM equivalent of libgcc_s? I found clang++ option --rtlib, but I don't know, what is LLVM's counterpart of GNU's libgcc_s.


Solution

  • The OpenMP runtime loads a tool by searching for a globally-visible function with the name ompt_start_tool [1]. If you are compiling your tool with a C++ compiler, make sure this function is declared extern "C" so the function's name is not mangled and can be found by the OpenMP runtime [2].

    You can check whether the ompt_start_tool function has been mangled using the nm command to see the symbols defined in your compiled library:

    nm libompt.so | grep ompt_start_tool

    As an additional tip for diagnosing the loading of OpenMP tools, you can use the OMP_TOOL_VERBOSE_INIT environment variable to diagnose problems with detecting and loading tools at runtime [3]. This shold help you verify whether the runtime is able to find and load your tool at all.

    References

    [1] https://www.openmp.org/spec-html/5.0/openmpsu177.html

    [2] https://isocpp.org/wiki/faq/mixing-c-and-cpp#call-cpp

    [3] https://www.openmp.org/spec-html/5.1/openmpse77.html