没有使用所需数量的处理器
我使用openmp并行运行以下fortran代码,但只有一个处理器正在工作。我向代码中添加了一些执行例程,如 OMP_SET_NUM_THREADS 和 OMP_GET_NUM_THREADS ,以遵循并行处理。以下是代码的相关部分:
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
或者在我添加的标题中逐个定义了omp参数
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默认使用隐式键入,意味着未声明的变量/函数以(啊,盎司)
是真实的。您的解决方案是添加正确类型的运行时例程,如:
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'
编辑:
并行区域外的线程数为1是好的但是,在并行区域中的确应该是4.它可能是混合固定格式和自由格式,前者需要omp指令(例如 C $ OMP
)在行的开始处(在列1上),如下所示:
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
如果您使用的是自由格式,那么您可以使用!$ OMP
在一行的任何地方。棘手的是,即使在固定格式的源代码中,大多数编译器也允许!
注释语句,但是只有当注释开始时,openmp指令才能使用。
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.