gfxgfx
 
Please login or register.

Login with username, password and session length
 
gfx gfx
gfx
1617 Posts in 535 Topics by 779 Members - Latest Member: rhoronjeff@comcast.net November 27, 2022, 09:27:45 PM
*
gfx* Home | Help | Search | Login | Register | gfx
gfx
Absoft User Forum  |  Support  |  General  |  Large Arrays, Stacks and Heaps
gfx
gfxgfx
 

Author Topic: Large Arrays, Stacks and Heaps  (Read 5656 times)

SarahC

  • Newbie
  • *
  • Posts: 4
Large Arrays, Stacks and Heaps
« on: January 14, 2011, 09:58:44 AM »
I have a program with many large arrays based on the size of a DEM (digital elevation model). The original configuration allocated all maximum size arrays at the beginning of the program after reading in the required size. All the large arrays are contained in modules. One subroutine which is called multiple times, acts on smaller subsets of those arrays, and additional automatic arrays that are needed to process the subsets are created within that subroutine.  For very large problems, I get a stack overflow crash. My questions are about the optimal way to address this issue in terms of prgram speed and memory usage. The program will eventually be used by different users on different platforms. In my understanding, these are the ways I can address this and the associated issues:

1. Increase the stack size: This will be problem dependent and may not be a very robust approach considering multiple users with very different input. If I do maximize the stack, is all that memory set aside, or is it used on an as needed basis, or is that platform/machine dependent?

2. Allocate all needed subroutine arrays to maximum DEM size at the same time as all the larger arrays near the start of the program. This is my current approach and seems to work well so far, but I am not sure how whether even larger DEMs may eventually cause an issue.

3. Use the "save" options for the automatic arrays within the subroutine. My question with this is how this works when the arrays are dimensioned to different ranges (within the total DEM range) each time the subroutine is called. Will it end up taking up the same amount of space as the full DEM range in the end, so is not a savings on memory usage and I may as well allocate that up front?

4. Allocate and deallocate arrays within the subroutine. I am assuming this would be a huge performance hit.

Thanks so much for any help you can offer!

forumadmin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 333
Re: Large Arrays, Stacks and Heaps
« Reply #1 on: January 14, 2011, 11:02:12 AM »
Increasing the stack size varies on each platform.  On Linux, you do it with the ulimit shell command and it applies only to the shell session.  Usage is "ulimit -s n" where n is the required stack size in KB. So "ulimit -s 32000" would get you about a 32 MB stack. You can ask for an unlimited stack with "ulimit -s unlimited". So Linux distributions will not allow an unlimited stack.

On OS X, you use the same command from the Terminal. However, the maximum is 32 MB for a user and 64 MB for a superuser. The OS X linker does support setting the stack from the load image (executable).  The option from the Absoft driver is -stack:n, where n is the size of the stack in bytes.

On Windows, the linker can be used to set the stack. The option from the Absoft driver is -stack:n, where n is the size of the stack in bytes.

On both OS X and Windows, the stack can be set from the Target page of AbsoftTools.

--

Automatic arrays cannot be saved. They are allocated on the stack.

ALLOCATE/DECALLOCATE is less efficient than static arrays and about the same as automatic arrays. But not terribly so. A lot depends on your indexing and rank characteristics. I would benchmark both cases using code that is representative of what your actual code looks like.

SarahC

  • Newbie
  • *
  • Posts: 4
Re: Large Arrays, Stacks and Heaps
« Reply #2 on: January 14, 2011, 11:23:16 AM »
Thanks for your reply! To be absolutely clear, by automatic arrays, I mean that I pass the dimensions for these two-dimensional arrays (i1,i2,j1,j2) as arguments and declare the subroutine arrays with those dimensions. "Save" does not work for those arrays? That is good to know.

Do you have an opinion on relative pros and cons of declaring DEM-sized arrays in the beginning of the program versus smaller allocated or automatic (assuming appropriate stack size) array declarations on each subroutine call? The subroutine can be called on the order of hundreds of thousands of times or more depending on the problem.

Also, how does the stack work in terms of entering and exiting the subroutine? Is the memory freed on exit? I have examples that run for quite a while with similar sized subroutine arrays and then crash at some point, and I was wondering if it was a problem with access to contiguous memory based on prior usage from previous subroutine calls.

Thanks again for your help.

forumadmin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 333
Re: Large Arrays, Stacks and Heaps
« Reply #3 on: January 14, 2011, 11:51:52 AM »
That sounds like an automatic array. Like this:

      call sub(100,200)
      ....
      subroutine sub(nr,nc)
      real a(nr,nc)

This is an automatic array. It is created on entry and destroyed on exit. All stack space is recovered.

Are these 1 meter DEMs or less dense? What is the area you are working with? If the area is constant for each run and only the geographic area changes, I would simply allocate them statically with a SAVE statement. That will move them from the stack to the heap and allow for efficient code generation. In particular, execution would benefit from auto-vectorization (-O3 and above) and auto-parallelization (-apo or -O5).

SarahC

  • Newbie
  • *
  • Posts: 4
Re: Large Arrays, Stacks and Heaps
« Reply #4 on: January 14, 2011, 12:23:27 PM »
The cell size and area changes with every problem, so static dimensioning is not a possibility. It is a landslide program that takes different sized "scoops" out of the terrain based on user-defined area and/or volume constraints.

Absoft User Forum  |  Support  |  General  |  Large Arrays, Stacks and Heaps
 

gfxgfx
gfx gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!