Fortran Fortran - compilator dependent, intrinsic array manipulation, memory problem

AI Thread Summary
The discussion centers around a Fortran program that performs a cyclic rotation on a dynamically allocated array, but encounters a segmentation fault when executed with large input sizes (over 10^6). The issue seems to arise from the use of the `cshift` function, which likely creates a temporary array that exceeds stack memory limits. Increasing the stack memory limit can prevent the segmentation fault, indicating that the problem is related to memory management. The code behaves differently depending on the compiler used; it compiles without issue in gfortran but fails in ifort v12.0.0. The conversation suggests that controlling the allocation of temporary arrays—whether on the stack or heap—may be crucial, as this behavior varies between compilers.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
2K
Replies
4
Views
2K
Replies
8
Views
3K
Replies
2
Views
3K
Replies
5
Views
8K
Replies
13
Views
3K
Replies
1
Views
4K
Replies
5
Views
13K
Back
Top