0

我有这段代码。我正在尝试应用 OpenMP,__gnu_parallel::for_each并使其并行化,但这些方法都不起作用。

我该怎么办?这里 make 是一个集合向量,集合中包含的类型是OctCell*

该算法给出了正确的输出,但不会加速代码。我有4个核心。

void Oct :: applyFunction3(void (*Function)(OctCell* cell), unsigned int level)
{
    __gnu_parallel::for_each(make.at(level).begin(),make.at(level).end(),Function);
}

功能是

void directionalSweepX(OctCell* cell) {
OctCell* positiveCell,*negativeCell;
     positiveCell = cell->getNeighbour(RIGHT);
   negativeCell = cell->getNeighbour(LEFT);
    addFluxToConserveds(cell, positiveCell, negativeCell, X);
}

addFluxtoConserveds 执行以下操作

void addFluxToConserveds(OctCell* cell, OctCell* positiveCell, OctCell* negativeCell, SWEEP_DIRECTION direction) {

    double deltaT = pow(2.0, cell->getLevel() - cell->getParentOct()->lMin)*gDeltaT;
    // You have corrected that delta t is delta (L)
    double alpha = (1 << (int) cell->getParentOct()->lMin) * gDeltaT/gL;// whats the purpose f <<

    double beta = alpha/8.0;
    double gamma;
    double Flux[5] = {0.0, 0.0, 0.0, 0.0, 0.0};

    if ( positiveCell == 0) {
        Flux[direction+1] = getPressure(cell);
    } else if ( positiveCell->isLeaf() ) {
        computeFlux(cell, positiveCell, direction, Flux);
        gamma = (positiveCell->getLevel() == cell->getLevel())  ? alpha : beta;
    }

    for (int i=0; i<5; i++) {
        cell->mConserveds_n[i] -= alpha * Flux[i];
        if (positiveCell) positiveCell->mConserveds_n[i] += gamma * Flux[i];
    }

    Flux[0] = Flux[1] = Flux[2] = Flux[3] = Flux[4] = 0.0;

    if ( negativeCell == 0 ) {
        Flux[direction+1] = getPressure(cell);
    } else if (negativeCell->isLeaf() && negativeCell->getLevel() == cell->getLevel() - 1 ) {
        computeFlux(negativeCell, cell, direction, Flux);
    }

    for (int i=0; i<5; i++) {
        cell->mConserveds_n[i] += alpha * Flux[i];
        if (negativeCell) negativeCell->mConserveds_n[i] -= beta * Flux[i];
   }

}
4

1 回答 1

0

使用#include <omp.h>.

在函数addFluxtoConserveds中,您可以将 a 添加#pragma omp for到两个 for 循环中。这是因为每次迭代不依赖于其他迭代来完成。因为你有一个对第二个 for 循环很重要的连续代码,所以你不能在这里使用sectionsor tasks

什么是顺序实现applyFunction3

您必须记住关于 OpenMP 的一件重要事情。即使在同一系列处理器中(英特尔双核与英特尔双核;英特尔与 AMD 等),在一个架构上编译的程序也不会针对所有其他架构进行优化。这意味着它在它编译的原始架构上运行得很快,而在其他架构上它只是运气。

于 2012-07-06T07:44:56.310 回答