Steve
Steve

Reputation: 89

OpenACC: How to apply openacc pragma to "macro loops"

I defined these macros:

#define I_LOOP(g, i)     _ibeg = g->lbeg[IDIR]; _iend = g->lend[IDIR];  \
                      for (i = _ibeg; i <= _iend; i++)
#define J_LOOP(g, j)     _jbeg = g->lbeg[JDIR]; _jend = g->lend[JDIR];  \
                      for (j = _jbeg; i <= _jend; j++)

I have this loop I want to parallelize

  #pragma acc parallel loop collapse(2) 
  I_LOOP(g, i){
  J_LOOP(g, j){
    U0[j][i] = Uc[j][i];
  }}

but I get error: this kind of pragma may not be used here.

Is there a way I can parallelize this loop with the macros?

Upvotes: 0

Views: 110

Answers (1)

tschwinge
tschwinge

Reputation: 356

First, OpenACC loop requires the for loop to be tightly nested inside it, that is without the preceding _ibeg, _iend assignments.

Second, for this kind of #define usage, you may be able to cook something up with _Pragma (see https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html, etc.):

#define I_LOOP(g, i)     _ibeg = g->lbeg[IDIR]; _iend = g->lend[IDIR];  \
                      _Pragma("acc parallel loop private(_jbeg, _jend") \
                      for (i = _ibeg; i <= _iend; i++)
#define J_LOOP(g, j)     _jbeg = g->lbeg[JDIR]; _jend = g->lend[JDIR];  \
                      _Pragma(acc loop) \
                      for (j = _jbeg; j <= _jend; j++)

(Untested; you didn't provide stand-alone example.)

(Notice I also fixed i <= _jend typo.)

Possibly indirection via #define DO_PRAGMA(x) _Pragma(#x) might be useful:

#define DO_PRAGMA(x) _Pragma(#x)

#define I_LOOP(g, i, pragma)     _ibeg = g->lbeg[IDIR]; _iend = g->lend[IDIR];  \
                      DO_PRAGMA(pragma) \
                      for (i = _ibeg; i <= _iend; i++)
#define J_LOOP(g, j, pragma)     _jbeg = g->lbeg[JDIR]; _jend = g->lend[JDIR];  \
                      DO_PRAGMA(pragma) \
                      for (j = _jbeg; j <= _jend; j++)

..., and then:

I_LOOP(g, i, "acc parallel loop private(_jbeg, _jend"){
J_LOOP(g, j, "acc loop"){
  U0[j][i] = Uc[j][i];
}}

Some more code restructuring would be necessary for using the collapse clause, which again requires the for loops to be tightly nested.

Upvotes: 0

Related Questions