How to set openmp thread stack to unlimited?

2019-06-11 06:02发布

问题:

Can someone tell me how to set OpenMP stack size to unlimited?
Like this link: Why Segmentation fault is happening in this openmp code?

I also have a project written by Fortran (customer‘s complex code), if I set OMP_STACKSIZE, the project is running normally. If I unset it, the project fails.

But, different input data have different OMP_STACKSIZE, so I must try it for each inputdata, (because I must save memory).

Can I set the OpenMP stack like pthread (ulimit -s unlimited)? Or have some way to set omp stack size dynamically?

I'm using RHEL 6.1, and the Intel compiler.

Thanks a lot!

回答1:

There is big difference between how the stacks of the main thread and of the worker threads are implemented.

The "unlimited" stack of the main thread starts at the highest virtual address available in user mode and grows downwards until it meets the program break (the end of the data segment) or hits another memory allocation (either named or anonymous mapping) at which point the program crashes.

Any additional stacks have to be placed somewhere in memory between the program break and the bottom of the main stack. They cannot have an arbitrary extendible length since their initial placements (i.e. the distance between their beginnings) determines their maximum sizes (and vice versa - the specified maximum sizes determine their initial placement). This is the reason why the Linux implementation of pthread_create(3) (used by virtually all OpenMP runtimes in order to create new threads) states:

On Linux/x86-32, the default stack size for a new thread is 2 megabytes. Under the NPTL threading implementation, if the RLIMIT_STACK soft resource limit at the time the program started has any value other than "unlimited", then it determines the default stack size of new threads. Using pthread_attr_setstacksize(3), the stack size attribute can be explicitly set in the attr argument used to create a thread, in order to obtain a stack size other than the default.

In other words, the answer is no - you cannot specify unlimited stack size for threads other than the main one.