Abolfazl
Abolfazl

Reputation: 1227

openmp two consecutive loops, problem with reduction clause

There is two consecutive loops and there is a reduction clause in the second loop.

#pragma opm parallel
{
#pragma omp for
    for (size_t i = 0; i < N; ++i)
    {
      
    }
#pragma omp barrier
#pragma omp for reduction(+ \
                    : sumj)
    for (size_t i = 0; i < N; ++i)
    {
        sumj = 0.0;
        for (size_t j = 0; j < adjList[i].size(); ++j)
        {
            sumj += 0;
        }
        Jac[i, i] = sumj;
    }
}

to reduce the creating threads overhead I wand to keep the threads and use them in the second loop, but I get the following error

lib.cpp:131:17: error: reduction variable ‘sumj’ is private in outer context
         #pragma omp for reduction(+ \
                 ^~~

how to fix that?

Upvotes: 2

Views: 163

Answers (1)

Adriel Jr
Adriel Jr

Reputation: 2681

I'm not sure what you are trying to do, but it seems that something like this would do what you expect:

#pragma omp parallel
{
    #pragma omp for
    for (size_t i = 0; i < N; ++i)
    {
      
    }
    #pragma omp barrier
    #pragma omp for
    for (size_t i = 0; i < N; ++i)
    {
        double sumj = 0.0;
        for (size_t j = 0; j < adjList[i].size(); ++j)
        {
            sumj += 0;
        }
        Jac[i, i] = sumj;
    }
}

Reduce would be useful in the case of an "omp for" in the interior loop.

Upvotes: 1

Related Questions