9#include <kernel/util/dist.hpp>
10#include <kernel/util/time_stamp.hpp>
11#include <kernel/util/statistics.hpp>
12#include <kernel/lafem/matrix_mirror.hpp>
13#include <kernel/lafem/power_diag_matrix.hpp>
37 template<
typename MT_,
typename VMT_>
48#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
77#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
79 const std::vector<VMT_>& mirrors_row,
const std::vector<VMT_>& mirrors_col) :
84 _recv_reqs(ranks.size()),
86 _recv_bufs(ranks.size())
88 const std::size_t n = ranks.size();
90 XASSERTM(mirrors_row.size() == n,
"invalid row vector mirror count");
91 XASSERTM(mirrors_col.size() == n,
"invalid column vector mirror count");
96 for(std::size_t i(0); i < n; ++i)
98 const VMT_& mir_r = mirrors_row.at(i);
99 const VMT_& mir_c = mirrors_col.at(i);
106 SynchMatrix(
const Dist::Comm&,
const std::vector<int>& ranks,
const std::vector<VMT_>&,
const std::vector<VMT_>&) :
125#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
128 XASSERTM(!_initialized,
"SynchMatrix object is already initialized");
130 const std::size_t n =
_ranks.size();
133 for(std::size_t i(0); i < n; ++i)
139 std::vector<std::array<Index,4>> recv_dims(n), send_dims(n);
142 for(std::size_t i(0); i < n; ++i)
144 _recv_reqs[i] =
_comm.
irecv(recv_dims.at(i).data(), std::size_t(4),
_ranks.at(i));
148 for(std::size_t i(0); i < n; ++i)
151 send_dims.at(i)[0] = sbuf.
num_rows();
152 send_dims.at(i)[1] = sbuf.
num_cols();
154 send_dims.at(i)[3] = sbuf.
num_nzes();
162 for(std::size_t i(0); i < n; ++i)
165 Index nrows = recv_dims.at(i)[0];
166 Index ncols = recv_dims.at(i)[1];
167 Index nepnz = recv_dims.at(i)[2];
168 Index nnze = recv_dims.at(i)[3];
174 std::vector<Memory::TypedView<typename MT_::IndexType>> recv_views(n);
175 std::vector<Memory::TypedView<typename MT_::IndexType>> send_views(n);
178 for(std::size_t i(0); i < n; ++i)
180 recv_views[i] = _recv_bufs.at(i).row_ptr_view_w();
181 _recv_reqs[i] =
_comm.
irecv(recv_views[i].get_w(), _recv_bufs.at(i).num_rows() + std::size_t(1),
_ranks.at(i));
188 for(std::size_t i(0); i < n; ++i)
190 send_views[i] =
_send_bufs.at(i).row_ptr_view_r();
198 for(std::size_t i(0); i < n; ++i)
200 recv_views[i] = _recv_bufs.at(i).col_idx_view_w();
201 _recv_reqs[i] =
_comm.
irecv(recv_views[i].get_w(), _recv_bufs.at(i).num_nzes(),
_ranks.at(i));
208 for(std::size_t i(0); i < n; ++i)
210 send_views[i] =
_send_bufs.at(i).col_idx_view_r();
221 void init(
const MT_&)
223 XASSERTM(!_initialized,
"SynchMatrix object is already initialized");
234#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
237 XASSERTM(_initialized,
"SynchMatrix object has not been initialized");
239 const std::size_t n =
_ranks.size();
241 std::vector<Memory::TypedView<typename MT_::DataType>> recv_views(n);
242 std::vector<Memory::TypedView<typename MT_::DataType>> send_views(n);
245 for(std::size_t i(0); i < n; ++i)
250 recv_views[i] = buf.val_view_w();
255 for(std::size_t i(0); i < n; ++i)
263 send_views[i] = buf.val_view_r();
268 for(std::size_t idx(0u); _recv_reqs.
wait_any(idx); )
271 recv_views[idx].release();
272 _mirrors.at(idx).scatter_axpy(matrix, _recv_bufs.at(idx));
281 XASSERTM(_initialized,
"SynchMatrix object has not been initialized");
286 template <
typename MT_,
typename SVMT_,
int blocks_>
290 using VMT_ =
typename SVMT_::SubMirrorType;
294#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
295 std::vector<std::shared_ptr<SynchMatrix<MT_, VMT_>>> synch_matrix_list;
296 std::vector<std::vector<VMT_>> mirrors_row_split;
297 std::vector<std::vector<VMT_>> mirrors_col_split;
300#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
302 const std::vector<SVMT_>& mirrors_row,
const std::vector<SVMT_>& mirrors_col) :
304 synch_matrix_list(blocks_),
305 mirrors_row_split(blocks_),
306 mirrors_col_split(blocks_)
308 for (
int block(0) ; block < blocks_ ; ++block)
310 for (
Index i(0) ; i < mirrors_row.size() ; ++i)
314 for (
Index i(0) ; i < mirrors_col.size() ; ++i)
319 synch_matrix_list.at((
size_t)block) = std::make_shared<SynchMatrix<MT_, VMT_>>(comm, ranks, mirrors_row_split.at((
size_t)block), mirrors_col_split.at((
size_t)block));
324 SynchMatrix(
const Dist::Comm&,
const std::vector<int>& ranks,
const std::vector<SVMT_>&,
const std::vector<SVMT_>&) :
331#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
334 XASSERTM(!_initialized,
"SynchMatrix object is already initialized");
336 for (
int block(0) ; block < blocks_ ; ++block)
338 synch_matrix_list.at((
size_t)block)->init(matrix.
get(block, block));
346 XASSERTM(!_initialized,
"SynchMatrix object is already initialized");
351#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
354 XASSERTM(_initialized,
"SynchMatrix object has not been initialized");
356 for (
int block(0) ; block < blocks_ ; ++block)
358 synch_matrix_list.at((
size_t)block)->exec(matrix.
get(block, block));
364 XASSERTM(_initialized,
"SynchMatrix object has not been initialized");
389 template<
typename MT_,
typename VMT_>
391 const std::vector<VMT_>& mirrors_row,
const std::vector<VMT_>& mirrors_col)
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
Communication Request vector class.
Request & get_request(std::size_t idx)
Returns a (const) reference to a single request in the vector.
bool wait_any(std::size_t &idx, Status &status)
Blocks until one of the active requests has been fulfilled.
void wait_all()
Blocks until all active requests are fulfilled.
Ticket class for asynchronous global matrix conversion.
std::vector< BufferMatrixType > _send_bufs
send and receive buffers
void exec(MT_ &matrix)
Converts a type-0 matrix to a type-1 matrix.
LAFEM::MatrixMirrorBuffer< typename MT_::DataType, typename MT_::IndexType > BufferMatrixType
the buffer matrix type
SynchMatrix(const SynchMatrix &)=delete
deleted copy constructor
SynchMatrix & operator=(const SynchMatrix &)=delete
deleted copy assignment operator
std::vector< int > _ranks
the neighbor ranks
Dist::RequestVector _send_reqs
send and receive request vectors
SynchMatrix(const Dist::Comm &comm, const std::vector< int > &ranks, const std::vector< VMT_ > &mirrors_row, const std::vector< VMT_ > &mirrors_col)
Constructor.
LAFEM::MatrixMirror< typename MT_::DataType, typename MT_::IndexType > MatrixMirrorType
the matrix mirror type
const Dist::Comm & _comm
our communicator
void init(const MT_ &matrix)
Initializes the internal buffers for synchronization.
std::vector< MatrixMirrorType > _mirrors
the matrix mirrors
Matrix Mirror Buffer class template.
Index num_rows() const
Retrieve matrix row count.
Index num_cols() const
Retrieve matrix column count.
Index num_nzes() const
Retrieve non zero element count.
Index num_nzes_raw() const
Retrieve total length of value array.
Index entries_per_nonzero() const
Retrieve entries per non zero element count.
Matrix-Mirror class template.
Power-Diag-Matrix meta class template.
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
void synch_matrix(MT_ &target, const Dist::Comm &comm, const std::vector< int > &ranks, const std::vector< VMT_ > &mirrors_row, const std::vector< VMT_ > &mirrors_col)
Synchronizes a type-0 matrix.
std::uint64_t Index
Index data type.