Titas Chanda
Titas Chanda

Reputation: 573

Switch on/off openmp using macros

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.

Upvotes: 1

Views: 714

Answers (1)

Anedar
Anedar

Reputation: 4275

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.

Upvotes: 2

Related Questions