【问题标题】:OpenMP reduction on user defined Fortran type containing allocatable array包含可分配数组的用户定义的 Fortran 类型的 OpenMP 缩减
【发布时间】: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 作为初始化子句吗?
  • 是的,这正是我想要的。谢谢!!

标签: fortran openmp reduction


【解决方案1】:

在归约初始化子句中,我们对变量的访问是有限的,这使得可变长度的数组构造函数变得困难。但是,我们可以使用 C++ approach 的 Fortran 版本。

我们可以使用变量omp_orig来指代“要减少的原始变量的存储”:

!$omp declare reduction (+ : my_type : omp_out = omp_out + omp_in) &
!$omp&   initializer (omp_priv=omp_orig)

这里的赋值语句成功分配了每个私有副本的数组组件。

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 2014-10-18
    • 1970-01-01
    • 2020-03-17
    • 2014-09-12
    • 2017-07-20
    • 1970-01-01
    • 2012-07-17
    • 2016-12-17
    相关资源
    最近更新 更多