13 #include <Eigen/Dense>
17 #include "eckit/exception/Exceptions.h"
18 #include "eckit/mpi/Comm.h"
26 const eckit::mpi::Comm &
world();
29 const eckit::mpi::Comm &
myself();
35 template <
typename SERIALIZABLE>
36 void send(
const eckit::mpi::Comm & comm,
const SERIALIZABLE & sendobj,
37 const int dest,
const int tag) {
38 std::vector<double> sendbuf;
39 sendobj.serialize(sendbuf);
40 comm.send(sendbuf.data(), sendbuf.size(),
dest, tag);
45 template <
typename SERIALIZABLE>
46 void receive(
const eckit::mpi::Comm & comm, SERIALIZABLE & recvobj,
47 const int source,
const int tag) {
48 size_t sz = recvobj.serialSize();
49 std::vector<double> recvbuf(sz);
50 eckit::mpi::Status
status = comm.receive(recvbuf.data(), sz, source, tag);
52 recvobj.deserialize(recvbuf, ii);
58 void gather(
const eckit::mpi::Comm & comm,
const std::vector<double> &
send,
59 std::vector<double> & recv,
const size_t root);
63 template <
typename SERIALIZABLE>
64 void gather(
const eckit::mpi::Comm & comm,
const std::vector<SERIALIZABLE> &
send,
65 std::vector<SERIALIZABLE> & recv,
const size_t root) {
66 if (comm.size() > 1) {
67 std::vector<double> sendbuf;
68 std::vector<double> recvbuf;
70 for (
const SERIALIZABLE & jsend :
send) jsend.serialize(sendbuf);
72 gather(comm, sendbuf, recvbuf, root);
74 if (comm.rank() == root) {
76 for (SERIALIZABLE & jrecv : recv) jrecv.deserialize(recvbuf, indx);
86 void allGather(
const eckit::mpi::Comm & comm,
87 const Eigen::VectorXd &, std::vector<Eigen::VectorXd> &);
106 template <
typename CIter,
typename Iter>
109 std::vector<double> serializedLocalData;
110 for (CIter it = first; it != last; ++it)
111 it->serialize(serializedLocalData);
113 eckit::mpi::Buffer<double> buffer(comm.size());
114 comm.allGatherv(serializedLocalData.begin(), serializedLocalData.end(), buffer);
116 size_t numDeserializedDoubles = 0;
117 for (Iter it = recvbuf; numDeserializedDoubles != buffer.buffer.size(); ++it)
118 it->deserialize(buffer.buffer, numDeserializedDoubles);