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

a question about pass multi dimension array to subroutine.

史_建鑫
Beginner
1,626 Views

Hi, I want to ask a question about pass multi dimension array to subroutine.

I know there are three ways to achieve this goal. First is the explicit-shape dummy array
the example code is below:
[fortran]
subroutine foo(data, n, m)
    integer, intent(in) :: n, m
    integer,dimension(n,m), intent(in) :: data
end subroutine foo
[/fortran]

the second is assumed-shape dummy arrays, but this method require the explicit interface of the subroutine
[fortran]
subroutine foo(data)
    integer,dimension(:,:), intent(in) :: data
end subroutine foo
[/fortran]

the third is assumed-size dummy arrays, which declare all the dimension of the array except the last dimention.
[fortran]
subroutine foo(data)
    integer,dimension(5,*), intent(in) :: data
end subroutine foo
[/fortran]

I have some question:
1. the summarize above was right?
2. In the second method, I think the compiler actually pass the dimension method in the dummy argument, just like the stdcall reference calling convention, am i right?
3. How does the third method achieve the goal? I thought the compiler knew the total size of the array, the last dimension was calculated by division. am i right?
4. if the 3rd is right, how does the compiler know the total size of the array, especially when the subroutine is not explicit.

0 Kudos
3 Replies
TimP
Honored Contributor III
1,626 Views

In your last example, you don't show any way to know the array size either at compile or run time.  There would be relatively limited possibilities in how you could operate on the array.

In the first example, if you don't have bounds checking or whole-array assignment, it may make no difference whether you declare dimension(n,m) or dimension(n,*).  In Fortran 90, the * is called assumed size (it's assumed that the array is made sufficiently large by the caller).  You would not be able to use a whole-array operation such as data = 2 * data, but you could use data(:,:m) = 2 * data(:,:m)

0 Kudos
mecej4
Honored Contributor III
1,626 Views

I know there are three ways to achieve this goal.
You have not stated the goal. Is it just to pass an array, or to be able to know all the attributes of the array in the subroutine? I cannot make sense of "actually pass the dimension method in the dummy argument".
3. ... 4. if the 3rd is right, how does the compiler know the total size of the array
It does not know the size. That is why subroutines that take array arguments need to be also provided arguments or access to COMMON or module variables that contain this information.

0 Kudos
史_建鑫
Beginner
1,626 Views

mecej

Thank you for your help!

1. I apologize, what i mean is " I think the compiler actually pass the dimension infomation in the dummy argument, just like the stdcall reference calling convention"

2. the goal is to pass an array, and use it in the subroutine

0 Kudos
Reply