gatherv.h 5.65 KB
Newer Older
Lorenz Hübschle-Schneider's avatar
Import  
Lorenz Hübschle-Schneider committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#pragma once

#include <errno.h>
#include <mpi.h>

#include <cstdint>
#include <cstring>
#include <numeric>
#include <vector>

#include <boost/mpi/communicator.hpp>
#include <boost/mpi/collectives.hpp>
#include <boost/mpi/packed_iarchive.hpp>
#include <boost/mpi/packed_oarchive.hpp>

Lorenz Hübschle-Schneider's avatar
Fixup  
Lorenz Hübschle-Schneider committed
16
#include <boost/serialization/string.hpp>
Lorenz Hübschle-Schneider's avatar
Import  
Lorenz Hübschle-Schneider committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#include <boost/serialization/utility.hpp>
#include <boost/serialization/vector.hpp>

#include "common.h"

namespace unsafe_mpi {

template <typename T, typename transmit_type = uint64_t>
static void gatherv_trivial(const boost::mpi::communicator &comm,
                            const std::vector<T> &in, std::vector<T> &out,
                            const int root) {

    static_assert((sizeof(T)/sizeof(transmit_type)) * sizeof(transmit_type) == sizeof(T),
                  "Invalid transmit_type for element type (sizeof(transmit_type) is not a multiple of sizeof(T))");

    // exchange sizes
    const int factor = sizeof(T) / sizeof(transmit_type);
    const int sendsize = static_cast<int>(in.size() * factor);

    const auto datatype = boost::mpi::get_mpi_datatype<transmit_type>();
    const auto sendptr = reinterpret_cast<const transmit_type*>(in.data());

    if (comm.rank() == root) {
        // Receive sizes
        std::vector<int> sizes;
        sizes.reserve(comm.size());
        boost::mpi::gather(comm, sendsize, sizes, root);

        // Calculate displacements from spaces
        std::vector<int> displacements(sizes.size() + 1);
        std::partial_sum(sizes.begin(), sizes.end(), displacements.begin() + 1);
        const int outsize = displacements.back();
        // Allocate space
        // in terms of #elements -> divide by factor
        out.resize(outsize / factor);

        auto recvptr = reinterpret_cast<transmit_type*>(out.data());
        MPI_Gatherv(sendptr, sendsize, datatype,
                    recvptr, sizes.data(), displacements.data(),
                    datatype, root, comm);
    } else {
        // send size, then gather
        boost::mpi::gather(comm, sendsize, root);
        MPI_Gatherv(sendptr, sendsize, datatype,
                    nullptr, nullptr, nullptr,
                    datatype, root, comm);
    }
}

// UNTESTED, mostly copied from allgatherv
template <typename T>
static void gatherv_serialize(const boost::mpi::communicator &comm, const std::vector<T> &in, std::vector<T> &out, const int root) {
    // Step 1: serialize input data
    boost::mpi::packed_oarchive oa(comm);
    if (!in.empty())
        oa << in;

    // Step 2: exchange sizes (archives' .size() is measured in bytes)
    // Need to cast to int because this is what MPI uses as size_t...
    const int in_size = static_cast<int>(in.size()),
        transmit_size = (in.empty() ? 0 : static_cast<int>(oa.size()));
    // If in.empty(), transmit_size is 0 so we don't really care
    auto sendptr = const_cast<void*>(oa.address());

    if (comm.rank() == root) {
        std::vector<int> in_sizes(comm.size()), transmit_sizes(comm.size());
        boost::mpi::gather<int>(comm, in_size,       in_sizes.data(), root);
        boost::mpi::gather<int>(comm, transmit_size, transmit_sizes.data(), root);

        // Step 3: calculate displacements from sizes (prefix sum)
        std::vector<int> displacements(comm.size() + 1);
        displacements[0] = sizeof(boost::mpi::packed_iarchive);
        for (int i = 1; i <= comm.size(); ++i) {
            displacements[i] = displacements[i-1] + transmit_sizes[i-1];
        }

        // Step 4: allocate space for result and MPI_Allgatherv
        char* recv = new char[displacements.back()];

        int status = MPI_Gatherv(sendptr, transmit_size, MPI_PACKED, recv,
                                 transmit_sizes.data(), displacements.data(),
                                 MPI_PACKED, root, comm);

        if (status != 0) {
            ERR << "MPI_Allgatherv returned " << status << ", errno " << errno << std::endl;
            return;
        }


        // Step 5: deserialize received data
        // Preallocate storage to prevent reallocations
        std::vector<T> temp;
        size_t largest_size = *std::max_element(in_sizes.begin(), in_sizes.end());
        temp.reserve(largest_size);
        out.reserve(std::accumulate(in_sizes.begin(), in_sizes.end(), 0));

        // Deserialize archives one by one, inserting elements at the end of ̀out̀
        for (int i = 0; i < comm.size(); ++i) {
            if (in_sizes[i] == 0) {
                // We can ignore processes which didn't have anything to send
                continue;
            }
            boost::mpi::packed_iarchive archive(comm);
            archive.resize(transmit_sizes[i]);
            memcpy(archive.address(), recv + displacements[i], transmit_sizes[i]);

            temp.clear();
            temp.resize(in_sizes[i]);
            archive >> temp;
            out.insert(out.end(), temp.begin(), temp.end());
        }

    } else {
        boost::mpi::gather<int>(comm, in_size, root);
        boost::mpi::gather<int>(comm, transmit_size, root);

        int status = MPI_Gatherv(sendptr, transmit_size, MPI_PACKED,
                                 nullptr, nullptr, nullptr,
                                 MPI_PACKED, root, comm);
        if (status != 0) {
            ERR << "MPI_Allgatherv returned " << status << ", errno " << errno << std::endl;
            return;
        }


    }

}

Lorenz Hübschle-Schneider's avatar
Fixup  
Lorenz Hübschle-Schneider committed
146 147 148 149 150 151 152 153 154 155

template <typename T, typename transmit_type = uint64_t>
static void gatherv(const boost::mpi::communicator &comm, const std::vector<T> &in, std::vector<T> &out, const int root) {
    if (is_trivial_enough<T>::value) {
        gatherv_trivial<T, transmit_type>(comm, in, out, root);
    } else {
        gatherv_serialize<T>(comm, in, out, root);
    }
}

Lorenz Hübschle-Schneider's avatar
Import  
Lorenz Hübschle-Schneider committed
156
}