I have managed to compile OpenMP on my macbook but it does not run the application on multiple threads. I used the answer here.
Here is my CMakeList.txt:
cmake_minimum_required(VERSION 3.12)
project(playground)
if(APPLE)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
if(CMAKE_C_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_C_FLAGS "-Xpreprocessor -Xclang -fopenmp")
set(OpenMP_C_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_CXX_FLAGS "-Xpreprocessor -Xclang -fopenmp")
set(OpenMP_CXX_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
endif()
find_package(OpenMP REQUIRED)
add_executable(helloworld openmp.cpp)
set(OMPIncludeDirectory "/opt/homebrew/opt/libomp/include")
target_include_directories(helloworld PUBLIC ${OMPIncludeDirectory} )
target_link_libraries(helloworld PUBLIC /opt/homebrew/opt/libomp/lib/libomp.dylib)
This one compiles but the application doesn't run on multiple threads (only single).
If I don't explicitly provide the library path and instead change the last line to:
target_link_libraries(helloworld PUBLIC OpenMP::OpenMP_CXX)
It gives me linkage error:
cmake --build build
[ 50%] Linking CXX executable helloworld
ld: library not found for -lomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [helloworld] Error 1
make[1]: *** [CMakeFiles/helloworld.dir/all] Error 2
make: *** [all] Error 2
I tried compiling by hand using the following command and it works. The executable also runs on multiple threads:
c++ openmp.cpp -I/opt/homebrew/opt/libomp/include -L/opt/homebrew/opt/libomp/lib/ -lomp -Xclang -fopenmp
Here is my simple test program:
// OpenMP program to print Hello World
// using C language
// OpenMP header
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// Beginning of parallel region
#pragma omp parallel
{
printf("Hello World... from thread = %d\n",
omp_get_thread_num());
}
// Ending of parallel region
}
What am I missing here? Please help.