FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
container.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
10#include <kernel/util/memory_arbiter.hpp>
11#include <kernel/lafem/base.hpp>
12#include <kernel/util/type_traits.hpp>
13#include <kernel/util/random.hpp>
14#include <kernel/util/pack.hpp>
15
16
17#include <vector>
18#include <array>
19#include <memory>
20#include <algorithm>
21#include <limits>
22#include <cmath>
23#include <typeinfo>
24#include <string>
25#include <type_traits>
26#include <cstdlib>
27#include <stdint.h>
28
29namespace FEAT
30{
34 namespace LAFEM
35 {
50 {
51 private:
52 CompressionModes elements_compression, indices_compression;
53 FEAT::Real tolerance;
54 public:
60#ifdef FEAT_HAVE_ZLIB
61 explicit SerialConfig() :
62 elements_compression(CompressionModes::elements_zlib),
63 indices_compression(CompressionModes::indices_zlib),
64 tolerance(FEAT::Real(-1.))
65#else
66 explicit SerialConfig() :
67 elements_compression(CompressionModes::elements_off),
68 indices_compression(CompressionModes::indices_off),
69 tolerance(FEAT::Real(-1.))
70#endif
71 {
72 }
85 explicit SerialConfig(bool zlib_compression, bool zfp_compression, FEAT::Real tol = FEAT::Real(-1.))
86 {
89 if(zlib_compression)
90 {
93 }
94 if(zfp_compression)
95 {
96 set_tolerance(tol);
98 }
99 }
100
108 {
110 switch(temp)
111 {
113 break;
115#ifdef FEAT_HAVE_ZLIB
116 break;
117#else
118 XABORTM("Zlib compression was enabled for elements, but zlib bibliography was not loaded. Please configure FEAT with -zlib flag!");
119#endif
121#ifdef FEAT_HAVE_ZFP
122 XASSERTM(tolerance > FEAT::Real(0.), "tolerance ist smaller or equall 0 while zfp is enabled!");
123 break;
124#else
125 XABORTM("Zfp compression was enabled for elements, but zfp bibliography was not loaded. Please configure FEAT with -zfp flag!");
126#endif
127 default:
128 XABORTM("No legal CompressionModes was given");
129 }
130 elements_compression = temp;
131 }
139 {
140 CompressionModes temp = comp & CompressionModes::indices_mask;
141 switch(temp)
142 {
144 break;
146#ifdef FEAT_HAVE_ZLIB
147 break;
148#else
149 XABORTM("Zlib compression was enabled for indices, but zlib bibliography was not loaded. Please configure FEAT with -zlib flag!");
150#endif
151 default:
152 XABORTM("No legal CompressionModes was given");
153 }
154 indices_compression = temp;
155 }
156
164 {
165 if(elements_compression == CompressionModes::elements_zfp)
166 {
167 XASSERTM(tol > FEAT::Real(0.), "Zfp enabled, but tolerance is smaller or equal to 0!");
168 }
169 tolerance = tol;
170 }
178 {
179 return elements_compression;
180 }
181
189 {
190 return indices_compression;
191 }
192
200 {
201 return tolerance;
202 }
203
204 }; //class SerialConfig
205
206
222 template <typename DT_, typename IT_>
224 {
225 template <typename DT2_, typename IT2_>
226 friend class Container;
227
228 protected:
230 std::vector<Memory::Arbiter> _elements;
232 std::vector<Memory::Arbiter> _indices;
234 std::vector<Index> _elements_size;
236 std::vector<Index> _indices_size;
238 std::vector<Index> _scalar_index;
240 std::vector<DT_> _scalar_dt;
241
243 mutable std::array<std::shared_ptr<void>, to_underlying(BackendIdx::num_backends)> _backend_workspaces;
244
245 void _copy_content(const Container & other, bool full)
246 {
247 // avoid self-copy
248 if(this == &other)
249 return;
250
251 XASSERTM(_elements.size() == other._elements.size(), "Container size mismatch!");
252 XASSERTM(_indices.size() == other._indices.size(), "Container size mismatch!");
253 XASSERTM(_scalar_index.size() == other._scalar_index.size(), "Container size mismatch!");
254 XASSERTM(_scalar_dt.size() == other._scalar_dt.size(), "Container size mismatch!");
255
256 if (full)
257 {
258 this->_scalar_index.assign(other._scalar_index.begin(), other._scalar_index.end());
259 this->_scalar_dt.assign(other._scalar_dt.begin(), other._scalar_dt.end());
260
261 for (Index i(0) ; i < _indices.size() ; ++i)
262 {
263 XASSERTM(_indices_size.at(i) == other._indices_size.at(i), "Container size mismatch!");
264 _indices.at(i).copy(other._indices.at(i));
265 }
266 }
267
268 for (Index i(0) ; i < _elements.size() ; ++i)
269 {
270 XASSERTM(_elements_size.at(i) == other._elements_size.at(i), "Container size mismatch!");
271 _elements.at(i).copy(other._elements.at(i));
272 }
273
274 }
275
284 template <typename DT2_, typename IT2_>
285 void assign(const Container<DT2_, IT2_> & other)
286 {
287 this->reset_workspace();
288 this->_elements.clear();
289 this->_indices.clear();
290 this->_elements_size.clear();
291 this->_indices_size.clear();
292 this->_scalar_index.clear();
293 this->_scalar_dt.clear();
294
295 this->_elements_size.assign(other.get_elements_size().begin(), other.get_elements_size().end());
296 this->_indices_size.assign(other.get_indices_size().begin(), other.get_indices_size().end());
297 this->_scalar_index.assign(other.get_scalar_index().begin(), other.get_scalar_index().end());
298 if constexpr (std::is_same<DT_, DT2_>::value)
299 {
300 this->_scalar_dt.assign(other.get_scalar_dt().begin(), other.get_scalar_dt().end());
301 }
302 else
303 {
304 for(auto t : other.get_scalar_dt())
305 {
306 this->_scalar_dt.push_back(DT_(t));
307 }
308 }
309
310 this->_elements.resize(other._elements.size());
311
312 if constexpr (std::is_same<DT_, DT2_>::value)
313 {
314 for (Index i(0) ; i < this->_elements.size() ; ++i)
315 this->_elements.at(i).attach(other._elements.at(i));
316 }
317 else
318 {
319 for (Index i(0) ; i < this->_elements_size.size() ; ++i)
320 {
321 this->_elements.at(i) = Memory::Arbiter(sizeof(DT_) * this->_elements_size.at(i));
322 this->_elements.at(i).template convert<DT_, DT2_>(other._elements.at(i));
323 }
324 }
325
326 this->_indices.resize(other._indices.size());
327
328 if constexpr (std::is_same<IT_, IT2_>::value)
329 {
330 for (Index i(0) ; i < this->_indices.size() ; ++i)
331 this->_indices.at(i).attach(other._indices.at(i));
332 }
333 else
334 {
335 for (Index i(0) ; i < this->_indices_size.size() ; ++i)
336 {
337 this->_indices.at(i) = Memory::Arbiter(sizeof(IT_) * this->_indices_size.at(i));
338 this->_indices.at(i).template convert<IT_, IT2_>(other._indices.at(i));
339 }
340 }
341 }
342
352 template <typename DT2_ = DT_, typename IT2_ = IT_>
353 std::uint64_t _serialized_size(const LAFEM::SerialConfig& config = LAFEM::SerialConfig()) const
354 {
356 tc.assign(*this);
357
358 std::uint64_t gsize(4 * sizeof(std::uint64_t)); //raw array size + magic number + type_index DT_ + type_index IT_
359 gsize += 7 * sizeof(std::uint64_t); // size of all seven stl containers
360 gsize += 2 * tc._elements_size.size() * sizeof(std::uint64_t); // _elements_size contents + _elements_arrays bytesize
361 gsize += 2 * tc._indices_size.size() * sizeof(std::uint64_t); // _indices_size contents + _indices_arrays bytesize
362 gsize += tc._scalar_index.size() * sizeof(std::uint64_t); // _scalar_index contents
363 gsize += tc._scalar_dt.size() * sizeof(DT2_); // _scalar_dt contents
364
365 CompressionModes compress = config.get_elements_compression() | config.get_indices_compression();
366 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
367 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
368 if((compress & CompressionModes::elements_mask) == CompressionModes::elements_zfp) // If zfp is used for elements
369 {
370 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
371 }
372 else if((compress & CompressionModes::elements_mask) == CompressionModes::elements_zlib) //If zlib is used and zfp is not used elements
373 {
374 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
375 }
376 if((compress & CompressionModes::indices_mask) == CompressionModes::indices_zlib) // If zlib is used for indices
377 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
378
379
380 FEAT::Real tolerance = config.get_tolerance();
381
382 for (Index i(0) ; i < tc._elements_size.size() ; ++i)
383 {
384 gsize += Pack::estimate_size(tc._elements_size.at(i), compression_type_elements, (double)tolerance); // upper_bound for (compressed) elements
385 }
386
387 for (Index i(0) ; i < tc._indices_size.size() ; ++i)
388 {
389 gsize += Pack::estimate_size(tc._indices_size.at(i), compression_type_index); // upper_bound for (compressed) indices
390 }
391 gsize += 16; //padding for datatype alignment mismatch
392
393 return gsize;
394 }
395
435 template <typename DT2_ = DT_, typename IT2_ = IT_>
436 std::vector<char> _serialize(FileMode mode, const SerialConfig& config = SerialConfig()) const
437 {
438 std::uint64_t raw_size = 0u;
439 FEAT::Real tolerance = config.get_tolerance();
440
441 CompressionModes compress = config.get_elements_compression() | config.get_indices_compression();
442 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
443 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
444 if((compress & CompressionModes::elements_mask) == CompressionModes::elements_zfp) // If zfp is used for elements
445 {
446 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
447 }
448 else if((compress & CompressionModes::elements_mask) == CompressionModes::elements_zlib) //If zlib is used and zfp is not used elements
449 {
450 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
451 }
452 if((compress & CompressionModes::indices_mask) == CompressionModes::indices_zlib) // If zlib is used for indices
453 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
454
456 tc.assign(*this);
457
458 std::uint64_t gsize = this->template _serialized_size<DT2_, IT2_>(config);
459
460 std::vector<char> result((size_t(gsize)));
461 char * array(result.data());
462 std::uint64_t * uiarray(reinterpret_cast<std::uint64_t *>(array));
463 DT2_ * dtarray(reinterpret_cast<DT2_ *>(array));
464 IT2_ * itarray(reinterpret_cast<IT2_ *>(array));
465
467 #if defined(FEAT_COMPILER_CLANG)
468 std::uint64_t magic = (std::uint64_t)static_cast<__underlying_type(FileMode)>(mode);
469 #else
470 std::uint64_t magic = (std::uint64_t)static_cast<typename std::underlying_type<FileMode>::type>(mode);
471 #endif
472 uiarray[0] = gsize; //we will overwrite this at the end with the finalized data...
473 uiarray[1] = magic;
474 uiarray[2] = Type::Traits<DT_>::feature_hash();
475 uiarray[3] = Type::Traits<IT_>::feature_hash();
476 uiarray[4] = tc._elements.size();
477 uiarray[5] = tc._indices.size();
478 uiarray[6] = tc._elements_size.size();
479 uiarray[7] = tc._indices_size.size();
480 uiarray[8] = tc._scalar_index.size();
481 uiarray[9] = tc._scalar_dt.size();
482 uiarray[10] = (std::uint64_t)compress;
483 raw_size += 11 * (std::uint64_t)sizeof(std::uint64_t);
484
485 Index global_i(11); // count how many elements of std::uint64_t have been inserted so far
486 Index local_elements(0); // count where elements array bytesize starts
487 Index local_index(0); // same for index...
488
489 for (Index i(0) ; i < tc._elements_size.size() ; ++i)
490 {
491 uiarray[i + global_i] = tc._elements_size.at(i);
492 }
493 global_i += Index(tc._elements_size.size());
494 local_elements = global_i;
495 for (Index i(0) ; i < tc._elements_size.size() ; ++i)
496 {
497 uiarray[i + global_i] = (std::uint64_t)0u; //placeholder, until we calculate the real data...
498 }
499 global_i += Index(tc._elements_size.size());
500 raw_size += 2 * std::uint64_t(tc._elements_size.size()) * std::uint64_t(sizeof(std::uint64_t));
501
502 for (Index i(0) ; i < tc._indices_size.size() ; ++i)
503 {
504 uiarray[i + global_i] = tc._indices_size.at(i);
505 }
506 global_i += Index(tc._indices_size.size());
507 local_index = global_i;
508 for (Index i(0) ; i < tc._indices_size.size() ; ++i)
509 {
510 uiarray[i + global_i] = 0;
511 }
512 global_i += Index(tc._indices_size.size());
513 raw_size += 2 * std::uint64_t(tc._indices_size.size()) * std::uint64_t(sizeof(std::uint64_t));
514
515 for (Index i(0) ; i < tc._scalar_index.size() ; ++i)
516 {
517 uiarray[i + global_i] = tc._scalar_index.at(i);
518 }
519 global_i += Index(tc._scalar_index.size());
520 raw_size += std::uint64_t(tc._scalar_index.size()) * std::uint64_t(sizeof(std::uint64_t));
521
522 global_i = Index((global_i * sizeof(std::uint64_t) + sizeof(DT2_) - 1u) / sizeof(DT2_)); // now counting how many DT2 have been inserted so far
523
524 for (Index i(0) ; i < tc._scalar_dt.size() ; ++i)
525 {
526 dtarray[i + global_i] = tc._scalar_dt.at(i);
527 }
528 global_i += Index(tc._scalar_dt.size());
529 raw_size += std::uint64_t(tc._scalar_dt.size()) * std::uint64_t(sizeof(DT2_));
530
532 {
533 global_i = Index((global_i * sizeof(DT2_) + sizeof(char) - 1u) / sizeof(char)); // now counting how many bytes/chars have been inserted so far
534 for (Index i(0) ; i < tc._elements.size() ; ++i)
535 {
537 char* dest = &result[global_i];
538 std::size_t est_buff_size = Pack::estimate_size(tc._elements_size.at(i), compression_type_elements, (double)tolerance);
539 std::size_t real_size = Pack::encode<DT2_>(dest, v.get_r(), est_buff_size, tc._elements_size.at(i), compression_type_elements, false, (double)tolerance);
540 XASSERTM(real_size != 0u, "could not encode elements");
541 uiarray[i + local_elements] = (std::uint64_t)real_size; //save used memory for compressed array
542 global_i += (Index)real_size; //go forward the number of bytes(chars)
543 raw_size += (std::uint64_t)real_size * (std::uint64_t)sizeof(char);
544 }
545 global_i = Index((global_i * sizeof(char) + sizeof(DT2_) - 1u) / sizeof(DT2_)); // go back to DT2_ indexing
546 }
547 else
548 {
549 for (Index i(0) ; i < tc._elements.size() ; ++i)
550 {
552 Memory::memcopy_main(&dtarray[global_i], v.raw_r(), tc._elements_size.at(i) * sizeof(DT2_));
553 uiarray[i + local_elements] = (std::uint64_t) tc._elements_size.at(i) * sizeof(DT2_);
554 global_i += tc._elements_size.at(i);
555 raw_size += (std::uint64_t) tc._elements_size.at(i) * sizeof(DT2_);
556 }
557 }
558 if((compress & CompressionModes::indices_mask) != CompressionModes::indices_off)
559 {
560 global_i = Index((global_i * sizeof(DT2_) + sizeof(char) - 1u) / sizeof(char)); // now counting how many bytes/chars have been inserted so far
561 for (Index i(0) ; i < tc._indices.size() ; ++i)
562 {
564 char* dest = &result[global_i];
565 std::size_t est_buff_size = Pack::estimate_size(tc._indices_size.at(i), compression_type_index);
566 std::size_t real_size = Pack::encode<IT2_>(dest, v.get_r(), est_buff_size, tc._indices_size.at(i), compression_type_index, false);
567 XASSERTM(real_size != 0u, "could not encode indices");
568 uiarray[i + local_index] = (std::uint64_t) real_size;
569 global_i += Index(real_size);
570 raw_size += std::uint64_t(real_size) * std::uint64_t(sizeof(char));
571 }
572 }
573 else
574 {
575 global_i = Index((global_i * sizeof(DT2_) + sizeof(IT2_) - 1u) / sizeof(IT2_)); // now counting IT2_ elements
576 for (Index i(0) ; i < tc._indices.size() ; ++i)
577 {
579 Memory::memcopy_main(&itarray[global_i], v.raw_r(), tc._indices_size.at(i) * sizeof(IT2_));
580 uiarray[i + local_index] = (std::uint64_t) tc._indices_size.at(i) * sizeof(IT2_);
581 global_i += tc._indices_size.at(i);
582 raw_size += (std::uint64_t) tc._indices_size.at(i) * sizeof(IT2_);
583 }
584 }
585 uiarray[0] = raw_size + 16u; //padding
586 //std::cout << "Compressed size is: " << uiarray[0] << "\n";
587
588 result.resize(std::size_t(uiarray[0]));
589 return result;
590 }
591
604 template <typename DT2_ = DT_, typename IT2_ = IT_>
605 void _serialize(FileMode mode, std::ostream & file, const SerialConfig& config = SerialConfig()) const
606 {
607 auto temp(this->template _serialize<DT2_, IT2_>(mode, config));
608 file.write(temp.data(), long(temp.size()));
609 if (!file.good())
610 XABORTM("Error in _serialize - file ostream is not good anymore!");
611 }
612
621 template <typename DT2_ = DT_, typename IT2_ = IT_>
622 void _deserialize(FileMode mode, std::vector<char> & input)
623 {
624 this->clear();
626 tc.clear();
627
628 char * array(input.data());
629 std::uint64_t * uiarray(reinterpret_cast<std::uint64_t *>(array));
630 DT2_ * dtarray(reinterpret_cast<DT2_ *>(array));
631 IT2_ * itarray(reinterpret_cast<IT2_ *>(array));
632
634#if defined(FEAT_COMPILER_CLANG)
635 std::uint64_t magic = (std::uint64_t)static_cast<__underlying_type(FileMode)>(mode);
636#else
637 std::uint64_t magic = (std::uint64_t)static_cast<typename std::underlying_type<FileMode>::type>(mode);
638#endif
639 XASSERTM(magic == uiarray[1], "_deserialize: given FileMode incompatible with given array!");
640
641 //ensure that we have the same integral/floating type configuration, that was used when storing the serialized data
648
649 if (sizeof(DT_) > Type::Helper::extract_type_size(uiarray[2]))
650 std::cerr<<"Warning: You are reading a container floating point in higher precision then it was saved before!\n";
651
652 if (sizeof(IT_) > Type::Helper::extract_type_size(uiarray[3]))
653 std::cerr<<"Warning: You are reading a container integral type in higher precision then it was saved before!\n";
654
655 CompressionModes compress = (CompressionModes)uiarray[10];
656 //test whether system has right third_party software loaded:
657#ifndef FEAT_HAVE_ZLIB
659 "Data was compressed with ZLIB! To read in data, please configure FEAT with zlib enabled.\n For more information see FEAT documentation.");
660 XASSERTM((compress & CompressionModes::indices_mask) != CompressionModes::indices_zlib,
661 "Data was compressed with ZLIB! To read in data, please configure FEAT with zlib enabled.\n For more information see FEAT documentation.");
662#endif
663#ifndef FEAT_HAVE_ZFP
665 "Data was compressed with zfp! To read in data, please configure FEAT with zfp enabled.\n For more information see FEAT documentation.");
666#endif
667 std::vector<std::uint64_t> elements_bytes(uiarray[6]); //temp arrays to keep track of bytessize of compressed arrays
668 std::vector<std::uint64_t> indices_bytes(uiarray[7]);
669 Index global_i(11);
670 for (std::uint64_t i(0) ; i < uiarray[6] ; ++i)
671 {
672 tc._elements_size.push_back(Index(uiarray[i + global_i]));
673 }
674 global_i += Index(uiarray[6]);
675 for (std::uint64_t i(0) ; i < uiarray[6] ; ++i)
676 {
677 elements_bytes[i] = uiarray[i + global_i];
678 }
679 global_i += Index(uiarray[6]);
680
681 for (std::uint64_t i(0) ; i < uiarray[7] ; ++i)
682 {
683 tc._indices_size.push_back(Index(uiarray[i + global_i]));
684 }
685 global_i += Index(uiarray[7]);
686 for (std::uint64_t i(0) ; i < uiarray[7] ; ++i)
687 {
688 indices_bytes[i] = uiarray[i + global_i];
689 }
690 global_i += Index(uiarray[7]);
691
692 for (std::uint64_t i(0) ; i < uiarray[8] ; ++i)
693 {
694 tc._scalar_index.push_back(Index(uiarray[i + global_i]));
695 }
696 global_i += Index(uiarray[8]);
697
698 global_i = Index((global_i * sizeof(std::uint64_t) + sizeof(DT2_) - 1u) / sizeof(DT2_));
699 for (std::uint64_t i(0) ; i < uiarray[9] ; ++i)
700 {
701 tc._scalar_dt.push_back(dtarray[i + global_i]);
702 }
703 global_i += Index(uiarray[9]);
704
705 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
706 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
707 if((compress & CompressionModes::elements_mask) == CompressionModes::elements_zfp)// If zfp is used
708 {
709 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
710 }
711 else if((compress & CompressionModes::elements_mask) == CompressionModes::elements_zlib) //If zlib is loaded and zfp is not used
712 {
713 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
714 }
715 if((compress & CompressionModes::indices_mask) == CompressionModes::indices_zlib)
716 {
717 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
718 }
719
721 {
722 global_i = Index((global_i * sizeof(DT2_) + sizeof(char) - 1u) / sizeof(char));
723 for(Index i(0); i < Index(uiarray[4]); ++i)
724 {
725 tc._elements.push_back(Memory::Arbiter(sizeof(DT2_) * tc._elements_size.at(i)));
727 if(0u == Pack::decode(v.get_w(), &array[global_i], (std::size_t) tc._elements_size.at(i), (std::size_t) elements_bytes[i], compression_type_elements, false))
728 XABORTM("Cannot decode compressed elements array");
729 global_i += (Index)elements_bytes[i];
730 }
731 global_i = Index((global_i * sizeof(char) + sizeof(DT2_) - 1u) / sizeof(DT2_));
732 }
733 else
734 {
735 for (Index i(0) ; i < Index(uiarray[4]) ; ++i)
736 {
737 tc._elements.push_back(Memory::Arbiter(sizeof(DT2_) * tc._elements_size.at(i)));
739 v.copy_raw_from(&dtarray[global_i], Memory::Location::main);
740 global_i += tc._elements_size.at(i);
741 }
742 }
743
744 if((compress & CompressionModes::indices_mask) != CompressionModes::indices_off)
745 {
746 global_i = Index((global_i * sizeof(DT2_) + sizeof(char) - 1u) / sizeof(char));
747 for (Index i(0) ; i < Index(uiarray[5]) ; ++i)
748 {
749 tc._indices.push_back(Memory::Arbiter(sizeof(IT2_) * tc._indices_size.at(i)));
751 if(0u == Pack::decode(v.get_w(), &array[global_i], (std::size_t) tc._indices_size.at(i), (std::size_t) indices_bytes[i], compression_type_index, false))
752 XABORTM("Cannot decode compressed indice array");
753 global_i += (Index)indices_bytes[i];
754 }
755 }
756 else
757 {
758 global_i = Index((global_i * sizeof(DT2_) + sizeof(IT2_) - 1u) / sizeof(IT2_));
759 for (Index i(0) ; i < Index(uiarray[5]) ; ++i)
760 {
761 tc._indices.push_back(Memory::Arbiter(sizeof(IT2_) * tc._indices_size.at(i)));
763 v.copy_raw_from(&itarray[global_i], Memory::Location::main);
764 global_i += tc._indices_size.at(i);
765 }
766 }
767
768 this->assign(tc);
769 }
770
779 template <typename DT2_ = DT_, typename IT2_ = IT_>
780 void _deserialize(FileMode mode, std::istream & file)
781 {
782 std::uint64_t tsize;
783 file.read((char *)&tsize, (long)(sizeof(std::uint64_t)));
784 std::vector<char> temp((size_t(tsize)));
785 file.seekg(-(long)sizeof(std::uint64_t), file.cur);
786 file.read(temp.data(), (long)(tsize));
787 if (!file.good())
788 XABORTM("Error in _deserialize - file istream is not good anymore!");
789 this->template _deserialize<DT2_, IT2_>(mode, temp);
790 }
791
792 public:
793 Container() = default;
794
800 virtual ~Container() = default;
801
810 _elements(std::move(other._elements)),
811 _indices(std::move(other._indices)),
813 _indices_size(std::move(other._indices_size)),
814 _scalar_index(std::move(other._scalar_index)),
815 _scalar_dt(std::move(other._scalar_dt)),
817 {
818 other._elements.clear();
819 other._indices.clear();
820 other._elements_size.clear();
821 other._indices_size.clear();
822 other._scalar_index.clear();
823 other._scalar_dt.clear();
824 }
825
831 void format()
832 {
833 for (Index i(0) ; i < _elements.size() ; ++i)
834 _elements.at(i).format();
835 }
836
837 void format(DT_ value)
838 {
840 for (Index i(0) ; i < _elements.size() ; ++i)
841 {
842 _elements.at(i).format(value, mem_loc);
843 }
844 }
845
853 void format(Random & rng, DT_ min, DT_ max)
854 {
857 Backend::warn_missing("LAFEM::Container::format(Random&, ...)");
858 for (Index i(0) ; i < _elements.size() ; ++i)
859 {
861 for(Index j(0); j < v.size(); ++j)
862 v[j] = rng(min, max);
863 }
864 }
865
866 void init_workspace() const
867 {
868 //do nothing
869 }
870
877 void reset_workspace() const
878 {
879 std::for_each(_backend_workspaces.begin(), _backend_workspaces.end(), [](auto& a){a.reset();});
880 }
881
885 virtual void clear()
886 {
887 this->reset_workspace();
888 this->_elements.clear();
889 this->_indices.clear();
890 this->_elements_size.clear();
891 this->_indices_size.clear();
892 this->_scalar_index.clear();
893 this->_scalar_dt.clear();
894 }
895
904 void clone(const Container & source, CloneMode clone_mode = CloneMode::Weak)
905 {
906 if (this == &source)
907 {
908 XABORTM("Trying to self-clone a lafem container!");
909 }
910
911 this->clear();
912
913 this->_scalar_index.assign(source._scalar_index.begin(), source._scalar_index.end());
914 this->_scalar_dt.assign(source._scalar_dt.begin(), source._scalar_dt.end());
915 this->_elements_size.assign(source._elements_size.begin(), source._elements_size.end());
916 this->_indices_size.assign(source._indices_size.begin(), source._indices_size.end());
917
918 this->_indices.resize(source._indices.size());
919 this->_elements.resize(source._elements.size());
920
921 // 0 = allocate only, 1 = shallow copy, 2 = deep copy
922 int what_indices = 0;
923 int what_elements = 0;
924 switch(clone_mode)
925 {
927 what_indices = 0;
928 what_elements = 0;
929 break;
930
932 what_indices = 1;
933 what_elements = 0;
934 break;
935
937 what_indices = 1;
938 what_elements = 1;
939 break;
940
941 case CloneMode::Weak:
942 what_indices = 1;
943 what_elements = 2;
944 break;
945
946 case CloneMode::Deep:
947 what_indices = 2;
948 what_elements = 2;
949 break;
950 }
951
952 // shallow copy indices if required, otherwise allocate new indices
953 if(what_indices == 1)
954 {
955 for (Index i(0) ; i < source._indices.size() ; ++i)
956 this->_indices.at(i).attach(source._indices.at(i));
957 }
958 else
959 {
960 for (Index i(0) ; i < source._indices.size() ; ++i)
961 this->_indices.at(i) = Memory::Arbiter(this->_indices_size.at(i) * sizeof(IT_));
962 }
963
964 // deep copy indices if required
965 if(what_indices == 2)
966 {
967 for (Index i(0) ; i < source._indices.size() ; ++i)
968 this->_indices.at(i).copy(source._indices.at(i));
969 }
970
971 // shallow copy elements if required, otherwise allocate new elements
972 if(what_elements == 1)
973 {
974 for (Index i(0) ; i < source._elements.size() ; ++i)
975 this->_elements.at(i).attach(source._elements.at(i));
976 }
977 else
978 {
979 for (Index i(0) ; i < source._elements.size() ; ++i)
980 this->_elements.at(i) = Memory::Arbiter(this->_elements_size.at(i) * sizeof(DT_));
981 }
982
983 // deep copy elements if required
984 if(what_elements == 2)
985 {
986 for (Index i(0) ; i < source._elements.size() ; ++i)
987 this->_elements.at(i).copy(source._elements.at(i));
988 }
989 }
990
992 template <typename DT2_, typename IT2_>
993 void clone(const Container<DT2_, IT2_> & source, CloneMode clone_mode = CloneMode::Weak)
994 {
995 Container t;
996 t.assign(source);
997 this->clone(t, clone_mode);
998 }
999
1007 void move(Container && source)
1008 {
1009 if (this == &source)
1010 return;
1011
1012 this->_elements = std::move(source._elements);
1013 this->_indices = std::move(source._indices);
1014 this->_elements_size = std::move(source._elements_size);
1015 this->_indices_size = std::move(source._indices_size);
1016 this->_scalar_index = std::move(source._scalar_index);
1017 this->_scalar_dt = std::move(source._scalar_dt);
1018 this->_backend_workspaces = std::move(source._backend_workspaces);
1019
1020 source._elements.clear();
1021 source._indices.clear();
1022 source._elements_size.clear();
1023 source._indices_size.clear();
1024 }
1025
1027 std::uint64_t get_checkpoint_size(const LAFEM::SerialConfig& config)
1028 {
1029 return this->template _serialized_size<>(config);
1030 }
1031
1033 void restore_from_checkpoint_data(std::vector<char> & data)
1034 {
1035 this->template _deserialize<>(FileMode::fm_binary, data);
1036 }
1037
1039 std::uint64_t set_checkpoint_data(std::vector<char>& data, const LAFEM::SerialConfig& config)
1040 {
1041 auto buffer = this->template _serialize<>(FileMode::fm_binary, config);
1042 data.insert(std::end(data), std::begin(buffer), std::end(buffer));
1043 return std::uint64_t(buffer.size());
1044 }
1045
1051 std::size_t bytes() const
1052 {
1053 std::size_t tbytes(0);
1054
1055 for (Index i(0) ; i < _elements_size.size() ; ++i)
1056 {
1057 tbytes += std::size_t(_elements_size.at(i) * sizeof(DT_));
1058 }
1059
1060 for (Index i(0) ; i < _indices_size.size() ; ++i)
1061 {
1062 tbytes += std::size_t(_indices_size.at(i) * sizeof(IT_));
1063 }
1064
1065 tbytes += std::size_t(_scalar_index.size() * sizeof(Index));
1066 tbytes += std::size_t(_scalar_dt.size() * sizeof(DT_));
1067
1068 return tbytes;
1069 }
1070
1076 const std::vector<Index> & get_elements_size() const
1077 {
1078 return _elements_size;
1079 }
1080
1086 const std::vector<Index> & get_indices_size() const
1087 {
1088 return _indices_size;
1089 }
1090
1096 const std::vector<Index> & get_scalar_index() const
1097 {
1098 return _scalar_index;
1099 }
1100
1106 const std::vector<DT_> & get_scalar_dt() const
1107 {
1108 return _scalar_dt;
1109 }
1110
1111 const std::shared_ptr<void>& get_workspace(PreferredBackend backend) const
1112 {
1114 }
1115
1121 static String name()
1122 {
1123 return "Container";
1124 }
1125 };
1126 } // namespace LAFEM
1127} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
static BackendIdx backend_to_index(PreferredBackend backend)
Helper function for idx mapping.
Definition: backend.hpp:66
static PreferredBackend get_preferred_backend()
get current preferred backend
Definition: backend.cpp:32
static void warn_missing(const char *operation, PreferredBackend preferred_backend, PreferredBackend fallback_backend=PreferredBackend::generic)
issues a warning about unsupported backend usage
Definition: backend.cpp:77
Container base class.
Definition: container.hpp:224
void _deserialize(FileMode mode, std::istream &file)
Deserialization of complete container entity with possible decompression.
Definition: container.hpp:780
std::size_t bytes() const
Returns the total amount of bytes allocated.
Definition: container.hpp:1051
Container(Container &&other)
Move Constructor.
Definition: container.hpp:809
virtual ~Container()=default
Destructor.
void reset_workspace() const
Resets the workspace allocation.
Definition: container.hpp:877
std::vector< char > _serialize(FileMode mode, const SerialConfig &config=SerialConfig()) const
Serialization of complete container entity(with options of lossless and lossy compression of data arr...
Definition: container.hpp:436
std::array< std::shared_ptr< void >, to_underlying(BackendIdx::num_backends)> _backend_workspaces
List of backend workspaces.
Definition: container.hpp:243
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
void clone(const Container< DT2_, IT2_ > &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Definition: container.hpp:993
const std::vector< DT_ > & get_scalar_dt() const
Returns a list of all scalar values with datatype dt.
Definition: container.hpp:1106
void move(Container &&source)
Assignment move operation.
Definition: container.hpp:1007
const std::vector< Index > & get_indices_size() const
Returns a list of all Index array sizes.
Definition: container.hpp:1086
const std::vector< Index > & get_elements_size() const
Returns a list of all data array sizes.
Definition: container.hpp:1076
std::uint64_t get_checkpoint_size(const LAFEM::SerialConfig &config)
Calculate size.
Definition: container.hpp:1027
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Definition: container.hpp:904
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
Definition: container.hpp:234
std::uint64_t _serialized_size(const LAFEM::SerialConfig &config=LAFEM::SerialConfig()) const
Calculation of the serialized size with optional compression of complete container entity.
Definition: container.hpp:353
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
Definition: container.hpp:285
static String name()
Returns a descriptive string.
Definition: container.hpp:1121
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Definition: container.hpp:1033
std::uint64_t set_checkpoint_data(std::vector< char > &data, const LAFEM::SerialConfig &config)
Definition: container.hpp:1039
void format(Random &rng, DT_ min, DT_ max)
Reset all elements of the container to random values.
Definition: container.hpp:853
const std::vector< Index > & get_scalar_index() const
Returns a list of all scalar values with datatype index.
Definition: container.hpp:1096
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
Definition: container.hpp:232
virtual void clear()
Free all allocated arrays.
Definition: container.hpp:885
void _serialize(FileMode mode, std::ostream &file, const SerialConfig &config=SerialConfig()) const
Serialization of complete container entity(with options of lossless and lossy compression of data arr...
Definition: container.hpp:605
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
Definition: container.hpp:236
std::vector< DT_ > _scalar_dt
List of scalars with datatype DT_.
Definition: container.hpp:240
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
Definition: container.hpp:230
void _deserialize(FileMode mode, std::vector< char > &input)
Deserialization of complete container entity with possible decompression.
Definition: container.hpp:622
std::vector< Index > _scalar_index
List of scalars with datatype index.
Definition: container.hpp:238
Config class for serialize parameter.
Definition: container.hpp:50
void set_tolerance(FEAT::Real tol)
method for setting tolerance
Definition: container.hpp:163
SerialConfig()
Default Constructor.
Definition: container.hpp:66
CompressionModes get_elements_compression() const
method for getting elements_compression
Definition: container.hpp:177
FEAT::Real get_tolerance() const
method for getting tolerance
Definition: container.hpp:199
SerialConfig(bool zlib_compression, bool zfp_compression, FEAT::Real tol=FEAT::Real(-1.))
Constructor.
Definition: container.hpp:85
void set_indices_compression(CompressionModes comp)
method for setting indices_compression
Definition: container.hpp:138
void set_elements_compression(CompressionModes comp)
method for setting elements_compression
Definition: container.hpp:107
CompressionModes get_indices_compression() const
method for getting indices_compression
Definition: container.hpp:188
Memory arbiter class.
Typed memory view class.
Index size() const
Returns the number of array elements mapped to this view.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
Raw memory view class.
void copy_raw_from(const void *src_ptr, Location src_loc)
Copies the contents of a given memory region into the memory of this memory view.
const void * raw_r() const
Returns a const raw pointer to the memory block that grants read access.
Pseudo-Random Number Generator.
Definition: random.hpp:54
String class implementation.
Definition: string.hpp:47
LAFEM common type definitions.
CompressionModes
Definition: base.hpp:76
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
@ read
read-only access
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
std::size_t estimate_size(const std::size_t count, const Pack::Type type, const double tolerance)
Computes the estimated (upper bound) pack buffer size for an array.
Definition: pack.cpp:557
Type
Type enumeration.
Definition: pack.hpp:70
std::size_t decode(T_ *dst, void *buf, const std::size_t count, const std::size_t buf_size, const Pack::Type pack_type, bool swap_bytes)
Decodes an array from a packed buffer.
Definition: pack.cpp:694
FEAT namespace.
Definition: adjactor.hpp:12
PreferredBackend
The backend that shall be used in all compute heavy calculations.
Definition: backend.hpp:18
double Real
Real data type.
@ value
specifies whether the space should supply basis function values
constexpr auto to_underlying(E e) noexcept
Cast enum to underlying type.
std::uint64_t Index
Index data type.
static bool extract_signedness(uint64_t feature_hash)
extracts sign feature from a given types feature hash
Definition: type_traits.hpp:51
static bool extract_intness(uint64_t feature_hash)
extracts integral feature from a given types feature hash
Definition: type_traits.hpp:39
static bool extract_floatness(uint64_t feature_hash)
extracts floating point feature from a given types feature hash
Definition: type_traits.hpp:45
static size_t extract_type_size(uint64_t feature_hash)
extracts sizeof datatype from a given types feature hash
Definition: type_traits.hpp:33
basic Type Traits struct
Definition: type_traits.hpp:73