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(const GateRowType* row_gate, const 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
121 void init_workspace() const
122 {
123 _matrix.init_workspace();
124 }
125
126 void reset_workspace() const
127 {
128 _matrix.reset_workspace();
129 }
130
136 LocalMatrix_& local()
137 {
138 return _matrix;
139 }
140
146 const LocalMatrix_& local() const
147 {
148 return _matrix;
149 }
150
151 template<typename OtherGlobalMatrix_>
152 void convert(GateRowType* row_gate, GateColType* col_gate, const OtherGlobalMatrix_ & other)
153 {
154 this->_row_gate = row_gate;
155 this->_col_gate = col_gate;
156 this->_matrix.convert(other.local());
157 }
158
165 {
166 return _row_gate;
167 }
168
175 {
176 return _col_gate;
177 }
178
184 const Dist::Comm* get_comm() const
185 {
186 return _row_gate != nullptr ? _row_gate->get_comm() : nullptr;
187 }
188
198 {
199 return Matrix(_row_gate, _col_gate, _matrix.clone(mode));
200 }
201
208 {
209 return VectorTypeL(_row_gate, _matrix.create_vector_l());
210 }
211
218 {
219 return VectorTypeR(_col_gate, _matrix.create_vector_r());
220 }
221
233 {
235 }
236
237 Index num_cols_raw() const
238 {
240 }
241
253 {
255 }
256
257 Index num_rows_raw() const
258 {
260 }
261
273 {
274 Index my_num_nzes(_matrix.num_nzes());
275 const Dist::Comm* comm = (_row_gate != nullptr ? _row_gate->get_comm() : (_col_gate != nullptr ? _col_gate->get_comm() : nullptr));
276 if((comm != nullptr) && (comm->size() > 1))
277 comm->allreduce(&my_num_nzes, &my_num_nzes, std::size_t(1), Dist::op_sum);
278 return my_num_nzes;
279 }
280
281 Index num_nzes_raw() const
282 {
283 Index my_num_nzes(_matrix.num_nzes_raw());
284 const Dist::Comm* comm = (_row_gate != nullptr ? _row_gate->get_comm() : (_col_gate != nullptr ? _col_gate->get_comm() : nullptr));
285 if((comm != nullptr) && (comm->size() > 1))
286 comm->allreduce(&my_num_nzes, &my_num_nzes, std::size_t(1), Dist::op_sum);
287 return my_num_nzes;
288 }
289
291 std::size_t bytes() const
292 {
293 size_t my_bytes(_matrix.bytes());
294 const Dist::Comm* comm = (_row_gate != nullptr ? _row_gate->get_comm() : (_col_gate != nullptr ? _col_gate->get_comm() : nullptr));
295 if((comm != nullptr) && (comm->size() > 1))
296 comm->allreduce(&my_bytes, &my_bytes, std::size_t(1), Dist::op_sum);
297 return my_bytes;
298 }
299
313 void extract_diag(VectorTypeL& diag, bool sync = true) const
314 {
315 _matrix.extract_diag(diag.local());
316 if(sync)
317 {
318 diag.sync_0();
319 }
320 }
321
337 void apply(VectorTypeL& r, const VectorTypeR& x) const
338 {
339 _matrix.apply(r.local(), x.local());
340 r.sync_0();
341 }
342
358 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
359 {
360 _matrix.apply_transposed(r.local(), x.local());
361 r.sync_0();
362 }
363
381 auto apply_async(VectorTypeL& r, const VectorTypeR& x) const -> decltype(r.sync_0_async())
382 {
383 _matrix.apply(r.local(), x.local());
384 return r.sync_0_async();
385 }
386
404 auto apply_transposed_async(VectorTypeR& r, const VectorTypeL& x) const -> decltype(r.sync_0_async())
405 {
406 _matrix.apply_transposed(r.local(), x.local());
407 return r.sync_0_async();
408 }
409
433 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, const DataType alpha = DataType(1)) const
434 {
435 // copy y to r
436 r.copy(y);
437
438 // convert from type-1 to type-0
439 r.from_1_to_0();
440
441 // r <- r + alpha*A*x
442 _matrix.apply(r.local(), x.local(), r.local(), alpha);
443
444 // synchronize r
445 r.sync_0();
446 }
447
471 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, const DataType alpha = DataType(1)) const
472 {
473 // copy y to r
474 r.copy(y);
475
476 // convert from type-1 to type-0
477 r.from_1_to_0();
478
479 // r <- r + alpha*A^T*x
480 _matrix.apply_transposed(r.local(), x.local(), r.local(), alpha);
481
482 // synchronize r
483 r.sync_0();
484 }
485
511 auto apply_async(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, const DataType alpha = DataType(1)) const -> decltype(r.sync_0_async())
512 {
513 // copy y to r
514 r.copy(y);
515
516 // convert from type-1 to type-0
517 r.from_1_to_0();
518
519 // r <- r + alpha*A*x
520 _matrix.apply(r.local(), x.local(), r.local(), alpha);
521
522 // synchronize r
523 return r.sync_0_async();
524 }
525
551 auto apply_transposed_async(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, const DataType alpha = DataType(1)) const -> decltype(r.sync_0_async())
552 {
553 // copy y to r
554 r.copy(y);
555
556 // convert from type-1 to type-0
557 r.from_1_to_0();
558
559 // r <- r + alpha*A^T*x
560 _matrix.apply_transposed(r.local(), x.local(), r.local(), alpha);
561
562 // synchronize r
563 return r.sync_0_async();
564 }
565
582 void lump_rows(VectorTypeL& lump, bool sync = true) const
583 {
584 XASSERTM(lump.local().size() == _matrix.num_rows(), "lump vector size does not match matrix row count!");
585
586 _matrix.lump_rows(lump.local());
587
588 if(sync)
589 {
590 lump.sync_0();
591 }
592 }
593
610 VectorTypeL lump_rows(bool sync = true) const
611 {
613
614 lump_rows(lump, sync);
615
616 return lump;
617 }
618
643 LocalMatrix_ convert_to_1() const
644 {
645 ASSERTM(_col_gate, "Column gate is not set!");
646 ASSERTM(_row_gate, "Row gate is not set!");
647 LocalMatrix_ locmat = _matrix.clone(LAFEM::CloneMode::Weak);
649 synch.init(locmat);
650 synch.exec(locmat);
651 return locmat;
652 }
653
682 void convert_to_1(LocalMatrix_& matrix_sync_1, bool full_copy = true) const
683 {
684 matrix_sync_1.copy(this->local(), full_copy);
685 if((_row_gate != nullptr) && (_col_gate != nullptr))
686 {
688 synch.init(matrix_sync_1);
689 synch.exec(matrix_sync_1);
690 }
691 }
692
695 {
696 return _matrix.get_checkpoint_size(config);
697 }
698
700 void restore_from_checkpoint_data(std::vector<char>& data)
701 {
702 _matrix.restore_from_checkpoint_data(data);
703 }
704
706 std::uint64_t set_checkpoint_data(std::vector<char>& data, LAFEM::SerialConfig& config)
707 {
708 return _matrix.set_checkpoint_data(data, config);
709 }
710 }; // class Matrix<...>
711 } // namespace Global
712} // 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:1657
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
Definition: dist.cpp:658
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
Global gate implementation.
Definition: gate.hpp:78
void convert(const Gate< LVT2_, MT2_ > &other)
Conversion function for same vector container type but with different MDI-Type.
Definition: gate.hpp:238
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
Definition: gate.hpp:185
Index get_num_global_dofs() const
Returns the number of native global DOFs.
Definition: gate.hpp:443
Index get_num_global_dofs_raw() const
Returns the number of raw global DOFs.
Definition: gate.hpp:486
std::vector< Mirror_ > _mirrors
vector mirrors
Definition: gate.hpp:100
const Dist::Comm * _comm
our communicator
Definition: gate.hpp:96
std::vector< int > _ranks
communication ranks
Definition: gate.hpp:98
Global Matrix wrapper class template.
Definition: matrix.hpp:40
Index num_rows() const
Returns the total number of rows in this distributed matrix.
Definition: matrix.hpp:252
Matrix(const GateRowType *row_gate, const GateColType *col_gate, Args_ &&... args)
Forwarding constructor.
Definition: matrix.hpp:109
Index num_cols() const
Gets the total number of columns in this distributed matrix.
Definition: matrix.hpp:232
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:700
Matrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a clone of this global matrix.
Definition: matrix.hpp:197
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:146
VectorTypeR create_vector_r() const
Creates and returns a new R-compatible global vector object.
Definition: matrix.hpp:217
std::size_t bytes() const
Returns the total amount of bytes allocated.
Definition: matrix.hpp:291
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:433
VectorTypeL create_vector_l() const
Creates and returns a new L-compatible global vector object.
Definition: matrix.hpp:207
std::uint64_t get_checkpoint_size(LAFEM::SerialConfig &config)
Calculate size.
Definition: matrix.hpp:694
const GateRowType * _row_gate
a pointer to the row gate responsible for synchronization
Definition: matrix.hpp:76
std::uint64_t set_checkpoint_data(std::vector< char > &data, LAFEM::SerialConfig &config)
Definition: matrix.hpp:706
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:471
LocalMatrix_ convert_to_1() const
Computes and returns the type-1 conversion of this matrix as a local matrix.
Definition: matrix.hpp:643
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:164
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Performs a matrix-vector multiplication: r <- A^T*x.
Definition: matrix.hpp:358
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:381
VectorTypeL lump_rows(bool sync=true) const
Computes and returns the lumped rows of the matrix as a vector.
Definition: matrix.hpp:610
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:682
const GateColType * get_col_gate() const
Returns a const pointer to the internal column gate of the matrix.
Definition: matrix.hpp:174
static constexpr bool is_global
this is a global matrix class
Definition: matrix.hpp:70
void extract_diag(VectorTypeL &diag, bool sync=true) const
Extracts the main diagonal of the matrix as a vector.
Definition: matrix.hpp:313
void lump_rows(VectorTypeL &lump, bool sync=true) const
Computes the lumped rows of the matrix as a vector.
Definition: matrix.hpp:582
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:511
Index num_nzes() const
Returns the total number of non-zeros in this distributed matrix.
Definition: matrix.hpp:272
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:404
const GateColType * _col_gate
a pointer to the column gate responsible for synchronization
Definition: matrix.hpp:78
void apply(VectorTypeL &r, const VectorTypeR &x) const
Performs a matrix-vector multiplication: r <- A*x.
Definition: matrix.hpp:337
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:551
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gates of the matrix.
Definition: matrix.hpp:184
LocalMatrix_ & local()
Returns a reference to the internal local LAFEM matrix object.
Definition: matrix.hpp:136
Ticket class for asynchronous global matrix conversion.
Definition: synch_mat.hpp:39
void exec(MT_ &matrix)
Converts a type-0 matrix to a type-1 matrix.
Definition: synch_mat.hpp:235
void init(const MT_ &matrix)
Initializes the internal buffers for synchronization.
Definition: synch_mat.hpp:126
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:348
void from_1_to_0()
Converts a type-1 vector into a type-0 vector.
Definition: vector.hpp:173
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:205
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:122
void sync_0()
Performs a type-0 synchronization of the vector, i.e. sums up all local DOF contributions.
Definition: vector.hpp:188
Config class for serialize parameter.
Definition: container.hpp:50
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
Definition: dist.hpp:571
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.