tom
tom

Reputation: 413

Defining custom MPI type based on preprocessor directive

Just a quick question regarding MPI datatypes, I want to offer both single and double precision support which I do through preprocessor directives. For my sequential program I do

#if defined(USE_SINGLE_PRECISION)
  using floatT = float;
#elif defined(USE_DOUBLE_PRECISION)
  using floatT = double;
#endif

Naively I though I could do something similar for MPI, i.e.

#if defined(USE_SINGLE_PRECISION)
  using MPI_FLOAT_T = MPI_FLOAT;
#elif defined(USE_DOUBLE_PRECISION)
  using MPI_FLOAT_T = MPI_DOUBLE;
#endif

It seems though, that MPI_FLOAT and MPI_DOUBLE are not quite what I would expect them to be. Is there an easy fix for this? I am probably just missing something here.

Upvotes: 0

Views: 145

Answers (1)

Gilles Gouaillardet
Gilles Gouaillardet

Reputation: 8395

MPI_FLOAT and MPI_DOUBLE are not types (like float or double), but "variables" of type MPI_Datatype, so the C++ using syntax cannot be using here.

FWIW, MPI_Datatype is an opaque type that is implemented as

  • an int in MPICH and its derivatives (and MPI_FLOAT is a hardcoded integer)
  • an opaque pointer in Open MPI

A portable way to solve your issue could be to

#if defined(USE_SINGLE_PRECISION)
  #define MPI_FLOAT_T MPI_FLOAT
#elif defined(USE_DOUBLE_PRECISION)
  #define MPI_FLOAT_T MPI_DOUBLE
#endif

Note there might be a more C++ish way of doing it, but I am not the right person to advise on that.

Upvotes: 2

Related Questions