Compiler-dependent OpenMP min/max reduction pragma

Solution 1:

You can use _Pragma() from C++11 to have a function-like macro that conditionally enables a pragma:

#include <cmath>

#ifdef _MSC_VER
#define OMP_MINX()
#else
#define OMP_MINX() _Pragma("omp parallel for reduction(min:x)")
#endif

int main() {
  double x = 10.0;
  double array[50];
  int array_size = 50;

  OMP_MINX()
  for (int i = 0; i < array_size; i++) {
    x = std::fmin(x, array[i]);
  }
}

but I haven't figured out how to make gcc accept anything other than a literal string for the argument to allow using an arbitrary variable - not even the preprocessor's stringification operator works, meaning you might as well just use

#ifndef _MSC_VER
#pragma omp parallel for reduction(min:x)
#endif
for (int i = 0; i < array_size; i++) {
  x = std::fmin(x, array[i]);
}