Home > Blockchain >  External openMP build does not set threads equal to OMP_NUM_THREADS
External openMP build does not set threads equal to OMP_NUM_THREADS

Time:10-28

I want to use an external build for OpenMP (not the one that comes natively with the compiler).

For the external build am cloning https://github.com/llvm-mirror/openmp.git and then cmake with the following options:

cmake                                                                     \
  -DCMAKE_BUILD_TYPE=Debug                                                \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON                                      \
  -DCMAKE_CXX_FLAGS="-std=c  17"                                          \
  -DCMAKE_CXX_COMPILER=clang                                              \
  -DCMAKE_C_COMPILER=clang                                                \
  -S ${OMP_DIR} -B ${OMP_DIR}/cmake-build/Debug

After that I cmake --build and cmake --install.

For my application build and application linking to OpenMP I use the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.18)

project(foo C CXX)

find_package(OpenMP)
add_executable(hello_world hello.cpp)
target_link_libraries(hello_world <path_to_external_openmp>/cmake-install/Debug/lib/libomp.so)

The source file is just a dummy:

#pragma omp parallel
  {
    std::cout << "Hello World, from thread: " << omp_get_thread_num()
              << std::endl;
  }

Finally, when I execute OMP_NUM_THREADS=4 ./hello_world I get the following result which is clearly not the one expected:

Hello World, from thread: 0

Also, note that ldd ./hello_world gives:

linux-vdso.so.1 (0x00007fffd311b000)                                                                                    
libomp.so => <path_to_external_openmp>/cmake-install/Debug/lib/libomp.so (0x00007fcbcbca0000)                                
libstdc  .so.6 => /lib/x86_64-linux-gnu/libstdc  .so.6 (0x00007fcbcba70000)   
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcbcb840000)                
/lib64/ld-linux-x86-64.so.2 (0x00007fcbcbde4000)                                                                        
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcbcb750000)                                                       
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fcbcb730000)

Any ideas on what could be the reason for this unexpected behavior?

CodePudding user response:

You need the -fopenmp flag; either add set(CMAKE_CXX_FLAGS "-fopenmp") to the CMakeLists.txt file or -DCMAKE_CXX_FLAGS="-fopenmp" to the cmake command.

As a side note, you can also remove the find_package(OpenMP) since you are explicitly linking against the library with the absolute path.

  • Related