0

I have the following code:

#pragma omp parallel
{
    #pragma omp single
    {
        for(node* p = head; p; p = p->next)
        {
            preprocess(p);

            #pragma omp task
            process(p);
        }
    }
}

I would like to know when do the threads start computing the tasks. As soon as the task is created with #pragma omp task or only after all tasks are created?

Edit:

int* array = (int*)malloc...
#pragma omp parallel
{
    #pragma omp single
    {
        while(...){
            preprocess(array);

            #pragma omp task firstprivate(array)
            process(array);
        }
    }
}

1 Answer 1

1

In your example, the worker threads can start executing the created tasks as soon as they have been created. There's no need to wait for the completion of the creation of all tasks before the first task is executed.

So, basically, after the first task has been created by the producer, one worker will pick it up and start executing the task. However, be advised that the OpenMP runtime and compiler have certain freedom in this. They might defer execution a bit or even execute some of the tasks in place.

If you want to read up the details, you will need to dig through the OpenMP specification at www.openmp.org. It's a bit hard to read, but it is the definitive source of information.

Cheers, -michael

Sign up to request clarification or add additional context in comments.

4 Comments

When I print the number of the thread inside of process(p) with omp_get_thread_num(), it always shows me thread 0. Shouldn't it switch between the threads?
I already fixed that problem, but thanks. Now I got another one. I changed the code to the one in the edit above. I want to work over a copy of an array that was computed previously. In each iteration I the array is updated. Can I use firstprivate to prevent the array that is being processed by a thread of getting updated?
It would be great if you could post your solution to the original problem here, so that other users could also make use of it. The answer to your new question is: nope, you can't. firstprivate will privatize a copy of the pointer, but not the pointed-to data. You need to do that yourself.
The problem was that I was compiling on an old compiler that didn't support OpenMP Tasks. As soon as I changed to a newer one it started working. With static arrays firstprivate works but for some reason only the master thread executes the parallel region. Do you have any idea why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.