Fortran - compilator dependent, intrinsic array manipulation, memory problem

Click For Summary
SUMMARY

The discussion focuses on a Fortran program that encounters a segmentation fault when performing cyclic rotation on large dynamically allocated arrays using the cshift function. The issue arises specifically when the program is compiled with gfortran, while ifort v12.0.0 does not produce the same error. Increasing the stack memory limit can prevent the segmentation fault, indicating that the cshift function may be creating temporary arrays that exceed stack limits. The discussion emphasizes the importance of understanding compiler-specific behavior regarding memory allocation.

PREREQUISITES
  • Understanding of Fortran programming, specifically array manipulation
  • Familiarity with dynamic memory allocation in Fortran
  • Knowledge of compiler options and their impact on memory management
  • Experience with debugging tools in Fortran, such as -check all
NEXT STEPS
  • Research how to control temporary array creation in Fortran, focusing on stack vs. heap allocation
  • Learn about increasing stack size limits in different operating systems
  • Explore the differences in memory management between gfortran and ifort
  • Investigate alternative methods for cyclic rotation without using cshift
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with large datasets and dynamic memory allocation, as well as anyone troubleshooting segmentation faults in their Fortran applications.

lacek
Messages
2
Reaction score
0
Welcome all,

I have a following code:

Code:
program aaaa
implicit none 

double precision, dimension(:), allocatable :: inp,outp
integer ::n,i

read(*,*) n

allocate(inp(n),outp(n))

write(*,*) allocated(inp), allocated(outp)

write(*,*) inp(1)
inp=cshift(outp,-size(outp)/2)
write(*,*) inp(1)

end program aaaa

It really does nothing - it just applies cyclic rotation of dynamically created array.
But when I use is for large n (in my case - over 10^6) then Segmentation fault occurs. More precisely this is the output:

Code:
T T
  0.000000000000000E+000
Segmentation fault

I also discovered that by increasing the limit for stack memory the segmentation fault can be avoided. Clearly I am using cshift not appropriately.

Additionally the code perform does not exists when the compilation is done with gfortran, but it exists with ifort v12.0.0.

What is going on? I tried to compile with -check all, but it provides no info.
 
Technology news on Phys.org
I guess cshift() is creating a temporary array somewhere. That seems a reasonable thing for it to do, since it function doesn't "know" you are just assigning its output to another variable. For example your code might say something like
Code:
inp=(cshift(outp,1) + cshift(outp,2))/2

I think you need to find out if you can control where temporaries are created (on the stack or the heap) and/or how to make the stack big enough. Those things are compiler-dependent, which is why the defaults "work" for one compiler but not for the other.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
13K