Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

MAXVAL Stack overflow problem

Amalia_B_
Beginner
1,053 Views

I have a large 3-dimensional array and I'm trying to do an element-by-element maximum on the first 2 dimensions using the MAXVAL function.  When I do, I get a stack overflow error.  Is there a size limit to the MAXVAL intrinsic function?  The code is abbreviated below with constants in the array declarations and allocations instead of variables just to show the size:

program main

real, allocatable :: arr2(:,:), arr3(:,:,:)

allocate( arr3( 0:1000, 1:440, 1:6 ), source = 0.0 )

allocate( arr2( 0:1000, 1:440 ), source = 0.0 )

...! assign values to arr3

!---This gives me the stack overflow error---

arr2 = maxval( arr3,dim=3)

!------------

!-----This code works of course when broken out----

do concurrent( i = 0:1000, j = 1:440 )
   arr2(i,j) = max( arr3(i,j,1), arr3(i,j,2), arr3(i,j,3), &
                    arr3(i,j,4), arr3(i,j,5), arr3(i,j,6) )
end do

!-----------------------

But I would think is much less efficient.  Is this a compiler bug?

Amalia

0 Kudos
2 Replies
Steven_L_Intel1
Employee
1,053 Views

The problem is that the compiler is constructing a temporary value for the result of maxval and it puts this on the stack by default. I suggest that you add the /heap-arrays option (Fortran > Optimization > Heap Arrays > 0) to have it used the heap for these temps.

0 Kudos
John_Campbell
New Contributor II
1,053 Views

Amalia,

You may find the problem disappear if you restructure arr3 to: allocate( arr3( 1:6, 0:1000, 1:440 ), source = 0.0 ). This would certainly improve the memory access performance.

John

0 Kudos
Reply