对包含可分配数组的用户定义Fortran类型的OpenMP缩减
我想对用户定义的Fortran类型执行OpenMP缩减。我知道OpenMP 不支持reduction子句中的Fortran类型,但可以定义 自己的削减。这在下面的示例中完成。这也能起作用,也能起作用 期望对包含可分配数组的用户定义Fortran类型的OpenMP缩减,fortran,openmp,reduction,Fortran,Openmp,Reduction,我想对用户定义的Fortran类型执行OpenMP缩减。我知道OpenMP 不支持reduction子句中的Fortran类型,但可以定义 自己的削减。这在下面的示例中完成。这也能起作用,也能起作用 期望 module types !!! your type this can contain scalars and arrays type my_type Real*8,allocatable,dimension( : ) ::x end type !!! make
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 reduction语句的数组初始值设定项硬编码为初始值设定项(omp_priv=my_type([0,0,0,0,0,0,0]))
我必须在那里放10个零,因为数组的长度是10。
是否可以使用变量名N(数组长度)?在reduce initializer子句中,我们对变量的访问受到限制,这使得可变长度的数组构造函数变得困难。但是,我们可以使用的是Fortran版本的 我们可以使用变量
omp_orig
来引用“要减少的原始变量的存储”:
这里的赋值语句成功地分配了每个私有副本的数组组件。在reduce initializer子句中,我们对变量的访问受到限制,这使得可变长度的数组构造函数变得困难。但是,我们可以使用的是Fortran版本的 我们可以使用变量
omp_orig
来引用“要减少的原始变量的存储”:
这里的赋值语句成功地分配了每个私有副本的数组组件。我正在尝试找出如何创建此初始值设定项(omp_priv=my_type([0,0,0,0,0,0,0,0]))可用于可变大小。是否只想使用
omp\u priv=omp\u orig
作为初始值设定项子句?是的,这正是我想要的。非常感谢。我正在试图找出如何使此初始值设定项(omp_priv=my_type([0,0,0,0,0,0,0,0])可用于可变大小。您不只是想使用omp_priv=omp_orig
作为初始值设定项子句吗?是的,这正是我想要的。非常感谢。
!$omp declare reduction (+ : my_type : omp_out = omp_out + omp_in) &
!$omp& initializer (omp_priv=omp_orig)