0

I am new with c++ and trying to send a bool datatype through MPI, but c++ does not support this data type.

I tried to make it MPI_BYTE and MPI_INT but it prints nothing.

#include <iostream>
#include "mpi.h"

using namespace std;

int main(int argc, char **argv)
{ 
    int R,P;
    MPI_Status status; 
    bool check = false;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &P);
    MPI_Comm_rank(MPI_COMM_WORLD, &R);
    if (R == 0)
    {
       check = true;
       MPI_Send(&check,1,MPI_BYTE,1,1,MPI_COMM_WORLD);
    }
    else if (R == 1)
    {  
       MPI_Recv(&check,1,MPI_BYTE,0,1,MPI_COMM_WORLD,&status);
       cout << R <<"\t check is \t"<< check << endl;
    }

    MPI_Finalize();
    return 0;
}

There are no error messages.

Ahmed Hady
  • 13
  • 1
  • 3
  • 1
    What do you mean by "it prints nothing"? Do you see `1 check is 0` or nothing at all? Maybe your output is redirected to a file? Or maybe you don't have 2 nodes ready? What is the value you got from `MPI_Comm_size`? Is `If` a typo while copying code here or is it present in your actual code? – Yksisarvinen Aug 21 '19 at 20:07
  • 1
    Since MPI doesn't support `bool`, it seems you'd be better off sending a `char`, rather than trying something which isn't supported. – john Aug 21 '19 at 20:08
  • 1
    "_there are no error messages_" 1) Compilation error messages? That doesn't mean anything, since your code can, still, contain undefined behavior. 2) Your code doesn't even check if `MPI_Send`, and `MPI_Recv` succeeded. How do you know, that they succeeded? 3) Doesn't your program have undefined behavior, since you are trying to send 1 element of `bool` type (which has size of `1` byte), but are telling the MPI, that you are sending an `int` (which has the typical size of `4` bytes)? – Algirdas Preidžius Aug 21 '19 at 20:11

1 Answers1

6

The standard (MPI 3.1 table 3.4 page 27) defines MPI_CXX_BOOL to be used with the C++ bool datatype.

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30