4

I know that the storage of a std::vector<bool> is not necessarily an array of bools .

If I want to send receive int data stored in a std::vector<int>, I would use MPI_Send(vect.data(),num_of_ints,MPI_INT,dest_rk,tag,comm).

How should I use MPI_Send to send a std::vector<bool> ? In particular :

  • Can / should I use vect.data() as the pointer to buffer ?
  • What MPI type should I give ? Somehow, I feel like MPI_CXX_BOOL does not apply (see this question)
  • What number of elements should I give ? (related to the previous point)
Christophe
  • 68,716
  • 7
  • 72
  • 138
G. Fougeron
  • 318
  • 1
  • 13
  • 1
    check [this answer regarding serialization](https://stackoverflow.com/questions/51230764/serialization-deserialization-of-a-vector-of-integers-in-c) – nivpeled Jan 22 '20 at 13:37
  • 1
    i would not use a `std::vector` in this case, because strictly speaking it is not a vector of `bool`s. `std::vector` or a vector of `struct Bool { bool x;};` are possible alternatives – 463035818_is_not_an_ai Jan 22 '20 at 13:45
  • You can also look into using `boost::vector` which is the standard vector where T=bool. – rubenvb Jan 22 '20 at 13:47

1 Answers1

5

std::vector<bool> specialization does not have the data() member function. The underlying storage scheme is not specified by the standard:

There is no requirement that the data be stored as a contiguous allocation of bool values. A space-optimized representation of bits is recommended instead.

The only reasonable option to send std::vector<bool> is to copy it into a vector of char (or some similar type like std::int8_t), and then send that vector. A better option might be to avoid std::vector<bool> in your code from the very beginning.

Evg
  • 25,259
  • 5
  • 41
  • 83