CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   Question for Fortran experts (

jojo September 26, 2006 13:47

Question for Fortran experts
Hi everybody,

I have a question which, curiously, is not addressed in recent Fortran handbooks (maybe I have missed the single-line remark in the 1000-pages books I have read...):

What is best for optimization?

1) allocate dynamic arrays in a subroutine (i.e., arrays will be allocated/de-allocated each time the routine is called)

2) pass working arrays as arguments from the main program, as it used to be in former versions of Fortran.

I prefer solution 1) as it improves the readiness of the code but I am not sure it is the most efficient one from a computational point of view.


opaque September 26, 2006 14:21

Re: Question for Fortran experts
Dear jojo..

The answer is language independent..

Try allocating/freeing memory within a long loop and your program will crawl to a halt.. In particular, if the chunk sizes changes within the loop..

If your routine is called a few times.. Go for 1.. If your routine is called many times, you are better of going with 2..

Take a finite element assembly routine that is called for every element.. You could be talking about millions of calls.

Not a definite answer, but hope it helps,


Arek September 26, 2006 15:27

Re: Question for Fortran experts

IMHO the better way is allocating arrays only once and passing them as an argument or using modules. It's probably true for any language. See "Matlab guide" by D. and N. Higham, chapter "Optimizing M-files". Of course it depends what is your primary goal speed or memory usage optimization. Regards Arek

Renato. September 26, 2006 15:35

Re: Question for Fortran experts
The best for optimization is usually the simplest form, option 2, static arrays in the main program being passed as arguments for subroutines, but, in my opinion, it's only useful for academic purposes nowadays since it's hard to keep a large program that is supposed to run several different cases with distinct memory requirements. In my case, I try to group the biggest and more used arrays in few modules (2 at most) and the memory allocation is done once whithin a "MemoryManagement" routine at the beginning of the program to diminish memory trash and fragmentation effects. Of course, whenever possible I reuse some of these arrays for other purposes to avoid new allocations.



Mani September 27, 2006 07:09

Re: Question for Fortran experts
Let's repeat it one more time :)

Allocate your arrays in a subroutine only if the arrays are not huge and the subroutine is not the one limiting the performance of your program.

For efficiency, option 2 is preferred because allocating (searching for free memory) takes time. Now you still have two options: pass the array as argument, or use a module (common block in legacy Fortran). You can make your subroutine more generic by using arguments, and I am not sure that the access of memory through a module is any more efficient than that. If the array in question is used in other subroutines as well, and the subroutine is written to work exclusively on this global array, a module would be the convenient choice.

jojo September 27, 2006 08:30

Re: Question for Fortran experts
Well! You answered my question. Thanks a lot!

All times are GMT -4. The time now is 23:41.