<bdo id='IvvAT'></bdo><ul id='IvvAT'></ul>
    <i id='IvvAT'><tr id='IvvAT'><dt id='IvvAT'><q id='IvvAT'><span id='IvvAT'><b id='IvvAT'><form id='IvvAT'><ins id='IvvAT'></ins><ul id='IvvAT'></ul><sub id='IvvAT'></sub></form><legend id='IvvAT'></legend><bdo id='IvvAT'><pre id='IvvAT'><center id='IvvAT'></center></pre></bdo></b><th id='IvvAT'></th></span></q></dt></tr></i><div id='IvvAT'><tfoot id='IvvAT'></tfoot><dl id='IvvAT'><fieldset id='IvvAT'></fieldset></dl></div>
    <tfoot id='IvvAT'></tfoot>

      <small id='IvvAT'></small><noframes id='IvvAT'>

      <legend id='IvvAT'><style id='IvvAT'><dir id='IvvAT'><q id='IvvAT'></q></dir></style></legend>
    1. OpenMP:嵌套并行化有什么好处?

      OpenMP: What is the benefit of nesting parallelizations?(OpenMP:嵌套并行化有什么好处?)

          <bdo id='SLXt9'></bdo><ul id='SLXt9'></ul>
          <tfoot id='SLXt9'></tfoot>
              <tbody id='SLXt9'></tbody>

            • <small id='SLXt9'></small><noframes id='SLXt9'>

            • <i id='SLXt9'><tr id='SLXt9'><dt id='SLXt9'><q id='SLXt9'><span id='SLXt9'><b id='SLXt9'><form id='SLXt9'><ins id='SLXt9'></ins><ul id='SLXt9'></ul><sub id='SLXt9'></sub></form><legend id='SLXt9'></legend><bdo id='SLXt9'><pre id='SLXt9'><center id='SLXt9'></center></pre></bdo></b><th id='SLXt9'></th></span></q></dt></tr></i><div id='SLXt9'><tfoot id='SLXt9'></tfoot><dl id='SLXt9'><fieldset id='SLXt9'></fieldset></dl></div>
              <legend id='SLXt9'><style id='SLXt9'><dir id='SLXt9'><q id='SLXt9'></q></dir></style></legend>
              • 本文介绍了OpenMP:嵌套并行化有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                据我了解,#pragma omp parallel 及其变体基本上是在多个并发线程中执行以下块,这与 CPU 的数量相对应.当有嵌套并行化时 - 并行内并行化,并行函数内并行化等 - 内部并行化会发生什么?

                From what I understand, #pragma omp parallel and its variations basically execute the following block in a number of concurrent threads, which corresponds to the number of CPUs. When having nested parallelizations - parallel for within parallel for, parallel function within parallel function etc. - what happens on the inner parallelization?

                我是 OpenMP 的新手,我想到的情况可能相当简单 - 将向量与矩阵相乘.这是在两个嵌套的 for 循环中完成的.假设 CPU 的数量小于向量中的元素数量,尝试并行运行内部循环有什么好处?总线程数会大于CPU数,还是内循环会顺序执行?

                I'm new to OpenMP, and the case I have in mind is probably rather trivial - multiplying a vector with a matrix. This is done in two nested for loops. Assuming the number of CPUs is smaller than the number of elements in the vector, is there any benefit in trying to run the inner loop in parallel? Will the total number of threads be larger than the number of CPUs, or will the inner loop be executed sequentially?

                推荐答案

                (1) OpenMP 中的嵌套并行:http://docs.oracle.com/cd/E19205-01/819-5270/aewbc/index.html

                (1) Nested parallelism in OpenMP: http://docs.oracle.com/cd/E19205-01/819-5270/aewbc/index.html

                您需要通过设置OMP_NESTEDomp_set_nested 来开启嵌套并行,因为很多实现默认关闭了这个功能,甚至有些实现并不完全支持嵌套并行.如果打开,每当您遇到 parallel for 时,OpenMP 将创建 OMP_NUM_THREADS 中定义的线程数.因此,如果是 2 级并行,则线程总数将为 N^2,其中 N = OMP_NUM_THREADS.

                You need to turn on nested parallelism by setting OMP_NESTED or omp_set_nested because many implementations turn off this feature by default, even some implementations didn't support nested parallelism fully. If turned on, whenever you meet parallel for, OpenMP will create the number of threads as defined in OMP_NUM_THREADS. So, if 2-level parallelism, the total number of threads would be N^2, where N = OMP_NUM_THREADS.

                这种嵌套的并行性会导致过度订阅(即繁忙线程的数量大于核心数),这可能会降低加速比.在极端情况下,嵌套并行被递归调用,线程可能会膨胀(例如,创建 1000 个线程),而计算机只会浪费时间进行上下文切换.在这种情况下,您可以通过设置omp_set_dynamic来动态控制线程数.

                Such nested parallelism will cause oversubscription, (i.e., the number of busy threads is greater than the cores), which may degrade the speedup. In an extreme case, where nested parallelism is called recursively, threads could be bloated (e.g., creating 1000s threads), and computer just wastes time for context switching. In such case, you may control the number of threads dynamically by setting omp_set_dynamic.

                (2) 矩阵向量乘法示例:代码如下:

                (2) An example of matrix-vector multiplication: the code would look like:

                // Input:  A(N by M), B(M by 1)
                // Output: C(N by 1)
                for (int i = 0; i < N; ++i)
                  for (int j = 0; j < M; ++j)
                     C[i] += A[i][j] * B[j];
                

                通常,由于线程的分叉/连接开销,在可能存在外循环的情况下并行化内循环是不好的.(尽管许多 OpenMP 实现预创建线程,但它仍然需要一些将任务分派到线程并在 parallel-for 结束时调用隐式障碍)

                In general, parallelizing inner loops while outer loops are possible is bad because of forking/joining overhead of threads. (though many OpenMP implementations pre-create threads, it still requires some to dispatch tasks to threads and to call implicit barrier at the end of parallel-for)

                您担心的是 N <# CPU.对对对,这种情况下,加速会受限于N,让嵌套并行肯定会有好处.

                Your concern is the case of where N < # of CPU. Yes, right, in this case, the speedup would be limited by N, and letting nested parallelism will definitely have benefits.

                然而,如果 N 足够大,那么代码会导致超额订阅.我只是在考虑以下解决方案:

                However, then the code would cause oversubscription if N is sufficiently large. I'm just thinking the following solutions:

                • 更改循环结构,以便仅存在 1 级循环.(看起来可行)
                • 专门化代码:如果 N 很小,则执行嵌套并行,否则不要这样做.
                • 使用 omp_set_dynamic 嵌套并行.但是,请确定omp_set_dynamic 是如何控制线程数和线程活动的.实现可能会有所不同.
                • Changing the loop structure so that only 1-level loop exists. (It looks doable)
                • Specializing the code: if N is small, then do nested parallelism, otherwise don't do that.
                • Nested parallelism with omp_set_dynamic. But, please make it sure how omp_set_dynamic controls the number of threads and the activity of threads. Implementations may vary.

                这篇关于OpenMP:嵌套并行化有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                  • <tfoot id='lv5ZM'></tfoot>

                    <legend id='lv5ZM'><style id='lv5ZM'><dir id='lv5ZM'><q id='lv5ZM'></q></dir></style></legend>
                      <bdo id='lv5ZM'></bdo><ul id='lv5ZM'></ul>
                        <tbody id='lv5ZM'></tbody>
                        <i id='lv5ZM'><tr id='lv5ZM'><dt id='lv5ZM'><q id='lv5ZM'><span id='lv5ZM'><b id='lv5ZM'><form id='lv5ZM'><ins id='lv5ZM'></ins><ul id='lv5ZM'></ul><sub id='lv5ZM'></sub></form><legend id='lv5ZM'></legend><bdo id='lv5ZM'><pre id='lv5ZM'><center id='lv5ZM'></center></pre></bdo></b><th id='lv5ZM'></th></span></q></dt></tr></i><div id='lv5ZM'><tfoot id='lv5ZM'></tfoot><dl id='lv5ZM'><fieldset id='lv5ZM'></fieldset></dl></div>

                        <small id='lv5ZM'></small><noframes id='lv5ZM'>