【发布时间】:2017-08-09 11:36:58
【问题描述】:
如果在循环内执行 Vtk 代码,我很难理解为什么与 OpenMP 并行的 for-loop 不会使用所有 n_threads 线程 (=2x #cores)。具体来说,我想将线/射线与网格相交。我关注了this tutorial
- 从网格构建 OBB 树
- 将所有需要的线与网格相交
因为我想并行化它,所以我创建了n_threads 树,这样每个线程都可以使用它自己的树实例:
// Pre-allocate the array
int n_threads = omp_get_max_threads();
trees = std::vector<vtkSmartPointer<vtkOBBTree>>((unsigned int) n_threads);
// Build n_threads OBB trees
#pragma omp parallel for num_threads(n_threads)
for (int t = 0; t < n_threads; ++t)
{
trees[t] = vtkSmartPointer<vtkOBBTree>::New();
vtkSmartPointer<vtkPolyData> mesh_clone = vtkSmartPointer<vtkPolyData>::New();
#pragma omp critical (build_mesh_tree)
{
mesh_clone->DeepCopy(mesh);
}
trees[t]->SetDataSet(mesh_clone);
trees[t]->BuildLocator();
}
然后我遍历所有点来计算origin和points中每个点的交集
#pragma omp parallel for num_threads(n_threads)
for (unsigned long i = 0; i < n_points; ++i)
{
vtkSmartPointer<vtkPoints> intersection_points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkIdList> cell_ids = vtkSmartPointer<vtkIdList>::New();
int this_thread = omp_get_thread_num();
int code = trees[this_thread]->IntersectWithLine(
origin.data(), // pointer to the raw data
points.at(i).data(), // buffer of a Eigen matrix
intersection_points,
cell_ids);
// Do something with intersection_points and cell_ids
}
OpenMP 对于简单的 C++ 代码已经证明可以正常工作。然而,当包裹 Vtk 调用时,它并没有达到它的目的。我想这是因为 Vtk 已经提供了parallelization framework (ref. to the guide)。
如果是这样,您能否解释一下,为什么 OpenMP 无法并行运行与 vtk 相关的代码?如果不是,可能是什么原因?
【问题讨论】:
标签: c++ multithreading parallel-processing openmp vtk