Search code examples
c++boostmpibroadcastopenmpi

How to broadcast a boost vector using MPI?


I am trying to broadcast a boost vector to the other processes from my manager process as follows:

MPI_Bcast(&b, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);

where b is defined as:

boost::numeric::ublas::vector<double> b(N);

The vector b is initialized by the manager process and then broadcasted to the worker processes, however, if I print the vector b out, I still get random results for all processes which are not the manager process. Am I broadcasting incorrectly? (To be clear, all processes are calling on MPI_Bcast, not just the manager process.)


Solution

  • Broadcasting call requires access to raw data. Boost uBLAS vector is opaque data structure, which does not guarantee raw data is at the start of the vector (it might be length, or pointer, or whatever developer feels is good to make uBLAS vector), or whether it won't change order between BOOST releases. Typical trick, which works for STL vector as well, is to get first element of the vector and take its address

    boost::numeric::ublas::vector<double> b(N);
    
    MPI_Bcast(&b[0], N, MPI_DOUBLE, 0, MPI_COMM_WORLD);