1

I am trying to build game of life MPI version. However, when I send out true, I received false, since there is no MPI_BOOL, I used MPI_C_BOOL. The code snippet is below.

vector<bool> send_data_top;
vector<bool> recv_data_top(n);
vector<bool> send_data_down;
vector<bool> recv_data_down(n);

request = new MPI_Request[4];
for (int j = 0; j < n; j++) {
    send_data_top.push_back(neigh_grid[n + j + 3]);
    send_data_down.push_back(neigh_grid[last_row_index * (n + 2) + j + 1]);
    cout << "send data: " << send_data_down[j] << endl;
    cout << "send data top: " << send_data_top[j] << endl;
}
MPI_Isend(&send_data_top[0], n, MPI_C_BOOL, id - 1, 0, MPI_COMM_WORLD, &request[0]);
MPI_Isend(&send_data_down[0], n, MPI_C_BOOL, 0, 0, MPI_COMM_WORLD, &request[1]);
MPI_Irecv(&recv_data_top[0], n, MPI_C_BOOL, id - 1, 0, MPI_COMM_WORLD, &request[2]);
MPI_Irecv(&recv_data_down[0], n, MPI_C_BOOL, 0, 0, MPI_COMM_WORLD, &request[3]);
MPI_Waitall(4, request, MPI_STATUS_IGNORE);
delete[] request;
//assign receive values to neigh_grid
for (int j = 0; j < n; j++) {
    neigh_grid[j + 1] = recv_data_top[j];
    neigh_grid[(last_row_index + 1) * (n + 2) + j + 1] = recv_data_down[j];
    cout << recv_data_top[j] << endl;
    cout << recv_data_down[j] << endl;
}

The output is

send data: 1
send data top: 1
0
0

I have tried changing the type to MPI_CXX_BOOL and MPI_INT but none of them work and I cannot find a similar situation online. Can anyone figure out what caused this?

Xinyu Xiong
  • 53
  • 1
  • 6
  • 1
    `operator[]` of `std::vector` returns a reference, which is [_proxy class representing a reference to a single bool_](https://en.cppreference.com/w/cpp/container/vector_bool). This simply won't work, use a vector of chars instead together with `MPI_CHAR`. BTW, `MPI_C_BOOL` is for C's type `_Bool`, not for C++'s `bool` anyway. – Daniel Langr Feb 19 '20 at 16:09
  • 1
    Does this answer your question? [Send a c++ std::vector via mpi](https://stackoverflow.com/questions/59860967/send-a-c-stdvectorbool-via-mpi) – Daniel Langr Feb 19 '20 at 16:10
  • Yea verily, `vector` is truly a strange beast unlike all other containers. – user4581301 Feb 19 '20 at 16:15
  • @DanielLangr then why MPI_INT doesn't work since true and false are regarded as 1 and 0 by default – Xinyu Xiong Feb 19 '20 at 16:23
  • @XinyuXiong If you use `MPI_INT`, then you need to have a vector of ints (`std::vector`). – Daniel Langr Feb 19 '20 at 17:27

0 Answers0