tauran
tauran

Reputation: 8036

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). (Similar to: How to deal with OpenMP thread pool contention I guess). 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.

edit:

Target will be Linux (gcc) and 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). One thread executes doTask(). Later you give the same Task object again into the thread pool, and again, ... . So doTask() (and the parallel section) will be executed by different threads. I wonder if this is handled by OpenMP efficiently (e.g. the threads for the section are not recreated every time).

Upvotes: 2

Views: 3370

Answers (1)

ejd
ejd

Reputation: 1757

Vitor's comment is correct. It is hard to tell whether or not this will cause problems, because the answer depends on many factors (i.e., 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". 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. A current example of this is gcc bug 42616 (OMP'ed loop inside pthread leads to crash). 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. 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. The OpenMP runtime then created the additional threads for the region and ran the region. 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. So it can work - but you have to be careful.

Upvotes: 4

Related Questions