我试图用openmp在其中计算每个点和所有其他点之间的距离并行实现距离矩阵,所以我认为到目前为止最好的算法成本O(n^2)我的算法在8处理器机器上使用10个线程使用openmp的性能并不比运行时间的串行方法更好,所以我想知道在我的openmp方法实现中是否有任何错误,因为这是我第一次使用openmp,所以如果我的apporach中有任何错误或者更好的“更快”的方法,请让我知道。以下是我的代码,其中“dat”是包含数据点的向量。openmp并行性能
map <int, map< int, double> > dist; //construct the distance matrix
int c=count(dat.at(0).begin(),dat.at(0).end(),delm)+1;
#pragma omp parallel for shared (c,dist)
for(int p=0;p<dat.size();p++)
{
for(int j=p+1;j<dat.size();j++)
{
double ecl=0;
string line1=dat.at(p);
string line2=dat.at(j);
for (int i=0;i<c;i++)
{
double num1=atof(line1.substr(0,line1.find_first_of(delm)).c_str());
line1=line1.substr(line1.find_first_of(delm)+1).c_str();
double num2=atof(line2.substr(0,line2.find_first_of(delm)).c_str());
line2=line2.substr(line2.find_first_of(delm)+1).c_str();
ecl += (num1-num2)*(num1-num2);
}
ecl=sqrt(ecl);
#pragma omp critical
{
dist[p][j]=ecl;
dist[j][p]=ecl;
}
}
}
你可以发表更多的行,使其成为一个运行的例子吗? – 2012-01-27 20:46:15
你在双重嵌套循环中有一个'#pragma omp critical',你想知道瓶颈在哪里? o.O – ildjarn 2012-01-27 20:59:54