I have a subroutine that populates a rank 1 array of arbitrary length.
subroutine populate_array_1D(array)
implicit none
integer, dimension(:), intent(out) :: array
array(:) = 1
end subroutine populate_array_1D
I now want to modify this routine using overloading to accept higher-dimensioned arrays.
interface populate_array
module procedure populate_array_1D, populate_array_2D, populate_array_3D
end interface populate_array
What I want to do is to refer the higher-dimensioned subroutines back to the 1D subroutine.
My ideas so far:
Slice the array, and pass the slices one-by-one:
subroutine populate_array_2D(array) implicit none integer, dimension(:,:), intent(out) :: array integer :: i do i = lbound(array, 2), ubound(array, 2) call populate_array_1D(array(:, i)) end do end subroutine populate_array_2DCreate a new, automatic 1D array, and then use
reshape:subroutine populate_array_3D(array) implicit none integer, dimension(:,:,:), intent(out) :: array integer :: tmp_array(size(array)) call populate_array_1D(tmp_array) array = reshape(tmp_array, shape(array)) end subroutine populate_array_3D
Assume that, like in this example, the result of either version will be okay, I'm only worried about the performance. (I should probably add that I can't just make an elemental subroutine because it can't be pure.)
Or is there an even better version, where I can pass a multidimensional array into a subroutine pretending it's a 1D array?
(Ultimately I want to replace the call to RANDOM_NUMBER with a PRNG that is as deterministic as possible across different compilers and/or machines, assuming they use the same seed.)
impure elemental. Thanks @francescalus