Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Automatic arrays on the stack:  The most efficient way to create temporary arrays is to use local arrays declared in the subroutine.  These arrays will be placed on the stack (although sometimes compiler options are needed to force larger automatic arrays to be placed on the stack).  To create an array on the stack is nearly instant, as it just requires incrementing the stack pointer.   One drawback of stack variables is that each openMP thread must have it's own stack, and this memory must be allocated ahead of time, with the OMP_STACKSIZE environment variable.  Making this too small and the code will segfault when it runs out of stack memory, and making it too large, then allocating all this stacksize when the code starts can leave insufficient memory for the rest of the model.  

Stack arrays created inside a threaded region will be thread private.  Stack arrays created outside a threaded region can be shared by threads started later on in the subroutine, and marking such arrays "private" in openMP directives will create multiple copies of the array for each thread, which may or may not be what you want.  

Heap arrays are in the global memory space and accessible by all thread.  Sharing these arrays across threads requires synchronization if threads will be changing them independently.   Marking these arrays as private in an openMP directive should never be done. 

Recommendation:

  1. Allocate dynamic arrays only at a high level and infrequently
  2. use stack arrays or for all subroutine array temporaries
  3. understand the size of these arrays in order to have a good estimate of the needed stacksize
  4. See below for similar problems with array slicing causing hidden allocation & array copies

...