Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Make intent in array be global

Stephen_W_
Novice
125 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
Valued Contributor III
125 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

4 Replies
Arjen_Markus
Valued Contributor III
126 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

Steve_Lionel
Black Belt Retired Employee
125 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.

Stephen_W_
Novice
125 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. 

Stephen_W_
Novice
125 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.

Reply