如果共享共享,OpenMP会将堆栈变量或数据变量移到堆中吗? [英] Does OpenMP move stack or data variables to the heap if they are shared?

查看:185
本文介绍了如果共享共享,OpenMP会将堆栈变量或数据变量移到堆中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看这本OpenMP入门视频系列,演示者不断重复说堆是共享的,堆栈是私有的".还提到了数据和文本区域是共享的.但是,他给出了一些示例,其中显然共享了父线程的堆栈变量,并且他一直将这些变量称为在堆上".这是一个示例:

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

他声称变量indexcount是在堆上". index不是在父线程的堆栈上吗? count是不是静态变量,因此不是数据区域的一部分?在我自己的OMP程序中,如果我打印这些变量的地址,它们似乎分别位于堆栈和数据区域中.到目前为止,他并不是唯一将变量称为堆上"的变量,据我所知,这些变量是 shared ,但 not 在堆上.我只是想确保我对OMP的工作方式没有遗漏.

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规范第182页

OpenMP 4.5 Specification p.182

推荐答案

措辞(堆栈/堆)不精确

OpenMP标准没有引用 1 在其内存模型中进行堆栈.因此,我将通过定义的实现回答您的问题.如果共享共享变量,我认为没有理由将栈变量的数据移到堆中.如果您通过查看共享变量的指针看到它们在堆栈中,那么我实际上会相信这一点.

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.

即使C标准也不包含对堆栈或堆的引用.

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.

我推测Tim Mattson使用堆栈/堆是因为它们可能更广为人知.

I speculate that Tim Mattson uses stack/heap because they may be more broadly known.

1 OpenMP允许控制线程的堆栈大小,但没有进一步的参考,堆栈是什么.

即使您考虑简化:

  • 自动存储持续时间==堆栈"
  • 已分配/动态 2 存储持续时间==堆"
  • automatic storage duration == "stack"
  • allocated/dynamic2 storage duration == "heap"

您的分析是正确的,引用适用.根据2.15.1.1,对于在构造内部的作用域中未声明的具有自动存储持续时间的对象,没有预定的数据共享规则.因此,它是一个具有隐式确定的数据共享属性

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令人困惑的是,C标准使用分配的存储持续时间",而C ++ 和OpenMP使用动态存储持续时间".

2 Confusingly, the C standard uses "allocated storage duration" while C++ and OpenMP use "dynamic storage duration".

这篇关于如果共享共享,OpenMP会将堆栈变量或数据变量移到堆中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆