1

I want to switch on/off openmp parallel for loops, in specific parts of my code, where as parallelization in other parts will remain intact. Also I do not want to change the source code of that parts every time, so tried some macro hack like following.

#ifdef USE_PARALLEL
  #define USE_OPENMP_FOR #pragma omp parallel for
#else
  #define USE_OPENMP_FOR
#endif

So that in the source code, I could simply use...

USE_OPENMP_FOR
for ( int i = 0 ; i < 100 ; ++i ) {
  // some stuffs
}

And define the macro USE_PARALLEL in main.cpp file if I need those parallel loops.

But unfortunately this does not work at all. I know the problem is in #define QIC_LIB_OPENMP_FOR #pragma omp parallel for line. But could not find any solution.

Is there any way to solve this problem, with or without the macro hack?

EDIT:: This question is different from disable OpenMP in nice way as I wanted to switch off openmp in specific parts, not for the whole program. As suggested by Jarod42 and Anedar, _Pagma("...") solved my problem.

Community
  • 1
  • 1
Titas Chanda
  • 563
  • 1
  • 4
  • 14

1 Answers1

2

You basically can't use #pragma inside a #define, but you can use the pragma operator as _pragma("omp parallel for") inside a macro definition.

If this is not supported by your compiler, this should work:

#ifdef USE_PARALLEL
    #define USE_OPENMP_FOR omp parallel for
#else
    #define USE_OPENMP_FOR
#endif

#pragma USE_OPENMP_FOR
for ( int i = 0 ; i < 100 ; ++i ) {
  // some stuffs
}

Which will just resolve to an empty #pragma if USE_PARALLEL is not defined.

Anedar
  • 4,235
  • 1
  • 23
  • 41
  • Be aware that under gcc with the -Wall flag, this will fire the -Wunknown-pragmas warning. – Tim Feb 29 '16 at 18:40
  • `_Pragma(" ")` works well with gcc and clang. Though I don't know if it will work with icc or msvc. But the second solution did not work, it is giving `-Wunknown-pragmas` warning even with `USE_PARALLEL` defined . – Titas Chanda Feb 29 '16 at 18:42
  • _Pragma is standard C99. It certainly works with icc. – Jim Cownie Mar 01 '16 at 09:27