7

I’m trying to do something similar to another question, namely, conditionally include OpenMP pragmas in my program. However, I want to go one step further and avoid that the user needs to specify omp every time they use the pragma. In other words, I want the following code to compile:

#include <cstdio>
#include <omp.h>

#ifdef _OPENMP
#   define LIB_PRAGMA_OMP(x) _Pragma("omp " #x)
#else
#   define LIB_PRAGMA_OMP(x)
#endif

int main() {
    LIB_PRAGMA_OMP(parallel) {
        std::printf("Hello from thread %d\n", omp_get_thread_num());
    }
}

Unfortunately, this doesn’t work. The compiler complains:

error: _Pragma takes a parenthesized string literal

If I use the following form, it works, though:

#define LIB_PRAGMA_OMP(x) _Pragma(#x)

…

LIB_PRAGMA_OMP(omp parallel) …

However, I’d really like to avoid this redundancy. How can I paste the (stringified) tokens correctly inside the _Pragma operator?

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

2 Answers2

10

After much trial and error, it turns out that the simplest solution works:

#ifdef _OPENMP
#   define LIB_PRAGMA_OMP(x)  DO_PRAGMA(omp x)
#   define DO_PRAGMA(x) _Pragma ( #x )
#else
#   define LIB_PRAGMA_OMP(x)
#endif

With -DOPENMP, I get:

# 12 "test_op.cpp"
#pragma omp parallel
# 12 "test_op.cpp"

And without it, nothing.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1
#define MAKE_STRING(x)  #x
#define LIB_PRAGMA_OMP(x) _Pragma(MAKE_STRING(omp x))

Also works, if you prefer that. I like it better because it minimizes the amount of work this helper function does.

CodeMonkey
  • 4,067
  • 1
  • 31
  • 43