2

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?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Abolfazl
  • 1,047
  • 2
  • 12
  • 29
  • There is no need for a barrier after the first loop. Unless you explicitly remove it (by using the "nowait" clause on the "omp for") there is always a barrier at the end of an OpenMP for construct. – Jim Cownie Nov 10 '20 at 10:19
  • You need to show us more code, since, as the compiler is telling you, the problem is that the variable sumj is not a shared variable. But we can't see any of the variable declarations, so can't tell what is going on. – Jim Cownie Nov 10 '20 at 10:21

1 Answers1

1

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.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Adriel Jr
  • 2,451
  • 19
  • 25