使用 Critical 构造的 OpenMP 使我的代码崩溃
OpenMP using Critical construct crashes my code
所以我正在用 Fortran 编写一些并行代码,但我需要使用关键块来防止竞争条件。这是我的代码的基本版本(它是一个优化器):
do i=2,8,2
do j=1,9-i
Ftemp=1.0e20 !A large number
!$OMP parallel do default(shared) private(...variables...)
do k=1,N
###Some code that calculates variable Fobj###
!$OMP Critical
!$OMP Flush(Ftemp,S,Fv) !Variables I want optimized
if (Fobj.lt.Ftemp) then
S=Stemp
Fv=Ft
Ftemp=Fobj
end if
!OMP Flush(Ftemp,S,Fv)
!OMP end Critical
end do !Line 122
!$OMP end parallel do !Line 123
end do
end do
所以没有 openmp,代码工作正常。它也可以在没有关键命令的情况下运行(刷新命令很好)。我得到的错误是第 122 行的 "unexpected END statement" 和第 123 行的 "Unexpected !$OMP end parallel do statement"。我不知道为什么这不起作用,因为关键块完全包含在并行循环中并且没有 exit/goto 将离开或进入的语句...一些 goto 跳转到循环的主要部分,但永远不会离开它或 entering/bypassing 关键块。
正如 Hristo Iliev 在评论中指出的那样:您的关闭指令 !OMP end Critical
在 !
之后缺少 $
。
它被视为评论并被忽略。
所以我正在用 Fortran 编写一些并行代码,但我需要使用关键块来防止竞争条件。这是我的代码的基本版本(它是一个优化器):
do i=2,8,2
do j=1,9-i
Ftemp=1.0e20 !A large number
!$OMP parallel do default(shared) private(...variables...)
do k=1,N
###Some code that calculates variable Fobj###
!$OMP Critical
!$OMP Flush(Ftemp,S,Fv) !Variables I want optimized
if (Fobj.lt.Ftemp) then
S=Stemp
Fv=Ft
Ftemp=Fobj
end if
!OMP Flush(Ftemp,S,Fv)
!OMP end Critical
end do !Line 122
!$OMP end parallel do !Line 123
end do
end do
所以没有 openmp,代码工作正常。它也可以在没有关键命令的情况下运行(刷新命令很好)。我得到的错误是第 122 行的 "unexpected END statement" 和第 123 行的 "Unexpected !$OMP end parallel do statement"。我不知道为什么这不起作用,因为关键块完全包含在并行循环中并且没有 exit/goto 将离开或进入的语句...一些 goto 跳转到循环的主要部分,但永远不会离开它或 entering/bypassing 关键块。
正如 Hristo Iliev 在评论中指出的那样:您的关闭指令 !OMP end Critical
在 !
之后缺少 $
。
它被视为评论并被忽略。