Does MPI provide preprocessor macros?
Asked Answered
M

1

11

Does MPI standard provide a preprocessor macro, so my C/C++ code could branch if it is compiled by MPI-enabled compiler? Something like _OPENMP macro for OpenMP.

Mediatorial answered 5/2, 2015 at 21:3 Comment(2)
I want to #include "mpi.h" only if #if defined(_MPI)Mediatorial
Unfortunately, this feature doesn't exist because MPI is a library and doesn't assume any compiler support. You don't even need to use e.g. CC=mpicc to compile MPI codes. You can define this feature in your application/library build system though.Vanegas
S
6

According to the MPI standard (page 335), you can check for the MPI_VERSION macro:

In order to cope with changes to the MPI Standard, there are both compile-time and runtime ways to determine which version of the standard is in use in the environment one is using.

The "version" will be represented by two separate integers, for the version and subversion:

In C,

#define MPI_VERSION 3
#define MPI_SUBVERSION 0
Six answered 5/2, 2015 at 21:8 Comment(9)
I tried with OpenMPI 1.6.5 and MPI_VERSION is not defined. So, apparently, not all MPI implementations support it :(.Mediatorial
I don't know man, OpenMPI has had those defines in mpi.h since the birth of the OpenMPI project: see commit github.com/open-mpi/ompi/commit/566a050c2 -- though as it's one of the big "land huge chunks of the initial skeleton" commits, github won't show line 120 of ompi/include/mpi.h.in ... but it's in there.Adoration
@Rob: The problem seems to be that the macro is defined in the header, which sort of the defeats the OP's purpose of detecting whether he can include the header or not :-)Six
@Six That explains it. I wish it was defined by the compiler.Mediatorial
There is no MPI compiler. The compiler you use has no idea about MPI. It's like any other library. That's a fundamental difference w.r.t. OpenMP.Vanegas
This macro is only defined after inclusion of mpi.h, so does not answer the question. Explicitly, the user only wants to include mpi.h IF the compiler is mpic / mpic++. Check with 'echo | mpic++ -dM -E -' and 'echo "#include <mpi.h>" | mpic++ -dM -E -' .Cardiganshire
@wessel: Yes. My answer predates that clarification. I left it since it's still useful despite it not answering the question as it stands. Feel free to submit a better answer, which I will upvote ;-)Six
Yeah, I see, I should have read everything a bit more carefully. Sorry about that. And I think that you're right, and there is no way to detect mpicc. It defines zero new macros, at least that goes for OpenMPI.Cardiganshire
this is insanity! If I want to conditionally compile if OpenMP or MPI is available to me, I use MY OWN MACRO! #ifdef USE_MPI #include "mpi.h" and then guard the code calling MPI with the same #ifdef USE_MPI MPI_Init().Lawler

© 2022 - 2024 — McMap. All rights reserved.