For macOS, first do:
brew install libompand set in ~/.zshrc
export OpenMP_ROOT=$(brew --prefix)/opt/libomp
export LDFLAGS="$LDFLAGS -L${OpenMP_ROOT}/lib"| cmake_minimum_required(VERSION 3.19) | |
| project(OpenMPdemo LANGUAGES C) | |
| find_package(OpenMP COMPONENTS C REQUIRED) | |
| add_executable(hello hello_openmp.c) | |
| target_link_libraries(hello PRIVATE OpenMP::OpenMP_C) |
| /****************************************************************************** | |
| // https://computing.llnl.gov/tutorials/openMP/samples/C/omp_hello.c | |
| * FILE: omp_hello.c | |
| * DESCRIPTION: | |
| * OpenMP Example - Hello World - C/C++ Version | |
| * In this simple example, the master thread forks a parallel region. | |
| * All threads in the team obtain their unique thread number and print it. | |
| * The master thread only prints the total number of threads. Two OpenMP | |
| * library routines are used to obtain the number of threads and each | |
| * thread's number. | |
| * AUTHOR: Blaise Barney 5/99 | |
| * LAST REVISED: 04/06/05 | |
| ******************************************************************************/ | |
| #include <omp.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int main (void) | |
| { | |
| int nthreads, tid; | |
| /* Fork a team of threads giving them their own copies of variables */ | |
| #pragma omp parallel private(nthreads, tid) | |
| { | |
| /* Obtain thread number */ | |
| tid = omp_get_thread_num(); | |
| printf("Hello World from thread = %d\n", tid); | |
| /* Only master thread does this */ | |
| if (tid == 0) | |
| { | |
| nthreads = omp_get_num_threads(); | |
| printf("Number of threads = %d\n", nthreads); | |
| } | |
| } /* All threads join master thread and disband */ | |
| return EXIT_SUCCESS; | |
| } |
| $ cmake -B build | |
| -- The C compiler identification is Clang 16.0.6 | |
| -- Detecting C compiler ABI info | |
| -- Detecting C compiler ABI info - done | |
| -- Check for working C compiler: /usr/bin/clang - skipped | |
| -- Detecting C compile features | |
| -- Detecting C compile features - done | |
| -- Found OpenMP_C: -fopenmp=libomp (found version "5.0") | |
| -- Found OpenMP: TRUE (found version "5.0") found components: C | |
| -- Configuring done | |
| -- Generating done | |
| -- Build files have been written to: openmp_c/build | |
| $ cmake --build build | |
| [2/2] Linking C executable hello | |
| $ build/hello | |
| Hello World from thread = 0 | |
| Hello World from thread = 56 | |
| Hello World from thread = 2 | |
| Hello World from thread = 3 | |
| Hello World from thread = 6 | |
| Hello World from thread = 12 | |
| Hello World from thread = 26 | |
| Hello World from thread = 19 | |
| Hello World from thread = 20 | |
| Hello World from thread = 31 | |
| Hello World from thread = 14 | |
| Hello World from thread = 36 | |
| Hello World from thread = 48 | |
| Number of threads = 64 | |
| <... more messages from other threads> |
| % cmake -Bbuild --fresh | |
| -- The C compiler identification is AppleClang 14.0.3.14030022 | |
| -- Detecting C compiler ABI info | |
| -- Detecting C compiler ABI info - done | |
| -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped | |
| -- Detecting C compile features | |
| -- Detecting C compile features - done | |
| -- Found OpenMP_C: -Xclang -fopenmp (found version "5.0") | |
| -- Found OpenMP: TRUE (found version "5.0") found components: C | |
| -- Configuring done (0.4s) | |
| -- Generating done (0.0s) | |
| -- Build files have been written to: openmp_c/build | |
| % cmake --build build | |
| [2/2] Linking C executable hello | |
| % build/hello | |
| Hello World from thread = 0 | |
| Hello World from thread = 5 | |
| Hello World from thread = 4 | |
| Hello World from thread = 2 | |
| Hello World from thread = 7 | |
| Hello World from thread = 1 | |
| Hello World from thread = 6 | |
| Number of threads = 8 | |
| Hello World from thread = 3 |
Thank you!
Thank you!
Great! Thanks a lot!
It works. Thanks!
Works great, thanks!