我要解决的问题如下:
我有一个很大的观点集,我尝试将其分解为一些较小的观点。我知道较小的尺寸,并且我正在尝试同时使用较大的尺寸填充较小的尺寸。为了做到这一点,我在每个较小的点集上都有一组索引,这将有助于我正确地填充我的点集。我已经开发了以下代码。但是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/