每个人。希望有人能帮助我。我有一个代码可以在 openMP 上并行 Prim 的算法,我需要让它在 Xeon Phi 上工作。请帮我。我无法真正理解如何做到这一点。这是我在 openMP 上的代码。
void ParallelPrim(double *pMatrix, TTreeNode** pMinSpanningTree, int Size)
{
int LastAdded;
TGraphNode NearestNode;
TGraphNode **NotInMinSpanningTree = new TGraphNode* [Size-1];
LastAdded = 0;
for(int i = 0; i < Size-1; i++)
{
NotInMinSpanningTree[i] = new TGraphNode;
NotInMinSpanningTree[i]->NodeNum = i+1;
NotInMinSpanningTree[i]->Distance = -1.0f;
NotInMinSpanningTree[i]->ParentNodeNum = -1;
}
for(int Iter = 1; Iter < Size; Iter++)
{
#pragma omp parallel for
for(int i = 0; i < Size-1; i++)
if(NotInMinSpanningTree[i] != NULL)
{
double t1 = NotInMinSpanningTree[i]->Distance;
double t2 = pMatrix[(NotInMinSpanningTree[i]->NodeNum) * Size + LastAdded];
if(((t1 < 0) && (t2 > 0)) || (t1>0) && (t2 > 0) && (t1 > t2))
{
NotInMinSpanningTree[i]->Distance = t2;
NotInMinSpanningTree[i]->ParentNodeNum = LastAdded;
}
}
NearestNode.NodeNum = -1;
NearestNode.Distance = 3000;
#pragma omp parallel
{
TGraphNode ThreadNearestNode;
ThreadNearestNode.NodeNum = -1;
ThreadNearestNode.Distance = 3000;
#pragma omp for
for(int i = 0; i < Size-1; i++)
{
if(NotInMinSpanningTree[i] != NULL)
{
double t1 = NotInMinSpanningTree[i]->Distance;
double t2 = ThreadNearestNode.Distance;
if((t1 > 0) && (t1 < t2) )
{
ThreadNearestNode.Distance = t1;
ThreadNearestNode.NodeNum = NotInMinSpanningTree[i]->NodeNum;
}
}
}
#pragma omp critical
{
if(ThreadNearestNode.Distance < NearestNode.Distance)
{
NearestNode.Distance = ThreadNearestNode.Distance;
NearestNode.NodeNum = ThreadNearestNode.NodeNum;
}
}
}
pMinSpanningTree[NearestNode.NodeNum] = new TTreeNode;
pMinSpanningTree[NearestNode.NodeNum]->NodeNum = NotInMinSpanningTree[NearestNode.NodeNum-1]->ParentNodeNum;
pMinSpanningTree[NearestNode.NodeNum]->Distance = NearestNode.Distance;
int Parent = NotInMinSpanningTree[NearestNode.NodeNum-1]->ParentNodeNum;
if(pMinSpanningTree[Parent] != NULL)
{
TTreeNode *tmp = new TTreeNode;
tmp->Distance = NearestNode.Distance;
tmp->NodeNum = NearestNode.NodeNum;
}
else
{
pMinSpanningTree[Parent] = new TTreeNode;
pMinSpanningTree[Parent]->Distance = NearestNode.Distance;
pMinSpanningTree[Parent]->NodeNum = NearestNode.NodeNum;
}
LastAdded = NearestNode.NodeNum;
delete NotInMinSpanningTree[NearestNode.NodeNum - 1];
NotInMinSpanningTree[NearestNode.NodeNum - 1] = NULL;
}
delete[] NotInMinSpanningTree;
}