user12315211
user12315211

Reputation: 45

Invoking a subroutine where the argument is pointer alias

I am not sure whether this is legal according to Fortran Standard. I have a subroutine which I am calling through overloading. The point here that sometimes I may call the subroutine where I have pointer alias to real. Please have a look at this complete code.

module mpi_intf_exam
use mpi

interface GLB_SUM
module procedure GLB_SUM_INT
module procedure GLB_SUM_FLT
module procedure GLB_SUM_FLT_INPLACE
end interface 
integer :: mpierr

contains 


subroutine GLB_SUM_INT(buf, buf_out, n)
implicit none 
integer, intent(in) :: n
integer, intent(in)   :: buf(n)
integer, intent(out) :: buf_out(n)

call mpi_allreduce( buf, buf_out, n, MPI_INTEGER, MPI_SUM, MPI_COMM_WORLD, mpierr)

end subroutine 

subroutine GLB_SUM_FLT(buf, buf_out, n)
implicit none 
integer, intent(in) :: n
real, intent(in)   :: buf(n)
real, intent(out) :: buf_out(n)

call mpi_allreduce( buf, buf_out, n, MPI_REAL, MPI_SUM, MPI_COMM_WORLD, mpierr)

end subroutine 


subroutine GLB_SUM_FLT_INPLACE(buf, n)
implicit none 
integer, intent(in) :: n
real, intent(inout)   :: buf(n)


call mpi_allreduce( MPI_IN_PLACE, buf, n, MPI_REAL, MPI_SUM, MPI_COMM_WORLD, mpierr)

end subroutine 



end module 

program test_gather

use mpi_intf_exam

implicit none

integer :: rank, ierr, nranks, n, i
real, allocatable, target :: bufs(:),  bufr(:)
real, pointer :: ptr(:)
call mpi_init(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nranks, ierr)

n = 10
allocate(bufs(n))

ptr => bufs

call random_number(ptr)
ptr = ptr*(rank+1)
print*, sum(bufs), rank, "BEF"
call GLB_SUM(ptr,n) !

print*, sum(bufs), rank
end program

My call call GLB_SUM(ptr,n) is meant to call the routine GLB_SUM_FLT_INPLACE. But as you see this subroutine has real dummy argument, while I am invoking it with a real pointer. It works on IFORT V19.0.5 for this specific example. But is it valid? I could not find what the standard would say about such call.

Upvotes: 3

Views: 111

Answers (2)

francescalus
francescalus

Reputation: 32451

It is legal to use a pointer in this way.

When a pointer is used in a procedure reference to correspond to a non-pointer dummy argument, the pointer's target is taken to be associated as the actual argument. The pointer itself must be pointer associated. For Fortran 2018 you can see 15.5.2.3.

In the case of the question, each specific subroutine for the generic GLB_SUM has no pointer argument.

Upvotes: 3

There is no problem passing a pointer to a non-pointer dummy argument. Fortran does not require any dereferencing you may know from other programming languages. The subroutine just receives the array that is the target of the pointer. It is OK.

Upvotes: 2

Related Questions