views:

250

answers:

3
+1  Q: 

Turn off OpenMP

Hi, In my C++ program, I'd like to run its executable sometimes with and sometimes without using OpenMP (i.e. multi-threading or single-threading). I am considering any of the following two cases how my code is using OpenMP:

(1) Assume that my code is only having #include <omp.h> and OpenMP directives.

(2) Same as (1) and my code further calls OpenMP functions like omp_get_thread_num().

In order not to have different code for different running, is it the only way using some self-defined precompiler variable to guard where OpenMP appears in my code ?

Thanks and regards!

+2  A: 

Hi the easiest way to do this is

omp_set_num_threads(m_iUseableProcessors);

where m_iUseableProcessors is the number of processors you want to split the calculation over. I don't know how to do it without the OpenMP functions. You could probably #ifdef them out, but that makes you turn off OpenMP at compile time.

Steve
+3  A: 

You can use the environment variable:

set OMP_NUM_THREADS=1

Actually, it will not turn OpenMP off. It will force OpenMP to create only one thread for an application. It works without recompilation. I use this variable to test scalability on 1, 2, 3, 4 etc threads.

Vova
nice trick
Steve
A: 

You can put the include as follows:

#ifdef _OPENMP_
#include<omp.h> 
#endif

Now if you run your program without -fopenmp flag, it would ignore openmp directives

lava