Note that there are some explanatory texts on larger screens.

plurals
  1. POMPI BCast (broadcast) of a std::vector of structs
    primarykey
    data
    text
    <p>I've got a question regarding passing of a std::vector of structs via MPI.</p> <p>First off, details. I'm using OpenMPI 1.4.3 (MPI-2 compliant) with gcc. Note that I can't use boost MPI or OOMPI -- I'm bound to using this version.</p> <p>I've got a struct to aggregate some data:</p> <pre><code> struct Delta { Delta() : dX(0.0), dY(0.0), dZ(0.0) {}; Delta(double dx, double dy, double dz) : dX(dx), dY(dy), dZ(dz) {}; Delta(const Delta&amp; rhs) : dX(rhs.dX), dY(rhs.dY), dZ(rhs.dZ) {}; double dX; double dY; double dZ; }; typedef std::vector&lt;Delta&gt; DeltaLine; </code></pre> <p>and I have a DeltaLine that I'd like to broadcast, via MPI, to all the nodes.</p> <p>Can I do the following safely and portably? This works for me in my test case. I just want to make sure it's legal and kosher across different platforms and according to the C++ and MPI standards.</p> <p>Thanks! Madeleine.</p> <pre><code> //Create an MPI struct for the Delta class const int nItems=3; int blocklengths[nItems] = {1, 1, 1}; MPI_Datatype types[nItems] = {MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE}; MPI_Datatype MPI_DeltaType; MPI_Aint offsets[nItems]; offsets[0] = offsetof(Delta, dX); offsets[1] = offsetof(Delta, dY); offsets[2] = offsetof(Delta, dZ); MPI_Type_create_struct(nItems, blocklengths, offsets, types, &amp;MPI_DeltaType); MPI_Type_commit(&amp;MPI_DeltaType); //This is the vector to be filled, and its size DeltaLine deltaLine; unsigned deltaLineSize; //If this is the master proc, get the DeltaLine and its size if(amMaster()) { deltaLine = getMasterDeltaLine(); deltaLineSize = deltaLine.size(); } //Send out the correct size MPI_Bcast(&amp;deltaLineSize, 1, MPI_UNSIGNED, COMM_PROC, MPI_COMM_WORLD); //Size the delta line vector, and broadcast its contents deltaLine.reserve(deltaLineSize); MPI_Bcast(&amp;deltaLine.front(), deltaLineSize, MPI_DeltaType, COMM_PROC, MPI_COMM_WORLD); //Free up the type MPI_Type_free(&amp;MPI_DeltaType); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload