【发布时间】:2020-04-10 13:27:25
【问题描述】:
我想对用户定义的 Fortran 类型进行 OpenMP 缩减。我知道 OpenMP 不支持归约子句中的 Fortran 类型,但可以定义 自己的减持。这是在以下示例中完成的。这也有效并且做它是什么 预计
module types
!!! your type this can contain scalars and arrays
type my_type
Real*8,allocatable,dimension( : ) ::x
end type
!!! makes it possible to use the plus symbol for the reduction staement
!!! replaces my_add by plus symbol
interface operator(+)
module procedure :: my_add
end interface
!$omp declare reduction (+ : my_type : omp_out = omp_out + omp_in) initializer (omp_priv = my_type ([0,0,0,0,0,0,0,0,0,0]))
contains
function my_add( a1 , a2 )
type( my_type ),intent( in ) :: a1, a2
type( my_type ) :: my_add
my_add%x = a1%x + a2%x
return
end function my_add
end module types
program main
use types
use omp_lib
type(my_type) :: my_var
! Initialize the reduction variable before entering the OpenMP region
Allocate( my_var%x( 1:10 ) )
my_var%x = 0d0
!$omp parallel reduction (+ : my_var) num_threads(4)
my_var%x = omp_get_thread_num() + 6
print*,omp_get_thread_num()
!$omp end parallel
print *, "sum of x is ", my_var%x
end program
我现在的问题是可分配数组。
因为我将 OpenMP 缩减语句的数组初始值设定项硬编码为 initializer (omp_priv = my_type ([0,0,0,0,0,0,0,0,0,0]))
我必须在那里放 10 个零,因为数组分配的长度为 10。
是否可以使用变量名 N(数组长度)来做到这一点??
【问题讨论】:
-
我试图弄清楚如何使这个初始化程序 (omp_priv = my_type ([0,0,0,0,0,0,0,0,0,0])) 可用于变量大小。
-
你不想使用
omp_priv = omp_orig作为初始化子句吗? -
是的,这正是我想要的。谢谢!!