How to write VTK files in parallel
Writing a VTK file in Serial mode tutorials can be found on VTK example page and many various websites. However, a detailed example on how to output solution files in parallel (i.e. MPI) is almost nonexistent. The only related example can be found on VTK Wiki page, but it is incomplete and misleading. So we wanted share a tutorial on this post. If you find this example too basic or trivial, please contact us for more complex examples. We will be sharing an updated version of this post with our in-house developed library "PAVLOV" in the future.
The following example shows how to write an unstructured grid VTK file (.pvtu) in parallel using MPI:
In this tutorial, the goal is to output a data file in VTK format where each processor generates its own data from its partition. (4 processors were used) .
We start with initializing MPI as usual;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int mpi_size, mpi_rank; | |
MPI_Comm comm = MPI_COMM_WORLD; | |
MPI_Info info = MPI_INFO_NULL; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_size(comm, &mpi_size); | |
MPI_Comm_rank(comm, &mpi_rank); |
Then each processor defines its own point and cell array as if it is in serial mode. Then while writing its own grid file from its partition in ".vtu" format, the naming convention becomes important. The figures below show each vtu file separately.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(mpi_rank==0){ | |
writer->SetFileName("Voxel_0.vtu"); | |
} | |
if(mpi_rank==1){ | |
writer->SetFileName("Voxel_1.vtu"); | |
} | |
if(mpi_rank==2){ | |
writer->SetFileName("Voxel_2.vtu"); | |
} | |
if(mpi_rank==3){ | |
writer->SetFileName("Voxel_3.vtu"); | |
} | |
#if VTK_MAJOR_VERSION <= 5 | |
writer->SetInput(unstructuredGrid); | |
#else | |
writer->SetInputData(unstructuredGrid); | |
#endif | |
writer->Write(); |
Finally, the most important step is the writing the ".pvtu" file by a single processor (which can be root, where mpi_rank=0), which help to "stitch" all individual files into a unified handler and used as the main file to be opened by the visualization software.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(mpi_rank==0){ | |
vtkSmartPointer pwriter = vtkSmartPointer::New(); | |
pwriter->SetFileName("Voxel.pvtu"); | |
pwriter->SetNumberOfPieces(mpi_size); | |
#if VTK_MAJOR_VERSION <= 5 | |
pwriter->SetInput(unstructuredGrid); | |
#else | |
pwriter->SetInputData(unstructuredGrid); | |
#endif | |
pwriter->Write(); | |
} |
As a final remark, the main motivation behind writing the output files in parallel comes down to Amdahl's law which states that the maximum speedup a parallel software can achieve is limited by the fraction of serial portion it has. Thus, parallel VTK effectively decreases this serial portion of a software (which is usually non-negligible) and helps it to scale up better.
We are including all the source file that is used to generate this example on our github page (click DOWNLOAD , follow the link and click download on the github page). Once downloaded, please refer to the readme inside the tarball for compile and run instructions.
11 comments
Hi, Naoki
ReplyDeleteCould you share all the modifications you made on the code?
Thanks,
Hi,
ReplyDeleteThank you for the impressive example. I want to know whether the same thing can be used for vtk image data just be replacing the unstructured vtk header files with vtk image data headers ?
Hi,
ReplyDeleteThank you very much! This was very helpful.
Hi,
ReplyDeleteGreat tutorial, just wanted to let you know you can also achieve this with the parallel file writers that vtk provides (i.e. vtkXMLPUnstructuredGridWriter)
This comment has been removed by the author.
DeleteHello!
ReplyDeleteI could not download the source.
Could you please re enable it?
Thank you!
It has been reuploaded.
DeleteHi,
ReplyDeleteI have a question that how to build VTK with CMake for parallel computing?
Thank you!
Hi,
ReplyDeleteCould you please refresh a link for download source code?
Regards
Kuba
It has been reuploaded.
DeleteCool and that i have a tremendous give: Who Repairs House Windows remodel old house
ReplyDelete