Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Make intent in array be global

Stephen_W_
Novice
1,209 Views

I wonder how to make the array `array1` be global (module) variable, consider following code

module data
    implicit none
    real(8), allocatable, dimension(:,:) :: A
end module data

subroutine test(array1, n, m)
    use data
    implicit none
    integer, intent(in) :: n, m
    real(8), intent(in), dimension(n,m) :: array1
    
    allocate(A, source=array1)
    
    do_something
    
end subroutine test

`allocate(A, source=array1)` works but double store the same array, `save` is another method but not satisfied.

0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
1,209 Views

I am not entirely sure what you want to achieve, but you could define the array A to be a pointer:

module ..
     real(8), dimension(:,:), pointer :: A
end module

subroutine ...
     real(8), dimension(:,:), target :: array1

     A => array1

end subroutine

You have only one copy of the array then, but make sure that the array1 remains "alive" as long as you want to use A, while pointing to array1.

It might be better to move the allocation, via call move_alloc - array1 must be an allocatable array/pointer to an array. That way you transfer the memory to A and you only have to worry about the "aliveness" of A.

View solution in original post

0 Kudos
4 Replies
Arjen_Markus
Honored Contributor I
1,210 Views

I am not entirely sure what you want to achieve, but you could define the array A to be a pointer:

module ..
     real(8), dimension(:,:), pointer :: A
end module

subroutine ...
     real(8), dimension(:,:), target :: array1

     A => array1

end subroutine

You have only one copy of the array then, but make sure that the array1 remains "alive" as long as you want to use A, while pointing to array1.

It might be better to move the allocation, via call move_alloc - array1 must be an allocatable array/pointer to an array. That way you transfer the memory to A and you only have to worry about the "aliveness" of A.

0 Kudos
Steve_Lionel
Honored Contributor III
1,209 Views

A is already "global" - I too am not sure what you want to happen differently. As you have it, the allocate of the global array A occurs in the subroutine and the dummy argument array1 is copied into it - that's what source= does. There is no double store. What would you like to happen? Really, you don't need subroutine test at all - the ALLOCATE will do everything.

Do not use pointers here.

0 Kudos
Stephen_W_
Novice
1,209 Views

Sure, A is already global, but does not initialized, I just want dummy argument array1 be global, and the subroutine test will be called by other language. 

0 Kudos
Stephen_W_
Novice
1,209 Views

Arjen Markus wrote:

I am not entirely sure what you want to achieve, but you could define the array A to be a pointer:

module ..
     real(8), dimension(:,:), pointer :: A
end module

subroutine ...
     real(8), dimension(:,:), target :: array1

     A => array1

end subroutine

You have only one copy of the array then, but make sure that the array1 remains "alive" as long as you want to use A, while pointing to array1.

It might be better to move the allocation, via call move_alloc - array1 must be an allocatable array/pointer to an array. That way you transfer the memory to A and you only have to worry about the "aliveness" of A.

Many thanks, I have tested the pointer solution, yes, only one copy of the array.

0 Kudos
Reply