FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
dense_vector_blocked.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
12#include <kernel/util/math.hpp>
13#include <kernel/util/random.hpp>
14#include <kernel/util/statistics.hpp>
15#include <kernel/util/time_stamp.hpp>
16#include <kernel/util/tiny_algebra.hpp>
17#include <kernel/util/type_traits.hpp>
18#include <kernel/adjacency/permutation.hpp>
19#include <kernel/lafem/forward.hpp>
20#include <kernel/lafem/container.hpp>
21#include <kernel/lafem/arch/axpy_block.hpp>
22#include <kernel/lafem/arch/axpy_dense.hpp>
23#include <kernel/lafem/arch/component_invert_dense.hpp>
24#include <kernel/lafem/arch/component_product_dense.hpp>
25#include <kernel/lafem/arch/copy_stride.hpp>
26#include <kernel/lafem/arch/dot_product_block.hpp>
27#include <kernel/lafem/arch/dot_product_dense.hpp>
28#include <kernel/lafem/arch/max_rel_diff_dense.hpp>
29#include <kernel/lafem/arch/min_max_index_dense.hpp>
30#include <kernel/lafem/arch/min_max_value_block.hpp>
31#include <kernel/lafem/arch/min_max_value_dense.hpp>
32#include <kernel/lafem/arch/norm2_dense.hpp>
33#include <kernel/lafem/arch/norm2_block.hpp>
34#include <kernel/lafem/arch/scale_dense.hpp>
35#include <kernel/lafem/arch/scale_block.hpp>
36#include <kernel/lafem/arch/triple_dot_product_dense.hpp>
37#include <kernel/lafem/arch/triple_dot_product_block.hpp>
38
39#include <iostream>
40#include <fstream>
41#include <string>
42#include <stdint.h>
43
44namespace FEAT
45{
46 namespace LAFEM
47 {
65 template <typename DT_, typename IT_, int block_size_>
66 class DenseVectorBlocked : public Container<DT_, IT_>
67 {
68 public:
70 typedef DT_ DataType;
72 typedef IT_ IndexType;
74 static constexpr int block_size = block_size_;
77
79 template <typename DT2_ = DT_, typename IT2_ = IT_>
81
83 template <typename DataType2_, typename IndexType2_>
85
86 protected:
87 Index& _size_raw()
88 {
89 return this->_elements_size.at(0);
90 }
91
92 public:
98 DenseVectorBlocked() = default;
99
108 explicit DenseVectorBlocked(Index size_in)
109 {
110 if(size_in > Index(0))
111 {
112 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * std::size_t(block_size_) * size_in));
113 this->_elements_size.push_back(Index(block_size_) * size_in);
114 }
115 }
116
128 explicit DenseVectorBlocked(Index size_in, DT_ value) :
129 DenseVectorBlocked(size_in)
130 {
131 this->format(value);
132 }
133
142 explicit DenseVectorBlocked(Index size_in, Memory::Arbiter&& data)
143 {
144 if(size_in > Index(0))
145 {
146 this->_elements.push_back(std::forward<Memory::Arbiter>(data));
147 this->_elements_size.push_back(Index(block_size_) * size_in);
148 }
149 }
150
159 {
160 this->convert(other);
161 }
162
174 explicit DenseVectorBlocked(const DenseVectorBlocked & dv_in, Index size_in, Index offset_in)
175 {
176 XASSERT(size_in > Index(0));
177 XASSERTM(size_in + offset_in <= dv_in.size(), "Ranged vector part exceeds original vector size!");
178
179 const std::size_t vt_size = sizeof(DT_) * std::size_t(block_size_);
180 this->_elements.push_back(dv_in._elements.front().attach(offset_in * vt_size, size_in * vt_size));
181 this->_elements_size.push_back(size_in * Index(block_size_));
182 }
183
192 explicit DenseVectorBlocked(FileMode mode, const String& filename)
193 {
194 this->read_from(mode, filename);
195 }
196
205 explicit DenseVectorBlocked(FileMode mode, std::istream& file)
206 {
207 this->read_from(mode, file);
208 }
209
217 template <typename DT2_ = DT_, typename IT2_ = IT_>
218 explicit DenseVectorBlocked(std::vector<char> input)
219 {
220 this->deserialize<DT2_, IT2_>(input);
221 }
222
233 explicit DenseVectorBlocked(Random & rng, Index size_in, DataType min, DataType max) :
234 DenseVectorBlocked(size_in)
235 {
236 this->format(rng, min, max);
237 }
238
247 Container<DT_, IT_>(std::forward<DenseVectorBlocked>(other))
248 {
249 }
250
254 virtual ~DenseVectorBlocked() = default;
255
266 {
267 this->move(std::forward<DenseVectorBlocked>(other));
268
269 return *this;
270 }
271
280 {
282 t.clone(*this, clone_mode);
283 return t;
284 }
285
294 template< typename DT2_, typename IT2_>
296 {
297 Container<DT_, IT_>::clone(other, clone_mode);
298 }
299
307 template <typename DT2_, typename IT2_>
309 {
310 this->assign(other);
311 }
312
320 template <typename DT2_, typename IT2_>
322 {
323 XASSERTM(other.size() % Index(block_size_) == 0, "DenseVector cannot be converted to given blocksize!");
324 this->assign(other);
325 }
326
334 template <typename DT2_ = DT_, typename IT2_ = IT_>
335 void deserialize(std::vector<char> input)
336 {
337 this->template _deserialize<DT2_, IT2_>(FileMode::fm_dvb, input);
338 }
339
353 template <typename DT2_ = DT_, typename IT2_ = IT_>
354 std::vector<char> serialize(const LAFEM::SerialConfig& config = LAFEM::SerialConfig()) const
355 {
356 return this->template _serialize<DT2_, IT2_>(FileMode::fm_dvb, config);
357 }
358
360 Index size() const
361 {
362 return this->_elements_size.empty() ? Index(0) : this->_elements_size.at(0) / Index(block_size_);
363 }
364
367 {
368 return this->_elements_size.empty() ? Index(0) : this->_elements_size.at(0);
369 }
370
372 bool empty() const
373 {
374 return this->_elements_size.empty() || (this->_elements_size.at(0) <= Index(0));
375 }
376
379 {
380 return this->_elements.front();
381 }
382
385 {
386 return this->_elements.front();
387 }
388
390 {
391 if(this->_elements.empty())
393 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::read));
394 }
395
397 {
398 if(this->_elements.empty())
400 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::write));
401 }
402
403 Memory::TypedView<ValueType> elements_view_rw(Memory::Location loc = Memory::Location::main)
404 {
405 if(this->_elements.empty())
406 return Memory::TypedView<ValueType>();
407 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::read_write));
408 }
409
410 Memory::TypedView<ValueType> elements_view(Memory::Location loc, Memory::Access acc)
411 {
412 if(this->_elements.empty())
413 return Memory::TypedView<ValueType>();
414 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, acc));
415 }
416
417 Memory::TypedView<DT_> elements_view_raw_r(Memory::Location loc = Memory::Location::main) const
418 {
419 if(this->_elements.empty())
420 return Memory::TypedView<DT_>();
421 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::read));
422 }
423
424 Memory::TypedView<DT_> elements_view_raw_w(Memory::Location loc = Memory::Location::main)
425 {
426 if(this->_elements.empty())
427 return Memory::TypedView<DT_>();
428 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::write));
429 }
430
431 Memory::TypedView<DT_> elements_view_raw_rw(Memory::Location loc = Memory::Location::main)
432 {
433 if(this->_elements.empty())
434 return Memory::TypedView<DT_>();
435 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::read_write));
436 }
437
438 Memory::TypedView<DT_> elements_view_dt(Memory::Location loc, Memory::Access acc)
439 {
440 if(this->_elements.empty())
441 return Memory::TypedView<DT_>();
442 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, acc));
443 }
444
450 static String name()
451 {
452 return "DenseVectorBlocked";
453 }
454
461 void copy(const DenseVectorBlocked & x, bool full = false)
462 {
463 this->_copy_content(x, full);
464 }
465
472 void copy(const DenseVector<DT_, IT_>& x, bool full = false)
473 {
474 this->_copy_content(x, full);
475 }
476
483 void read_from(FileMode mode, const String& filename)
484 {
485 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
486 if (mode == FileMode::fm_mtx)
487 bin = std::ifstream::in;
488 std::ifstream file(filename.c_str(), bin);
489 if (! file.is_open())
490 XABORTM("Unable to open Vector file " + filename);
491 this->read_from(mode, file);
492 file.close();
493 }
494
501 void read_from(FileMode mode, std::istream & file)
502 {
503 switch (mode)
504 {
505 case FileMode::fm_mtx:
506 {
507 this->clear();
508 this->_scalar_index.push_back(0);
509
510 Index rows;
511 String line;
512 std::getline(file, line);
513 if (line.find("%%MatrixMarket matrix array real general") == String::npos)
514 {
515 XABORTM("Input-file is not a compatible mtx-vector-file");
516 }
517 while (! file.eof())
518 {
519 std::getline(file, line);
520 if (file.eof())
521 XABORTM("Input-file is empty");
522
523 String::size_type begin(line.find_first_not_of(" "));
524 if (line.at(begin) != '%')
525 break;
526 }
527 {
528 String::size_type begin(line.find_first_not_of(" "));
529 line.erase(0, begin);
530 String::size_type end(line.find_first_of(" "));
531 String srows(line, 0, end);
532 rows = (Index)atol(srows.c_str());
533 line.erase(0, end);
534
535 begin = line.find_first_not_of(" ");
536 line.erase(0, begin);
537 end = line.find_first_of(" ");
538 String scols(line, 0, end);
539 Index cols((Index)atol(scols.c_str()));
540 line.erase(0, end);
541 if (cols != 1)
542 XABORTM("Input-file is no dense-vector-file");
543 }
544
545 DenseVectorBlocked<DT_, IT_, block_size_> tmp(rows / block_size_);
546 Memory::TypedView<DT_> tmp_view(tmp.elements_view_raw_w());
547 DT_* pval = tmp_view.get_w();
548
549 while (! file.eof())
550 {
551 std::getline(file, line);
552 if (file.eof())
553 break;
554
555 String::size_type begin(line.find_first_not_of(" "));
556 line.erase(0, begin);
557 String::size_type end(line.find_first_of(" "));
558 String sval(line, 0, end);
559 DT_ tval((DT_)atof(sval.c_str()));
560
561 *pval = tval;
562 ++pval;
563 }
564 tmp_view.release();
565 this->move(std::move(tmp));
566 break;
567 }
568 case FileMode::fm_exp:
569 {
570 this->clear();
571 this->_scalar_index.push_back(0);
572
573 std::vector<DT_> data;
574
575 while (! file.eof())
576 {
577 std::string line;
578 std::getline(file, line);
579 if (line.find("#", 0) < line.npos)
580 continue;
581 if (file.eof())
582 break;
583
584 std::string n_z_s;
585
586 std::string::size_type first_digit(line.find_first_not_of(" "));
587 line.erase(0, first_digit);
588 std::string::size_type eol(line.length());
589 for (std::string::size_type i(0); i < eol; ++i)
590 {
591 n_z_s.append(1, line[i]);
592 }
593
594 DT_ n_z((DT_)atof(n_z_s.c_str()));
595
596 data.push_back(n_z);
597 }
598
599 DenseVectorBlocked<DT_, IT_, block_size_> tmp(data.size() / Index(block_size_));
600 {
601 Memory::TypedView<DT_> tmp_view(tmp.elements_view_raw_w());
602 tmp_view.convert_from(data.data());
603 }
604
605 this->move(std::move(tmp));
606 break;
607 }
608
609 case FileMode::fm_dvb:
610 [[fallthrough]];
611
613 {
614 this->clear();
615
616 this->template _deserialize<double, std::uint64_t>(FileMode::fm_dvb, file);
617 break;
618 }
619
620 default:
621 XABORTM("Filemode not supported!");
622 }
623 }
624
631 void write_out(FileMode mode, const String& filename) const
632 {
633 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
634 if (mode == FileMode::fm_mtx || mode == FileMode::fm_exp)
635 bin = std::ofstream::out;
636 std::ofstream file;
637 char* buff = nullptr;
638 if(mode == FileMode::fm_mtx)
639 {
640 buff = new char[LAFEM::file_out_stream_buffer_size];
641 file.rdbuf()->pubsetbuf(buff, LAFEM::file_out_stream_buffer_size);
642 }
643 file.open(filename.c_str(), bin);
644 if(! file.is_open())
645 XABORTM("Unable to open Matrix file " + filename);
646 this->write_out(mode, file);
647 file.close();
648 delete[] buff;
649 }
650
657 void write_out(FileMode mode, std::ostream & file) const
658 {
659 switch (mode)
660 {
661 case FileMode::fm_mtx:
662 {
663 const Index tsize(this->size_raw());
664 file << "%%MatrixMarket matrix array real general\n";
665 file << tsize << " " << 1 << "\n";
666 file << std::scientific << std::setprecision(Type::Traits<DT_>::format_precision);
667
668 const Memory::TypedView<DT_> elem_view(this->elements_view_raw_r());
669 for (Index i(0); i < tsize; ++i)
670 {
671 file << elem_view(i) << "\n";
672 }
673 break;
674 }
675
676 case FileMode::fm_exp:
677 {
678 const Index tsize(this->size_raw());
679 file << std::scientific << std::setprecision(Type::Traits<DT_>::format_precision);
680 const Memory::TypedView<DT_> elem_view(this->elements_view_raw_r());
681 for (Index i(0); i < tsize; ++i)
682 {
683 file << elem_view(i) << "\n";
684 }
685 break;
686 }
687
688 case FileMode::fm_dvb:
689 [[fallthrough]];
690
692 this->template _serialize<double, std::uint64_t>(FileMode::fm_dvb, file);
693 break;
694
695 default:
696 XABORTM("Filemode not supported!");
697 }
698 }
699
702
709 void axpy(
710 const DenseVectorBlocked & x,
711 const DT_ alpha = DT_(1))
712 {
713 XASSERTM(x.size() == this->size(), "Vector size does not match!");
714
715 if(this->empty())
716 return;
717
718 TimeStamp ts_start;
719 FEAT_KERNEL_MARKER_START("DV_axpy");
720
721 Arch::AxpyDense::template exec<DataType>(this->elements_arbiter(), alpha, x.elements_arbiter(), this->size_raw());
722
723 FEAT_KERNEL_MARKER_STOP("DV_axpy");
724 TimeStamp ts_stop;
725
726 Statistics::add_flops(this->size_raw() * 2);
727 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
728 }
729
738 const DenseVectorBlocked & x,
739 const ValueType alpha)
740 {
741 XASSERTM(x.size() == this->size(), "Vector size does not match!");
742
743 if(this->empty())
744 return;
745
746 TimeStamp ts_start;
747 FEAT_KERNEL_MARKER_START("DV_axpy");
748
749 Arch::AxpyBlock::template exec<ValueType>(this->elements_arbiter(), alpha, x.elements_arbiter(), this->size());
750
751 FEAT_KERNEL_MARKER_STOP("DV_axpy");
752 TimeStamp ts_stop;
753
754 Statistics::add_flops(this->size_raw() * 2);
755 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
756 }
757
765 {
766 XASSERTM(this->size() == x.size(), "Vector size does not match!");
767 XASSERTM(this->size() == y.size(), "Vector size does not match!");
768
769 if(this->empty())
770 return;
771
772 TimeStamp ts_start;
773
774 Arch::ComponentProductDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), y.elements_arbiter(), this->size_raw());
775
776 TimeStamp ts_stop;
777
779 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
780 }
781
788 void component_copy(const DenseVector<DT_, IT_>& x, const int block_)
789 {
790 XASSERTM(block_ >= 0, "Block index has to be a positive integer!");
791 XASSERTM(Index(block_) < this->size(), "Block index is too big!");
792 XASSERTM(this->size() == x.size(), "Vector size does not match!");
793
794 if(this->empty())
795 return;
796
797 TimeStamp ts_start;
798
799 Arch::CopyStride::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), block_size_, block_, 1, 0, x.size());
800
801 TimeStamp ts_stop;
802
804 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
805 }
806
813 void component_copy_to(DenseVector<DT_, IT_>& x, const int block_) const
814 {
815 XASSERTM(block_ >= 0, "Block index has to be a positive integer!");
816 XASSERTM(Index(block_) < this->size(), "Block index is too big!");
817 XASSERTM(this->size() == x.size(), "Vector size does not match!");
818
819 if(this->empty())
820 return;
821
822 TimeStamp ts_start;
823
824 Arch::CopyStride::template exec<DT_>(x.elements_arbiter(), this->elements_arbiter(), 1, 0, block_size_, block_, x.size());
825
826 TimeStamp ts_stop;
827
829 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
830 }
831
841 void component_invert(const DenseVectorBlocked & x, const DT_ alpha = DT_(1))
842 {
843 XASSERTM(this->size() == x.size(), "Vector size does not match!");
844
845 if(this->empty())
846 return;
847
848 TimeStamp ts_start;
849
850 Arch::ComponentInvertDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), alpha, this->size_raw());
851
852 TimeStamp ts_stop;
853
855 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
856 }
857
864 void scale(const DenseVectorBlocked & x, const DT_ alpha)
865 {
866 XASSERTM(x.size() == this->size(), "Vector size does not match!");
867
868 if(this->empty())
869 return;
870
871 TimeStamp ts_start;
872
873 Arch::ScaleDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), alpha, this->size_raw());
874
875 TimeStamp ts_stop;
876
878 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
879 }
880
887 void scale_blocked(const DenseVectorBlocked & x, const ValueType alpha)
888 {
889 XASSERTM(x.size() == this->size(), "Vector size does not match!");
890
891 if(this->empty())
892 return;
893
894 TimeStamp ts_start;
895
896 Arch::ScaleBlock::template exec<ValueType>(this->elements_arbiter(), x.elements_arbiter(), alpha, this->size());
897
898 TimeStamp ts_stop;
899
901 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
902 }
903
914 {
915 XASSERTM(x.size_raw() == this->size_raw(), "Vector size does not match!");
916 XASSERTM(y.size_raw() == this->size_raw(), "Vector size does not match!");
917 XASSERT(!this->empty());
918
919 TimeStamp ts_start;
920
921 DataType result = Arch::TripleDotProductDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), y.elements_arbiter(), this->size_raw());
922
923 TimeStamp ts_stop;
924
925 Statistics::add_flops(this->size_raw() * 3);
926 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
927
928 return result;
929 }
930
941 {
942 XASSERTM(x.size() == this->size(), "Vector size does not match!");
943 XASSERTM(y.size() == this->size(), "Vector size does not match!");
944 XASSERT(!this->empty());
945
946 TimeStamp ts_start;
947
948 ValueType result = Arch::TripleDotProductBlock::template exec<ValueType>(this->elements_arbiter(), x.elements_arbiter(), y.elements_arbiter(), this->size());
949
950 TimeStamp ts_stop;
951
952 Statistics::add_flops(this->size_raw() * 3);
953 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
954
955 return result;
956 }
957
966 {
967 XASSERTM(x.size() == this->size(), "Vector size does not match!");
968 XASSERT(!this->empty());
969
970 TimeStamp ts_start;
971
972 DataType result = Arch::DotProductDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), this->size_raw());
973
974 TimeStamp ts_stop;
975
976 Statistics::add_flops(this->size_raw() * 2);
977 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
978
979 return result;
980 }
981
990 {
991 XASSERTM(x.size() == this->size(), "Vector size does not match!");
992 XASSERT(!this->empty());
993
994 TimeStamp ts_start;
995
996 ValueType result = Arch::DotProductBlock::template exec<ValueType>(this->elements_arbiter(), x.elements_arbiter(), this->size());
997
998 TimeStamp ts_stop;
999
1000 Statistics::add_flops(this->size_raw() * 2);
1001 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1002
1003 return result;
1004 }
1005
1011 DT_ norm2() const
1012 {
1013 XASSERT(!this->empty());
1014
1015 TimeStamp ts_start;
1016
1017 DataType result = Arch::Norm2Dense::template exec<DT_>(this->elements_arbiter(), this->size_raw());
1018
1019 TimeStamp ts_stop;
1020
1021 Statistics::add_flops(this->size_raw() * 2);
1022 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1023
1024 return result;
1025 }
1026
1033 {
1034 XASSERT(!this->empty());
1035
1036 TimeStamp ts_start;
1037
1038 ValueType result = Arch::Norm2Block::template exec<ValueType>(this->elements_arbiter(), this->size());
1039
1040 TimeStamp ts_stop;
1041
1042 Statistics::add_flops(this->size_raw() * 2);
1043 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1044
1045 return result;
1046 }
1047
1053 DT_ norm2sqr() const
1054 {
1055 // fallback
1056 return Math::sqr(this->norm2());
1057 }
1058
1064 ValueType norm2sqr_blocked() const
1065 {
1066 ValueType result = norm2_blocked();
1067 for(int i = 0; i < block_size_; ++i)
1068 result[i] *= result[i];
1069 return result;
1070 }
1071
1078 {
1079 XASSERT(!this->empty());
1080
1081 TimeStamp ts_start;
1082
1083 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->size_raw(), false, true);
1084
1085 TimeStamp ts_stop;
1086 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1087
1088 return result;
1089 }
1090
1097 {
1098 XASSERT(!this->empty());
1099
1100 TimeStamp ts_start;
1101
1102 ValueType result = Arch::MinMaxValueBlock::template exec<ValueType>(this->elements_arbiter(), this->size(), false, true);
1103
1104 TimeStamp ts_stop;
1105 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1106
1107 return result;
1108 }
1109
1116 {
1117 XASSERT(!this->empty());
1118
1119 TimeStamp ts_start;
1120
1121 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->size_raw(), true, true);
1122
1123 TimeStamp ts_stop;
1124 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1125
1126 return result;
1127 }
1128
1135 {
1136 XASSERT(!this->empty());
1137
1138 TimeStamp ts_start;
1139
1140 ValueType result = Arch::MinMaxValueBlock::template exec<ValueType>(this->elements_arbiter(), this->size(), true, true);
1141
1142 TimeStamp ts_stop;
1143 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1144
1145 return result;
1146 }
1147
1153 DT_ max_element() const
1154 {
1155 XASSERT(!this->empty());
1156
1157 TimeStamp ts_start;
1158
1159 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->size_raw(), false, false);
1160
1161 TimeStamp ts_stop;
1162 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1163
1164 return result;
1165 }
1166
1173 {
1174 XASSERT(!this->empty());
1175
1176 TimeStamp ts_start;
1177
1178 ValueType result = Arch::MinMaxValueBlock::template exec<ValueType>(this->elements_arbiter(), this->size(), false, false);
1179
1180 TimeStamp ts_stop;
1181 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1182
1183 return result;
1184 }
1185
1191 DT_ min_element() const
1192 {
1193 XASSERT(!this->empty());
1194
1195 TimeStamp ts_start;
1196
1197 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->elements_arbiter(), this->size_raw(), true, false);
1198
1199 TimeStamp ts_stop;
1200 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1201
1202 return result;
1203 }
1204
1211 {
1212 XASSERT(!this->empty());
1213
1214 TimeStamp ts_start;
1215
1216 ValueType result = Arch::MinMaxValueBlock::template exec<ValueType>(this->elements_arbiter(), this->size(), true, false);
1217
1218 TimeStamp ts_stop;
1219 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1220
1221 return result;
1222 }
1223
1231 {
1232 XASSERTM(x.size() == this->size(), "vector size mismatch!");
1233 XASSERT(!this->empty());
1234
1235 TimeStamp ts_start;
1236
1237 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->elements_arbiter(), x.elements_arbiter(), this->size_raw());
1238
1239 TimeStamp ts_stop;
1240 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1241
1242 return result;
1243 }
1244
1253 bool same_layout(const DenseVectorBlocked& x) const
1254 {
1255 return this->size() == x.size();
1256 }
1257
1259
1262 {
1263 if(perm.empty())
1264 return;
1265
1266 XASSERTM(perm.size() == this->size(), "Container size does not match permutation size");
1267 XASSERT(!this->empty());
1268
1270
1271 perm.apply(view.get_w());
1272 }
1273
1275
1283 Index get_values(DT_ * const pval_set) const
1284 {
1285 this->_elements.front().copy_to(pval_set, Memory::Location::main);
1286 return this->size_raw();
1287 }
1288
1297 Index set_values(const DT_ * const pval_set)
1298 {
1299 this->_elements.front().copy(pval_set, Memory::Location::main);
1300 return this->size_raw();
1301 }
1303
1310 friend std::ostream & operator<<(std::ostream & lhs, const DenseVectorBlocked & b)
1311 {
1312 const Memory::TypedView<DT_> view(b.elements_view_raw_r());
1313 const Index n = b.size_raw();
1314 lhs << "[";
1315 for (Index i(0); i < n; ++i)
1316 {
1317 lhs << " " << view[i];
1318 }
1319 lhs << "]";
1320
1321 return lhs;
1322 }
1323
1330 {
1331 public:
1333 typedef DT_ DataType;
1334 typedef IT_ IndexType;
1336
1337 private:
1338 const IT_ _num_entries;
1340
1341 public:
1342 explicit ScatterAxpy(VectorType & vector) :
1343 _num_entries(IT_(vector.size())),
1345 {
1346 }
1347
1348 template <typename LocalVector_, typename Mapping_>
1349 void operator()(const LocalVector_ & loc_vec, const Mapping_ & mapping, DT_ alpha = DT_(1))
1350 {
1351 // loop over all local entries
1352 for (int i(0); i < mapping.get_num_local_dofs(); ++i)
1353 {
1354 // get dof index
1355 Index dof_idx = mapping.get_index(i);
1356 ASSERT(dof_idx < _num_entries);
1357
1358 // update vector data
1359 _data_view[dof_idx] += alpha * loc_vec[i];
1360 }
1361 }
1362 }; // class ScatterAxpy
1363
1370 {
1371 public:
1373 typedef DT_ DataType;
1374 typedef IT_ IndexType;
1376
1377 private:
1378 const Index _num_entries;
1379 const Memory::TypedView<ValueType> _data_view;
1380
1381 public:
1382 explicit GatherAxpy(const VectorType & vector) :
1383 _num_entries(vector.size()),
1384 _data_view(vector.elements_view_r())
1385 {
1386 }
1387
1388 template <typename LocalVector_, typename Mapping_>
1389 void operator()(LocalVector_ & loc_vec, const Mapping_ & mapping, DT_ alpha = DT_(1))
1390 {
1391 // loop over all local entries
1392 for (int i(0); i < mapping.get_num_local_dofs(); ++i)
1393 {
1394 // get dof index
1395 Index dof_idx = mapping.get_index(i);
1396 ASSERT(dof_idx < _num_entries);
1397
1398 // update local vector data
1399 loc_vec[i].axpy(alpha, _data_view(dof_idx));
1400 }
1401 }
1402 }; // class GatherAxpy
1403 }; // class DenseVectorBlocked<...>
1404 } // namespace LAFEM
1405} // namespace FEAT
#define ASSERT(expr)
Debug-Assertion macro definition.
Definition: assertion.hpp:229
#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.
Index size() const
returns the size of the permutation
bool empty() const
Checks whether the permutation is empty.
void apply(Tx_ *x, bool invert=false) const
Applies In-Situ permutation.
Container base class.
Definition: container.hpp:224
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
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
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
Definition: container.hpp:234
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
Definition: container.hpp:285
virtual void clear()
Free all allocated arrays.
Definition: container.hpp:885
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
Definition: container.hpp:230
std::vector< Index > _scalar_index
List of scalars with datatype index.
Definition: container.hpp:238
Gather-Axpy operation for DenseVectorBlocked.
Scatter-Axpy operation for DenseVectorBlocked.
Blocked Dense data vector class template.
DenseVectorBlocked(FileMode mode, std::istream &file)
Constructor.
bool same_layout(const DenseVectorBlocked &x) const
Checks if the structural layout of this vector matches that of another vector. This excludes comparis...
DenseVectorBlocked(Index size_in)
Constructor.
DenseVectorBlocked(DenseVectorBlocked &&other)
Move Constructor.
DenseVectorBlocked(const DenseVector< DT_, IT_ > &other)
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=LAFEM::SerialConfig()) const
Serialization of complete container entity.
void clone(const DenseVectorBlocked< DT2_, IT2_, block_size_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
void component_copy_to(DenseVector< DT_, IT_ > &x, const int block_) const
Copies the elements of a specific block of this blocked vector into a dense vector.
void scale(const DenseVectorBlocked &x, const DT_ alpha)
Calculate .
DT_ min_element() const
Retrieve the minimum value of this vector.
DataType dot(const DenseVectorBlocked &x) const
Calculate .
ValueType min_abs_element_blocked() const
Retrieve the absolute minimum value of this vector.
static String name()
Returns a descriptive string.
DenseVectorBlocked(const DenseVectorBlocked &dv_in, Index size_in, Index offset_in)
Constructor.
void copy(const DenseVectorBlocked &x, bool full=false)
Performs .
DenseVectorBlocked(std::vector< char > input)
Constructor.
void axpy(const DenseVectorBlocked &x, const DT_ alpha=DT_(1))
Calculate .
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
void component_product(const DenseVectorBlocked &x, const DenseVectorBlocked &y)
Calculate .
DataType triple_dot(const DenseVectorBlocked &x, const DenseVectorBlocked &y) const
Calculate .
static constexpr int block_size
Our size of a single block.
Index size() const
Returns the size of this vector, i.e. its number of entries.
DenseVectorBlocked(FileMode mode, const String &filename)
Constructor.
void component_copy(const DenseVector< DT_, IT_ > &x, const int block_)
Copies the elements of a dense vector into a specific block of this blocked vector.
void convert(const DenseVector< DT2_, IT2_ > &other)
Conversion method.
const Memory::Arbiter & elements_arbiter() const
Returns a reference to the element array arbiter.
DT_ norm2() const
Calculates and returns the euclidean norm of this vector.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
ValueType dot_blocked(const DenseVectorBlocked &x) const
Calculate .
DT_ max_rel_diff(const DenseVectorBlocked &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
void convert(const DenseVectorBlocked< DT2_, IT2_, block_size_ > &other)
Conversion method.
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
void permute(Adjacency::Permutation &perm)
Permute vector according to the given Permutation.
bool empty() const
Checks whether the vector is empty, i.e. if it has size 0.
ValueType max_abs_element_blocked() const
Retrieve the absolute maximum value of this vector.
ValueType min_element_blocked() const
Retrieve the minimum value of this vector.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
Index size_raw() const
Returns the size of this vector in scalar data type entries, i.e. its number of entries multiplied by...
Tiny::Vector< DT_, block_size_ > ValueType
Our value type.
virtual ~DenseVectorBlocked()=default
Destructor.
friend std::ostream & operator<<(std::ostream &lhs, const DenseVectorBlocked &b)
DenseVectorBlocked streaming operator.
DenseVectorBlocked(Index size_in, DT_ value)
Constructor.
void axpy_blocked(const DenseVectorBlocked &x, const ValueType alpha)
Calculate .
ValueType triple_dot_blocked(const DenseVectorBlocked &x, const DenseVectorBlocked &y) const
Calculate .
void copy(const DenseVector< DT_, IT_ > &x, bool full=false)
Performs .
DT_ min_abs_element() const
Retrieve the absolute minimum value of this vector.
ValueType norm2sqr_blocked() const
Calculates and returns the squared euclidean norm of this vector.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this vector.
DenseVectorBlocked(Index size_in, Memory::Arbiter &&data)
Constructor.
void scale_blocked(const DenseVectorBlocked &x, const ValueType alpha)
Calculate .
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
DT_ norm2sqr() const
Calculates and returns the squared euclidean norm of this vector.
DT_ max_element() const
Retrieve the maximum value of this vector.
DenseVectorBlocked(Random &rng, Index size_in, DataType min, DataType max)
Constructor.
ValueType max_element_blocked() const
Retrieve the maximum value of this vector.
DenseVectorBlocked()=default
Constructor.
DenseVectorBlocked & operator=(DenseVectorBlocked &&other)
Assignment move operator.
ValueType norm2_blocked() const
Calculates and returns the euclidean norm of this vector.
void component_invert(const DenseVectorBlocked &x, const DT_ alpha=DT_(1))
Performs .
DenseVectorBlocked clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
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.
Config class for serialize parameter.
Definition: container.hpp:50
Memory arbiter class.
Typed memory view class.
void convert_from(const T_ *ptr_src)
overload of convert_from for the identical type
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
void release() const
Releases this view.
Pseudo-Random Number Generator.
Definition: random.hpp:54
static void add_flops(Index flops)
Add an amount of flops to the global flop counter.
Definition: statistics.hpp:206
String class implementation.
Definition: string.hpp:47
Time stamp class.
Definition: time_stamp.hpp:54
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
Definition: time_stamp.hpp:100
Tiny Vector class template.
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
Definition: base.hpp:105
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
Access
Memory access enumeration.
@ read_write
read-write access
@ overlap
explicitly allow overlapping writes (see Overlapping Memory Views)
@ 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.
basic Type Traits struct
Definition: type_traits.hpp:73