【发布时间】:2021-06-10 17:49:08
【问题描述】:
我正在使用 parallel do 和 private 子句拆分一个 do 循环。在这个循环中,我向自身添加了一个变量。如果在这种情况下不需要关键块或原子语句,为什么会出现错误?
我该如何解决?
program trap
use omp_lib
implicit none
double precision::suma=0.d0 ! sum is a scalar
double precision:: h,x,lima,limb
integer::n,i, istart, iend, thread_num=4, total_threads, ppt
integer(kind=8):: tic, toc, rate
double precision:: time
double precision, dimension(4):: pi= 0.d0
call system_clock(count_rate = rate)
call system_clock(tic)
lima=0.0d0; limb=1.0d0; suma=0.0d0; n=100000000
h=(limb-lima)/n
suma=h*(f(lima)+f(limb))*0.5d0 !first and last points
ppt= n/total_threads
!$ call omp_set_num_threads(total_threads)
!$omp parallel do private (istart, iend, thread_num, i)
thread_num = omp_get_thread_num()
!$ istart = thread_num*ppt +1
!$ iend = min(thread_num*ppt + ppt, n)
do i=istart,iend ! this will control the loop in different threads
x=lima+i*h
suma=suma+f(x)
pi(thread_num+1)=suma
enddo
!$omp end parallel do
suma=sum(pi)
suma=suma*h
print *,"The value of pi is= ",suma ! print once from the first image
call system_clock(toc)
time = real(toc-tic)/real(rate)
print*, 'Time ', time, 's'
contains
double precision function f(y)
double precision:: y
f=4.0d0/(1.0d0+y*y)
end function f
end program trap
我收到以下错误:
test.f90:23:35:
23 | thread_num = omp_get_thread_num()
错误:(1) 处出现意外的赋值语句
test.f90:24:31:
24 | !$ istart = thread_num*ppt +1
错误:(1) 处出现意外的赋值语句
test.f90:25:40:
25 | !$ iend = min(thread_num*ppt + ppt, n)
错误:(1) 处出现意外的赋值语句
编译:
gfortran -fopenmp -Wall -Wextra -O2 -Wall -o prog.exe test.f90
./prog.exe
【问题讨论】:
-
为什么要手动设置循环边界而不是使用工作共享指令?这可以自动为您完成。
-
您是说 schedule 子句吗?如果我使用 workshare 指令,我需要手动拆分 do 循环(在本例中为 4 部分)。
-
@Isaac 不,工作共享指令都是:
omd do、omp sections、omp workshare,也许还有一些我不记得了。在共享工作的线程之间传播工作的所有指令。
标签: multithreading parallel-processing fortran openmp gfortran