I am running the following fortran code in parallel using openmp, but only one processor is working. I added some of the execution routines like OMP_SET_NUM_THREADS and OMP_GET_NUM_THREADS to the code to follow the parallel-processing. Here is the relevant part of the code:
integer a,b,omp_get_num_procs,omp_get_max_threads,
& omp_get_num_threads
open( unit=10 , file='threads' , status='new' )
a=4
call omp_set_num_threads(a)
write(10,*) 'num_proc=',omp_get_num_procs()
write(10,*) 'max_threads=',omp_get_max_threads()
write(10,*) 'num_threads=',omp_get_num_threads()
open( unit=7 , file='result' , status='new' )
!$OMP PARALLEL NUM_THREADS(4)
!$OMP DO DEFAULT(PRIVATE) Shared(rho1,rho2,Vnuc)
do i = 1 , nx
do j = 1 , ny
do k = 1 , nz
b = omp_get_num_threads()
write(*,*) 'Hello'
Write(10,*) 'Hello'
Write(10,*) b
write(10,100) omp_in_parallel()
100 format(l2)
...
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
or alternatively to the one by one definition of omp parameters in the header I added
include 'omp_lib.h'
and here is the result:
num_proc= 8
max_threads= 4
num_threads= 1
Hello
1
T
Hello
1
F
Hello
1
F
Hello
1
F
Hello
1
F
it continues like that. It is running but using only one processor. Can anyone help me?
Fortran uses implicit typing by default, meaning that undeclared variables/functions starting with
(a-h,o-z)
are real. Your solution is to either add the correct type of the runtime routines, like:or, better, add
implicit none
in the beginning to deactivate implicit typing and then include the header file:EDIT:
that the number of threads outside the parallel region is 1 is fine, however, in the parallel region it should indeed be 4. It could be possible that you're mixing fixed and free format, the former requires the omp directive (e.g.
C$OMP
) at the beginning of a line (on column 1), like so:If you are using free-form, then you can use
!$OMP
anywhere on a line. The tricky thing is that most compilers allow!
comment statements even in fixed format source code, but then openmp directives only work when the comment is at the beginning.Next to the missing
did you complile your program with OpenMP flags? If not, for gfortran it's e.g.
Edit: Here is the probably easiest program to test your issue:
Please compile this with e.g. gfortran like:
It should print you lines like:
Here, 3 is the threat number. If your processor has more than one core and you completely installed your compiler, this should work.