Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP: predetermined 'shared' for 'shared'?

Tags:

c++

vector

openmp

See this function (matrix-vector product):

std::vector<double> times(std::vector<std::vector<double> > const& A, std::vector<double> const& b, int m, int n) {

    std::vector<double> c;
    c.resize(n);

    int i, j;
    double sum;

    #pragma omp parallel for default(none) private(i, j, sum) shared(m, n, A, b, c)
    for (i = 0; i < m; ++i) {
        sum = 0.0;
        for (j = 0; j < n; j++) {
            sum += A[i][j] * b[j];
        }
        c[i] = sum;
    }

    return c;
}

When trying to compile this with OpenMP, the compiler fails with:

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"src/OpemMPTutorial.d" -MT"src/OpemMPTutorial.d" -o "src/OpemMPTutorial.o" "../src/OpemMPTutorial.cpp"
../src/OpemMPTutorial.cpp:127: warning: ignoring #pragma omp end
../src/OpemMPTutorial.cpp: In function 'std::vector<double, std::allocator<double> > times(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<double, std::allocator<double> >&, int, int)':
../src/OpemMPTutorial.cpp:200: error: 'b' is predetermined 'shared' for 'shared'
../src/OpemMPTutorial.cpp:200: error: 'A' is predetermined 'shared' for 'shared'
make: *** [src/OpemMPTutorial.o] Error 1

What's wrong here?

(Note that simply removing the const results in the same error.)

like image 834
clstaudt Avatar asked Nov 02 '12 16:11

clstaudt


1 Answers

I had a very similar problem and experienced that such a program can be compiled using Apple’s GCC 4.2 after I removed the shared const variables from the shared section of OpenMP directive. They are predetermined as shared since they are constant and there is no need to make a copy for each thread. And the compiler seems to just not accept telling it explicitely when it knows already…

I would also remove the default(none) specification (but see the comment below). OpenMP is intended to reduce explicit specifications, so let it do its job.

like image 72
Melebius Avatar answered Nov 19 '22 13:11

Melebius