[vtkusers] Speed up cell allocation

Andreas Hessenthaler hessenthaler at mechbau.uni-stuttgart.de
Sat Aug 1 06:21:32 EDT 2015


I'm not sure if this answers your question, but I ran into a similar
problem when I created cells for an unstructured grid.

If I did it one-by-one, it was really slow. What helped was creating numpy
arrays and use the SetCells method of the vtkUnstructuredGrid.

Assume, you have a numpy array tempElemS, that contains the node numbers
belonging to each hexahedron, e.g.
[first element's node list]
[second element's node list]
[...]
[last element's node list]

Here's a python code snippet, that sets the cell data (similar to
vtk.vtkPoints().SetData(...)):

# unstructured grid
ugridS      = vtk.vtkUnstructuredGrid()
# get cell type for tri-quadratic hexahedron
cellTypesS  = vtk.vtkTriQuadraticHexahedron().GetCellType()
# obtain numpy arrays that contain
# connectivity, cell types and cell locations (see below)
tempElemLinS, cellstypesS, cellslocationsS = \
  reshapeElementArray(tempElemS, cellTypesS)
# create cell array from connectivity list
cellsS = vtk.vtkCellArray()
# set cells:
# first argument is number of cells
# second argument is connectivity list as 1D array
cellsS.SetCells(int(tempElemLinS.shape[0]/28), \
  numpy_to_vtk(tempElemLinS, deep=1, \
               array_type=vtk.vtkIdTypeArray().GetDataType()))
# set cells for unstructured grid
sgridS.SetCells( \
  numpy_to_vtk(cellstypesS, deep=1, \
               array_type=vtk.vtkUnsignedCharArray().GetDataType()), \
  numpy_to_vtk(cellslocationsS, deep = 1, \
               array_type=vtk.vtkIdTypeArray().GetDataType()),
  cellsS)

Now, the function reshapeElementArray returns the following:

tempElemLinS - 1D array containing the connectivity list, e.g. for
tri-quadratic hexahedra:
tempElemLinS[0] = 27 # number of nodes of first element
tempElemLinS[1] = n0 # first node number of first element
...
tempElemLinS[27] = n26 # last node number of first element
tempElemLinS[28] = 27 # number of nodes of first element
tempElemLinS[29] = ... # first node number of second element
...
tempElemLinS[55] = ... # last node number of second element
...

cellstypesS - 1D array of size #elems with entries cellTypesS

cellslocationsS - 1D array of size #elems with entries indicating the
first index of each element in the 1D element array tempElemLinS, e.g.
cellslocationsS[0] = 0
cellslocationsS[1] = 28
...

Hope that helps!

On another note, in case you consider python. You can significantly speed
up computations in the reshapeElementArray function by using numpy and
cython without much programming overhead.

Additionally, you might wanna consider writing tempElemLinS to a file and
importing the file on start-up. I found this to be even faster in my case.

Cheers
Andreas



On Fri, July 31, 2015 4:18 pm, quadprob wrote:
Hi,
My program is suffering from very noticeable start up times even though
rendering itself is quite smooth. From profiling I've learnt that what's
taking up most of the time is the vtkCell constructor calls. So what I'm
essentially looking for is to speed up something like this:

public static void main(String []args) {
	int numElems = 739872;
	vtkCell[] elems = new vtkCell[numElems];

	long startTime = System.nanoTime();
	for(int i = 0; i < numElems; ++i) {
		elems[i] = new vtkHexahedron();
	}
	long stopTime = System.nanoTime();
	System.out.printf("Took %dms\n", (stopTime-startTime)/1000000);
}

Do I have any options other than parallelizing the main loop?



--
View this message in context:
http://vtk.1045678.n5.nabble.com/Speed-up-cell-allocation-tp5733208.html
Sent from the VTK - Users mailing list archive at Nabble.com.
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers





More information about the vtkusers mailing list