Fortran OpenMP并行函数调用
我有一些Fortran代码,是为科学目的编写的。有两个条件函数调用是按顺序测试和运行的。对这些函数的调用我想并行化,因为它们彼此不相关。然而,我不确定最好的方法是什么 我拥有的代码示例如下:Fortran OpenMP并行函数调用,fortran,openmp,Fortran,Openmp,我有一些Fortran代码,是为科学目的编写的。有两个条件函数调用是按顺序测试和运行的。对这些函数的调用我想并行化,因为它们彼此不相关。然而,我不确定最好的方法是什么 我拥有的代码示例如下: ... if ( flag1 ) then stat1 = func1( tm, dt, struct % a, struct % b ) if ( stat1 .gt. 0 ) then call die_error("error message") status = 1
...
if ( flag1 ) then
stat1 = func1( tm, dt, struct % a, struct % b )
if ( stat1 .gt. 0 ) then
call die_error("error message")
status = 1
return
end if
end if
if ( flag2 ) then
stat2 = func2( tm, dt, struct % a, struct % c )
if ( stat2 .gt. 0 ) then
call die_error("error message 2")
status = 1
return
end if
end if
...
...
!$omp parallel num_threads(2) firstprivate(tm, dt, struct % a) if(flag1 .and. flag2)
!$omp master
!$omp task lastprivate(stat1)
stat1 = func1( tm, dt, struct % a, struct % b )
!$omp end task
!$omp task lastprivate(stat2)
stat2 = func2( tm, dt, struct % a, struct % c )
!$omp end task
!$omp taskwait
if ( (stat1 .gt. 0) .or. (stat2 .gt. 0) ) then
call die_error("error message")
status = 1
return
end if
!$omp end master
!$omp end parallel
if ( flag1 .and. .not.flag2 ) then
stat1 = func1( tm, dt, struct % a, struct % b )
if ( stat1 .gt. 0 ) then
call die_error("error message")
status = 1
return
end if
end if
if ( flag2 .and. .not.flag1 ) then
stat2 = func2( tm, dt, struct % a, struct % c )
if ( stat2 .gt. 0 ) then
call die_error("error message 2")
status = 1
return
end if
end if
...
标志flag1
和flag2
是用户定义的,其中一个可能为真,或者两个都为真,或者两个都为假,这就是独立测试它们的原因
函数参数tm
和dt
分别是整数和双精度变量,但它们不会被func1
或func2
更改
自定义数据类型struct
包含三个双精度数组:a
、b
和c
。数组结构%a
不会被func1
或func2
更改。但是,struct%b
由func1
更改,struct%c
由func2
更改
在特定情况下,当flag1
和flag2
均为true时,可以并行调用func1
和func2
。但是,我不确定以下几点:
结构%a
,因为它不会被代码更改firstprivate(结构%a)
例如flag1
和flag2
都为真,如何正确实现我只希望并行化的条件...
if ( flag1 ) then
stat1 = func1( tm, dt, struct % a, struct % b )
if ( stat1 .gt. 0 ) then
call die_error("error message")
status = 1
return
end if
end if
if ( flag2 ) then
stat2 = func2( tm, dt, struct % a, struct % c )
if ( stat2 .gt. 0 ) then
call die_error("error message 2")
status = 1
return
end if
end if
...
...
!$omp parallel num_threads(2) firstprivate(tm, dt, struct % a) if(flag1 .and. flag2)
!$omp master
!$omp task lastprivate(stat1)
stat1 = func1( tm, dt, struct % a, struct % b )
!$omp end task
!$omp task lastprivate(stat2)
stat2 = func2( tm, dt, struct % a, struct % c )
!$omp end task
!$omp taskwait
if ( (stat1 .gt. 0) .or. (stat2 .gt. 0) ) then
call die_error("error message")
status = 1
return
end if
!$omp end master
!$omp end parallel
if ( flag1 .and. .not.flag2 ) then
stat1 = func1( tm, dt, struct % a, struct % b )
if ( stat1 .gt. 0 ) then
call die_error("error message")
status = 1
return
end if
end if
if ( flag2 .and. .not.flag1 ) then
stat2 = func2( tm, dt, struct % a, struct % c )
if ( stat2 .gt. 0 ) then
call die_error("error message 2")
status = 1
return
end if
end if
...
我在上面还有几个问题:
$是否需要omp主指令
指令$不能使用omp节
指令而不是$omp任务
返回的$如果func1
或func2
失败,omp并行指令是否安全