Li-Pin Juan
Li-Pin Juan

Reputation: 1245

Difference in execution time between the uses of function SPREAD and a plain do-loop

I want to investigate the difference in execution time between a plain do loop and function spread. I asked each method to refill a long array with an identical shorter array repetitively. The code for this purpose is shown in the text below. However, I kept receiving an error message indicating the presence of stack overflow. Could anyone let me know what is wrong with my code? Thanks,

Lee

... 
integer, dimension(:), allocatable :: long_ray,short_ray 
integer, dimension(:,:), allocatable :: box_ray          
integer :: i, long1, short1
long1 = 100000000
short1 = 100

allocate(long_ray(long1),short_ray(short1),box_ray(short1,long1/short1))
long_ray = [((i),i=1,long1)]
short_ray = [((i),i=1,short1)]
call system_clock(t1)
do i = 1,long1/short1
    long_ray((i-1)*short1+1:i*short1) = short_ray(:)
enddo
call system_clock(t2)
print*,'1 time: ',t2-t1

call system_clock(t1)
box_ray = spread(short_ray,2,long1/short1)
long_ray = reshape(box_ray, shape(long_ray))
call system_clock(t2)
print*,'2 time: ',t2-t1  
...

Edit to add: Well, I figured out why my program did not work (on my desktop with Intel Visual Fortran 2013 in Visual Studio 2012). The error is caused by my omission of deallocating one of the arraies.

After I added long_ray into the line that I didn't include in my code snippet: deallocate(int_array,short_array,box_ray), the program finally worked.

By the way, I found that, when the size of long1 doens't exceed 10^8, the execution time of the first code block (the plain do-loop) is far shorter than that of the block of function SPREAD. May I claim that do-loop is superior to SPREAD in refilling a long sequence?

Upvotes: 0

Views: 120

Answers (2)

The program worked for me in gfortran, but doesn't in Intel Fortran. You can force ifort to allocate arrays on the heap by using -heap-arrays n where n is a limit in kB. I use 200. It can make some computations slower, you have try and measure.

Upvotes: 2

IanH
IanH

Reputation: 21451

Your program overflows its stack because you don't have enough stack space.

Compilers often put the temporary objects associated with expression evaluation on the stack. There are a number of such expressions in your program, including the array constructors immediately after the allocate and the spread and reshape references later on. Given the value of long1 and the typical size of an integer - some of your expressions might require ~0.4 GB of stack space. That's a lot, particularly relative to the default stack size limit on one major platform of only 1 MB.

If you reduce the size of long1 and make sure that your program has a sufficient stack allocation, your code snippet works for me.

Edit to add: Be mindful that a smart optimiser may note that the arrays being defined are not being used - and hence not bother executing the code that defines them.

Upvotes: 1

Related Questions