I have one message which contains a unique id, and some information I need to send through MPI processes. To do so, I convert this message to an array of bits.
I convert one message to bit representation by using the std::bitset class. Now, I want to send it to another process with MPI.
I can use the function std::bitset::to_string() to convert each bit to a char; but the size of the message will increase to sizeof(char)*MSG_SIZE (where MSG_SIZE is equal to 256 in my case).
static const int MSG_SIZE = 256;
std::bitset<MSG_SIZE> msg;
msg = convert_to_bit(uint64_t uid, {additional information...});
// Using the function to_string(), my message is now of size (MSG_SIZE * sizeof(char))
// because each bit in the bitset is represented by a char (= 32 bits)
MPI_Send(msg.to_string().c_str(), msg.to_string().size(), MPI_BYTE, 1, 0, MPI_COMM_WORLD);
How can I avoid this situation, keep the size of the message equals to 256 bits ?
In fact, I would like a situation like this:
static const int MSG_SIZE = 256;
std::bitset<MSG_SIZE> msg;
msg = convert_to_bit(uint64_t uid, {additional information...});
// not necessary represented using char
// However I have no idea about which type I have to use
char* msg_in_bits = new char[MSG_SIZE / sizeof(char)];
msg_in_bits = do_something(msg);
MPI_Send(msg_in_bits, MSG_SIZE, MPI_BYTE, 1, 0, MPI_COMM_WORLD);
I only want to send a message of the real size of my message: MSG_SIZE = 256 bits. Not to increase the size of my message because I will represent each bit by a char (= 32bits). I want to represent a bit... by a bit, not a char.
Thank you
Something like this, not the only way to do it
#include <cstdint>
static const int MSG_SIZE = 256;
static const int MSG_SIZE_IN_BYTES = MSG_SIZE/8;
std::bitset<MSG_SIZE> msg = ...;
uint8_t msg_in_bits[MSG_SIZE_IN_BYTES] = {0};
for (int i = 0; i < MSG_SIZE; ++i)
if (msg[i])
msg_in_bits[i/8] |= 1 << (i%8);
MPI_Send(msg_in_bits, MSG_SIZE_IN_BYTES, MPI_BYTE, 1, 0, MPI_COMM_WORLD);