# Gigabyte-large vtkUnstructuredGrid bottleneck.

**URL:** https://discourse.vtk.org/t/gigabyte-large-vtkunstructuredgrid-bottleneck/1022
**Category:** Support
**Created:** [May 24, 2019, 3:54pm UTC](https://discourse.vtk.org/t/gigabyte-large-vtkunstructuredgrid-bottleneck/1022 "2019-05-24T15:54:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 24, 2019, 3:54pm UTC](https://discourse.vtk.org/t/gigabyte-large-vtkunstructuredgrid-bottleneck/1022/1 "2019-05-24T15:54:19Z")

</div>

Greetings,

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());
}
unstructuredGrid->SetPoints(hexaPoints);

```

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?

thanks,

Paulo

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 24, 2019, 6:39pm UTC](https://discourse.vtk.org/t/gigabyte-large-vtkunstructuredgrid-bottleneck/1022/2 "2019-05-24T18:39:35Z")

</div>

> [@Paulo\_Carvalho](#):
>
> vtkSmartPointer\< vtkHexahedron \> hexa = vtkSmartPointer\< vtkHexahedron \>::New();

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

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 24, 2019, 6:52pm UTC](https://discourse.vtk.org/t/gigabyte-large-vtkunstructuredgrid-bottleneck/1022/3 "2019-05-24T18:52:05Z")

</div>

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. 😊
