'Compiler-dependent OpenMP min/max reduction pragma
My C++ code uses OpenMP directives and needs to compile with GCC and Visual Studio 2019. OpenMP min/max reduction operators were introduced in OpenMP 3.1, but Visual Studio 2019 only supports OpenMP 2.0.
I'd like my code to revert to a serial loop under Visual Studio, but I understand that the preprocessor cannot represent a conditional pragma like this:
// invalid use of preprocessor:
#ifdef _MSC_VER
#define OMP_MIN(expr)
#else
#define OMP_MIN(expr) #pragma omp parallel for reduction(min:(expr))
#endif
double x = 10.0;
OMP_MIN(x)
for (int i = 0; i < array_size; i++) {
x = fmin(x, array[i]);
}
Is there a way to achieve this?
Solution 1:[1]
You can use the DO_PRAGMA() macro idiom explained in https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html.
#define DO_PRAGMA(x) _Pragma (#x)
#ifdef _MSC_VER
#define OMP_MINX(expr)
#else
#define OMP_MINX(expr) DO_PRAGMA(omp parallel for reduction(min: expr))
#endif
OMP_MINX(x) expands to the desired #pragma omp parallel for reduction(min: x) if _MSC_VER is not defined. expr is expanded first and afterwards the whole argument of DO_PRAGMA() is stringified before the _Pragma() operator is applied. This is what OP asked for.
Solution is inspired from this answer: How to concatenate strings in the arguments of _Pragma
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
