Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

specify thread number in openMP

maria
Principiante
1.117 Visualizações
Hi,

I have recenly implemented openMP in our software. I want the same source code that can
be complied parallelly or sequentially. The question is that howto do thatif I want to specify the number of threads as input?

My code is like the following:

....
call omp_set_num_threads(thread_num)
!$OMP parallel do private(num_thread)
DO I = 1, NP
num_thread=1
!$ num_thread = omp_get_thread_num()+1
.....
ENDDO


The number of threads thread_num is read from input. Inside the paralle region, the thread number num_thread from each thread
is used.For a non-openMP platform,thread_num is set to be1 from input.
!$ is treated as comment by a non-OpenMP compiler. So the do loop is run serially. But 'call omp_set_num_threads()' library can not be compiled in a non-openMP compiler.

Do you have any suggestions about setting the number of threads in openMP while the code can be compiled in non-opnMP platform? Thanks.

0 Kudos
4 Respostas
IanH
Colaborador honorário III
1.117 Visualizações
Could you just "omp comment" out the call in the same way?

!$ use omp_lib
...
!$ call omp_set_num_threads(thread_num)

You could use "normal" compiler directives too:

!DEC$ IF DEFINED(_OPENMP)
call omp_set_num_threads(thread_num)
!DEC$ ELSE
write (*,"(A)") "I can only deal with one thing at a time"
!DEC$ ENDIF

Or you could use the C-like preprocessor (/fpp command line option):

#ifdef _OPENMP
call omp_set_num_threads(thread_num)
#else
write (*,"(A)") "I'm feeling very serial today"
#endif
jimdempseyatthecove
Colaborador honorário III
1.117 Visualizações
You can also link with the OpenMP "stubs" library, which links in NO-OP functions.
Or you can write your own no-op stub functions.
Comment what may happen in your code when a requrest is made for more than one thread while linked with stubs. Or possibly write out a diagnostic message

write (*,*) "Attempting to specify ", nThreads, "threads when OpenMP not available, using 1 thread instead"
nThreads = 1

Jim
TimP
Colaborador honorário III
1.117 Visualizações
I use the following seemingly obvious method:
#ifdef _OPENMP

#else
num_thread=1
#endif

given that it is standard practice to guard all omp_ calls by #ifdef _OPENMP so as to permit compilation without OpenMP compiler.
jimdempseyatthecove
Colaborador honorário III
1.117 Visualizações
#if... requires a preprocessor

I prefer to use #if myself since I use IVF which has a preprocessor (also has OpenMP).

OP requested technique for system with compiler without OpenMP support. It is unknown as to if this compiler (or system) has a preprocessor for use with Fortran.

Jim
Responder