Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

omp_get_num_threads() and omp_get_thread_num() returning nonsense

I am just starting out with using OpenMP in Fortran using the Intel Fortran compiler and Visual Studio 2015. In the project properties I have "Fortran -> Language -> Process OpenMP Directives" set to "Generate Parallel Code (/Qopenmp)"

I have a simple program starting as follows:

program hellothreads
   integer threads, id
   call omp_set_num_threads(3)   
   threads = omp_get_num_threads()

   print *,"there are",  threads, "threads"

This produces

there are -2147483648 threads

which there certainly aren't. Setting the number of threads seems to work OK though, since:

   !$OMP Parallel private(id) shared(threads)
   threads = omp_get_num_threads()
   id = omp_get_thread_num()
   print *, "hello from thread", id, "out of", threads
   !$OMP end Parallel

outputs

hello from thread -2147483648 out of -2147483648

hello from thread -2147483648 out of -2147483648

hello from thread -2147483648 out of -2147483648

and continuing with:

   !$OMP Parallel private(id) shared(threads)
   threads = omp_get_num_threads()
   id = omp_get_thread_num()
   print *, "this is thread", id, "of", threads
   !$OMP end Parallel

outputs

this is thread -2147483648 of -2147483648

this is thread -2147483648 of -2147483648

Finally there is different weird behaviour if I call the OpenMP functions inside the a "print": e.g.:

   !$OMP Parallel private(id) shared(threads)
   print *, "this is thread", omp_get_num_threads(), "of", omp_get_thread_num()
   !$OMP end Parallel
   stop
end

Outputs

this is thread NaN of NaN

this is thread NaN of NaN

What is wrong with my configuration and/or code?

like image 438
MatthewJohnHeath Avatar asked May 04 '16 09:05

MatthewJohnHeath


Video Answer


1 Answers

Use implicit none in ALL your Fortran programs!!!

After doing that you will realize that the functions are not declared and assumed to be real. The nonsense real value is than converted to an integer value and stored in your variables which you print.

As @francescalus recommends in the comment, by use omp_lib you use a module which contains the correct declarations of the functions and will help you check if you are using them correctly.

like image 183
Vladimir F Героям слава Avatar answered Sep 28 '22 02:09

Vladimir F Героям слава