看到这个函数(矩阵向量积):
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;
}
Run Code Online (Sandbox Code Playgroud)
尝试使用OpenMP编译时,编译器失败:
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
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
(注意,只需删除const相同错误的结果.)
我遇到了一个非常相似的问题,并且在我const从sharedOpenMP 指令的部分中删除了共享变量后,可以使用 Apple 的 GCC 4.2 编译这样的程序。它们被预先确定为共享的,因为它们是恒定的,并且不需要为每个线程制作副本。编译器似乎只是不接受在它已经知道的情况下明确地告诉它......
我也会删除default(none)规范(但请参阅下面的评论)。OpenMP 旨在减少明确的规范,因此让它发挥作用。
这是由于 gcc-4.2 中 OpenMP 支持不足造成的。使用 gcc-4.7 编译该代码片段没有问题。