Automate MPI custom type creation with C++

Introduction

To create a custom (or derived) MPI datatype for a struct or tuple, we need to find block lengths, displacements and type of each member. This is usually hard-coded case by case and takes valuable time. Here, with the aid of C++20, variadic functions, and type-traits, I created a simple header library to automate this task.

Compiler

GCC 11 with flag std=c++20.

Code

The code with examples is on GitHub Here. To use it in your project only header MpiTypeMaker.h is needed.

Struct

If you have a struct like:

struct Particle
{
	float Index;
	char Name[3];
	std::array<double, 2> Location;
};

And you want to send and receive it via MPI. You just add the header,MpiTypeMaker.h, and call CreateCustomMpiType() with first an object of the struct and then all its members:

#include "MpiTypeMaker.h"

Particle p;
auto particleMpiType = 
    CreateCustomMpiType(p, p.Location, p.Index, p.Name);

That’s it. Now you can send a particle like:

MPI_Send(&particle, 1, particleMpiType, 0, 0, MPI_COMM_WORLD);

Tuple

For tuple the prcess is even easier, you don’t need to name the members. For example, for this tuple:

int id = 50;
array<char, 4> name{'J', 'a', 'c', 'k'};
array<double,2> data{7.1, 8.1};

auto person = make_tuple(id, name, data);

Just run this command:

auto personMpiType = CreateTupleMpiType(person);

Now send it like:

MPI_Send(&person, 1, personMpiType, 0, 0, MPI_COMM_WORLD);

Notes

Structs and tuples must be made of

  • primitive types like int, double, and char,
  • their C-Arrays like int[5]
  • or their std::array like std::array<double, 3>.

Do not use pointers, references or similar concepts like std::vector as members.

For a struct, if you miss adding a member to CreateCustomMpiType, the code still works and the MPI will ignore that member.

Run examples

Clone the project from GitHub:

git clone https://github.com/sorush-khajepor/MpiTypeMaker.git

In a terminal go into the downloaded folder and run

mkdir build
cd build
cmake ..
make

The source files of examples are in somePath/MpiTypeMaker/examples/. After taking the above step they are compiled in build folder. In a terminal opened there, run examples like:

mpirun -np 2 particles_struct 

Subscribe

I notify you of my new posts

Latest Posts

Comments

0 comment