我做使用OpenMP一些很簡單的測試,在C++中,我遇到一個問題,這可能是愚蠢的,但我無法找出什麼是錯的元素的個數。在下面的MWE:OpenMP的/ C++:在for循環
#include <iostream>
#include <ctime>
#include <vector>
#include <omp.h>
int main()
{
int nthreads=1, threadid=0;
clock_t tstart, tend;
const int nx=10, ny=10, nz=10;
int i, j, k;
std::vector<std::vector<std::vector<long long int> > > arr_par;
arr_par.resize(nx);
for (i=0; i<nx; i++) {
arr_par[i].resize(ny);
for (j = 0; j<ny; j++) {
arr_par[i][j].resize(nz);
}
}
tstart = clock();
#pragma omp parallel default(shared) private(threadid)
{
#ifdef _OPENMP
nthreads = omp_get_num_threads();
threadid = omp_get_thread_num();
#endif
#pragma omp master
std::cout<<"OpenMP execution with "<<nthreads<<" threads"<<std::endl;
#pragma omp end master
#pragma omp barrier
#pragma omp critical
{
std::cout<<"Thread id: "<<threadid<<std::endl;
}
#pragma omp for
for (i=0; i<nx; i++) {
for (j=0; j<ny; j++) {
for (k=0; k<nz; k++) {
arr_par[i][j][k] = i*j + k;
}
}
}
}
tend = clock();
std::cout<<"Elapsed time: "<<(tend - tstart)/double(CLOCKS_PER_SEC)<<" s"<<std::endl;
return 0;
}
nx
如果,ny
和nz
等於10
,代碼運行平穩。如果我將這些數字增加到20
,我會遇到段錯誤。它無論順序運行還是與OMP_NUM_THREADS=1
無關,無論有多少個元素。
我使用GCC 4.6.3編譯該死的東西與
g++ -std=c++0x -fopenmp -gstabs+ -O0 test.cpp -o test
。
任何想法將不勝感激!