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;
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.
Here each processor should give a file name with "filename_(mpi_rank).vtu" which will hint the "filename.pvtu" in which files it should merge while visualizing in an visualization software (i.e. Paraview, VisIt).
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.
After importing the main ".pvtu" file, one can see below that all the partitions show up as single unified mesh.
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.




