FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
vector.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/global/gate.hpp>
9#include <kernel/util/math.hpp>
10#include <kernel/lafem/container.hpp> // required for LAFEM::CloneMode
11
12namespace FEAT
13{
14 namespace Global
15 {
66 template<typename LocalVector_, typename Mirror_>
67 class Vector
68 {
69 public:
71
72 typedef typename LocalVector_::DataType DataType;
73 typedef typename LocalVector_::IndexType IndexType;
74 typedef LocalVector_ LocalVectorType;
75
77 template <typename LocalVector2_, typename Mirror2_ = Mirror_>
79
81 template <typename DataType2_, typename IndexType2_>
83 typename LocalVector_::template ContainerType<DataType2_, IndexType2_>,
84 typename Mirror_::template MirrorType<DataType2_, IndexType2_> >;
85
86 protected:
90 LocalVector_ _vector;
91
92 public:
95 _gate(nullptr),
96 _vector()
97 {
98 }
99
109 template<typename... Args_>
110 explicit Vector(const GateType* gate, Args_&&... args) :
111 _gate(gate),
112 _vector(std::forward<Args_>(args)...)
113 {
114 }
115
121 LocalVector_& local()
122 {
123 return _vector;
124 }
125
131 const LocalVector_& local() const
132 {
133 return _vector;
134 }
135
136 template<typename OtherGlobalVector_>
137 void convert(const GateType* gate, const OtherGlobalVector_ & other)
138 {
139 this->_gate = gate;
140 this->_vector.convert(other.local());
141 }
142
148 const GateType* get_gate() const
149 {
150 return _gate;
151 }
152
158 const Dist::Comm* get_comm() const
159 {
160 return (_gate != nullptr ? _gate->get_comm() : nullptr);
161 }
162
173 {
174 ASSERTM(_gate, "Gate is not set!");
175 _gate->from_1_to_0(this->_vector);
176 }
177
187 void sync_0()
188 {
189 ASSERTM(_gate, "Gate is not set!");
191 }
192
205 //decltype(_gate->sync_0(_vector)) sync_0_async()
206 {
207 ASSERTM(_gate, "Gate is not set!");
208 return _gate->sync_0_async(_vector);
209 }
210
220 void sync_1()
221 {
222 ASSERTM(_gate, "Gate is not set!");
224 }
225
238 //decltype(_gate->sync_1(_vector)) sync_1_async()
239 {
240 ASSERTM(_gate, "Gate is not set!");
241 return _gate->sync_1_async(_vector);
242 }
243
254 template<LAFEM::Perspective perspective_ = LAFEM::Perspective::pod>
255 Index size() const
256 {
257 ASSERTM(_gate, "Gate is not set!");
258 return _gate->template get_num_global_dofs<perspective_>();
259 }
260
270 {
271 return Vector(_gate, _vector.clone(mode));
272 }
273
284 {
285 XASSERTM(&(other.local()) != &(this->local()), "Trying to self-clone a Global::Vector!");
286 *this = other.clone(mode);
287 }
288
292 void clear()
293 {
294 _vector.clear();
295 }
296
302 void format(DataType alpha = DataType(0))
303 {
304 _vector.format(alpha);
305 }
306
320 void format(Random & rng, DataType min, DataType max)
321 {
322 _vector.format(rng, min, max);
323 sync_1();
324 }
325
332 void copy(const Vector& x)
333 {
334 // avoid self-copy
335 if(this != &x)
336 {
337 _vector.copy(x.local());
338 }
339 }
340
350 void axpy(const Vector& x, const DataType alpha = DataType(1))
351 {
352 _vector.axpy(x.local(), alpha);
353 }
354
364 void scale(const Vector& x, const DataType alpha)
365 {
366 _vector.scale(x.local(), alpha);
367 }
368
380 DataType dot(const Vector& x) const
381 {
382 ASSERTM(_gate, "Gate is not set!");
383 return _gate->dot(_vector, x.local());
384 }
385
398 {
399 ASSERTM(_gate, "Gate is not set!");
400 return _gate->dot_async(_vector, x.local());
401 }
402
411 DataType norm2sqr() const
412 {
413 return dot(*this);
414 }
415
425 {
426 return dot_async(*this);
427 }
428
437 DataType norm2() const
438 {
439 return Math::sqrt(norm2sqr());
440 }
441
451 {
452 ASSERTM(_gate, "Gate is not set!");
453 return _gate->dot_async(_vector, _vector, true);
454 }
455
467 void component_invert(const Vector& x, const DataType alpha = DataType(1))
468 {
469 _vector.component_invert(x.local(), alpha);
470 }
471
480 void component_product(const Vector& x, const Vector& y)
481 {
482 _vector.component_product(x.local(), y.local());
483 }
484
493 DataType max_abs_element() const
494 {
495 ASSERTM(_gate, "Gate is not set!");
496 return _gate->max(_vector.max_abs_element());
497 }
498
508 {
509 ASSERTM(_gate, "Gate is not set!");
510 return _gate->max_async(_vector.max_abs_element());
511 }
512
521 DataType min_abs_element() const
522 {
523 ASSERTM(_gate, "Gate is not set!");
524 return _gate->min(_vector.min_abs_element());
525 }
526
536 {
537 ASSERTM(_gate, "Gate is not set!");
538 return _gate->min_async(_vector.min_abs_element());
539 }
540
549 DataType max_element() const
550 {
551 ASSERTM(_gate, "Gate is not set!");
552 return _gate->max_async(_vector.max_element());
553 }
554
564 {
565 ASSERTM(_gate, "Gate is not set!");
566 return _gate->max_async(_vector.max_element());
567 }
568
577 DataType min_element() const
578 {
579 ASSERTM(_gate, "Gate is not set!");
580 return _gate->min(_vector.min_element());
581 }
582
592 {
593 ASSERTM(_gate, "Gate is not set!");
594 return _gate->min_async(_vector.min_element());
595 }
596
606 DataType max_rel_diff(const Vector & x) const
607 {
608 ASSERTM(_gate, "Gate is not set!");
609 return _gate->max(_vector.max_rel_diff(x));
610 }
611
622 {
623 ASSERTM(_gate, "Gate is not set!");
624 return _gate->max_async(_vector.max_rel_diff(x));
625 }
626
627
630 {
631 return _vector.get_checkpoint_size(config);
632 }
633
635 void restore_from_checkpoint_data(std::vector<char>& data)
636 {
637 _vector.restore_from_checkpoint_data(data);
638 }
639
641 std::uint64_t set_checkpoint_data(std::vector<char>& data, LAFEM::SerialConfig& config)
642 {
643 return _vector.set_checkpoint_data(data, config);
644 }
645 }; // class Vector<...>
646 } // namespace Global
647} // namespace FEAT
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Communicator class.
Definition: dist.hpp:1349
Global gate implementation.
Definition: gate.hpp:51
void sync_0(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
Definition: gate.hpp:408
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
void from_1_to_0(LocalVector_ &vector) const
Converts a type-1 vector into a type-0 vector.
Definition: gate.hpp:387
DataType max(DataType x) const
Computes the maximum of a scalar variable over all processes.
Definition: gate.hpp:619
ScalarTicketType min_async(DataType x) const
Computes the minimum of a scalar variable over all processes.
Definition: gate.hpp:602
ScalarTicketType dot_async(const LocalVector_ &x, const LocalVector_ &y, bool sqrt=false) const
Computes a synchronized dot-product of two type-1 vectors.
Definition: gate.hpp:536
ScalarTicketType max_async(DataType x) const
Computes the maximum of a scalar variable over all processes.
Definition: gate.hpp:635
void sync_1(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Definition: gate.hpp:457
VectorTicketType sync_1_async(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Definition: gate.hpp:485
DataType min(DataType x) const
Computes the minimum of a scalar variable over all processes.
Definition: gate.hpp:586
VectorTicketType sync_0_async(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
Definition: gate.hpp:432
DataType dot(const LocalVector_ &x, const LocalVector_ &y) const
Computes a synchronized dot-product of two type-1 vectors.
Definition: gate.hpp:506
Ticket class for asynchronous global operations on scalars.
Definition: synch_scal.hpp:31
Global vector wrapper class template.
Definition: vector.hpp:68
void sync_1()
Performs a type-1 synchronization of the vector, i.e. averages all local DOF contributions.
Definition: vector.hpp:220
void format(DataType alpha=DataType(0))
Reset all elements of the container to a given value or zero if missing.
Definition: vector.hpp:302
Index size() const
Returns the total number of entries in this distributed vector.
Definition: vector.hpp:255
void format(Random &rng, DataType min, DataType max)
Reset all elements of the container to random values.
Definition: vector.hpp:320
void component_product(const Vector &x, const Vector &y)
Computes the component-wise product of two vector.
Definition: vector.hpp:480
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Definition: vector.hpp:635
SynchScalarTicket< DataType > max_abs_element_async() const
Retrieve the absolute maximum value of this vector.
Definition: vector.hpp:507
Vector()
standard constructor
Definition: vector.hpp:94
const GateType * _gate
a pointer to the gate responsible for synchronization
Definition: vector.hpp:88
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gate of the vector.
Definition: vector.hpp:158
void copy(const Vector &x)
Copies the contents of another vector into this vector.
Definition: vector.hpp:332
LocalVector_ _vector
the internal local vector object
Definition: vector.hpp:90
DataType dot(const Vector &x) const
Computes the dot-product of this vector and another vector.
Definition: vector.hpp:380
const GateType * get_gate() const
Returns a const pointer to the internal gate of the vector.
Definition: vector.hpp:148
void from_1_to_0()
Converts a type-1 vector into a type-0 vector.
Definition: vector.hpp:172
SynchScalarTicket< DataType > min_abs_element_async() const
Retrieve the absolute minimum value of this vector.
Definition: vector.hpp:535
SynchScalarTicket< DataType > norm2sqr_async() const
Computes the squared Euclid norm of this vector.
Definition: vector.hpp:424
auto sync_1_async() -> decltype(_gate->sync_1_async(_vector))
Performs a type-1 synchronization of the vector, i.e. averages all local DOF contributions.
Definition: vector.hpp:237
SynchScalarTicket< DataType > dot_async(const Vector &x) const
Computes the dot-product of this vector and another vector.
Definition: vector.hpp:397
SynchScalarTicket< DataType > norm2_async() const
Computes the Euclid norm of this vector.
Definition: vector.hpp:450
DataType max_abs_element() const
Retrieve the absolute maximum value of this vector.
Definition: vector.hpp:493
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
Vector clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a clone of this global vector.
Definition: vector.hpp:269
SynchScalarTicket< DataType > min_element_async() const
Retrieve the minimum value of this vector.
Definition: vector.hpp:591
DataType min_element() const
Retrieve the minimum value of this vector.
Definition: vector.hpp:577
DataType min_abs_element() const
Retrieve the absolute minimum value of this vector.
Definition: vector.hpp:521
std::uint64_t set_checkpoint_data(std::vector< char > &data, LAFEM::SerialConfig &config)
Definition: vector.hpp:641
DataType norm2sqr() const
Computes the squared Euclid norm of this vector.
Definition: vector.hpp:411
void component_invert(const Vector &x, const DataType alpha=DataType(1))
Computes the component-wise inverse of a vector.
Definition: vector.hpp:467
SynchScalarTicket< DataType > max_element_async() const
Retrieve the maximum value of this vector.
Definition: vector.hpp:563
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:121
std::uint64_t get_checkpoint_size(LAFEM::SerialConfig &config)
Calculate size.
Definition: vector.hpp:629
SynchScalarTicket< DataType > max_rel_diff_async(const Vector &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
Definition: vector.hpp:621
void axpy(const Vector &x, const DataType alpha=DataType(1))
Performs an AXPY operation: this <- this + alpha*x.
Definition: vector.hpp:350
DataType norm2() const
Computes the Euclid norm of this vector.
Definition: vector.hpp:437
DataType max_element() const
Retrieve the maximum value of this vector.
Definition: vector.hpp:549
void scale(const Vector &x, const DataType alpha)
Sets this to a scaled vector: this <- alpha*x.
Definition: vector.hpp:364
void sync_0()
Performs a type-0 synchronization of the vector, i.e. sums up all local DOF contributions.
Definition: vector.hpp:187
void clone(const Vector &other, LAFEM::CloneMode mode=LAFEM::CloneMode::Weak)
Creates this as a clone of another global vector.
Definition: vector.hpp:283
DataType max_rel_diff(const Vector &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
Definition: vector.hpp:606
void clear()
Clears the underlying vector.
Definition: vector.hpp:292
const LocalVector_ & local() const
Returns a const reference to the internal local LAFEM vector object.
Definition: vector.hpp:131
Vector(const GateType *gate, Args_ &&... args)
Forwarding constructor.
Definition: vector.hpp:110
Config class for serialize parameter.
Definition: container.hpp:47
Pseudo-Random Number Generator.
Definition: random.hpp:54
T_ sqrt(T_ x)
Returns the square-root of a value.
Definition: math.hpp:300
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.