生成的任务多于线程数 [英] Generating more tasks than there are threads

查看:83
本文介绍了生成的任务多于线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几本OpenMP教程中读到,您所生成的任务不应多于线程数. 例如:不要启动超过有可用的线程,这意味着在封闭的并行区域中可用."

I read in several OpenMP tutorials that you should not generate more tasks than there are threads. For example: "Do not start more tasks than there are available threads, which means available in the enclosing parallel region."

假设我们要遍历二叉树,可以并行遍历节点的子树,并且我们的机器具有四个核心.按照上面的建议,我们在根目录生成两个任务,一个任务在左侧,另一个任务在右侧子树.在这两个任务中,我们生成两个嵌套任务,每个子树一个.现在,我们有四个任务,因此我们不再将它们分开.

Assume that we want to traverse a binary tree, that the subtrees of a node can be traversed in parallel, and that our machine has four cores. Following the advice above, we generate two tasks at the root, one for the left and one for the right subtree. Within both tasks, we generate two nested tasks, again one for each subtree. Now, we have four tasks, so we do not split them further.

但是,如果四个子树的大小不同,则某些内核将不得不等待.负载均衡继续进一步拆分并生成16个任务会更好吗?即使我们只有四个核心?

However, if the four subtrees are not of the same size, some cores will have to wait. Would it not be better for load balancing to continue splitting somewhat further and generate, say, 16 tasks? Even if we have only four cores?

通常生成一个不多于线程的任务是一个好建议吗?或者这是胡扯吗?

Is it generally a good advice to generate not more tasks then there are threads or is this nonsense?

推荐答案

您可以使用比线程更多的任务,这是任务的主要目的.是的,使用多于线程的任务将有助于负载均衡.

You can use more tasks than threads, that is the main purpose of tasks. And yes, using more tasks than threads will help will load balance.

所引用的文章对我来说似乎很关键,尽管他的批评有一些优点,但我不同意否定性.我不会深入探讨他针对OpenMP任务提出的每一个观点.是的,与任何并行性范例或C/C ++/Fortran一样,有很多方法可以使您摆脱OpenMP任务的困扰.

The referenced article seems rather critical to me and while there is some merit to his criticism, I would not agree with the negativity. I am not going to go into each and every point he makes against OpenMP tasks. Yes, there are ways to shoot yourself in the foot with OpenMP tasks, as there is with any parallelism paradigm or C/C++/Fortran in general.

在执行带有任务的树算法时,我担心的一件事是任务创建的开销.尽管任务比线程更轻量,但它们不是免费的.如果树上的叶子节点只有很少的指令,并且为每个节点创建任务,那么您将有巨大的开销.

One thing I would worry about when implementing tree algorithms with tasks is task creation overhead. While tasks are more lightweight than threads, they are not free. If you have only a few instructions your leaf nodes in the tree, and you create a task for everyone of them, you will have a huge overhead.

您可以使用omp task if (depth < theshold)进行此操作,但这仍会在更深的层中留下一些开销.为了完全避免这种情况,您必须实现遍历功能的两个版本,一个始终调用串行一个的序列号,以及一个有条件地调用任务或串行一个任务的任务号.这也可以提供更好的优化.您应该选择阈值,以使最小的task duration >> task overhead和任务数足以避免负载不平衡(取决于变化).

You can use omp task if (depth < theshold) for this, but this still leaves a little bit of overhead in the deeper layers. To totally avoid this, you have to implement two versions of the traversal function, a serial one which always calls the serial one and a task one which conditionally calls either the task or the serial one. That may also provide better optimization. You should chose the threshold such that minimum task duration >> task overhead and task count is sufficient to avoid load imbalances (depends on variation).

这篇关于生成的任务多于线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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