Gigabyte-large vtkUnstructuredGrid bottleneck.


I’m building a large unstructured grid (a reservoir model) from a data set. The part below works super fast:

// Create a VTK container with the points (mesh vertexes)
vtkSmartPointer< vtkPoints > hexaPoints = vtkSmartPointer< vtkPoints >::New();
hexaPoints->SetNumberOfPoints( geoGrid->getMeshNumberOfVertexes() );
hexaPoints->Allocate( hexaPoints->GetNumberOfPoints() );
for( int i = 0;  i < hexaPoints->GetNumberOfPoints(); ++i ){
	double x, y, z;
	geoGrid->getMeshVertexLocation( i, x, y, z );
	hexaPoints->InsertPoint(i, x, y, z);

After the code above executes, I have ~2.0GB loaded, and that was under 30s. The code below, where I define de cells (hexahedra), is taking more than an hour to complete:

// Create a VTK unstructured grid object (allows faults, erosions, and other geologic discordances )
vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
uint nCells = geoGrid->getMeshNumberOfCells();
unstructuredGrid->Allocate( nCells );
for( uint i = 0; i < nCells; ++i ) {
	uint vIds[8];
	geoGrid->getMeshCellDefinition( i, vIds );
	vtkSmartPointer< vtkHexahedron > hexa = vtkSmartPointer< vtkHexahedron >::New();
	hexa->GetPointIds()->SetId(0, vIds[0]);
	hexa->GetPointIds()->SetId(1, vIds[1]);
	hexa->GetPointIds()->SetId(2, vIds[2]);
	hexa->GetPointIds()->SetId(3, vIds[3]);
	hexa->GetPointIds()->SetId(4, vIds[4]);
	hexa->GetPointIds()->SetId(5, vIds[5]);
	hexa->GetPointIds()->SetId(6, vIds[6]);
	hexa->GetPointIds()->SetId(7, vIds[7]);
	unstructuredGrid->InsertNextCell(hexa->GetCellType(), hexa->GetPointIds());

During the loop above, memory consuption slowly increases. It seems that the process is re-allocating 2GB and appending a few MBs, which is of course very costly. I’ve already ruled out the call to getMeshCellDefinition(). I’m wondering whether the value passed to Allocate() is correct.

Any ideas?



The bottleneck is the iterative call to New() above. How do I create millions of cells efficiently?

It turns out that there is no need to re-create the vtkHexahedron object within the loop. It is only a placeholder for point ids for the InsertNextCell() call. It is only necessary to create it once before the loop. :blush:

1 Like