我很难理解为什么for循环,使用OpenMP并行化,不若VTK代码在循环内执行使用所有n_threads
线程(= 2X #cores)。具体而言,我想用网格相交线条/光线。我通过为什么不的OpenMP并行VTK IntersectWithLine代码
- 建设OBB树从网格
- 所有需要的线相交,其中网
因为我想并行它遵循this tutorial,我创建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相关的代码?如果不是,可能是什么原因?
感谢这个答复!我更新了我的问题。当然,覆盖(可能线程不安全)'intersection_points'和'intersection_cells'的内容是没有意义的。我现在把这段代码移到了循环中。 另外,我对OpenMP _fails_如何没有具体说明。计算是正确的。它只是简单地不会同时在所有线程上执行代码的vtk部分。我可以确认,虽然'n_threads'产生了。看来这个线程先调用一个vtk函数,然后锁定其他线程。 – maddin25