FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matrix.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
8#include <kernel/lafem/container.hpp> // required for LAFEM::CloneMode
9#include <kernel/global/gate.hpp>
10#include <kernel/global/vector.hpp>
11#include <kernel/global/synch_mat.hpp>
12
13namespace FEAT
14{
15 namespace Global
16 {
38 template<typename LocalMatrix_, typename RowMirror_, typename ColMirror_>
39 class Matrix
40 {
41 public:
42 typedef LocalMatrix_ LocalMatrixType;
43 typedef RowMirror_ RowMirrorType;
44 typedef ColMirror_ ColMirrorType;
45
46 typedef typename LocalMatrix_::DataType DataType;
47 typedef typename LocalMatrix_::IndexType IndexType;
48
49 typedef typename LocalMatrix_::VectorTypeL LocalVectorTypeL;
50 typedef typename LocalMatrix_::VectorTypeR LocalVectorTypeR;
51
54
57
59 template <typename LocalMatrix2_, typename RowMirror2_ = RowMirror_, typename ColMirror2_ = ColMirror_>
61
63 template <typename DataType2_, typename IndexType2_>
65 typename LocalMatrix_::template ContainerType<DataType2_, IndexType2_>,
66 typename RowMirror_::template MirrorType<DataType2_, IndexType2_>,
67 typename ColMirror_::template MirrorType<DataType2_, IndexType2_> >;
68
70 static constexpr bool is_global = true;
72 static constexpr bool is_local = false;
73
74 protected:
80 LocalMatrix_ _matrix;
81
82 public:
85 _row_gate(nullptr),
86 _col_gate(nullptr),
87 _matrix()
88 {
89 }
90
108 template<typename... Args_>
109 explicit Matrix(GateRowType* row_gate, GateColType* col_gate, Args_&&... args) :
110 _row_gate(row_gate),
111 _col_gate(col_gate),
112 _matrix(std::forward<Args_>(args)...)
113 {
114 if((_row_gate != nullptr) && (_col_gate != nullptr))
115 {
116 // the gates must be defined on the same communicator
118 }
119 }
120
126 LocalMatrix_& local()
127 {
128 return _matrix;
129 }
130
136 const LocalMatrix_& local() const
137 {
138 return _matrix;
139 }
140
141 template<typename OtherGlobalMatrix_>
142 void convert(GateRowType* row_gate, GateColType* col_gate, const OtherGlobalMatrix_ & other)
143 {
144 this->_row_gate = row_gate;
145 this->_col_gate = col_gate;
146 this->_matrix.convert(other.local());
147 }
148
155 {
156 return _row_gate;
157 }
158
165 {
166 return _col_gate;
167 }
168
174 const Dist::Comm* get_comm() const
175 {
176 return _row_gate != nullptr ? _row_gate->get_comm() : nullptr;
177 }
178
188 {
189 return Matrix(_row_gate, _col_gate, _matrix.clone(mode));
190 }
191
198 {
199 return VectorTypeL(_row_gate, _matrix.create_vector_l());
200 }
201
208 {
209 return VectorTypeR(_col_gate, _matrix.create_vector_r());
210 }
211
222 template<LAFEM::Perspective perspective_ = LAFEM::Perspective::pod>
224 {
225 return _col_gate->template get_num_global_dofs<perspective_>();
226 }
227
238 template<LAFEM::Perspective perspective_ = LAFEM::Perspective::pod>
239 Index rows() const
240 {
241 return _row_gate->template get_num_global_dofs<perspective_>();
242 }
243
254 template<LAFEM::Perspective perspective_ = LAFEM::Perspective::pod>
256 {
257 Index my_used_elements(_matrix.template used_elements<perspective_>());
258 const Dist::Comm* comm = (_row_gate != nullptr ? _row_gate->get_comm() : (_col_gate != nullptr ? _col_gate->get_comm() : nullptr));
259 if((comm != nullptr) && (comm->size() > 1))
260 comm->allreduce(&my_used_elements, &my_used_elements, std::size_t(1), Dist::op_sum);
261 return my_used_elements;
262 }
263
265 std::size_t bytes() const
266 {
267 size_t my_bytes(_matrix.bytes());
268 const Dist::Comm* comm = (_row_gate != nullptr ? _row_gate->get_comm() : (_col_gate != nullptr ? _col_gate->get_comm() : nullptr));
269 if((comm != nullptr) && (comm->size() > 1))
270 comm->allreduce(&my_bytes, &my_bytes, std::size_t(1), Dist::op_sum);
271 return my_bytes;
272 }
273
287 void extract_diag(VectorTypeL& diag, bool sync = true) const
288 {
289 _matrix.extract_diag(diag.local());
290 if(sync)
291 {
292 diag.sync_0();
293 }
294 }
295
311 void apply(VectorTypeL& r, const VectorTypeR& x) const
312 {
313 _matrix.apply(r.local(), x.local());
314 r.sync_0();
315 }
316
332 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
333 {
334 _matrix.apply_transposed(r.local(), x.local());
335 r.sync_0();
336 }
337
355 auto apply_async(VectorTypeL& r, const VectorTypeR& x) const -> decltype(r.sync_0_async())
356 {
357 _matrix.apply(r.local(), x.local());
358 return r.sync_0_async();
359 }
360
378 auto apply_transposed_async(VectorTypeR& r, const VectorTypeL& x) const -> decltype(r.sync_0_async())
379 {
380 _matrix.apply_transposed(r.local(), x.local());
381 return r.sync_0_async();
382 }
383
407 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, const DataType alpha = DataType(1)) const
408 {
409 // copy y to r
410 r.copy(y);
411
412 // convert from type-1 to type-0
413 r.from_1_to_0();
414
415 // r <- r + alpha*A*x
416 _matrix.apply(r.local(), x.local(), r.local(), alpha);
417
418 // synchronize r
419 r.sync_0();
420 }
421
445 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, const DataType alpha = DataType(1)) const
446 {
447 // copy y to r
448 r.copy(y);
449
450 // convert from type-1 to type-0
451 r.from_1_to_0();
452
453 // r <- r + alpha*A^T*x
454 _matrix.apply_transposed(r.local(), x.local(), r.local(), alpha);
455
456 // synchronize r
457 r.sync_0();
458 }
459
485 auto apply_async(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, const DataType alpha = DataType(1)) const -> decltype(r.sync_0_async())
486 {
487 // copy y to r
488 r.copy(y);
489
490 // convert from type-1 to type-0
491 r.from_1_to_0();
492
493 // r <- r + alpha*A*x
494 _matrix.apply(r.local(), x.local(), r.local(), alpha);
495
496 // synchronize r
497 return r.sync_0_async();
498 }
499
525 auto apply_transposed_async(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, const DataType alpha = DataType(1)) const -> decltype(r.sync_0_async())
526 {
527 // copy y to r
528 r.copy(y);
529
530 // convert from type-1 to type-0
531 r.from_1_to_0();
532
533 // r <- r + alpha*A^T*x
534 _matrix.apply_transposed(r.local(), x.local(), r.local(), alpha);
535
536 // synchronize r
537 return r.sync_0_async();
538 }
539
556 void lump_rows(VectorTypeL& lump, bool sync = true) const
557 {
558 XASSERTM(lump.local().size() == _matrix.rows(), "lump vector size does not match matrix row count!");
559
560 _matrix.lump_rows(lump.local());
561
562 if(sync)
563 {
564 lump.sync_0();
565 }
566 }
567
584 VectorTypeL lump_rows(bool sync = true) const
585 {
587
588 lump_rows(lump, sync);
589
590 return lump;
591 }
592
617 LocalMatrix_ convert_to_1() const
618 {
619 ASSERTM(_col_gate, "Column gate is not set!");
620 ASSERTM(_row_gate, "Row gate is not set!");
621 LocalMatrix_ locmat = _matrix.clone(LAFEM::CloneMode::Weak);
623 synch.init(locmat);
624 synch.exec(locmat);
625 return locmat;
626 }
627
656 void convert_to_1(LocalMatrix_& matrix_sync_1, bool full_copy = true) const
657 {
658 matrix_sync_1.copy(this->local(), full_copy);
659 if((_row_gate != nullptr) && (_col_gate != nullptr))
660 {
662 synch.init(matrix_sync_1);
663 synch.exec(matrix_sync_1);
664 }
665 }
666
669 {
670 return _matrix.get_checkpoint_size(config);
671 }
672
674 void restore_from_checkpoint_data(std::vector<char>& data)
675 {
676 _matrix.restore_from_checkpoint_data(data);
677 }
678
680 std::uint64_t set_checkpoint_data(std::vector<char>& data, LAFEM::SerialConfig& config)
681 {
682 return _matrix.set_checkpoint_data(data, config);
683 }
684 }; // class Matrix<...>
685 } // namespace Global
686} // namespace FEAT
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Communicator class.
Definition: dist.hpp:1349
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
Definition: dist.cpp:655
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1506
Global gate implementation.
Definition: gate.hpp:51
void convert(const Gate< LVT2_, MT2_ > &other)
Conversion function for same vector container type but with different MDI-Type.
Definition: gate.hpp:191
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
Definition: gate.hpp:138
std::vector< Mirror_ > _mirrors
vector mirrors
Definition: gate.hpp:73
const Dist::Comm * _comm
our communicator
Definition: gate.hpp:69
std::vector< int > _ranks
communication ranks
Definition: gate.hpp:71
Global Matrix wrapper class template.
Definition: matrix.hpp:40
Matrix(GateRowType *row_gate, GateColType *col_gate, Args_ &&... args)
Forwarding constructor.
Definition: matrix.hpp:109
GateRowType * _row_gate
a pointer to the row gate responsible for synchronization
Definition: matrix.hpp:76
GateColType * _col_gate
a pointer to the column gate responsible for synchronization
Definition: matrix.hpp:78
static constexpr bool is_local
this is not a local matrix class
Definition: matrix.hpp:72
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Definition: matrix.hpp:674
Matrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a clone of this global matrix.
Definition: matrix.hpp:187
LocalMatrix_ _matrix
the internal local matrix object
Definition: matrix.hpp:80
const LocalMatrix_ & local() const
Returns a const reference to the internal local LAFEM matrix object.
Definition: matrix.hpp:136
VectorTypeR create_vector_r() const
Creates and returns a new R-compatible global vector object.
Definition: matrix.hpp:207
std::size_t bytes() const
Returns the total amount of bytes allocated.
Definition: matrix.hpp:265
void apply(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, const DataType alpha=DataType(1)) const
Performs a matrix-vector multiplication: r <- y + alpha*A*x.
Definition: matrix.hpp:407
VectorTypeL create_vector_l() const
Creates and returns a new L-compatible global vector object.
Definition: matrix.hpp:197
std::uint64_t get_checkpoint_size(LAFEM::SerialConfig &config)
Calculate size.
Definition: matrix.hpp:668
std::uint64_t set_checkpoint_data(std::vector< char > &data, LAFEM::SerialConfig &config)
Definition: matrix.hpp:680
Index columns() const
Gets the total number of columns in this distributed matrix.
Definition: matrix.hpp:223
void apply_transposed(VectorTypeR &r, const VectorTypeL &x, const VectorTypeR &y, const DataType alpha=DataType(1)) const
Performs a matrix-vector multiplication: r <- y + alpha*A^T*x.
Definition: matrix.hpp:445
LocalMatrix_ convert_to_1() const
Computes and returns the type-1 conversion of this matrix as a local matrix.
Definition: matrix.hpp:617
Matrix()
standard constructor
Definition: matrix.hpp:84
const GateRowType * get_row_gate() const
Returns a const pointer to the internal row gate of the matrix.
Definition: matrix.hpp:154
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Performs a matrix-vector multiplication: r <- A^T*x.
Definition: matrix.hpp:332
auto apply_async(VectorTypeL &r, const VectorTypeR &x) const -> decltype(r.sync_0_async())
Performs a matrix-vector multiplication: r <- A*x.
Definition: matrix.hpp:355
VectorTypeL lump_rows(bool sync=true) const
Computes and returns the lumped rows of the matrix as a vector.
Definition: matrix.hpp:584
Index used_elements() const
Returns the total number of non-zeros in this distributed matrix.
Definition: matrix.hpp:255
void convert_to_1(LocalMatrix_ &matrix_sync_1, bool full_copy=true) const
Computes and returns the type-1 conversion of this matrix as a local matrix.
Definition: matrix.hpp:656
const GateColType * get_col_gate() const
Returns a const pointer to the internal column gate of the matrix.
Definition: matrix.hpp:164
static constexpr bool is_global
this is a global matrix class
Definition: matrix.hpp:70
Index rows() const
Returns the total number of rows in this distributed matrix.
Definition: matrix.hpp:239
void extract_diag(VectorTypeL &diag, bool sync=true) const
Extracts the main diagonal of the matrix as a vector.
Definition: matrix.hpp:287
void lump_rows(VectorTypeL &lump, bool sync=true) const
Computes the lumped rows of the matrix as a vector.
Definition: matrix.hpp:556
auto apply_async(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, const DataType alpha=DataType(1)) const -> decltype(r.sync_0_async())
Performs a matrix-vector multiplication: r <- y + alpha*A*x.
Definition: matrix.hpp:485
auto apply_transposed_async(VectorTypeR &r, const VectorTypeL &x) const -> decltype(r.sync_0_async())
Performs a matrix-vector multiplication: r <- A^T*x.
Definition: matrix.hpp:378
void apply(VectorTypeL &r, const VectorTypeR &x) const
Performs a matrix-vector multiplication: r <- A*x.
Definition: matrix.hpp:311
auto apply_transposed_async(VectorTypeR &r, const VectorTypeL &x, const VectorTypeR &y, const DataType alpha=DataType(1)) const -> decltype(r.sync_0_async())
Performs a matrix-vector multiplication: r <- y + alpha*A^T*x.
Definition: matrix.hpp:525
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gates of the matrix.
Definition: matrix.hpp:174
LocalMatrix_ & local()
Returns a reference to the internal local LAFEM matrix object.
Definition: matrix.hpp:126
Ticket class for asynchronous global matrix conversion.
Definition: synch_mat.hpp:37
void exec(MT_ &matrix)
Converts a type-0 matrix to a type-1 matrix.
Definition: synch_mat.hpp:226
void init(const MT_ &matrix)
Initializes the internal buffers for synchronization.
Definition: synch_mat.hpp:124
Global vector wrapper class template.
Definition: vector.hpp:68
void copy(const Vector &x)
Copies the contents of another vector into this vector.
Definition: vector.hpp:332
void from_1_to_0()
Converts a type-1 vector into a type-0 vector.
Definition: vector.hpp:172
auto sync_0_async() -> decltype(_gate->sync_0_async(_vector))
Performs a type-0 synchronization of the vector, i.e. sums up all local DOF contributions.
Definition: vector.hpp:204
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:121
void sync_0()
Performs a type-0 synchronization of the vector, i.e. sums up all local DOF contributions.
Definition: vector.hpp:187
Config class for serialize parameter.
Definition: container.hpp:47
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
Definition: dist.hpp:271
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.