gpt4 book ai didi

parallel-processing - OpenMP 线程私有(private)和私有(private)之间的区别

转载 作者:行者123 更新时间:2023-12-03 14:03:11 28 4
gpt4 key购买 nike

我正在尝试使用 OpenMP 并行化 C 程序。

我想了解更多:

  • 之间的区别线程私有(private) 指令和 私有(private) 子句和
  • 在这种情况下,我们必须使用它们中的任何一个。

  • 据我所知,区别在于 的全局范围线程私有(private) 以及跨平行区域的保留值。我在几个示例中发现,当一段代码包含一些必须私有(private)化的全局/静态变量时,这些变量包含在 中。线程私有(private) 使用 将列表及其初始值复制到私有(private)副本中复制 .

    但是,是否有任何规则阻止我们使用 私有(private) 处理全局/静态变量的子句?也许有任何实现细节?

    我在 OpenMP3.0 规范中找不到任何解释。

    最佳答案

    您必须记住的最重要的区别:

  • 一个 private变量是一个区域的本地变量,大部分时间都放在 上。堆栈 .变量隐私的生命周期是数据范围条款定义的持续时间。每个线程(包括主线程)都制作原始变量的私有(private)副本(新变量不再与原始变量存储相关联)。
  • 一个 threadprivate另一方面,变量最有可能放置在 中。堆 或在 thread local storage (可以看作是线程本地的全局内存)。一个 threadprivate变量跨区域持续存在(取决于某些限制)。主线程使用原始变量,所有其他线程制作原始变量的私有(private)副本(主变量仍然与原始变量存储关联)。


  • 还有更棘手的区别:
  • 变量定义为 private在进入构造时为每个线程未定义,并且在退出并行构造时未定义相应的共享变量; private 的初始状态指针未定义。
  • 但是 threadprivate 中的数据除非 copyin 进入第一个并行区域,否则应假定公共(public) block 未定义。条款被指定。当 threadprivate 中出现公共(public) block 时指令,每个线程副本在第一次使用之前初始化一次。
  • OpenMP Specifications (第 2.14.2 节)实际上对 threadprivate 给出了很好的描述(也更详细)。指示:

    Each copy of a threadprivate variable is initialized once, in the manner specified by the program, but at an unspecified point in the program prior to the first reference to that copy. The storage of all copies of a threadprivate variable is freed according to how static variables are handled in the base language, but at an unspecified point in the program.

    A program in which a thread references another thread’s copy of a threadprivate variable is non-conforming.

    The content of a threadprivate variable can change across a task scheduling point if the executing thread switches to another task that modifies the variable. For more details on task scheduling, see Section 1.3 on page 14 and Section 2.11 on page 113.

    In parallel regions, references by the master thread will be to the copy of the variable in the thread that encountered the parallel region.

    During a sequential part references will be to the initial thread’s copy of the variable. The values of data in the initial thread’s copy of a threadprivate variable are guaranteed to persist between any two consecutive references to the variable in the program.

    The values of data in the threadprivate variables of non-initial threads are guaranteed to persist between two consecutive active parallel regions only if all the following conditions hold:

    • Neither parallel region is nested inside another explicit parallel region.

    • The number of threads used to execute both parallel regions is the same.

    • The thread affinity policies used to execute both parallel regions are the same.

    • The value of the dyn-var internal control variable in the enclosing task region is false at entry to both parallel regions.

    If these conditions all hold, and if a threadprivate variable is referenced in both regions, then threads with the same thread number in their respective regions will reference the same copy of that variable.

  • 关于parallel-processing - OpenMP 线程私有(private)和私有(private)之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18022133/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com