FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
vector_mirror.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// includes, FEAT
9#include <kernel/lafem/container.hpp>
10#include <kernel/lafem/dense_vector.hpp>
11#include <kernel/lafem/dense_vector_blocked.hpp>
12#include <kernel/lafem/sparse_vector.hpp>
13#include <kernel/lafem/sparse_vector_blocked.hpp>
14#include <kernel/lafem/sparse_matrix_csr.hpp>
15#include <kernel/lafem/arch/mirror_block_gather.hpp>
16#include <kernel/lafem/arch/mirror_block_scatter.hpp>
17#include <kernel/lafem/arch/mirror_dense_gather.hpp>
18#include <kernel/lafem/arch/mirror_dense_scatter.hpp>
19#include <kernel/lafem/arch/mirror_sparse_block_gather.hpp>
20#include <kernel/lafem/arch/mirror_sparse_block_scatter.hpp>
21#include <kernel/lafem/arch/mirror_sparse_gather.hpp>
22#include <kernel/lafem/arch/mirror_sparse_scatter.hpp>
23
24namespace FEAT
25{
26 namespace LAFEM
27 {
28 namespace Intern
29 {
30 template<typename, typename = void>
31 static constexpr int dim_vec = 1;
32
33 template<typename T>
34 static constexpr int dim_vec<T,
35 std::void_t<decltype(T::BlockSize)>> = T::BlockSize;
36 }
65 template<typename DT_, typename IT_>
67 public Container<DT_, IT_>
68 {
69 public:
73 typedef DT_ DataType;
75 typedef IT_ IndexType;
76
78 template <typename DT2_ = DT_, typename IT2_ = IT_>
80
82 template <typename DataType2_, typename IndexType2_>
84
86 {
87 private:
88 const VectorMirror& _mirror;
89 const Memory::TypedView<IT_> _idx_view;
90
91 public:
93 typedef const IT_* ImageIterator;
94
95 explicit Adjactor(const VectorMirror& mirror) :
96 _mirror(mirror),
97 _idx_view(mirror.indices_view_r())
98 {
99 }
100
101 Adjactor(const Adjactor&) = delete;
102 Adjactor& operator=(const Adjactor&) = delete;
103
106 {
107 return _mirror.num_indices();
108 }
109
112 {
113 return _mirror.size();
114 }
115
117 ImageIterator image_begin(Index domain_node) const
118 {
119 XASSERTM(domain_node < _mirror.num_indices(), "Domain node index out of range");
120 return &_idx_view.get_r()[domain_node];
121 }
122
124 ImageIterator image_end(Index domain_node) const
125 {
126 XASSERTM(domain_node < _mirror.num_indices(), "Domain node index out of range");
127 return &_idx_view.get_r()[domain_node+1];
128 }
129 }; // class Adjactor
130
131 public:
134 {
135 this->_scalar_index.push_back(0); // target vector size
136 this->_scalar_index.push_back(0); // number of indices
137 }
138
153 explicit VectorMirror(Index size_in, Index num_idx)
154 {
155 this->_scalar_index.push_back(size_in); // target vector size
156 this->_scalar_index.push_back(num_idx); // number of indices
157 if(num_idx > Index(0))
158 {
159 this->_indices.push_back(Memory::Arbiter(num_idx * sizeof(IT_)));
160 this->_indices_size.push_back(num_idx);
161 }
162 }
163
166 BaseClass(std::forward<BaseClass>(other))
167 {
168 }
169
172 {
173 if(this == &other)
174 return *this;
175
176 this->move(std::forward<BaseClass>(other));
177
178 return *this;
179 }
180
190 {
191 VectorMirror mir(size_in, size_in);
192 Memory::TypedView<IndexType> idx = mir.indices_view_w();
193 for(Index i(0); i < size_in; ++i)
194 idx[i] = IndexType(i);
195 return mir;
196 }
197
207 {
208 return VectorMirror(tmpl_vec.size(), 0u);
209 }
210
219 template<int block_size_>
221 {
222 return VectorMirror(tmpl_vec.size(), 0u);
223 }
224
225 Adjactor adjactor() const
226 {
227 return Adjactor(*this);
228 }
229
238 {
239 VectorMirror t;
240 t.clone(*this, clone_mode);
241 return t;
242 }
243
252 template<typename DT2_, typename IT2_>
253 void clone(const VectorMirror<DT2_, IT2_> & other, CloneMode clone_mode = CloneMode::Weak)
254 {
255 Container<DT_, IT_>::clone(other, clone_mode);
256 }
257
264 template<typename DT2_, typename IT2_>
266 {
267 this->assign(other);
268 }
269
271 Index size() const
272 {
273 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(0);
274 }
275
277 bool empty() const
278 {
279 return this->_scalar_index.empty() || (this->_scalar_index.at(0) <= Index(0));
280 }
281
283 bool hollow() const
284 {
285 return this->_scalar_index.empty() || (this->_scalar_index.at(1) <= Index(0));
286 }
287
294 {
295 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(1);
296 }
297
298 Index nonzeros() const
299 {
300 return this->num_indices();
301 }
302
305 {
306 return this->_indices.front();
307 }
308
311 {
312 return this->_indices.front();
313 }
314
316 {
317 if(this->_indices.empty())
318 return Memory::TypedView<IT_>();
319 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read));
320 }
321
323 {
324 if(this->_indices.empty())
325 return Memory::TypedView<IT_>();
326 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::write));
327 }
328
329 Memory::TypedView<IT_> indices_view_rw(Memory::Location loc = Memory::Location::main)
330 {
331 if(this->_indices.empty())
332 return Memory::TypedView<IT_>();
333 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read_write));
334 }
335
336 Memory::TypedView<IT_> indices_view(Memory::Location loc, Memory::Access acc)
337 {
338 if(this->_indices.empty())
339 return Memory::TypedView<IT_>();
340 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, acc));
341 }
342
349 template<typename Vector_>
350 Index buffer_size(const Vector_& DOXY(vector)) const
351 {
352 return num_indices();
353 }
354
361 template<typename DT2_, typename IT2_, int block_size_>
363 {
364 return num_indices() * Index(block_size_);
365 }
366
373 template<typename DT2_, typename IT2_>
374 Index buffer_size(const SparseVector<DT2_, IT2_>& DOXY(vector)) const
375 {
376 return num_indices();
377 }
378
385 template<typename DT2_, typename IT2_, int block_size_>
387 {
388 return num_indices() * Index(block_size_);
389 }
390
397 template<typename Vector_>
399 {
401 }
402
415 void gather(
418 const Index buffer_offset = Index(0)) const
419 {
420 XASSERT(buffer_offset + this->num_indices() <= buffer.size());
421 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
422
423 if(this->hollow())
424 return;
425
426 LAFEM::Arch::MirrorDenseGather::template exec<DT_, IT_>(buffer_offset, this->num_indices(),
427 this->indices_arbiter(), buffer.elements_arbiter(), vector.elements_arbiter());
428 }
429
448 const DataType alpha = DataType(1),
449 const Index buffer_offset = Index(0)) const
450 {
451 XASSERT(buffer_offset + this->num_indices() <= buffer.size());
452 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
453
454 if(this->hollow())
455 return;
456
457 LAFEM::Arch::MirrorDenseScatter::template exec<DT_, IT_>(buffer_offset, this->num_indices(),
458 this->indices_arbiter(), buffer.elements_arbiter(), vector.elements_arbiter(), alpha);
459 }
460
473 template<int block_size_>
474 void gather(
477 const Index buffer_offset = Index(0)) const
478 {
479 XASSERT(buffer_offset + Index(block_size_)*this->num_indices() <= buffer.size());
480 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
481
482 if(this->hollow())
483 return;
484
485 LAFEM::Arch::MirrorBlockGather::template exec<DT_, IT_, block_size_>(buffer_offset, this->num_indices(),
486 this->indices_arbiter(), buffer.elements_arbiter(), vector.elements_arbiter());
487 }
488
504 template<int block_size_>
508 const DataType alpha = DataType(1),
509 const Index buffer_offset = Index(0)) const
510 {
511 XASSERT(buffer_offset + Index(block_size_)*this->num_indices() <= buffer.size());
512 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
513
514 if(this->hollow())
515 return;
516
517 LAFEM::Arch::MirrorBlockScatter::template exec<DT_, IT_, block_size_>(buffer_offset, this->num_indices(),
518 this->indices_arbiter(), buffer.elements_arbiter(), vector.elements_arbiter(), alpha);
519 }
520
533 void gather(
536 const Index buffer_offset = Index(0)) const
537 {
538 XASSERT(buffer_offset + this->num_indices() <= buffer.size());
539 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
540
541 if(this->hollow())
542 return;
543
544 LAFEM::Arch::MirrorSparseGather::template exec<DT_, IT_>(buffer_offset, this->num_indices(),
545 this->indices_arbiter(), buffer.elements_arbiter(),
546 vector.num_nzes(), vector.elements_arbiter(), vector.indices_arbiter());
547 }
548
567 const DataType alpha = DataType(1),
568 const Index buffer_offset = Index(0)) const
569 {
570 XASSERT(buffer_offset + this->num_indices() <= buffer.size());
571 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
572
573 if(this->hollow())
574 return;
575
576 LAFEM::Arch::MirrorSparseScatter::template exec<DT_, IT_>(buffer_offset, this->num_indices(),
577 this->indices_arbiter(), buffer.elements_arbiter(),
578 vector.num_nzes(), vector.elements_arbiter(), vector.indices_arbiter(), alpha);
579 }
580
593 template<int block_size_>
594 void gather(
597 const Index buffer_offset = Index(0)) const
598 {
599 XASSERT(buffer_offset + Index(block_size_)*this->num_indices() <= buffer.size());
600 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
601
602 if(this->hollow())
603 return;
604
605 LAFEM::Arch::MirrorSparseBlockGather::template exec<DT_, IT_, block_size_>(buffer_offset,
606 this->num_indices(), this->indices_arbiter(), buffer.elements_arbiter(),
607 vector.num_nzes(), vector.elements_arbiter(), vector.indices_arbiter());
608 }
609
625 template<int block_size_>
629 const DataType alpha = DataType(1),
630 const Index buffer_offset = Index(0)) const
631 {
632 XASSERT(buffer_offset + Index(block_size_)*this->num_indices() <= buffer.size());
633 XASSERTM(this->size() == vector.size(), "size mismatch between mirror and vector");
634
635 if(this->hollow())
636 return;
637
638 LAFEM::Arch::MirrorSparseBlockScatter::template exec<DT_, IT_, block_size_>(buffer_offset,
639 this->num_indices(), this->indices_arbiter(), buffer.elements_arbiter(),
640 vector.num_nzes(), vector.elements_arbiter(), vector.indices_arbiter(), alpha);
641 }
642
665 template<typename DT2_, typename IT2_>
666 Index mask_scatter(const DenseVector<DT2_, IT2_>& vector, std::vector<int>& mask,
667 const int value, const Index offset = Index(0)) const
668 {
669 XASSERT(Index(mask.size()) >= vector.size());
670 XASSERT(Index(mask.size()) >= this->size() + offset);
671 const Index n = this->num_indices();
672 const Memory::TypedView<IT_> idx = this->indices_view_r();
673
674 for(Index i(0); i < n; ++i)
675 mask[offset + idx[i]] = value;
676
677 return vector.size();
678 }
679
680 template<typename DT2_, typename IT2_>
681 Index mask_scatter_raw(const DenseVector<DT2_, IT2_>& vector, std::vector<int>& mask,
682 const int value, const Index offset = Index(0)) const
683 {
684 return mask_scatter(vector, mask, value, offset);
685 }
686
709 template<typename DT2_, typename IT2_, int block_size_>
711 const int value, const Index offset = Index(0)) const
712 {
713 XASSERT(Index(mask.size()) >= vector.size());
714 const Index n = this->num_indices();
715 const Memory::TypedView<IT_> idx = this->indices_view_r();
716
717 XASSERT(Index(mask.size()) >= this->size() + offset);
718 for(Index i(0); i < n; ++i)
719 mask[offset + idx[i]] = value;
720
721 return vector.size();
722 }
723
724 template<typename DT2_, typename IT2_, int block_size_>
725 Index mask_scatter_raw(const DenseVectorBlocked<DT2_, IT2_, block_size_>& vector, std::vector<int>& mask,
726 const int value, const Index offset = Index(0)) const
727 {
728 XASSERT(Index(mask.size()) >= vector.size());
729 const Index n = this->num_indices();
730 const Memory::TypedView<IT_> idx = this->indices_view_r();
731
732 XASSERT(Index(mask.size()) >= Index(block_size_)*this->size() + offset);
733 for(Index i(0); i < n; ++i)
734 {
735 const Index ibs = idx[i] * Index(block_size_);
736 for(int k(0); k < block_size_; ++k)
737 mask[offset + ibs + Index(k)] = value;
738 }
739 return vector.size();
740 }
741
742 friend std::ostream & operator<< (std::ostream & lhs, const VectorMirror & b)
743 {
744 Index n = b.num_indices();
745 const Memory::TypedView<IT_> idx = b.indices_view_r();
746
747 lhs << "[";
748 for (Index i(0) ; i < n ; ++i)
749 {
750 lhs << " " << idx[i];
751 }
752 lhs << "]";
753
754 return lhs;
755 }
756 }; // class VectorMirror<...>
757 } // namespace LAFEM
758} // 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
Container base class.
Definition: container.hpp:224
void move(Container &&source)
Assignment move operation.
Definition: container.hpp:1007
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Definition: container.hpp:904
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
Definition: container.hpp:285
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
Definition: container.hpp:232
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
Definition: container.hpp:236
std::vector< Index > _scalar_index
List of scalars with datatype index.
Definition: container.hpp:238
Blocked Dense data vector class template.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Dense data vector class template.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Sparse vector class template.
Index size() const
Returns the native size of the sparse vector.
Index num_nzes() const
Returns the number of non-zero elements in the sparse vector.
Sparse vector class template.
Index num_nzes() const
Returns the number of non-zero elements in the sparse vector.
Memory::Arbiter & elements_arbiter()
Returns a reference to the elements memory arbiter.
Index size() const
Returns the native size of the sparse vector.
Memory::Arbiter & indices_arbiter()
Returns a reference to the indices memory arbiter.
ImageIterator image_begin(Index domain_node) const
ImageIterator image_end(Index domain_node) const
const IT_ * ImageIterator
ImageIterator for Adjactor interface implementation.
Handles vector prolongation, restriction and serialization.
static VectorMirror make_hollow(const DenseVector< DT_, IT_ > &tmpl_vec)
Creates and returns a hollow mirror.
static VectorMirror make_identity(Index size_in)
Creates and returns an identity mirror.
Index buffer_size(const Vector_ &vector) const
Computes the required buffer size for a DenseVector.
void gather(LAFEM::DenseVector< DataType, IndexType > &buffer, const LAFEM::SparseVectorBlocked< DataType, IndexType, block_size_ > &vector, const Index buffer_offset=Index(0)) const
Gathers the buffer entries from a SparseVectorBlocked.
const Memory::Arbiter & indices_arbiter() const
Returns a reference to the element array arbiter.
Index size() const
Returns the size of this vector, i.e. its number of entries.
void gather(LAFEM::DenseVector< DataType, IndexType > &buffer, const LAFEM::DenseVector< DataType, IndexType > &vector, const Index buffer_offset=Index(0)) const
Gathers the buffer entries from a DenseVector.
Index buffer_size(const DenseVectorBlocked< DT2_, IT2_, block_size_ > &vector) const
Computes the required buffer size for a DenseVectorBlocked.
Index mask_scatter(const DenseVectorBlocked< DT2_, IT2_, block_size_ > &vector, std::vector< int > &mask, const int value, const Index offset=Index(0)) const
Updates a scatter mask vector for this mirror.
void scatter_axpy(LAFEM::SparseVectorBlocked< DataType, IndexType, block_size_ > &vector, const LAFEM::DenseVector< DataType, IndexType > &buffer, const DataType alpha=DataType(1), const Index buffer_offset=Index(0)) const
Scatters the buffer entries onto a SparseVectorBlocked.
void scatter_axpy(LAFEM::SparseVector< DataType, IndexType > &vector, const LAFEM::DenseVector< DataType, IndexType > &buffer, const DataType alpha=DataType(1), const Index buffer_offset=Index(0)) const
Scatters the buffer entries onto a SparseVector.
void gather(LAFEM::DenseVector< DataType, IndexType > &buffer, const LAFEM::SparseVector< DataType, IndexType > &vector, const Index buffer_offset=Index(0)) const
Gathers the buffer entries from a SparseVector.
IT_ IndexType
index-type typedef
DenseVector< DataType, IndexType > create_buffer(const Vector_ &vector) const
Creates a new buffer vector for a vector.
VectorMirror(Index size_in, Index num_idx)
Constructor.
VectorMirror()
default constructor
VectorMirror & operator=(VectorMirror &&other)
move-assign operator
void gather(LAFEM::DenseVector< DataType, IndexType > &buffer, const LAFEM::DenseVectorBlocked< DataType, IndexType, block_size_ > &vector, const Index buffer_offset=Index(0)) const
Gathers the buffer entries from a DenseVectorBlocked.
Memory::Arbiter & indices_arbiter()
Returns a reference to the element array arbiter.
VectorMirror clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
static VectorMirror make_hollow(const DenseVectorBlocked< DT_, IT_, block_size_ > &tmpl_vec)
Creates and returns a hollow mirror.
Index buffer_size(const SparseVector< DT2_, IT2_ > &vector) const
Computes the required buffer size for a SparseVector.
void scatter_axpy(LAFEM::DenseVector< DataType, IndexType > &vector, const LAFEM::DenseVector< DataType, IndexType > &buffer, const DataType alpha=DataType(1), const Index buffer_offset=Index(0)) const
Scatters the buffer entries onto a DenseVector.
void scatter_axpy(LAFEM::DenseVectorBlocked< DataType, IndexType, block_size_ > &vector, const LAFEM::DenseVector< DataType, IndexType > &buffer, const DataType alpha=DataType(1), const Index buffer_offset=Index(0)) const
Scatters the buffer entries onto a DenseVectorBlocked.
Index mask_scatter(const DenseVector< DT2_, IT2_ > &vector, std::vector< int > &mask, const int value, const Index offset=Index(0)) const
Updates a scatter mask vector for this mirror.
Index num_indices() const
Returns the number of indices in the mirror.
Index buffer_size(const SparseVectorBlocked< DT2_, IT2_, block_size_ > &vector) const
Computes the required buffer size for a SparseVectorBlocked.
void clone(const VectorMirror< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
DT_ DataType
data-type typedef
VectorMirror(VectorMirror &&other)
move-ctor
void convert(const VectorMirror< DT2_, IT2_ > &other)
Conversion method.
bool empty() const
Checks whether the vector is empty, i.e. if it has size 0.
bool hollow() const
Checks whether the vector is empty, i.e. if it has no nonzero entries.
Container< DT_, IT_ > BaseClass
our base class
Memory arbiter class.
Typed memory view class.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
Access
Memory access enumeration.
@ read_write
read-write access
@ read
read-only access
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.