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

I am trying to associate OpenMP with another language that emits C code (C code generator). From my point of view (I'm not a developer of another language), this will be easiest by calling a C function or functionally similar macro, instead of directly using #pragma or _Pragma. I don't have much experience with the C preprocessor, but I have a simple example found on wikipedia to work in an unsatisfactory way. Here is an example of C:

#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;
}

Now 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;
}

, , , . ( , )?

$ 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
+4
1

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;
}
+2

Source: https://habr.com/ru/post/1539624/


All Articles