How to disable OMP in the translation unit in the source file?

I have C / C ++ source files written with the OMP toolkit. For example, in a C ++ class file:

#pragma omp parallel sections
{
    #pragma omp section
        cp = ModularSquareRoot(cp, m_p);
    #pragma omp section
        cq = ModularSquareRoot(cq, m_q);
}

      

For testing and testing, I want to create a library with -fopenmp

but disable it in this class file. Hopefully I can add something like #pragma omp disable

or similar in the class header file to disable it for the translation unit. But it was #pragma omp disable

silently ignored.

I looked over Using OpenMP: Portable Parallel Memory Programming , but I haven't seen how to do it. (I might well have missed it because I didn't read it in full.)

I am trying to avoid modification CXXFLAGS

, makefile recipes, and Visual Studio project settings.

Is there a way to disable OMP on a specific translation unit in a source file?

+3


source to share


1 answer


I'm not sure if you can do this in the "easy" way.
The solution I came across was to use #define

to enable / disable OpenMP for a specific file and transfer your pragmas:

#ifdef USE_OMP
#pragma omp ...
#endif

      

Then at the top of your header file, you can specify

  • #define USE_OMP

    if you want to use OpenMP
  • or comment out the line //#define USE_OMP

    if you want to disable it.


It's not very elegant, but it does the job.

As pointed out in Avi Ginzburg's comments, you can also use a suggestion if

from OpenMP:

 pragma omp parallel sections if(USE_OMP)

      

where is USE_OMP

set to true or false. However, I'm not sure if this solution will give the same behavior as disabling OpenMP. Please note that depending on your version of OpenMP, the offer may not be available for all of your directives.

+3


source







All Articles