I have a C++ program using OpenMP, which will run on several machines that may have or not have OpenMP installed.
How could I make my program know if a machine has no OpenMP and ignore those #include <omp.h>
, OpenMP directives (like #pragma omp parallel ...
) and/or library functions (like tid = omp_get_thread_num();
) ?
Because OpenMP is built into a compiler, no external libraries need to be installed in order to compile this code.
OpenMP is a library for parallel programming in the SMP (symmetric multi-processors, or shared-memory processors) model. When programming with OpenMP, all threads share memory and data. OpenMP supports C, C++ and Fortran. The OpenMP functions are included in a header file called omp.
Set the project's platform toolset (project Properties -> General -> Platform Toolset) to "LLVM (clang-cl)". Enable Clang OpenMP support by adding -Xclang -fopenmp to the compiler options in project Properties -> C/C++ -> All Options -> Additional Options.
Enable OpenMPRight-click on your project in Solution Explorer, and select properties. Select C/C++ -> Language, and change OpenMP Support to Yes. Click ok, and make sure your application still compiles and runs.
OpenMP compilation adds the preprocessor definition "_OPENMP", so you can do:
#if defined(_OPENMP) #pragma omp ... #endif
For some examples, see http://bisqwit.iki.fi/story/howto/openmp/#Discussion and the code which follows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With