How to write VTK files in parallel

By 5:24 AM , , , , , , , ,

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.

You Might Also Like


  1. Hi, Naoki

    Could you share all the modifications you made on the code?


  2. Hi,

    Thanks for providing this tutorial. Initializer list doesn't work. It is only allowed when we declare the array. But I modified it accordingly and it works now.

    Hi Naoki,

    Just put the LIBLINK2 at the end in the compile line of Makefile.

  3. Hi,

    Thank 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 ?

  4. Hi,

    Thank you very much! This was very helpful.

  5. Hi,

    Great tutorial, just wanted to let you know you can also achieve this with the parallel file writers that vtk provides (i.e. vtkXMLPUnstructuredGridWriter)

  6. Hello!
    I could not download the source.
    Could you please re enable it?
    Thank you!

  7. Hi,

    I have a question that how to build VTK with CMake for parallel computing?

    Thank you!

  8. Hi,
    Could you please refresh a link for download source code?