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 Mirror_ MirrorType;
75 typedef LocalVector_ LocalVectorType;
76
78 template <typename LocalVector2_, typename Mirror2_ = Mirror_>
80
82 template <typename DataType2_, typename IndexType2_>
84 typename LocalVector_::template ContainerType<DataType2_, IndexType2_>,
85 typename Mirror_::template MirrorType<DataType2_, IndexType2_> >;
86
87 protected:
91 LocalVector_ _vector;
92
93 public:
96 _gate(nullptr),
97 _vector()
98 {
99 }
100
110 template<typename... Args_>
111 explicit Vector(const GateType* gate, Args_&&... args) :
112 _gate(gate),
113 _vector(std::forward<Args_>(args)...)
114 {
115 }
116
122 LocalVector_& local()
123 {
124 return _vector;
125 }
126
132 const LocalVector_& local() const
133 {
134 return _vector;
135 }
136
137 template<typename OtherGlobalVector_>
138 void convert(const GateType* gate, const OtherGlobalVector_ & other)
139 {
140 this->_gate = gate;
141 this->_vector.convert(other.local());
142 }
143
149 const GateType* get_gate() const
150 {
151 return _gate;
152 }
153
159 const Dist::Comm* get_comm() const
160 {
161 return (_gate != nullptr ? _gate->get_comm() : nullptr);
162 }
163
174 {
175 ASSERTM(_gate, "Gate is not set!");
176 _gate->from_1_to_0(this->_vector);
177 }
178
188 void sync_0()
189 {
190 ASSERTM(_gate, "Gate is not set!");
192 }
193
206 //decltype(_gate->sync_0(_vector)) sync_0_async()
207 {
208 ASSERTM(_gate, "Gate is not set!");
209 return _gate->sync_0_async(_vector);
210 }
211
221 void sync_1()
222 {
223 ASSERTM(_gate, "Gate is not set!");
225 }
226
239 //decltype(_gate->sync_1(_vector)) sync_1_async()
240 {
241 ASSERTM(_gate, "Gate is not set!");
242 return _gate->sync_1_async(_vector);
243 }
244
255 template<LAFEM::Perspective perspective_ = LAFEM::Perspective::pod>
256 Index size() const
257 {
258 ASSERTM(_gate, "Gate is not set!");
259 return _gate->template get_num_global_dofs<perspective_>();
260 }
261
271 {
272 return Vector(_gate, _vector.clone(mode));
273 }
274
285 {
286 XASSERTM(&(other.local()) != &(this->local()), "Trying to self-clone a Global::Vector!");
287 *this = other.clone(mode);
288 }
289
293 void clear()
294 {
295 _vector.clear();
296 }
297
303 void format(DataType alpha = DataType(0))
304 {
305 _vector.format(alpha);
306 }
307
321 void format(Random & rng, DataType min, DataType max)
322 {
323 _vector.format(rng, min, max);
324 sync_1();
325 }
326
333 void copy(const Vector& x)
334 {
335 // avoid self-copy
336 if(this != &x)
337 {
338 _vector.copy(x.local());
339 }
340 }
341
351 void axpy(const Vector& x, const DataType alpha = DataType(1))
352 {
353 _vector.axpy(x.local(), alpha);
354 }
355
365 void scale(const Vector& x, const DataType alpha)
366 {
367 _vector.scale(x.local(), alpha);
368 }
369
381 DataType dot(const Vector& x) const
382 {
383 ASSERTM(_gate, "Gate is not set!");
384 return _gate->dot(_vector, x.local());
385 }
386
399 {
400 ASSERTM(_gate, "Gate is not set!");
401 return _gate->dot_async(_vector, x.local());
402 }
403
412 DataType norm2sqr() const
413 {
414 return dot(*this);
415 }
416
426 {
427 return dot_async(*this);
428 }
429
438 DataType norm2() const
439 {
440 return Math::sqrt(norm2sqr());
441 }
442
452 {
453 ASSERTM(_gate, "Gate is not set!");
454 return _gate->dot_async(_vector, _vector, true);
455 }
456
468 void component_invert(const Vector& x, const DataType alpha = DataType(1))
469 {
470 _vector.component_invert(x.local(), alpha);
471 }
472
481 void component_product(const Vector& x, const Vector& y)
482 {
483 _vector.component_product(x.local(), y.local());
484 }
485
494 DataType max_abs_element() const
495 {
496 ASSERTM(_gate, "Gate is not set!");
497 return _gate->max(_vector.max_abs_element());
498 }
499
509 {
510 ASSERTM(_gate, "Gate is not set!");
511 return _gate->max_async(_vector.max_abs_element());
512 }
513
522 DataType min_abs_element() const
523 {
524 ASSERTM(_gate, "Gate is not set!");
525 return _gate->min(_vector.min_abs_element());
526 }
527
537 {
538 ASSERTM(_gate, "Gate is not set!");
539 return _gate->min_async(_vector.min_abs_element());
540 }
541
550 DataType max_element() const
551 {
552 ASSERTM(_gate, "Gate is not set!");
553 return _gate->max_async(_vector.max_element());
554 }
555
565 {
566 ASSERTM(_gate, "Gate is not set!");
567 return _gate->max_async(_vector.max_element());
568 }
569
578 DataType min_element() const
579 {
580 ASSERTM(_gate, "Gate is not set!");
581 return _gate->min(_vector.min_element());
582 }
583
593 {
594 ASSERTM(_gate, "Gate is not set!");
595 return _gate->min_async(_vector.min_element());
596 }
597
607 DataType max_rel_diff(const Vector & x) const
608 {
609 ASSERTM(_gate, "Gate is not set!");
610 return _gate->max(_vector.max_rel_diff(x._vector));
611 }
612
623 {
624 ASSERTM(_gate, "Gate is not set!");
625 return _gate->max_async(_vector.max_rel_diff(x._vector));
626 }
627
628
631 {
632 return _vector.get_checkpoint_size(config);
633 }
634
636 void restore_from_checkpoint_data(std::vector<char>& data)
637 {
638 _vector.restore_from_checkpoint_data(data);
639 }
640
642 std::uint64_t set_checkpoint_data(std::vector<char>& data, LAFEM::SerialConfig& config)
643 {
644 return _vector.set_checkpoint_data(data, config);
645 }
646 }; // class Vector<...>
647 } // namespace Global
648} // 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:613
ScalarTicketType min_async(DataType x) const
Computes the minimum of a scalar variable over all processes.
Definition: gate.hpp:596
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:530
ScalarTicketType max_async(DataType x) const
Computes the maximum of a scalar variable over all processes.
Definition: gate.hpp:629
void sync_1(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Definition: gate.hpp:454
VectorTicketType sync_1_async(LocalVector_ &vector) const
Synchronizes a type-1 vector, resulting in a type-1 vector.
Definition: gate.hpp:482
DataType min(DataType x) const
Computes the minimum of a scalar variable over all processes.
Definition: gate.hpp:580
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:500
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:221
void format(DataType alpha=DataType(0))
Reset all elements of the container to a given value or zero if missing.
Definition: vector.hpp:303
Index size() const
Returns the total number of entries in this distributed vector.
Definition: vector.hpp:256
void format(Random &rng, DataType min, DataType max)
Reset all elements of the container to random values.
Definition: vector.hpp:321
void component_product(const Vector &x, const Vector &y)
Computes the component-wise product of two vector.
Definition: vector.hpp:481
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Definition: vector.hpp:636
SynchScalarTicket< DataType > max_abs_element_async() const
Retrieve the absolute maximum value of this vector.
Definition: vector.hpp:508
Vector()
standard constructor
Definition: vector.hpp:95
const GateType * _gate
a pointer to the gate responsible for synchronization
Definition: vector.hpp:89
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gate of the vector.
Definition: vector.hpp:159
void copy(const Vector &x)
Copies the contents of another vector into this vector.
Definition: vector.hpp:333
LocalVector_ _vector
the internal local vector object
Definition: vector.hpp:91
DataType dot(const Vector &x) const
Computes the dot-product of this vector and another vector.
Definition: vector.hpp:381
const GateType * get_gate() const
Returns a const pointer to the internal gate of the vector.
Definition: vector.hpp:149
void from_1_to_0()
Converts a type-1 vector into a type-0 vector.
Definition: vector.hpp:173
SynchScalarTicket< DataType > min_abs_element_async() const
Retrieve the absolute minimum value of this vector.
Definition: vector.hpp:536
SynchScalarTicket< DataType > norm2sqr_async() const
Computes the squared Euclid norm of this vector.
Definition: vector.hpp:425
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:238
SynchScalarTicket< DataType > dot_async(const Vector &x) const
Computes the dot-product of this vector and another vector.
Definition: vector.hpp:398
SynchScalarTicket< DataType > norm2_async() const
Computes the Euclid norm of this vector.
Definition: vector.hpp:451
DataType max_abs_element() const
Retrieve the absolute maximum value of this vector.
Definition: vector.hpp:494
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
Vector clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a clone of this global vector.
Definition: vector.hpp:270
SynchScalarTicket< DataType > min_element_async() const
Retrieve the minimum value of this vector.
Definition: vector.hpp:592
DataType min_element() const
Retrieve the minimum value of this vector.
Definition: vector.hpp:578
DataType min_abs_element() const
Retrieve the absolute minimum value of this vector.
Definition: vector.hpp:522
std::uint64_t set_checkpoint_data(std::vector< char > &data, LAFEM::SerialConfig &config)
Definition: vector.hpp:642
DataType norm2sqr() const
Computes the squared Euclid norm of this vector.
Definition: vector.hpp:412
void component_invert(const Vector &x, const DataType alpha=DataType(1))
Computes the component-wise inverse of a vector.
Definition: vector.hpp:468
SynchScalarTicket< DataType > max_element_async() const
Retrieve the maximum value of this vector.
Definition: vector.hpp:564
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:122
std::uint64_t get_checkpoint_size(LAFEM::SerialConfig &config)
Calculate size.
Definition: vector.hpp:630
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:622
void axpy(const Vector &x, const DataType alpha=DataType(1))
Performs an AXPY operation: this <- this + alpha*x.
Definition: vector.hpp:351
DataType norm2() const
Computes the Euclid norm of this vector.
Definition: vector.hpp:438
DataType max_element() const
Retrieve the maximum value of this vector.
Definition: vector.hpp:550
void scale(const Vector &x, const DataType alpha)
Sets this to a scaled vector: this <- alpha*x.
Definition: vector.hpp:365
void sync_0()
Performs a type-0 synchronization of the vector, i.e. sums up all local DOF contributions.
Definition: vector.hpp:188
void clone(const Vector &other, LAFEM::CloneMode mode=LAFEM::CloneMode::Weak)
Creates this as a clone of another global vector.
Definition: vector.hpp:284
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:607
void clear()
Clears the underlying vector.
Definition: vector.hpp:293
const LocalVector_ & local() const
Returns a const reference to the internal local LAFEM vector object.
Definition: vector.hpp:132
Vector(const GateType *gate, Args_ &&... args)
Forwarding constructor.
Definition: vector.hpp:111
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.