Python:如何并行调用VTK函数?
我正在尝试加快python脚本的速度,它使用Python:如何并行调用VTK函数?,python,parallel-processing,vtk,Python,Parallel Processing,Vtk,我正在尝试加快python脚本的速度,它使用vtk方法(和vtkobjects)来处理几何测量。由于我的一些方法包括在非常相似的网格上循环并计算每个网格的封闭点,因此我只想将这些for循环并行化: averaged_contained_points = [] for intersection_actor in intersection_actors: contained_points = vtk_mesh.points_inside_mesh(point_data=point_data
vtk
方法(和vtkobjects
)来处理几何测量。由于我的一些方法包括在非常相似的网格上循环并计算每个网格的封闭点,因此我只想将这些for循环并行化:
averaged_contained_points = []
for intersection_actor in intersection_actors:
contained_points = vtk_mesh.points_inside_mesh(point_data=point_data, mesh=intersection_actor.GetMapper().GetInput())
mean_pos = np.mean(contained_points, axis=0)
averaged_contained_points.append(mean_pos)
在这种情况下,函数vtk\u mesh.points\u In\u mesh
调用vtk.vtkSelectEnclosedPoints()
,并将vtkActor
和vtkPolyData
作为输入
主要问题是:如何将其转换为并行运行
我最初的尝试是导入多处理
,但后来我切换到导入pathos.multiprocessing
,这似乎有一些优点,但它们的工作原理相当相似
问题是下面的代码不起作用
它会导致以下类型的错误:
pickle.PicklingError:无法pickle'vtkobject'对象:(vtkPolyData)0x111ed5bf0
我做了一些研究,发现vtkobjects可能无法腌制:
然而,由于我找不到一个与可用答案并行运行python vtk代码的解决方案,如果您有任何建议,请告诉我
[编辑]
我没有尝试实现线程,主要是因为我在这个线程中阅读了答案的注释:
在CPython上使用多线程不会提供更好的性能
由于全局解释器锁(GIL),用于纯Python代码
- 线程似乎没有使用pickle:
- 如果您希望使用多处理或pickle,则应该使用可拾取对象作为函数的输入,例如,请参阅tvtk()或使用字符串作为vtkreader/writer的输入
def functionWithPickableInput(inputstring0):
r0 = vtk.vtkPolyDataReader()
r0.ReadFromInputStringOn()
r0.SetInputString(inputstring0 )
r0.Update()
polydata0 = r0.GetOutput()
return functionWithVtkInput(polydata0)
#compute the strings to use as input (they are the content of the correspondent vtk file)
vtkstrings = []
w = vtk.vtkPolyDataWriter()
w.WriteToOutputStringOn()
for mesh in meshes:
w.SetInputData(mesh)
w.Update()
w.WriteToOutputStringOn()
vtkstrings.append(w.GetOutputString())
在这里,我选择在内存中写入所有内容(请参见,)中的方法。
当然,您必须在并行循环之外调用writer,因此您必须判断writer的开销相对于要并行化的函数是否合理。您还可以从文件中读取数据,
如果你有内存问题
- 如果您熟悉MPI,请查看mpi4py
def functionWithPickableInput(inputstring0):
r0 = vtk.vtkPolyDataReader()
r0.ReadFromInputStringOn()
r0.SetInputString(inputstring0 )
r0.Update()
polydata0 = r0.GetOutput()
return functionWithVtkInput(polydata0)
#compute the strings to use as input (they are the content of the correspondent vtk file)
vtkstrings = []
w = vtk.vtkPolyDataWriter()
w.WriteToOutputStringOn()
for mesh in meshes:
w.SetInputData(mesh)
w.Update()
w.WriteToOutputStringOn()
vtkstrings.append(w.GetOutputString())