FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
tuple_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#include <kernel/lafem/dense_vector.hpp>
9#include <kernel/lafem/tuple_vector.hpp>
10#include <kernel/lafem/meta_element.hpp>
11
12namespace FEAT
13{
14 namespace LAFEM
15 {
27 template<
28 typename First_,
29 typename... Rest_>
31 {
32 template<typename,typename...>
33 friend class TupleMirror;
34
35 typedef TupleMirror<Rest_...> RestClass;
36
37 public:
39 static constexpr int num_blocks = RestClass::num_blocks + 1;
40
42 typedef typename First_::DataType DataType;
44 typedef typename First_::IndexType IndexType;
45
46 // ensure that all sub-vector have the same data-type
47 static_assert(std::is_same<DataType, typename RestClass::DataType>::value, "sub-mirrors have different data-types");
48 static_assert(std::is_same<IndexType, typename RestClass::IndexType>::value, "sub-mirrors have different index-types");
49
51 template <typename DT2_ = DataType, typename IT2_ = IndexType>
53 typename First_::template MirrorType<DT2_, IT2_>,
54 typename Rest_::template MirrorType<DT2_, IT2_>...>;
55
57 template <typename DataType2_, typename IndexType2_>
59
60 protected:
62 First_ _first;
65
67 TupleMirror(First_&& the_first, RestClass&& the_rest) :
68 _first(std::forward<First_>(the_first)),
69 _rest(std::forward<RestClass>(the_rest))
70 {
71 }
72
73 public:
76 {
77 }
78
80 explicit TupleMirror(First_&& the_first, Rest_&&... the_rest) :
81 _first(std::forward<First_>(the_first)),
82 _rest(std::forward<Rest_>(the_rest)...)
83 {
84 }
85
88 _first(std::forward<First_>(other._first)),
89 _rest(std::forward<RestClass>(other._rest))
90 {
91 }
92
95 {
96 if(this != &other)
97 {
98 _first = std::forward<First_>(other._first);
99 _rest = std::forward<RestClass>(other._rest);
100 }
101 return *this;
102 }
103
110 {
111 return TupleMirror(_first.clone(clone_mode), _rest.clone(clone_mode));
112 }
113
120 template<typename... SubMirror2_>
122 {
123 this->_first.convert(other._first);
124 this->_rest.convert(other._rest);
125 }
126
135 template<typename Tv_, typename... Tw_>
137 {
138 return TupleMirror(First_::make_hollow(tmpl_vec.first()), RestClass::make_hollow(tmpl_vec.rest()));
139 }
140
142 std::size_t bytes() const
143 {
144 return _first.bytes() + _rest.bytes();
145 }
146
152 bool empty() const
153 {
154 return _first.empty() && _rest.empty();
155 }
156
162 bool hollow() const
163 {
164 return _first.hollow() && _rest.hollow();
165 }
166
168 First_& first()
169 {
170 return _first;
171 }
172
173 const First_& first() const
174 {
175 return _first;
176 }
177
178 RestClass& rest()
179 {
180 return _rest;
181 }
182
183 const RestClass& rest() const
184 {
185 return _rest;
186 }
188
195 template<typename Tv_, typename... Tw_>
197 {
198 return _first.buffer_size(vector.first()) + _rest.buffer_size(vector.rest());
199 }
200
207 template<typename Tv_, typename... Tw_>
209 {
211 }
212
222 template<int i_>
223 typename TupleElement<i_, First_, Rest_...>::Type& at()
224 {
226 }
227
229 template<int i_>
230 typename TupleElement<i_, First_, Rest_...>::Type const& at() const
231 {
233 }
234
236 template<typename Tv_, typename... Tw_>
237 void gather(
240 const Index buffer_offset = Index(0)) const
241 {
242 _first.gather(buffer, vector.first(), buffer_offset);
243 _rest.gather(buffer, vector.rest(), buffer_offset + _first.buffer_size(vector.first()));
244 }
245
247 template<typename Tv_, typename... Tw_>
251 const DataType alpha = DataType(1),
252 const Index buffer_offset = Index(0)) const
253 {
254 _first.scatter_axpy(vector.first(), buffer, alpha, buffer_offset);
255 _rest.scatter_axpy(vector.rest(), buffer, alpha, buffer_offset + _first.buffer_size(vector.first()));
256 }
257
259 template<typename Tv_, typename... Tw_>
260 Index mask_scatter(const LAFEM::TupleVector<Tv_, Tw_...>& vector, std::vector<int>& mask,
261 const int value, const Index offset = Index(0)) const
262 {
263 Index nf = _first.mask_scatter(vector.first(), mask, value, offset);
264 Index nr = _rest.mask_scatter(vector.rest(), mask, value, offset + nf);
265 return nf + nr;
266 }
267
269 template<typename Tv_, typename... Tw_>
270 Index mask_scatter_raw(const LAFEM::TupleVector<Tv_, Tw_...>& vector, std::vector<int>& mask,
271 const int value, const Index offset = Index(0)) const
272 {
273 Index nf = _first.mask_scatter_raw(vector.first(), mask, value, offset);
274 Index nr = _rest.mask_scatter_raw(vector.rest(), mask, value, offset + nf);
275 return nf + nr;
276 }
277
279 void dump(std::ostream& os) const
280 {
281 os << this->first() << ",";
282 this->rest().dump(os);
283 }
284 }; // class TupleMirror<...>
285
287 template<typename First_>
288 class TupleMirror<First_>
289 {
290 template<typename,typename...>
291 friend class TupleMirror;
292
293 public:
295 static constexpr int num_blocks = 1;
296
297 typedef typename First_::DataType DataType;
298 typedef typename First_::IndexType IndexType;
299
300 template <typename DT2_ = DataType, typename IT2_ = IndexType>
301 using MirrorType = TupleMirror<typename First_::template MirrorType<DT2_, IT2_> >;
302
304 template <typename DataType2_, typename IndexType2_>
305 using MirrorTypeByDI = MirrorType<DataType2_, IndexType2_>;
306
307 protected:
308 First_ _first;
309
310 public:
311 TupleMirror()
312 {
313 }
314
315 explicit TupleMirror(First_&& the_first) :
316 _first(std::forward<First_>(the_first))
317 {
318 }
319
320 TupleMirror(TupleMirror&& other) :
321 _first(std::forward<First_>(other._first))
322 {
323 }
324
325 TupleMirror& operator=(TupleMirror&& other)
326 {
327 if(this != &other)
328 {
329 _first = std::forward<First_>(other._first);
330 }
331 return *this;
332 }
333
334 TupleMirror clone() const
335 {
336 return TupleMirror(_first.clone());
337 }
338
339 TupleMirror clone(CloneMode clone_mode = CloneMode::Weak) const
340 {
341 return TupleMirror(_first.clone(clone_mode));
342 }
343
344 template<typename SubMirror2_>
345 void convert(const TupleMirror<SubMirror2_>& other)
346 {
347 this->_first.convert(other._first);
348 }
349
350 template<typename Tv_>
351 static TupleMirror make_hollow(const TupleVector<Tv_>& tmpl_vec)
352 {
353 return TupleMirror(First_::make_hollow(tmpl_vec.first()));
354 }
355
357 std::size_t bytes() const
358 {
359 return _first.bytes();
360 }
361
362 bool empty() const
363 {
364 return _first.empty();
365 }
366
367 bool hollow() const
368 {
369 return _first.hollow();
370 }
371
372 First_& first()
373 {
374 return _first;
375 }
376
377 const First_& first() const
378 {
379 return _first;
380 }
381
382 template<typename Tv_>
383 Index buffer_size(const TupleVector<Tv_>& vector) const
384 {
385 return _first.buffer_size(vector.first());
386 }
387
388 template<typename Tv_>
389 DenseVector<DataType, IndexType> create_buffer(const TupleVector<Tv_>& vector) const
390 {
391 return DenseVector<DataType, IndexType>(buffer_size(vector));
392 }
393
394 template<int i_>
395 First_& at()
396 {
397 static_assert(i_ == 0, "invalid sub-mirror index");
398 return _first;
399 }
400
401 template<int i_>
402 const First_& at() const
403 {
404 static_assert(i_ == 0, "invalid sub-mirror index");
405 return _first;
406 }
407
408 template<typename Tv_>
409 void gather(
410 LAFEM::DenseVector<DataType, IndexType>& buffer,
411 const LAFEM::TupleVector<Tv_>& vector,
412 const Index buffer_offset = Index(0)) const
413 {
414 _first.gather(buffer, vector.first(), buffer_offset);
415 }
416
417 template<typename Tv_>
418 void scatter_axpy(
419 LAFEM::TupleVector<Tv_>& vector,
420 const LAFEM::DenseVector<DataType, IndexType>& buffer,
421 const DataType alpha = DataType(1),
422 const Index buffer_offset = Index(0)) const
423 {
424 _first.scatter_axpy(vector.first(), buffer, alpha, buffer_offset);
425 }
426
427 template<typename Tv_>
428 Index mask_scatter(const LAFEM::TupleVector<Tv_>& vector, std::vector<int>& mask,
429 const int value, const Index offset = Index(0)) const
430 {
431 return _first.mask_scatter(vector.first(), mask, value, offset);
432 }
433
434 template<typename Tv_>
435 Index mask_scatter_raw(const LAFEM::TupleVector<Tv_>& vector, std::vector<int>& mask,
436 const int value, const Index offset = Index(0)) const
437 {
438 return _first.mask_scatter_raw(vector.first(), mask, value, offset);
439 }
440
442 void dump(std::ostream& os) const
443 {
444 os << this->first();
445 }
446 }; // class TupleMirror<First_>
447
448 template <typename First_, typename... Rest_>
449 inline std::ostream& operator<< (std::ostream & os, const TupleMirror<First_, Rest_...>& x)
450 {
451 os << "[";
452 x.dump(os);
453 os << "]";
454 return os;
455 }
457 } // namespace LAFEM
458} // namespace FEAT
Dense data vector class template.
TupleVector meta-mirror class template.
bool empty() const
Checks whether the mirror is empty.
TupleMirror(TupleMirror &&other)
move-ctor
bool hollow() const
Checks whether the mirror is hollow.
TupleMirror(First_ &&the_first, Rest_ &&... the_rest)
sub-mirror emplacement ctor
First_::DataType DataType
sub-mirror data-type
static TupleMirror make_hollow(const TupleVector< Tv_, Tw_... > &tmpl_vec)
Creates and returns an empty mirror.
static constexpr int num_blocks
number of mirror blocks
TupleMirror< typename First_::template MirrorType< DT2_, IT2_ >, typename Rest_::template MirrorType< DT2_, IT2_ >... > MirrorType
Our 'base' class type.
TupleElement< i_, First_, Rest_... >::Type & at()
Returns a sub-mirror block.
DenseVector< DataType, IndexType > create_buffer(const TupleVector< Tv_, Tw_... > &vector) const
Creates a new buffer vector for a TupleVector.
std::size_t bytes() const
Returns the total amount of bytes allocated.
Index mask_scatter_raw(const LAFEM::TupleVector< Tv_, Tw_... > &vector, std::vector< int > &mask, const int value, const Index offset=Index(0)) const
void scatter_axpy(LAFEM::TupleVector< Tv_, Tw_... > &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.
TupleMirror & operator=(TupleMirror &&other)
move-assign operator
TupleElement< i_, First_, Rest_... >::Type const & at() const
Returns a sub-mirror block.
void gather(LAFEM::DenseVector< DataType, IndexType > &buffer, const LAFEM::TupleVector< Tv_, Tw_... > &vector, const Index buffer_offset=Index(0)) const
Gathers the buffer entries from a DenseVector.
void dump(std::ostream &os) const
auxiliary function for operator<< implementation
void convert(const TupleMirror< SubMirror2_... > &other)
Conversion method.
First_::IndexType IndexType
sub-mirror index-type
MirrorType< DataType2_, IndexType2_ > MirrorTypeByDI
this typedef lets you create a mirror with new Data and Index types
Index mask_scatter(const LAFEM::TupleVector< Tv_, Tw_... > &vector, std::vector< int > &mask, const int value, const Index offset=Index(0)) const
Updates a scatter mask vector for this mirror.
TupleMirror(First_ &&the_first, RestClass &&the_rest)
data-move ctor; this one is protected for a reason
First_ _first
the first sub-mirror
RestClass _rest
all remaining sub-mirrors
TupleMirror clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
Index buffer_size(const TupleVector< Tv_, Tw_... > &vector) const
Computes the required buffer size for a TupleVector.
Variadic TupleVector class template.
@ other
generic/other permutation strategy
@ rest
restriction (multigrid)
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
Tuple container element helper class template.