Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

calling the subroutine expecting multi-dimension array with the address of one-dimension array

liuhuafei
Beginner
683 Views

Hello!

    I write a subroutine setrx and setix expecting a multi-dimensional array.
When they are called in main code, an address of a location in a one dimensional array
can be passed to them.  It works well.

   But After I put the setrx and setix in a interface, calling the setx with the address
of one-dimension array will lead compile errors.

  I  need the help. Thanks in advance.


!#########################################################
program main
!#########################################################
implicit none

interface setx
  subroutine setrx(m,n,x)
  integer,intent(in) :: m
  integer,intent(in) :: n
  real,intent(out)   :: x(m,*)
  end subroutine setrx

  subroutine setix(m,n,x)
  integer,intent(in) :: m
  integer,intent(in) :: n
  integer,intent(out):: x(m,*)
  end subroutine setix
end interface setx

real    :: rx(10),rxx(3,10)
integer :: ix(10),ixx(4,10)

call setrx(1,10,rx)
call setix(1,10,ix)
call setrx(3,10,rxx)
call setix(4,10,ixx)
!
!  the following calls will lead two errors
!
! error #6285: There is no matching specific subroutine for this generic subroutine call.   [SETX]
! ===================================================
call setx(1,10,rx) 
call setx(1,10,ix)
! ===================================================
!
!  the following calls are ok
!
call setx(3,10,rxx)
call setx(4,10,ixx)
!
end
end program main

!=====================================================
subroutine setrx(m,n,x)
!=====================================================
implicit none
integer,intent(in) :: m
integer,intent(in) :: n
real,intent(out)   :: x(m,*)

integer :: i,j

do j=1,n
  do i=1,m
   x(i,j) = 1.0*i*j
  end do
end do
end subroutine setrx

!=====================================================
subroutine setix(m,n,x)
!=====================================================
implicit none
integer,intent(in) :: m
integer,intent(in) :: n
integer,intent(out):: x(m,*)

integer :: i,j

do j=1,n
  do i=1,m
   x(i,j) = i*j
  end do
end do
end subroutine setix

0 Kudos
3 Replies
mecej4
Honored Contributor III
683 Views

There is another important point at play here than passing a 1-D array or scalar in place of a 2-D dummy argument: overlaid module procedures are called using their generic name. For this reason, the compiler must find a specific subroutine by matching arguments, and it finds none that takes a 1-D array argument.

One way of making the code acceptable to the compiler is to add specific subroutines, say setix1d and setrx1d, that take a 1-D array, of the appropriate type, as the first argument.

0 Kudos
Simon_Geard
New Contributor I
683 Views

The point of interfaces is to detect such a situation so you can be happy in the knowledge that the compiler is doing the right thing.

If you put your 'set' subroutines in a module you could create specific versions for 1 and 2 d arrays and use module procedure to define a generic interface (as suggested above).

In this particular case you might want to consider doing the array initialization directly:

[fortran]

x = reshape([((i*j,i=1,m),j=1,n)],[m,n])

[/fortran]

 

 

0 Kudos
liuhuafei
Beginner
683 Views

The purpose of code is cast a address of one-dimension array into multi-dimension array in subroutine.

In subroutines the multi-dimension array will be readable and easy to be manipulated.

For example;

I define a large one-dimension array, F(1000).  I wan to use F(200:499) as a three-dimension coordinates array.

So I'd like to cast the F(200) into a multi-dimension array of  x[3,100] when subroutines setx is called.

call setx(3,100,F(200))

In subroutines setx, the X(3,100) will be readable.

 

 

0 Kudos
Reply