Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The C++ standard guarantees that the elements of <code>std::vector</code> are stored contiguously in memory and that <code>std::vector::reserve()</code> (re-)allocates memory if necessary at the time of call, therefore your solution is perfectly valid from memory management point of view. Though, as Solkar noted, <code>std::vector::reserve()</code> only reserves memory space but the vector object is not aware that there is data being directly written in that memory and therefore keeps the previous element count (zero for freshly created vectors). This can be fixed by calling <code>std::vector::resize()</code> before the second broadcast operation.</p> <p>One comment though that applies to all cases when constructed MPI datatypes are used to send arrays - you should take care of possible padding between the consecutive array elements. In other words, it is possible for the following to hold because of possible padding at the end of the <code>struct</code>:</p> <pre><code>(char*)&amp;deltaLine[1] - (char*)&amp;deltaLine[0] != mpi_extentof(MPI_DeltaType) </code></pre> <p>where <code>mpi_extentof</code> is the extent of the MPI datatype as returned by <code>MPI_Type_get_extent()</code>. Because MPI uses the extent to determine where each array element starts, it is advisable to explicitly set it for any structure type that is used to send more than one element. With MPI-1 this is typically done by adding one special structure element of the <code>MPI_UB</code> pseudotype, but in modern MPI code (or in MPI-2 in general) one should use <code>MPI_Type_create_resized</code> for that purpose:</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_proto, 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_proto); // Resize the type so that its length matches the actual structure length // Get the constructed type lower bound and extent MPI_Aint lb, extent; MPI_Type_get_extent(MPI_DeltaType_proto, &amp;lb, &amp;extent); // Get the actual distance between to vector elements // (this might not be the best way to do it - if so, substitute a better one) extent = (char*)&amp;deltaLine[1] - (char*)&amp;deltaLine[0]; // Create a resized type whose extent matches the actual distance MPI_Type_create_resized(MPI_DeltaType_proto, lb, extent, &amp;MPI_DeltaType); MPI_Type_commit(&amp;MPI_DeltaType); </code></pre> <p>In your case there are only <code>double</code> elements in the structure and no padding is expected, therefore doing this all is not necessary. But keep it in mind for your future work with MPI.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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