问题
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?
回答1:
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:
integer omp_get_num_threads
or, better, add implicit none
in the beginning to deactivate implicit typing and then include the header file:
implicit none
include 'omp_lib.h'
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:
program test
include 'omp_lib.h'
write(*,*) 'num_proc = ',omp_get_num_procs()
write(*,*) 'max_threads = ',omp_get_max_threads()
write(*,*) 'num_threads = ',omp_get_num_threads()
C$OMP PARALLEL DO
do i=1,4
write(*,*) 'num_threads = ',omp_get_num_threads()
end do
end
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.
回答2:
Next to the missing
use omp_lib
did you complile your program with OpenMP flags? If not, for gfortran it's e.g.
-fopenmp
Edit: Here is the probably easiest program to test your issue:
program prog
use omp_lib
implicit none
integer :: i, tnr
call omp_set_num_threads( 4 ) !number of threats used in the parallel environment
!$omp parallel private( i )
!$omp do
do i = 1, 20
tnr = omp_get_thread_num() ! get threat number
write( *, * ) 'Thread', tnr, ':', i
end do
!$omp end do
!$omp end parallel
end program prog
Please compile this with e.g. gfortran like:
gfortran -fopenmp prog.f90 -o prog
It should print you lines like:
Thread 3 : 11
Here, 3 is the threat number. If your processor has more than one core and you completely installed your compiler, this should work.
来源:https://stackoverflow.com/questions/19806817/the-desired-number-of-processors-are-not-used