将数组切片作为参数传递时的Fortran性能

将数组切片作为参数传递时的Fortran性能

本文介绍了将数组切片作为参数传递时的Fortran性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢fortran的数组切片表示法(array(1:n)),但我想知道是否在不必要时使用它们是否会对性能产生影响.

I like fortran's array-slicing notation (array(1:n)), but I wonder whether I take a performance hit if I use them when it's not necessary.

例如,考虑以下简单的快速排序代码(它可以工作,但显然并没有注意选择一个好的枢轴):

Consider, for example, this simple quicksort code (It works, but obviously it's not taking care to pick a good pivot):

recursive subroutine quicksort(array, size)

    real, dimension(:), intent(inout) :: array
    integer, intent(in) :: size
    integer :: p

    if (size > 1) then
        p = partition(array, size, 1)
        call quicksort(array(1:p-1), p-1)
        call quicksort(array(p+1:size), size-p)
    end if

end subroutine quicksort

function partition(array, size, pivotdex) result(p)

    real, dimension(:), intent(inout) :: array
    integer, intent(in) :: size, pivotdex
    real :: pivot
    integer :: i, p

    pivot = array(pivotdex)
    call swap(array(pivotdex), array(size))

    p=1
    do i=1,size-1
        if (array(i) < pivot) then
            call swap(array(i), array(p))
            p=p+1
        end if
    end do

    call swap(array(p), array(size))

end function partition

subroutine swap(a, b)

    real, intent(inout) :: a, b
    real :: temp
    temp = a
    a = b
    b = temp

end subroutine swap

我可以轻松地传递整个数组以及递归部分应该在哪里工作的索引,但是我喜欢这种方式的代码.但是,当我调用quicksort(array(1:p-1), p-1)时,它是否使临时数组可以进行操作,还是只是使引用结构变浅或类似的东西?这是一个足够有效的解决方案吗?

I could just as easily pass the whole array along with the indices of where the recursive parts should be working, but I like the code this way. When I call quicksort(array(1:p-1), p-1), however, does it make a temporary array to operate on, or does it just make a shallow reference structure or something like that? Is this a sufficiently efficient solution?

这个问题是相关的,但由于存在以下原因,它似乎构成了临时数组大跨度切片和显式大小的伪变量,所以我对此感到安全,对吧?

This question is related, but it seems like it makes temporary arrays because of the strided slice and explicit-sized dummy variables, so I'm safe from that, right?

推荐答案

您的子数组

  array(1:p-1)

是连续的,只要array是连续的.

is contiguous, provided array is contiguous.

此外,您使用假定的形状数组虚拟参数

Also, you use an assumed shape array dummy argument

  real, dimension(:), intent(inout) :: array

不需要临时的.仅传递假定形状数组的描述符.而且,由于您的子数组是连续的,因此即使假定大小或显式大小,或假定大小为contiguous的伪参数也可以.

There is no need for a temporary. Just the descriptor of an assumed shape array is passed. And as your subarray is contiguous, even an assumed size, or explicit size, or assumed size dummy argument with the contiguous attribute would be OK.

这篇关于将数组切片作为参数传递时的Fortran性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:40