How to wrap OpenMP directives (#pragmas) as a function or function-like macro?

bba*_*ker 5 c macros openmp c-preprocessor

I'm trying to interface OpenMP with another language that emits C code (a C code generator). From my perspective (I'm not the designer of the other language), it will be easiest to do this by calling a C function or function-like macro instead of using #pragma or _Pragma directly. I do not have much experience with the C preprocessor, but I have gotten a simple example found on wikipedia to work in a non-satisfactory way. Here is the original C example:

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) {
  int th_id, nthreads;
  #pragma omp parallel private(th_id)
  {
    th_id = omp_get_thread_num();
    printf("Hello World from thread %d\n", th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threads\n",nthreads);
    }
  }
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

Now here is where I create a macro (pragma_omp_parallel_private) to do what I want:

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

#define STR(x) #x
#define STRINGIFY(x) STR(x) 
#define omp_par_pri STRINGIFY(omp parallel private)

char omp_test[] = omp_par_pri;
#define pragma_omp_parallel_private(thread_id)        \
  _Pragma(STRINGIFY(omp parallel private ## (thread_id)))

int main (int argc, char *argv[]) {
  int th_id, nthreads;

  // #pragma omp parallel private(th_id)
  pragma_omp_parallel_private(th_id)
  {
    th_id = omp_get_thread_num();
    printf("Hello World from thread %d\n", th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threads\n",nthreads);
    }
  }
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

预处理器将发出错误,但如果我实际编译预处理代码,它会按预期工作。有没有更合法的方法来实现这一点(更不用说在大多数构建管道中不会导致构建错误的方法)?

$ gcc -E -fopenmp wikiHello.c > wikiHello_pp.c
wikiHello.c:11:34: error: pasting "private" and "(" does not give a valid preprocessing token
   _Pragma(STRINGIFY(omp parallel private ## (thread_id)))
                                  ^
wikiHello.c:17:3: note: in expansion of macro ‘pragma_omp_parallel_private’
   pragma_omp_parallel_private(th_id)
   ^
$ gcc -fopenmp wikiHello_pp.c

$ ./a.exe
Hello World from thread 3
Hello World from thread 6
Hello World from thread 4
Hello World from thread 7
Hello World from thread 1
Hello World from thread 5
Hello World from thread 2
Hello World from thread 0
There are 8 threads
Run Code Online (Sandbox Code Playgroud)

Mas*_*ano 2

pragma在将其转换为字符串文字之前,我会将其连接起来。像这样的东西:

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

#define STR(x) #x
#define STRINGIFY(x) STR(x) 
#define CONCATENATE(X,Y) X ( Y )

#define pragma_omp_parallel_private(x) \
  _Pragma( STRINGIFY( CONCATENATE(omp parallel private,x) ) )

int main (int argc, char *argv[]) {
  int th_id, nthreads;

  // #pragma omp parallel private(th_id)
  pragma_omp_parallel_private(th_id)
  {
    th_id = omp_get_thread_num();
    printf("Hello World from thread %d\n", th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threads\n",nthreads);
    }
  }
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)