Fortran语言的OpenMP将在哪里数组分配 [英] Fortran OpenMP where will the array be allocated

查看:841
本文介绍了Fortran语言的OpenMP将在哪里数组分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于用Fortran OpenMP和分配数组的问题。原因很简单:将在哪里空间分配?如果我有像

I have a question about Fortran-OpenMP and allocatable arrays. It's simple: Where will the space be allocated? If I have something like

!$omp parallel default(shared) private(arr)
!$omp critical
  allocate( arr(BIGNUMBER) )
!$omp end critical

!do calculations with many arr accesses

!$omp critical
  deallocate( arr )
!$omp end critical
!$omp end parallel

将空间堆栈或堆中的分配呢?如果是在堆中,有没有code以上,并像这样

will the space be allocated on the stack, or the heap? If it's on the heap, is there a difference between the code above and something like this

allocate( arr(BIGNUMBER, nThread) )
!$omp parallel default(shared) private(localArr)
  iThread = omp_get_thread_num()

  localArr => arr(:, iThread)

  !do calculations with many localArr accesses
!$omp end parallel

deallocate( arr )


  • 在第一code,有两个关键区域。我会假设,他们将放慢的执行,而不是规模非常好。 (实际上,我不知道如果我能离开他们,因为分配的线程救?)但是,如果阵列是在栈上分配的,那么它应该是因为更快的访问速度更快。

  • 在第二code我肯定会对堆,这是慢访问阵列。但是,如果在第一个code中的数组在堆上分配为好,然后我会保存关键reagions +它只有一个分配/解除分配。应该快了?

  • 是否数组的大小在这玩什么角色呢?

  • 如果它是在堆上进行分配,有没有办法来强制栈?
  • 在分配

    短的问题基本上是:这似乎是该问题的最佳解决方案。

    The short question is basically: Which would seem to be the optimal solution for the problem?

    推荐答案

    的OpenMP趋于栈上分配自动变量(包括数组)。当你明确的分配我会假设他们将在堆中分配,但要注意,Fortran标准没有关于堆栈或堆发言了,它是由编译器。在恩。 1号我将离开临界区出来,因为你分配私有变量。有关的大小,还有有时会因过大自动数组的栈溢出,但是这可能不是你的情况。什么是我不知道的最快的方法。

    OpenMP tends to allocate automatic variable (including arrays) on the stack. When you do explicit allocation I would assume they will be allocated on the heap, but note that Fortran standard does not speak about stack or heap at all, it's up to the compiler. In ex. number 1 I would leave the critical sections out, because you are allocating private variables. Regarding to the size, there are sometimes stack overflows due to too large automatic arrays, but this is probably not your case. What is the fastest approach I don't know.

    编辑::该计划似乎在堆上分配阵列

    edit: This programs seems to allocate arrays on the heap

    integer,parameter :: BIGNUMBER = 100000000
    real,dimension(:),allocatable :: arr
    
    !$omp parallel default(shared) private(Arr)
    allocate( arr(BIGNUMBER) )
      iThread = omp_get_thread_num()
    
      arr = 5
    
      print *, arr
    
    deallocate( arr )
    !$omp end parallel
    
    
    end
    

    和这一个堆栈(失败)

    integer,parameter :: BIGNUMBER = 100000000
    real arr(BIGNUMBER)
    
    !$omp parallel default(shared) private(Arr)
      iThread = omp_get_thread_num()
    
      arr = 5
    
      print *, arr
    
    !$omp end parallel
    
    
    end
    

    这篇关于Fortran语言的OpenMP将在哪里数组分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆