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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Large Array as a Subroutine Input/Output

Status
Not open for further replies.

insung

Technical User
Oct 1, 2007
3
KR
I have a large array which need to be allocatable due to its size.
(Allocatable memory can exeed 2GB in my machine.)
And this array need to be input and output of some subroutine.

If I declare in the subroutine
"REAL, DIMENSION:)), INTENT(INOUT), ALLOCATABLE :: array"
then error occurs.
(This array is already allocated in the main program.)

Is there any way around?
 
It seems that you're allocating it locally in stead of globally. Try:

SUBROUTINE MySubroutine(isize,array)
IMPLICIT NONE
INTEGER, INTENT(IN) :: isize
REAL, DIMENSION(isize), INTENT(INOUT) :: array
...
...
END SUBROUTINE MySubroutine
 
Either
"SUBROUTINE MySubroutine(isize,array)
IMPLICIT NONE
INTEGER, INTENT(IN) :: isize
REAL, DIMENSION(isize), INTENT(INOUT) :: array"
or
"REAL, DIMENSION:)), INTENT(INOUT) :: array"
do not work.
If I specify the size of array, it gives errors due to memory restriction.

One possible solution is convert full subroutine to main routine, but it's GREAT AMOUNT of work.
So I'm looking for another way.
 
I'm not sure, but someone told me once that Fortran copies all variables in another memory block when you pass them by means of the header of a subroutine.

So, assuming that that is true (maybe someone in this forum can confirm that) all data in ARRAY(1:ISIZE) will be copied as soon as you pass it into MySubroutine. This may cause the memory problem.

In fortran 90 a solution may be to use pointers, but I don't know how to do that, so if someone reads this...

What I personally would do, if this ARRAY(1:ISIZE) is very common data used by a lot of subroutines, is.. making a COMMON as the word already says, like that you use the memory only once. As your data is INTENT(INOUT) this seems a quick solution that needs minor changes in the code.
 
In my long gone days of FORTRAN 77, I understood that variables were passed into subroutines by reference.

Maybe it's changed these days, what with gigabytes of memory and stuff.
 
In the later versions, they may have optimized it using intent. If intent is in, then it is optional whether you pass by reference or by value. Either way, it is possible for the compiler to check whether it is a constant or not. If intent is out or inout then it has to be by reference.
 
That's interesting, I'm working often with large data as well.

So, imagine that it's INTENT(IN). How do you control that then? Whether you pass by reference or by value I mean?

Is this somewhere in the compiler options?
 
Historical notes: all Fortran versions never pass arrays by value. Moreover: Fortran does not pass args by value. Scalar args was passed by reference or by value/result (both methods are conformed with standard).
 
I'd forgotten about value/result. Last used that term 28 years ago. It is value/result I was thinking of: not value.

Apologies for misleading.
 
Sometimes it's good news if you're wrong. So I have been told wrongly. Luckily, we don't use the same data twice in the memory and we don't have to worry about it with compiler options etc, I'm happy to hear this.

Bad news however for Insung who initiated this discussion. He probably really has a lack of memory.

Regards,

Gerrit
 
Thank you for so many kind replies.
So this memory problem is nothing to do with subroutine then.
I must optimize main routine maybe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top