「dat」のデータポイントのk最近傍(KNN)を実行しようとしているので、最初のステップは、各ポイントと他のすべてのポイントの間の距離行列を作成し、各ポイントについてK最近傍を見つけます。次のコードは、openmpなしでシリアルで完全に機能します。ただし、openmpを使用すると、セグメンテーション違反が発生します。このエラーは、k個の最小要素のインデックスを含む最小値を更新する方法に関係していると思います。ベクトルが最小の「リダクション」を使用する必要があるのではないかと思いましたが、使用方法がわからないので、正しいか間違っているかわからないので、このセグメンテーション違反を克服するための助けをいただければ幸いです。
vector<vector<double> > dist(dat.size(), vector<double>(dat.size()));
size_t p,j;
ptrdiff_t i;
vector<double> sumKnn;
vector<vector<int > > smallest(dat.size(), vector<int>(k));
#pragma omp parallel for private(p,j,i) default(shared)
for(p=0;p<dat.size();++p)
{
int mycont=0;
for (j = p+1; j < dat.size(); ++j)
{
double ecl = 0.0;
for (i = 0; i < c; ++i)
{
ecl += (dat[p][i] - dat[j][i]) * (dat[p][i] - dat[j][i]);
}
ecl = sqrt(ecl);
dist[p][j] = ecl;
dist[j][p] = ecl;
int index=0;
if(mycont<k && j!=p)
{
smallest[p][j-p-1]=j;
mycont++;
}
else
{
double max=0.0;
int index=0;
for(int i=0;i<smallest[p].size();i++)
{
if(max < dist[p][smallest[p][i]])
{
index=i;
max=dist[p][smallest[p][i]];
}
}
if(max>dist[p][j])
{
smallest[p].erase(smallest[p].begin()+index);
smallest[p].push_back(j);
}
}
}
double sum=0.0;
for(int r=0;r<k;r++)
sum+= dist[p][smallest[p][r]];
sumKnn.push_back(sum);
}