Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

parallel processing - the desired number of processors are not used

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...