简体   繁体   中英

OpenMP: predetermined 'shared' for 'shared'?

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.)

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.

This is caused by insufficient OpenMP support in gcc-4.2. The code fragment compiles without problems using gcc-4.7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM