[ACCEPTED]-Ignore OpenMP on machine that does not have it-openmp

Accepted answer
Score: 68

OpenMP compilation adds the preprocessor 3 definition "_OPENMP", so you can 2 do:

#if defined(_OPENMP)
   #pragma omp ...

For some examples, see http://bisqwit.iki.fi/story/howto/openmp/#Discussion and the code 1 which follows.

Score: 25

Compilers are supposed to ignore #pragma directives 12 they don't understand; that's the whole 11 point of the syntax. And the functions 10 defined in openmp.h have simple well-defined meanings 9 on a non-parallel system -- in particular, the 8 header file will check for whether the compiler 7 defines ENABLE_OPENMP and, if it's not enabled, provide 6 the right fallbacks.

So, all you need is 5 a copy of openmp.h to link to. Here's one: http://cms.mcc.uiuc.edu/qmcdev/docs/html/OpenMP_8h-source.html .

The 4 relevant portion of the code, though, is 3 just this:

#if defined(ENABLE_OPENMP)
#include <omp.h>
typedef int omp_int_t;
inline omp_int_t omp_get_thread_num() { return 0;}
inline omp_int_t omp_get_max_threads() { return 1;}

At worst, you can just take those 2 three lines and put them in a dummy openmp.h file, and 1 use that. The rest will just work.

Score: 0

OpenMP is a compiler runtime thing and not 18 a platform thing.

ie. If you compile your 17 app using Visual Studio 2005 or higher, then 16 you always have OpenMP available as the 15 runtime supports it. (and if the end-user 14 doesn't have the Visual Studio C runtime 13 installed, then your app won't work at all).

So, you 12 don't need to worry, if you can use it, it 11 will always be there just like functions 10 such as strcmp. To make sure they have the 9 CRT, then you can install the visual studio 8 redistributable.


ok, but GCC 4.1 will 7 not be able to compile your openMP app, so 6 the issue is not the target machine, but 5 the target compiler. As all compilers have 4 pre-defined macros giving their version, wrap 3 your OpenMP calls with #ifdef blocks. for 2 example, GCC uses 3 macros to identify the 1 compiler version, __GNUC__, __GNUC_MINOR__ and __GNUC_PATCHLEVEL__

Score: 0

How could I make my program know if a machine 27 has no OpenMP and ignore those #include <omp.h>, OpenMP 26 directives (like #pragma omp parallel ...) and/or library functions 25 (like tid = omp_get_thread_num();) ?

Here's a late answer, but we just 24 got a bug report due to use of #pragma omp simd on Microsoft compilers.

According 23 to OpenMP Specification, section 2.2:

Conditional Compilation

In implementations that 22 support a preprocessor, the _OPENMP macro 21 name is defined to have the decimal value 20 yyyymm where yyyy and mm are the year 19 and onth designations of the version of 18 the OpenMP API that the implementation 17 supports.

It appears modern Microsoft compilers 16 only support OpenMP from sometime between 15 2000 and 2005. I can only say "sometime 14 between" because OpenMP 2.0 was released 13 in 2000, and OpenMP 2.5 was released in 12 2005. But Microsoft advertises a version 11 from 2002.

Here are some _OPENMP numbers...

  • Visual Studio 2012 - OpenMP 200203
  • Visual Studio 2017 - OpenMP 200203
  • IBM XLC 13.01 - OpenMP 201107
  • Clang 7.0 - OpenMP 201107
  • GCC 4.8 - OpenMP 201107
  • GCC 8.2 - OpenMP 201511

So if 10 you want to use, say #pragma omp simd to guard a loop, and 9 #pragma omp simd is available in OpenMP 4.0, then:

#if _OPENMP >= 201307
    #pragma omp simd
    for (size_t i = 0; i < 16; ++i)
        data[i] += x[i];
    for (size_t i = 0; i < 16; ++i)
        data[i] += x[i];

which 8 will run on several machines that may have 7 or not have OpenMP installed.

And to be 6 clear, you probably need to build your program 5 on each of those machines. The x86_64 ABI does not 4 guarantee OpenMP is available on x86, x32 3 or x86_64 machines. And I have not read 2 you can build on one machine, and then run 1 on another machine.

More Related questions