Converting Pointer-Based Algorithms to Zero Copy Arrays

In my last blog, I demonstrated how VTK’s new “zero copy” infrastructure can be used to develop data arrays that use memory layouts different than VTK’s default. Many of VTK’s algorithm will work out of box with these arrays – the ones that depend on virtual methods such as SetTuple()/GetTuple() and SetValue()/GetValue(). However, certain algorithms need to be modified slightly. We’ll cover how this can be achieved here.

If you pass a subclass of vtkMappedDataArray<> to an algorithm that accesses the raw data pointer of an array via the GetVoidPointer() method or its ilk, you will see the following error at runtime:

If you see this error, it may be a good idea to refactor the offending algorithm to avoid this deep copy (which is slow and wastes memory). As an example, let’s consider a piece of code that uses the vtkImagePointsArray from the last blog.

The idea behind this is to treat an image data as a vtkStructuredGrid without creating an explicit point array. Not very useful on its own but it can be extended fairly easily to represent dataset not currently efficiently handled by VTK such as a grid curvilinear in x-y but regular in z, a regular spherical grid etc. When you run this example, you will notice the warning I mentioned above. The result will be correct, however. If you track down the origin of the warning, you end up in vtkGridSynchronizedTemplates3D() :

The problem is in the GetVoidPointer(0) call that asks for a raw pointer of type PointsType (double in this case). The solution to this issue is described in detail in David Lonie’s excellent Wiki page. Using the method described by David, I converted vtkGridSynchronizedTemplates3D. The full code can be found here. The change is minimal. I first made a change to the following function:

For those not familiar with VTK’s template macros, vtkTemplateMacro expands to a switch statement that looks like the following.

This allows calling a templated function such as ContourGrid with the right template type. See above for the original ContourGrid signature with 2 template arguments. I changed the single template argument ContourGrid to the following.

Note the usage of vtkDataArrayIteratorMacro macro. This macro was introduced by David along with vtkDABegin and vtkDAValueType. See his Wiki page for details. I then changed the other ContourGrid method as follows.

Note how points is now of type InputIterator which is a template argument magically defined within the vtkDataArrayIteratorMacro. This class leverages the vtkMappedDataArray<> API to provide pointer style semantics. This is it! After these changes, the pipeline shown above can now be run without any deep copies.

In future blogs, I will continue to expand on zero copy structures as well as dig into the cost of using these abstractions in terms of performance.

2 Responses to Converting Pointer-Based Algorithms to Zero Copy Arrays

  1. Andrew Maclean says:

    Just a quick note. Your “last blog” link is not working, I think it should point to:

  2. Berk Geveci says:

    Thanks! I fixed it.

Questions or comments are always welcome!