Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

technical question

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
DE
Hi to every body,

Sorry if my question is a little strange.

In writing a subroutine or function, I am wondering whether it is important that we specify the input exactly or not?
I mean which one is faster? Does it actually have any difference?

My program do this kind of things a lot and time saving is really important for me, it is in parallel and will take a month to complete the simulation.

e.g:
suppose in main I have:
Code:
Integer, Dimension(1:1000000) :: ls
in part of the program, I just want to use 10 of these starting from 1

So which one would be faster
Code:
n=10
Call compute(i, j, ls, n)
or
Code:
n=10
Call compute(i, j, ls(1:n), n)
or
Code:
n=10
Call compute(i, j, ls(1), n)
.
.
.
Code:
Subroutine compute(i, j, ls, n)
  Implicit None
..
  Integer, Dimension(1:*) :: ls
...

End Subroutine compute
or
Code:
Subroutine compute(i, j, ls, n)
  Implicit None
..
  Integer, Dimension(1:n) :: ls
...

End Subroutine compute

Please tell me how one should write these, does it actually differ?????????

Thank you very much.

Cheers,


 
Well, they are still other possibilities as demonstrated at the end of the present message.

But first of all, let us examine the following calling sequence :

Code:
n=10
Call compute(i, j, ls(1), n)

This one is doubtful because it passes an array element instead of a part of the array. A compiler may complain about it. In fact, it works well if you compile the subroutine "Compute" alone as an external procedure, because this was the only way in Fortran-77 and Fortran-77 remains a subset of next successive norms Fortran-90-95-2003-2008 (with very few exceptions).

So all the examples you gave are correct and equivalent in practice, even if I prefer the last subroutine definition (more precise).

There is another possibility which is more Fortran-90 oriented. All the other ones correspond to the default argument passing mode (the Fortran-77 one).

Code:
Subroutine compute(i, j, ls)
  Implicit None
..
  Integer, Dimension(1:) :: ls
...

End Subroutine compute

Or simply :

Code:
Subroutine compute(i, j, ls)
  Implicit None
..
  Integer, Dimension(:) :: ls
...

End Subroutine compute

With the calling sequence :

Code:
Call compute(i, j, ls(1:n))

The main difference with the other cases you have mentioned is the possible use of the intrinsic function SIZE within the subroutine. SIZE(ls) returns 10 in that case, even if the dimension is not explicitly passed as argument.

But the subroutine must be included into a module or must have an explicit interface in the calling procedure, just to indicate the compiler that the size must be passed as well as the array (else the function SIZE cannot work properly).


François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top