c++ - OpenMP自动更新数组值

标签 c++ multithreading parallel-processing openmp atomic

我要解决的问题如下:
我有一个很大的观点集,我尝试将其分解为一些较小的观点。我知道较小的尺寸,并且我正在尝试同时使用较大的尺寸填充较小的尺寸。为了做到这一点,我在每个较小的点集上都有一组索引,这将有助于我正确地填充我的点集。我已经开发了以下代码。但是omp atomic capture指令不能帮助我获得与顺序执行相同的结果。

// points
std::vector<Eigen::Matrix3Xd> points(numberofPointSets);

// initialize arrays
for (auto& pointSetInformation: pointSetsInformation)
{
    int pointSetSize = getPointSetSize();
    int index = getIndexOfPointsSet();

    points[index].resize(3, pointSetSize);
}

// currentIndices to assign specific columns of the matrix in parallel
std::vector<int> currentIndices(numberofPointSets, -1);

// Assign arrays
#pragma omp parallel num_threads(numberOfThreads) shared(pointsArray, points, currentIndices) default(none)
{
    int index;
    int currentIndexInCurrentPointSet;
    double point[3];

    #pragma omp for schedule(static)
    for (int i = 0; i < input->GetNumberOfPoints(); ++i)
    {
        index = getIndexOfPointsSet();

        // update currentIndices (we start from -1, that's why this happens first)
        #pragma omp atomic capture
        currentIndexInCurrentPointSet = ++currentIndices[index];

        pointsArray->GetPoint(i, point);
        points[index](0, currentIndexInCurrentPointSet) = point[0];
        points[index](1, currentIndexInCurrentPointSet) = point[1];
        points[index](2, currentIndexInCurrentPointSet) = point[2];
    }
}
我想念什么吗? omp atomic capture指令可以更新数组单元格,还是仅更新变量?
在此先多谢。
编辑:
有没有一种方法可以强制执行与i变量相同的操作顺序?

最佳答案

Is there a way to enforce the same order of operation as the i variable?


您尝试使用OpenMP ordered子句
#pragma omp parallel num_threads(numberOfThreads) shared(pointsArray, points, currentIndices) default(none)
{
    int index;
    int currentIndexInCurrentPointSet;
    double point[3];

    #pragma omp for ordered schedule(static, 1)
    for (int i = 0; i < input->GetNumberOfPoints(); ++i)
    {
        #pragma omp ordered
        {
            index = getIndexOfPointsSet();
            currentIndexInCurrentPointSet = ++currentIndices[index];
        }
        pointsArray->GetPoint(i, point);
        points[index](0, currentIndexInCurrentPointSet) = point[0];
        points[index](1, currentIndexInCurrentPointSet) = point[1];
        points[index](2, currentIndexInCurrentPointSet) = point[2];
    }
}

关于c++ - OpenMP自动更新数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66374558/

相关文章:

algorithm - 计算二维函数积分的最佳并行方法

c++ - 按成员(member)值投

c++ - 如何在C++中创建目录

C# 在实例对象中锁定对象

compilation - 如何并行化可以在多台机器上分配任务的 "make"命令

Mysql存储过程并行处理

c++ - Objective-C++中的智能指针可以完全取代ARC吗?

c++ - 构建 KleeNet 时出现“对 _M_hook 的 undefined reference ”错误

java volatile 数组,我的测试结果与预期不符

c# - 调度任务以供将来执行