For-loop inside parallel region

If there is a for-loop inside a parallel region, would for-loop be parallelized again or every thread will execute its own for-loop?

T sum;

#pragma omp parallel
{
    #pragma omp for reduction(+: sum)
    for (;;)
    {
        T priv_var;

        sum += priv_var;
    }
}

Answers


Yes, this code will cause OpenMP to parallelise the for loop across the threads that are spawned by the parallel region. However, I believe that your current for statement is invalid for OpenMP parallelisation. You need to explicitly provide an integer loop variable, start and end, and increment expression.

In effect, your code will then be equivalent to a single loop with #pragma omp parallel for reduction(+: sum).

More information on MDSN


Need Your Help

How to insert values in table after adding new column at once?

mysql sql database primary-key insert-update

I have a mysql table. I have inserted a new column namely s_no (integer) at first position. Now I want to insert values from 1, 2, 3 at once and also want to make it primary key.