Does OpenMP move stack or data variables to the heap if they are shared?
I'm watching this Introduction to OpenMP series of videos, and the presenter keeps repeating that "heap is shared, stack is private". It is also mentioned that data and text areas are shared. However he gives examples where stack variables of the parent thread are obviously shared and he keeps referring to those variables as being "on the heap". Here's an example:
https://youtu.be/dlrbD0mMMcQ?t=2m57s
He claims that variables index
and count
are "on the heap". Isn't index
on the stack of the parent thread? Isn't count
a static variable, and so part of the data area? In my own OMP programs, if I print the address of such variables they appear to be on the stack and data area, respectively. This is not the only place so far that he has referred to variables as "on the heap" that from what I can tell, are shared, but not on the heap. I just want to make sure I'm not missing something about the way OMP works.
Reading the specification, the clearest statement I could find of this for "implicit sharing" of stack variables outside the parallel region:
For constructs other than task generating constructs or target constructs, if no default clause is present, these variables reference the variables with the same names that exist in the enclosing context
OpenMP 4.5 Specification p.182
The wording (stack/heap) is imprecise
The OpenMP standard makes no reference1 to stack or heap in it's memory model. So I would answer your question with implementation defined. I see no reason to move the data of stack variables to the heap if they are shared. If you see by looking at pointers of shared variables, that they are on the stack, I would in fact believe that.
Even the C standard contains no reference to either stack or heap.
In my opinion you should not use the concepts that aren't part of the standards (and hence depend on implementation) to reason about correctness. Instead use storage duration and scopes.
I speculate that Tim Mattson uses stack/heap because they may be more broadly known.
1 OpenMP allows control of the stacksize for threads, but with no further reference what the stack is.
The particular example you pointed out is wrong
even if you consider the simplification:
Your analysis is correct, the citation applies. As per 2.15.1.1, there is no predetermined data-sharing rule for objects with automatic storage duration that is not declared in a scope inside the construct. Hence it is a variable with implicitly determined data-sharing attributes.
2 Confusingly, the C standard uses "allocated storage duration" while C++ and OpenMP use "dynamic storage duration".
链接地址: http://www.djcxy.com/p/13908.html上一篇: 程序堆栈和堆,它们是如何工作的?