mpi4py and VTK

We recently added mpi4py as one of the third party libraries in VTK. Below is a quote from the mpi4py explaining what it is.

MPI for Python (mpi4py) provides bindings of the Message Passing Interface (MPI) standard for the Python programming language, allowing any Python program to exploit multiple processors.

This package is constructed on top of the MPI–1/MPI–2 specification and provides an object oriented interface which closely follows MPI–2 C++ bindings. It supports point-to-point (sends, receives) and collective (broadcasts, scatters, gathers) communications of any picklable Python object as well as optimized communications of Python object exposing the single-segment buffer interface (NumPy arrays, builtin bytes/string/array objects).

See the mpi4py page for details.

We have been using mpi4py in ParaView for several years and with the recent introduction of the numpy_interface module to VTK, we decided to move the mpi4py dependency to VTK as well. This allowed us to support data parallelism with MPI in the numpy_interface module. I will discuss this in an upcoming blog in more detail.

Using mpi4py is pretty straightforward. The following can be called from vtkpython, pvtkpython and python.

Note that if you are going to mix parallel VTK and mpi4py, we recommend using pvtkpython, which initializes several VTK data structures that make it easier for algorithms to access MPI communicators.

VTK also provides a Python-accessible interface to MPI in the vtkMPIController and vtkMPICommunicator classes. However, these classes were not designed to be used from Python and as such provide only a small set of methods. Most often, you will use mpi4py when coding in Python.

In some cases, specially when using MPI groups, it is necessary to pass the communicator used by VTK to mpi4py or vice versa. We developed a simple utility class to enable this. This class is called vtkMPI4PyCommunicator and is used as follows.

Since mpi4py works very nicely with numpy arrays and VTKArray is a subclass of numpy.ndarray (see previous posts [1], [2] and [3]), it is very straightforward to use them together as follows.

When this is executed as

it prints

In my next blog, I will talk about how to use the algorithms module in parallel. Until then, happy Message Passing.

Many thanks to Ben Boeckel for moving mpi4py to VTK and implementing vtkMPI4PyCommunicator.

2 Responses to mpi4py and VTK

  1. Phil S. says:

    The syntax in your examples is a little confusing. Did you import vtkMPI4Py? When I try to use it with vtk.vtkMPI4Py I am getting an error that it is not defined. Also you have a variable controller which I’m assuming should be contr (or else it is not defined either). I have a script that I can run with pvtkpython, but I am trying to test setting up my own controller. Can you clear up these definitions please?

  2. Phil S. says:

    I just checked my cmake build and noticed I missed the vtkMPI4Py flag.

Questions or comments are always welcome!