简体   繁体   English

在线程池中执行的OpenMP代码

[英]OpenMP code executed in thread pool

I'm thinking about a design were a thread pool will execute code blocks, which may contain OpenMP statements (parallel for mostly). 我在考虑一个设计,一个线程池将执行代码块,其中可能包含OpenMP语句(大多数情况下是并行的)。 (Similar to: How to deal with OpenMP thread pool contention I guess). (类似于:我猜想如何处理OpenMP线程池争用 )。 My question is if it will cause problems or lead to bad performance if an OpenMP parallel region is executed by a different thread everytime. 我的问题是,如果每次由不同的线程执行OpenMP并行区域,是否会引起问题或导致性能下降。

edit: 编辑:

Target will be Linux (gcc) and Windows (msvc). 目标将是Linux(gcc)和Windows(msvc)。

I will benchmark it, when my first prototype is done (which will be influenced by the answers I get here). 当我完成第一个原型时,我将对其进行基准测试(这将受到我在这里得到的答案的影响)。

Here is a simple example: 这是一个简单的示例:

class Task
{
public:
    void doTask()
    {
        #pragma omp parallel
        {
            // do work in parallel
        }
    }
};

Now imagine you create an instance of Task give it to a thread pool (thread-0, ..., thread-n). 现在,假设您创建一个Task实例,并将其分配给线程池(线程0,...,线程n)。 One thread executes doTask() . 一个线程执行doTask() Later you give the same Task object again into the thread pool, and again, ... . 稍后,您将相同的Task对象再次提供给线程池,然后再次。 So doTask() (and the parallel section) will be executed by different threads. 因此, doTask() (和并行部分)将由不同的线程执行。 I wonder if this is handled by OpenMP efficiently (eg the threads for the section are not recreated every time). 我想知道这是否由OpenMP有效处理(例如,该部分的线程不是每次都重新创建)。

Vitor's comment is correct. Vitor的评论是正确的。 It is hard to tell whether or not this will cause problems, because the answer depends on many factors (ie, the data layout, how you are accessing the data, the cache size, the type of processor you are running on, and the list goes on). 很难确定这是否会造成问题,因为答案取决于许多因素(例如,数据布局,访问数据的方式,缓存大小,运行的处理器类型以及列表)继续)。

What I can say, is that you may or may not be able to get this to work. 我能说的是,您可能或可能无法使它正常工作。 The OpenMP spec - as well as most of the other threading models - don't say anything about how or if the models will "play nicely together". OpenMP规范-以及大多数其他线程模型-没有说明模型如何或是否可以“很好地配合”。 For example, even though some OpenMP implementations use pthreads for the underlying implementation, unless the implementation has done some work, the user cannot directly call the pthreads library and get it to work together with OpenMP. 例如,即使某些OpenMP实现将pthreads用于基础实现,除非该实现完成了某些工作,否则用户无法直接调用pthreads库并使它与OpenMP一起使用。 A current example of this is gcc bug 42616 (OMP'ed loop inside pthread leads to crash). 当前的示例是gcc错误42616(pthread中的OMP循环导致崩溃)。 Another example is Intel, whose compiler supports many parallel models, but has tried hard to get them to work together. 另一个例子是英特尔,它的编译器支持许多并行模型,但是努力使它们协同工作。 Since you haven't said what compiler you are going to use, all I can say is try a small sample code to see if it works before you commit to doing something large. 由于您还没有说过要使用什么编译器,所以我只能说一小段示例代码,看看它是否可以工作,然后再承诺做一些大的事情。

I have tried something like this in the past. 我过去曾尝试过类似的方法。 I used pthreads that then used OpenMP constructs. 我使用了pthread,然后使用了OpenMP构造。 What I found was that for my application it worked okay. 我发现对于我的应用程序来说,它可以正常工作。 Each pthread was considered an initial thread when the OpenMP parallel region was encountered. 遇到OpenMP并行区域时,每个pthread被视为一个初始线程。 The OpenMP runtime then created the additional threads for the region and ran the region. 然后,OpenMP运行时为该区域创建其他线程并运行该区域。 Since most OpenMP implementations don't destroy the threads, but put them in a free pool to reuse when another region is encountered, the overhead seemed fine - but then I had a lot of work to do in the region. 由于大多数OpenMP实现不会破坏线程,而是将它们放入空闲池中,以便在遇到另一个区域时可以重用,因此开销似乎很好-但是那时我在该区域有很多工作要做。 So it can work - but you have to be careful. 因此它可以工作-但您必须小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以为任何函数编写通用代码,使其可以(异步)执行并从线程池中获得返回值? - Is there a way to write generic code for any function such that it can be executed (asyncronously) and a return value obtained from a thread pool? OpenMP:并行运行两个函数,每个函数占线程池的一半 - OpenMP: run two functions in parallel, each by half of thread pool 在没有默认构造函数的情况下,每个线程在OpenMP中执行一次代码 - Execute piece of code once per thread in OpenMP without default constructor 如何确保部分代码仅由一个线程运行(在openmp中) - How to make sure that part of code is run only by one thread (in openmp) OpenMP #pragma,只有一个线程在我的代码上运行 - OpenMP #pragma, only one thread working on my code OpenMP代码远比串行内存或线程开销瓶颈慢? - OpenMP code far slower than serial - memory or thread overhead bottleneck? 在XP中也加载的DLL中使用新的Vista线程池API(XP中未使用线程池代码) - Using the new Vista Thread Pool API in a DLL also loaded in XP (thread pool code not used in XP) OpenMP并行线程 - OpenMP parallel thread 使用openmp并行化中止线程 - Abort thread with openmp parallelization OpenMP线程创建 - OpenMP Thread Creation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM