Fortran performance when passing array slices as arguments -
i fortran's array-slicing notation (array(1:n)
), wonder whether take performance hit if use them when it's not necessary.
consider, example, simple quicksort code (it works, it's not taking care pick pivot):
recursive subroutine quicksort(array, size) real, dimension(:), intent(inout) :: array integer, intent(in) :: size integer :: p if (size > 1) 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 i=1,size-1 if (array(i) < pivot) call swap(array(i), array(p)) p=p+1 end if end call swap(array(p), array(size)) end function partition subroutine swap(a, b) real, intent(inout) :: a, b real :: temp temp = = b b = temp end subroutine swap
i pass whole array along indices of recursive parts should working, code way. when call quicksort(array(1:p-1), p-1)
, however, make temporary array operate on, or make shallow reference structure or that? sufficiently efficient solution?
this question related, seems makes temporary arrays because of strided slice , explicit-sized dummy variables, i'm safe that, right?
your subarray
array(1:p-1)
is contiguous, provided array
contiguous.
also, use assumed shape array dummy argument
real, dimension(:), intent(inout) :: array
there no need temporary. descriptor of assumed shape array passed. , subarray contiguous, assumed size, or explicit size, or assumed size dummy argument contiguous
attribute ok.
Comments
Post a Comment