[vtk-developers] More zero-copy array support for Python

David Gobbi david.gobbi at gmail.com
Fri Jun 16 08:37:52 EDT 2017


Hi All,

Yesterday I merged the wrapping of the vtkSOADataArrays, and also added a
new wrapper hint to assist with VTK's zero-copy methods.

Zero-copy is when an array uses an existing block of memory.  In the Python
wrappers, this generally means making a VTK array from a numpy array or
vice-versa.  Or making a VTK image from a PIL image.  Or using memmap to
share a VTK data set among multiple processes.

The new wrapper hint VTK_ZEROCOPY can be applied to method parameters that
are used for zero-copy operations:

  void vtkFloatArray::SetArray(VTK_ZEROCOPY float *ptr, vtkIdType n, int
save);

This hint allows the Python wrappers to do the zero-copy magic with
SetArray:

  import numpy
  import vtk
  c = numpy.array([1,2,3], 'f')
  a = vtk.vtkFloatArray()
  a.SetArray(c, c.size, True)
  a.array = c  # keep reference to "c"

Astute VTKers will note that this was already possible with SetVoidArray(),
as used by the vtk.numpy_support module, but SetArray() adds type checking.

Also, SetArray() works with the newly-wrapped vtkSOADataArrays, so perhaps
in the future numpy_support.numpy_to_vtk() can be expanded to support SOA
arrays:

  c0 = numpy.array([1,2,3], 'f')
  c1 = numpy.array([4,5,6], 'f')
  a = vtk.vtkSOADataArrayTemplate[c0.dtype]()
  a.SetNumberOfComponents(2)
  a.SetArray(0, c0, c0.size, True, True)
  a.SetArray(1, c1, c1.size, True, True)

In the future this could be done with  a = vtk.numpy_support.numpy_to_vtk(
(c0,c1) ).

I haven't yet implemented the mechanism to go in the other direction, i.e.
to create a numpy array from a VTK SOA array.

Also: vtkSOADataArrayTemplate is now wrapped, but vtkAOSDataArrayTemplate
is not.  Currently the wrappers think that vtkDataArray is the immediate
superclass of vtkFloatArray etc.  This could be changed, if there are use
cases for directly instantiating vtkAOSDataArrayTemplate.

Cheers,
 - David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170616/c0543b20/attachment.html>


More information about the vtk-developers mailing list