FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
synch_mat.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
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>
14
15#include <vector>
16#include <array>
17
18namespace FEAT
19{
20 namespace Global
21 {
37 template<typename MT_, typename VMT_>
39 {
40 public:
45
46 protected:
47 bool _initialized;
48#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
52 std::vector<int> _ranks;
54 std::vector<MatrixMirrorType> _mirrors;
58 std::vector<BufferMatrixType> _send_bufs, _recv_bufs;
59#endif // FEAT_HAVE_MPI || DOXYGEN
60
61 public:
77#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
78 SynchMatrix(const Dist::Comm& comm, const std::vector<int>& ranks,
79 const std::vector<VMT_>& mirrors_row, const std::vector<VMT_>& mirrors_col) :
80 _initialized(false),
81 _comm(comm),
82 _ranks(ranks),
83 _send_reqs(ranks.size()),
84 _recv_reqs(ranks.size()),
85 _send_bufs(ranks.size()),
86 _recv_bufs(ranks.size())
87 {
88 const std::size_t n = ranks.size();
89
90 XASSERTM(mirrors_row.size() == n, "invalid row vector mirror count");
91 XASSERTM(mirrors_col.size() == n, "invalid column vector mirror count");
92
93 _mirrors.reserve(n);
94
95 // create matrix mirrors and buffers
96 for(std::size_t i(0); i < n; ++i)
97 {
98 const VMT_& mir_r = mirrors_row.at(i);
99 const VMT_& mir_c = mirrors_col.at(i);
100
101 // create matrix mirror
102 _mirrors.push_back(MatrixMirrorType(mir_r, mir_c));
103 }
104 }
105#else // non-MPI version
106 SynchMatrix(const Dist::Comm&, const std::vector<int>& ranks, const std::vector<VMT_>&, const std::vector<VMT_>&) :
107 _initialized(false)
108 {
109 XASSERT(ranks.empty());
110 }
111#endif // FEAT_HAVE_MPI
112
114 SynchMatrix(const SynchMatrix &) = delete;
116 SynchMatrix & operator=(const SynchMatrix &) = delete;
117
125#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
126 void init(const MT_& matrix)
127 {
128 XASSERTM(!_initialized, "SynchMatrix object is already initialized");
129
130 const std::size_t n = _ranks.size();
131
132 // create matrix mirror buffers
133 for(std::size_t i(0); i < n; ++i)
134 {
135 _send_bufs.at(i) = _mirrors.at(i).create_buffer(matrix);
136 }
137
138 // receive buffer dimensions vector
139 std::vector<std::array<Index,4>> recv_dims(n), send_dims(n);
140
141 // post send-buffer dimension receives
142 for(std::size_t i(0); i < n; ++i)
143 {
144 _recv_reqs[i] = _comm.irecv(recv_dims.at(i).data(), std::size_t(4), _ranks.at(i));
145 }
146
147 // send send-buffer dimensions
148 for(std::size_t i(0); i < n; ++i)
149 {
150 const BufferMatrixType& sbuf = _send_bufs.at(i);
151 send_dims.at(i)[0] = sbuf.num_rows();
152 send_dims.at(i)[1] = sbuf.num_cols();
153 send_dims.at(i)[2] = sbuf.entries_per_nonzero();
154 send_dims.at(i)[3] = sbuf.num_nzes();
155 _send_reqs[i] = _comm.isend(send_dims.at(i).data(), std::size_t(4), _ranks.at(i));
156 }
157
158 // wait for all receives to finish
159 _recv_reqs.wait_all();
160
161 // create receive buffers and post receives
162 for(std::size_t i(0); i < n; ++i)
163 {
164 // get the receive buffer dimensions
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];
169
170 // allocate receive buffer
171 _recv_bufs.at(i) = BufferMatrixType(nrows, ncols, nnze, nepnz);
172 }
173
174 std::vector<Memory::TypedView<typename MT_::IndexType>> recv_views(n);
175 std::vector<Memory::TypedView<typename MT_::IndexType>> send_views(n);
176
177 // post buffer row-pointer array receives
178 for(std::size_t i(0); i < n; ++i)
179 {
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));
182 }
183
184 // wait for all previous sends to finish
186
187 // post buffer row-pointer array sends
188 for(std::size_t i(0); i < n; ++i)
189 {
190 send_views[i] = _send_bufs.at(i).row_ptr_view_r();
191 _send_reqs[i] = _comm.isend(send_views[i].get_r(), _send_bufs.at(i).num_rows() + std::size_t(1), _ranks.at(i));
192 }
193
194 // wait for all previous receives to finish
195 _recv_reqs.wait_all();
196
197 // post buffer column-index array receives
198 for(std::size_t i(0); i < n; ++i)
199 {
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));
202 }
203
204 // wait for all previous sends to finish
206
207 // post buffer column-index array sends
208 for(std::size_t i(0); i < n; ++i)
209 {
210 send_views[i] = _send_bufs.at(i).col_idx_view_r();
211 _send_reqs[i] = _comm.isend(send_views[i].get_r(), _send_bufs.at(i).num_nzes(), _ranks.at(i));
212 }
213
214 // wait for all receives and sends to finish
215 _recv_reqs.wait_all();
217
218 _initialized = true;
219 }
220#else // non-MPI version
221 void init(const MT_&)
222 {
223 XASSERTM(!_initialized, "SynchMatrix object is already initialized");
224 _initialized = true;
225 }
226#endif // FEAT_HAVE_MPI
227
234#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
235 void exec(MT_& matrix)
236 {
237 XASSERTM(_initialized, "SynchMatrix object has not been initialized");
238
239 const std::size_t n = _ranks.size();
240
241 std::vector<Memory::TypedView<typename MT_::DataType>> recv_views(n);
242 std::vector<Memory::TypedView<typename MT_::DataType>> send_views(n);
243
244 // create receive buffers and post receives
245 for(std::size_t i(0); i < n; ++i)
246 {
247 BufferMatrixType& buf = _recv_bufs.at(i);
248
249 // post receive
250 recv_views[i] = buf.val_view_w();
251 _recv_reqs.get_request(i) = _comm.irecv(recv_views[i].get_w(), buf.num_nzes_raw(), _ranks.at(i));
252 }
253
254 // post sends
255 for(std::size_t i(0); i < n; ++i)
256 {
257 BufferMatrixType& buf = _send_bufs.at(i);
258
259 // gather from mirror
260 _mirrors.at(i).gather(buf, matrix);
261
262 // post send
263 send_views[i] = buf.val_view_r();
264 _send_reqs.get_request(i) = _comm.isend(send_views[i].get_r(), buf.num_nzes_raw(), _ranks.at(i));
265 }
266
267 // process all pending receives
268 for(std::size_t idx(0u); _recv_reqs.wait_any(idx); )
269 {
270 // scatter the receive buffer
271 recv_views[idx].release();
272 _mirrors.at(idx).scatter_axpy(matrix, _recv_bufs.at(idx));
273 }
274
275 // wait for all sends to finish
277 }
278#else // non-MPI version
279 void exec(MT_&)
280 {
281 XASSERTM(_initialized, "SynchMatrix object has not been initialized");
282 }
283#endif // FEAT_HAVE_MPI
284 }; // class SynchMatrix
285
286 template <typename MT_, typename SVMT_, int blocks_>
287 class SynchMatrix<LAFEM::PowerDiagMatrix<MT_, blocks_>, SVMT_>
288 {
289 public:
290 using VMT_ = typename SVMT_::SubMirrorType;
292 bool _initialized;
293
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;
298#endif // FEAT_HAVE_MPI || DOXYGEN
299
300#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
301 SynchMatrix(const Dist::Comm& comm, const std::vector<int>& ranks,
302 const std::vector<SVMT_>& mirrors_row, const std::vector<SVMT_>& mirrors_col) :
303 _initialized(false),
304 synch_matrix_list(blocks_),
305 mirrors_row_split(blocks_),
306 mirrors_col_split(blocks_)
307 {
308 for (int block(0) ; block < blocks_ ; ++block)
309 {
310 for (Index i(0) ; i < mirrors_row.size() ; ++i)
311 {
312 mirrors_row_split.at((size_t)block).push_back(mirrors_row.at(i).get(block).clone(LAFEM::CloneMode::Shallow));
313 }
314 for (Index i(0) ; i < mirrors_col.size() ; ++i)
315 {
316 mirrors_col_split.at((size_t)block).push_back(mirrors_col.at(i).get(block).clone(LAFEM::CloneMode::Shallow));
317 }
318
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));
320
321 }
322 }
323#else // non-MPI version
324 SynchMatrix(const Dist::Comm&, const std::vector<int>& ranks, const std::vector<SVMT_>&, const std::vector<SVMT_>&) :
325 _initialized(false)
326 {
327 XASSERT(ranks.empty());
328 }
329#endif // FEAT_HAVE_MPI
330
331#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
332 void init(const SMT_& matrix)
333 {
334 XASSERTM(!_initialized, "SynchMatrix object is already initialized");
335
336 for (int block(0) ; block < blocks_ ; ++block)
337 {
338 synch_matrix_list.at((size_t)block)->init(matrix.get(block, block));
339 }
340
341 _initialized = true;
342 }
343#else // non-MPI version
344 void init(const SMT_&)
345 {
346 XASSERTM(!_initialized, "SynchMatrix object is already initialized");
347 _initialized = true;
348 }
349#endif // FEAT_HAVE_MPI
350
351#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
352 void exec(SMT_& matrix)
353 {
354 XASSERTM(_initialized, "SynchMatrix object has not been initialized");
355
356 for (int block(0) ; block < blocks_ ; ++block)
357 {
358 synch_matrix_list.at((size_t)block)->exec(matrix.get(block, block));
359 }
360 }
361#else // non-MPI version
362 void exec(SMT_&)
363 {
364 XASSERTM(_initialized, "SynchMatrix object has not been initialized");
365 }
366#endif // FEAT_HAVE_MPI
367 };
368
389 template<typename MT_, typename VMT_>
390 void synch_matrix(MT_& target, const Dist::Comm& comm, const std::vector<int>& ranks,
391 const std::vector<VMT_>& mirrors_row, const std::vector<VMT_>& mirrors_col)
392 {
393 SynchMatrix<MT_, VMT_> synch(comm, ranks, mirrors_row, mirrors_col);
394 synch.init(target);
395 synch.exec(target);
396 }
397 } // namespace Global
398} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Communicator class.
Definition: dist.hpp:1657
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Definition: dist.cpp:719
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
Definition: dist.cpp:707
Communication Request vector class.
Definition: dist.hpp:948
Request & get_request(std::size_t idx)
Returns a (const) reference to a single request in the vector.
Definition: dist.hpp:1063
bool wait_any(std::size_t &idx, Status &status)
Blocks until one of the active requests has been fulfilled.
Definition: dist.cpp:332
void wait_all()
Blocks until all active requests are fulfilled.
Definition: dist.cpp:327
Ticket class for asynchronous global matrix conversion.
Definition: synch_mat.hpp:39
std::vector< BufferMatrixType > _send_bufs
send and receive buffers
Definition: synch_mat.hpp:58
void exec(MT_ &matrix)
Converts a type-0 matrix to a type-1 matrix.
Definition: synch_mat.hpp:235
LAFEM::MatrixMirrorBuffer< typename MT_::DataType, typename MT_::IndexType > BufferMatrixType
the buffer matrix type
Definition: synch_mat.hpp:44
SynchMatrix(const SynchMatrix &)=delete
deleted copy constructor
SynchMatrix & operator=(const SynchMatrix &)=delete
deleted copy assignment operator
std::vector< int > _ranks
the neighbor ranks
Definition: synch_mat.hpp:52
Dist::RequestVector _send_reqs
send and receive request vectors
Definition: synch_mat.hpp:56
SynchMatrix(const Dist::Comm &comm, const std::vector< int > &ranks, const std::vector< VMT_ > &mirrors_row, const std::vector< VMT_ > &mirrors_col)
Constructor.
Definition: synch_mat.hpp:78
LAFEM::MatrixMirror< typename MT_::DataType, typename MT_::IndexType > MatrixMirrorType
the matrix mirror type
Definition: synch_mat.hpp:42
const Dist::Comm & _comm
our communicator
Definition: synch_mat.hpp:50
void init(const MT_ &matrix)
Initializes the internal buffers for synchronization.
Definition: synch_mat.hpp:126
std::vector< MatrixMirrorType > _mirrors
the matrix mirrors
Definition: synch_mat.hpp:54
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.
Definition: synch_mat.hpp:390
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.