Allocatable array already allocated problem in Fortran

Allocatable array already allocated problem in Fortran

wronski11的头像

Dear Forum Members,

 

I am using the Intel Fortran Compiler Version 11.1, the code I am working with uses extensively memory allocation. What I am trying to do is run the code in a loop. Te problem is that this code allocates memory but some of the arrays are not deallocated. It has a fortran STOP in the end which frees the memory, when the code finishes. However if I put a loop around it the code tries to allocate arrays which are already allocated. One can check this issue quite conveniently whith the following command:

If (allocated(array(size)))  deallocate (array)

allocate(array)

This is however quite difficult to implement because I have about 800 places where the allocation is done.

In principle if the arrays are allocated inside a subroutine and they are not declared with “SAVE” after the RETURN statement in the end, they are deleted. However there are many places with SAVE statement in my code.

 My question is how can I check precisely where this double allocation is taking place. The code tells me just that I have double allocation error, but where exactly and which array is causing the problem is not mentioned. I hope that this can be solved with compiler flags. I have also license for the Visual Fortran compiler, in case necessary I am able to switch to it.

I would appreciate any ideas how to overcome this problem.

Thank you in advance

4 帖子 / 0 new
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项
wronski11的头像

I think that I have figured something out. I can use the “ –traceback “ option to see where the fatal errors occur. However I would appreciate if someone have a better idea.

mecej4的头像

Quote:

The code tells me just that I have double allocation error, but where exactly and which array is causing the problem is not mentioned.

If you compiled with /traceback, you already know "where exactly" in terms of routine and source line number.

In an EXE compiled in "release" mode, variable names do not get stored, so if you have multiple variables in a single ALLOCATE statement the run-time cannot tell you the name of the individual variable which caused the problem. Nor am I sure that knowing the name of the variable would help you much, since that variable may have been allocated and deallocated a number of times already. Even if you break up the ALLOCATE statement into multiple statements each of which allocates one variable, locate the one that caused an error and "fix" that particular instance by writing IF(.NOT.ALLOCATED(varname))ALLOCATE(varname(..)), there is no immunity from further errors arising from one of the other variables.

Some compilers enable producing a log file of allocations and deallocations, but with your 800 places where allocation is done perusing the log file may take "forever".

Ultimately, planning and managing allocations that one chooses to do with ALLOCATE/DEALLOCATE is something the programmer has to do.

wronski11的头像

Hi thanks for the reply, I figured out the “-traceback” option and found which arrays are causing the problem. I am quite happy now because I can execute the code in sequential mode. Actually as far as I understand it the “-traceback” will show all severe errors, and exactly where they appear.
I think that I solved my problem. Thanks for the help

登陆并发表评论。