Search code examples
c++openmppragma

How do I create a wrapper for a pragma and have it compile properly?


I've been trying to build a wrapper around MPI and OpenMP to have unified format to code in instead of having to keep switching between MPI_xxx, omp_xxx and #pragma omp xxx.

I've been having issues creating a wrapper around the various #pragma omp directives, so far the best I've gotten is to have it as:

#define _mmc_(x) _Pragma("omp ## #x")

(mmc is the tentative name for my library)

So if I wanted to have

#pragma omp parallel for

The corresponding wrapper should be

_mmc_(parallel for)

However when it compiles the compiler seems to evaluate it differently, giving me the compilation warning

test.cpp:22:0: warning: ignoring #pragma omp  [-Wunknown-pragmas]
     _mmc_(parallel for)

I am compiling with mpic++ for MPICH 3.0.4 around gcc 4.8.4 in Ubuntu 14.04, with the flags

-fopenmp -lm -std=c++11 -Wall

Is there something that I can do or add to the code to make this work, or is this just something that cannot be done with the current tools?


Solution

  • This should work, as seen here:

    #define PRAGMA(x) _Pragma(#x)
    #define _mmc_(x) PRAGMA(omp x)
    

    _Pragma is a bit strict on taking a string literal, so we make that literal from the entire pragma argument string rather than piecing it together inside _Pragma. Preprocessor operators will not work inside a string literal, as you've tried in your post.