FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sparse_matrix_cscr.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
11#include <kernel/util/memory_arbiter.hpp>
12#include <kernel/util/statistics.hpp>
13#include <kernel/util/time_stamp.hpp>
14#include <kernel/adjacency/graph.hpp>
15#include <kernel/adjacency/permutation.hpp>
16#include <kernel/lafem/forward.hpp>
17#include <kernel/lafem/container.hpp>
18#include <kernel/lafem/dense_vector.hpp>
19#include <kernel/lafem/sparse_layout.hpp>
20#include <kernel/lafem/vector_mirror.hpp>
21#include <kernel/lafem/arch/axpy_dense.hpp>
22#include <kernel/lafem/arch/max_rel_diff_dense.hpp>
23#include <kernel/lafem/arch/norm2_dense.hpp>
24#include <kernel/lafem/arch/matvecmult_cscr_dense.hpp>
25#include <kernel/lafem/arch/scale_dense.hpp>
26
27// includes, system
28#include <fstream>
29
30namespace FEAT
31{
32 namespace LAFEM
33 {
57 template <typename DT_, typename IT_ = Index>
58 class SparseMatrixCSCR : public Container<DT_, IT_>
59 {
60 public:
62 typedef DT_ DataType;
64 typedef IT_ IndexType;
66 typedef DT_ ValueType;
74 template <typename DT2_ = DT_, typename IT2_ = IT_>
76
78 template <typename DataType2_, typename IndexType2_>
80
81 static constexpr bool is_global = false;
82 static constexpr bool is_local = true;
83
84 protected:
85 Index & _rows()
86 {
87 return this->_scalar_index.at(0);
88 }
89
90 Index & _cols()
91 {
92 return this->_scalar_index.at(1);
93 }
94
95 Index & _nonzero_rows()
96 {
97 return this->_scalar_index.at(2);
98 }
99
100 Index & _nzes()
101 {
102 return this->_scalar_index.at(3);
103 }
104
105 void _alloc(Index rows, Index cols, Index nz_rows, Index nzes)
106 {
107 XASSERT(this->_scalar_index.empty());
108 XASSERT(this->_indices.empty());
109 XASSERT(this->_elements.empty());
110
111 this->_scalar_index.push_back(rows);
112 this->_scalar_index.push_back(cols);
113 this->_scalar_index.push_back(nz_rows);
114 this->_scalar_index.push_back(nzes);
115
116 if(rows <= Index(0))
117 return;
118
119 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * (nz_rows + 1)));
120 this->_indices_size.push_back(nz_rows + 1);
121
122 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * nz_rows));
123 this->_indices_size.push_back(nz_rows);
124
125 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * nzes));
126 this->_indices_size.push_back(nzes);
127
128 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * nzes));
129 this->_elements_size.push_back(nzes);
130 }
131
132 public:
139 {
140 this->_scalar_index.push_back(0);
141 this->_scalar_index.push_back(0);
142 this->_scalar_index.push_back(0);
143 this->_scalar_index.push_back(0);
144 }
145
157 explicit SparseMatrixCSCR(Index rows_in, Index columns_in)
158 {
159 this->_scalar_index.push_back(rows_in);
160 this->_scalar_index.push_back(columns_in);
161 this->_scalar_index.push_back(0);
162 this->_scalar_index.push_back(0);
163 }
164
177 explicit SparseMatrixCSCR(Index rows, Index cols, Index nz_rows, Index nzes)
178 {
179 XASSERT(rows != Index(0) && cols != Index(0));
180 this->_alloc(rows, cols, nz_rows, nzes);
181 }
182
191 {
192 this->_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
193 this->_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
194
195 for(const auto& idx : layout_in.get_indices())
196 this->_indices.push_back(idx.attach());
197
198 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * _nzes()));
199 this->_elements_size.push_back(_nzes());
200 }
201
209 template <typename MT_>
210 explicit SparseMatrixCSCR(const MT_ & other)
211 {
212 this->convert(other);
213 }
214
223 explicit SparseMatrixCSCR(FileMode mode, const String& filename)
224 {
225 this->read_from(mode, filename);
226 }
227
236 explicit SparseMatrixCSCR(FileMode mode, std::istream& file)
237 {
238 this->read_from(mode, file);
239 }
240
254 explicit SparseMatrixCSCR(const Index rows_in, const Index columns_in,
255 DenseVector<IT_, IT_> & row_ptr_in, DenseVector<IT_, IT_> & row_idx_in,
256 DenseVector<IT_, IT_> & col_idx_in, DenseVector<DT_, IT_> & val_in)
257 {
259 XASSERT(col_idx_in.size() > 0);
260 XASSERT(val_in.size() == col_idx_in.size());
261 XASSERT(row_ptr_in.size() > 0);
262 XASSERT(row_idx_in.size() > 0);
263 XASSERT(row_idx_in.size() + 1 == row_ptr_in.size());
264
265 this->_scalar_index.push_back(rows_in);
266 this->_scalar_index.push_back(columns_in);
267 this->_scalar_index.push_back(row_idx_in.size());
268 this->_scalar_index.push_back(val_in.size());
269
270 this->_indices.push_back(row_ptr_in.elements_arbiter().attach());
271 this->_indices_size.push_back(row_ptr_in.size());
272 this->_indices.push_back(row_idx_in.elements_arbiter().attach());
273 this->_indices_size.push_back(row_idx_in.size());
274 this->_indices.push_back(col_idx_in.elements_arbiter().attach());
275 this->_indices_size.push_back(col_idx_in.size());
276 this->_elements.push_back(val_in.elements_arbiter().attach());
277 this->_elements_size.push_back(val_in.size());
278 }
279
288 explicit SparseMatrixCSCR(const SparseMatrixCSR<DT_, IT_> & csr, const VectorMirror<DT_, IT_> & non_zero_rows)
289 {
290 XASSERT(non_zero_rows.num_indices() > 0);
291
292 const Memory::TypedView<IT_> row_ptr_in = csr.row_ptr_view_r();
293 const Memory::TypedView<DT_> val_in = csr.val_view_r();
294 const Memory::TypedView<IT_> col_idx_in = csr.col_idx_view_r();
295 const Memory::TypedView<IT_> mir_idx = non_zero_rows.indices_view_r();
296
297 // count number of non-zeros
298 Index nz_rows = non_zero_rows.num_indices();
299 Index nnzes(0);
300 for (Index i(0) ; i < nz_rows ; ++i)
301 nnzes += row_ptr_in[mir_idx[i]+1] - row_ptr_in[mir_idx[i]];
302
303 this->_alloc(csr.num_rows(), csr.num_cols(), nz_rows, nnzes);
304
305 Memory::TypedView<DT_> val = this->val_view_w();
306 Memory::TypedView<IT_> row_ptr = this->row_ptr_view_w();
307 Memory::TypedView<IT_> col_idx = this->col_idx_view_w();
308 Memory::TypedView<IT_> row_idx = this->row_idx_view_w();
309
310 row_ptr[0] = IT_(0);
311 for(Index i(0); i < nz_rows; ++i)
312 {
313 IT_ row = mir_idx[i];
314 IT_ j = row_ptr[i];
315 row_idx[i] = row;
316 for(IT_ k(row_ptr_in[row]); k < row_ptr_in[row+1]; ++k, ++j)
317 {
318 val[j] = val_in[k];
319 col_idx[j] = col_idx_in[k];
320 }
321 row_ptr[i+1] = j;
322 }
323
324 XASSERT(row_ptr[nz_rows] == nnzes);
325 }
326
334 explicit SparseMatrixCSCR(const Adjacency::Graph & graph)
335 {
336 // get number of rows, columns and indices
337 Index num_rows = graph.get_num_nodes_domain();
338 Index num_cols = graph.get_num_nodes_image();
340
341 if (num_nzes <= Index(0))
342 {
343 this->_alloc(num_rows, num_cols, Index(0), Index(0));
344 return;
345 }
346
347 // get graph arrays
348 const Index* dom_ptr = graph.get_domain_ptr();
349 const Index* img_idx = graph.get_image_idx();
350
351 // count number of non-empty rows
352 Index num_nzrs = Index(0);
353 for(Index i(0); i < num_rows; ++i)
354 {
355 num_nzrs += Index(dom_ptr[i] < dom_ptr[i+1] ? 1 : 0);
356 }
357
358 // allocate output matrix
359 this->_alloc(num_rows, num_cols, num_nzrs, num_nzes);
360
361 // get matrix arrays
362 Memory::TypedView<IT_> trow_ptr = this->row_ptr_view_w();
363 Memory::TypedView<IT_> trow_idx = this->row_idx_view_w();
364 Memory::TypedView<IT_> tcol_idx = this->col_idx_view_w();
365
366 // fill arrays
367 trow_ptr[0] = IndexType(dom_ptr[0]);
368 for(Index i(0), j(0); i < num_rows; ++i)
369 {
370 if(dom_ptr[i] < dom_ptr[i + 1])
371 {
372 ASSERT(j < num_nzrs);
373 trow_idx[ j] = IndexType(i);
374 trow_ptr[++j] = IndexType(dom_ptr[i+1]);
375 }
376 }
377
378 for(Index k(0); k < num_nzes; ++k)
379 tcol_idx[k] = IndexType(img_idx[k]);
380 }
381
389 template <typename DT2_ = DT_, typename IT2_ = IT_>
390 explicit SparseMatrixCSCR(std::vector<char> input)
391 {
392 this->deserialize<DT2_, IT2_>(input);
393 }
394
403 Container<DT_, IT_>(std::forward<SparseMatrixCSCR>(other))
404 {
405 }
406
415 {
416 this->move(std::forward<SparseMatrixCSCR>(other));
417
418 return *this;
419 }
420
430 {
432 t.clone(*this, clone_mode);
433 return t;
434 }
435
444 template<typename DT2_, typename IT2_>
446 {
447 Container<DT_, IT_>::clone(other, clone_mode);
448 }
449
457 template <typename DT2_, typename IT2_>
459 {
460 this->assign(other);
461 }
462
470 template <typename MT_>
471 void convert(const MT_ & a)
472 {
473 XASSERT((void*)this != (void*)&a);
474
475 this->clear();
476
477 const Index num_rows_a = a.num_rows_raw();
478 const Index num_cols_a = a.num_cols_raw();
479 const Index num_nzes_a = a.num_nzes_raw();
480 if(num_nzes_a <= Index(0))
481 {
482 this->_alloc(num_rows_a, num_cols_a, Index(0), Index(0));
483 return;
484 }
485
486 Index num_nz_rows_a(0);
487 for (Index i(0); i < num_rows_a; ++i)
488 {
489 num_nz_rows_a += Index(a.row_degree(i) > Index(0) ? 1 : 0);
490 }
491
492 this->_alloc(num_rows_a, num_cols_a, num_nz_rows_a, num_nzes_a);
493
494 Memory::TypedView<IT_> row_ptr = this->row_ptr_view_w();
495 Memory::TypedView<IT_> row_idx = this->row_idx_view_w();
496 Memory::TypedView<IT_> col_idx = this->col_idx_view_w();
497 Memory::TypedView<DT_> val = this->val_view_w();
498
499 row_ptr[0] = Index(0);
500 for(Index row(0), k(0); row < num_rows_a; ++row)
501 {
502 Index nz = a.row_degree(row);
503 if(nz > Index(0))
504 {
505 a.get_row_col_indices(row, &col_idx[row_ptr[k]], IT_(0));
506 a.get_row_values(row, &val[row_ptr[k]]);
507 row_idx[k] = IT_(row);
508 row_ptr[k+1] = row_ptr[k] + IT_(nz);
509 ++k;
510 }
511 }
512 }
513
522 {
523 this->clear();
524
525 this->_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
526 this->_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
527
528 for(const auto& idx : layout_in.get_indices())
529 this->_indices.push_back(idx.attach());
530
531 this->_elements.push_back(Memory::Arbiter(_nzes() * sizeof(DT_)));
532 this->_elements_size.push_back(_nzes());
533
534 return *this;
535 }
536
544 template <typename DT2_ = DT_, typename IT2_ = IT_>
545 void deserialize(std::vector<char> input)
546 {
547 this->template _deserialize<DT2_, IT2_>(FileMode::fm_cscr, input);
548 }
549
560 template <typename DT2_ = DT_, typename IT2_ = IT_>
561 std::vector<char> serialize(const LAFEM::SerialConfig& config = SerialConfig()) const
562 {
563 return this->template _serialize<DT2_, IT2_>(FileMode::fm_cscr, config);
564 }
565
572 void read_from(FileMode mode, const String& filename)
573 {
574 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
575 if(mode == FileMode::fm_mtx)
576 bin = std::ifstream::in;
577 std::ifstream file(filename.c_str(), bin);
578 if (! file.is_open())
579 XABORTM("Unable to open Matrix file " + filename);
580 read_from(mode, file);
581 file.close();
582 }
583
590 void read_from(FileMode mode, std::istream& file)
591 {
592 switch(mode)
593 {
595 [[fallthrough]];
596
598 this->template _deserialize<double, std::uint64_t>(FileMode::fm_cscr, file);
599 break;
600
601 default:
602 XABORTM("Filemode not supported!");
603 }
604 }
605
612 void write_out(FileMode mode, const String& filename) const
613 {
614 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
615 if(mode == FileMode::fm_mtx)
616 bin = std::ofstream::out;
617 std::ofstream file;
618 char* buff = nullptr;
619 if(mode == FileMode::fm_mtx)
620 {
621 buff = new char[LAFEM::file_out_stream_buffer_size];
622 file.rdbuf()->pubsetbuf(buff, LAFEM::file_out_stream_buffer_size);
623 }
624 file.open(filename.c_str(), bin);
625 if(! file.is_open())
626 XABORTM("Unable to open Matrix file " + filename);
627 write_out(mode, file);
628 file.close();
629 delete[] buff;
630 }
631
638 void write_out(FileMode mode, std::ostream& file) const
639 {
640 switch(mode)
641 {
643 [[fallthrough]];
644
646 this->template _serialize<double, std::uint64_t>(FileMode::fm_cscr, file);
647 break;
648
649 default:
650 XABORTM("Filemode not supported!");
651 }
652 }
653
660 {
662 }
663
665 bool empty() const
666 {
667 return this->_scalar_index.empty() || (this->_scalar_index.at(0) == Index(0));
668 }
669
671 bool hollow() const
672 {
673 return this->_scalar_index.empty() || (this->_scalar_index.at(3) == Index(0));
674 }
675
682 {
683 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(0);
684 }
685
692 {
693 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(1);
694 }
695
702 {
703 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(2);
704 }
705
712 {
713 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(3);
714 }
715
718 {
719 return this->num_rows();
720 }
721
724 {
725 return this->num_cols();
726 }
727
730 {
731 return this->num_nzes();
732 }
733
736 {
737 return this->_indices.at(0);
738 }
739
742 {
743 return this->_indices.at(0);
744 }
745
748 {
749 return this->_indices.at(1);
750 }
751
754 {
755 return this->_indices.at(1);
756 }
757
760 {
761 return this->_indices.at(2);
762 }
763
766 {
767 return this->_indices.at(2);
768 }
769
772 {
773 return this->_elements.front();
774 }
775
778 {
779 return this->_elements.front();
780 }
781
783 {
784 if(this->_elements.empty())
785 return Memory::TypedView<DT_>();
786 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::read));
787 }
788
790 {
791 if(this->_elements.empty())
792 return Memory::TypedView<DT_>();
793 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::write));
794 }
795
796 Memory::TypedView<DT_> val_view_rw(Memory::Location loc = Memory::Location::main)
797 {
798 if(this->_elements.empty())
799 return Memory::TypedView<DT_>();
800 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::read_write));
801 }
802
803 Memory::TypedView<DT_> val_view(Memory::Location loc, Memory::Access acc)
804 {
805 if(this->_elements.empty())
806 return Memory::TypedView<DT_>();
807 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, acc));
808 }
809
810 Memory::TypedView<IT_> row_ptr_view_r(Memory::Location loc = Memory::Location::main) const
811 {
812 if(this->_indices.empty())
813 return Memory::TypedView<IT_>();
814 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read));
815 }
816
817 Memory::TypedView<IT_> row_ptr_view_w(Memory::Location loc = Memory::Location::main)
818 {
819 if(this->_indices.empty())
820 return Memory::TypedView<IT_>();
821 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::write));
822 }
823
824 Memory::TypedView<IT_> row_ptr_view_rw(Memory::Location loc = Memory::Location::main)
825 {
826 if(this->_indices.empty())
827 return Memory::TypedView<IT_>();
828 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read_write));
829 }
830
831 Memory::TypedView<IT_> row_ptr_view(Memory::Location loc, Memory::Access acc)
832 {
833 if(this->_indices.empty())
834 return Memory::TypedView<IT_>();
835 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, acc));
836 }
837
838 Memory::TypedView<IT_> row_idx_view_r(Memory::Location loc = Memory::Location::main) const
839 {
840 if(this->_indices.empty())
841 return Memory::TypedView<IT_>();
842 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, Memory::Access::read));
843 }
844
845 Memory::TypedView<IT_> row_idx_view_w(Memory::Location loc = Memory::Location::main)
846 {
847 if(this->_indices.empty())
848 return Memory::TypedView<IT_>();
849 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, Memory::Access::write));
850 }
851
852 Memory::TypedView<IT_> row_idx_view_rw(Memory::Location loc = Memory::Location::main)
853 {
854 if(this->_indices.empty())
855 return Memory::TypedView<IT_>();
856 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, Memory::Access::read_write));
857 }
858
859 Memory::TypedView<IT_> row_idx_view(Memory::Location loc, Memory::Access acc)
860 {
861 if(this->_indices.empty())
862 return Memory::TypedView<IT_>();
863 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, acc));
864 }
865
866 Memory::TypedView<IT_> col_idx_view_r(Memory::Location loc = Memory::Location::main) const
867 {
868 if(this->_indices.empty())
869 return Memory::TypedView<IT_>();
870 return Memory::TypedView<IT_>(this->_indices.at(2).view(loc, Memory::Access::read));
871 }
872
873 Memory::TypedView<IT_> col_idx_view_w(Memory::Location loc = Memory::Location::main)
874 {
875 if(this->_indices.empty())
876 return Memory::TypedView<IT_>();
877 return Memory::TypedView<IT_>(this->_indices.at(2).view(loc, Memory::Access::write));
878 }
879
880 Memory::TypedView<IT_> col_idx_view_rw(Memory::Location loc = Memory::Location::main)
881 {
882 if(this->_indices.empty())
883 return Memory::TypedView<IT_>();
884 return Memory::TypedView<IT_>(this->_indices.at(2).view(loc, Memory::Access::read_write));
885 }
886
887 Memory::TypedView<IT_> col_idx_view(Memory::Location loc, Memory::Access acc)
888 {
889 if(this->_indices.empty())
890 return Memory::TypedView<IT_>();
891 return Memory::TypedView<IT_>(this->_indices.at(2).view(loc, acc));
892 }
893
899 static String name()
900 {
901 return "SparseMatrixCSCR";
902 }
903
910 void copy(const SparseMatrixCSCR & x, bool full = false)
911 {
912 this->_copy_content(x, full);
913 }
914
917
926 void axpy(const SparseMatrixCSCR & x, const DT_ alpha = DT_(1))
927 {
928 XASSERTM(x.num_rows() == this->num_rows(), "Matrix rows do not match!");
929 XASSERTM(x.num_cols() == this->num_cols(), "Matrix columns do not match!");
930 XASSERTM(x.num_nzes() == this->num_nzes(), "Matrix nonzeros do not match!");
931
932 if(this->empty())
933 return;
934
935 TimeStamp ts_start;
936
937 Arch::AxpyDense::template exec<DT_>(this->val_arbiter(), alpha, x.val_arbiter(), this->num_nzes());
938
939 TimeStamp ts_stop;
940
941 Statistics::add_flops(this->num_nzes() * 2);
942 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
943 }
944
951 void scale(const SparseMatrixCSCR & x, const DT_ alpha)
952 {
953 XASSERTM(x.num_rows() == this->num_rows(), "Row count does not match!");
954 XASSERTM(x.num_cols() == this->num_cols(), "Column count does not match!");
955 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
956
957 if(this->empty())
958 return;
959
960 TimeStamp ts_start;
961
962 Arch::ScaleDense::template exec<DT_>(this->val_arbiter(), x.val_arbiter(), alpha, this->num_nzes());
963
964 TimeStamp ts_stop;
965
967 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
968 }
969
975 DT_ norm_frobenius() const
976 {
977 XASSERT(!this->empty());
978
979 TimeStamp ts_start;
980
981 DT_ result = Arch::Norm2Dense::template exec<DT_>(this->val_arbiter(), this->num_nzes());
982
983 TimeStamp ts_stop;
984
985 Statistics::add_flops(this->num_nzes() * 2);
986 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
987
988 return result;
989 }
990
997 DT_ max_rel_diff(const SparseMatrixCSCR& x) const
998 {
999 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
1000 XASSERT(!this->empty());
1001
1002 TimeStamp ts_start;
1003
1004 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->val_arbiter(), x.val_arbiter(), this->num_nzes());
1005
1006 TimeStamp ts_stop;
1007 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1008
1009 return result;
1010 }
1011
1020 bool same_layout(const SparseMatrixCSCR& x) const
1021 {
1022 if (this->num_rows() != x.num_rows())
1023 return false;
1024 if (this->num_cols() != x.num_cols())
1025 return false;
1026 if (this->num_nzes() != x.num_nzes())
1027 return false;
1028 if (this->num_nonzero_rows() != x.num_nonzero_rows())
1029 return false;
1030
1031 if(this->num_nzes() == Index(0))
1032 return true;
1033
1034 // check if the arbiters for row_ptr, row_idx and col_idx are the same
1035 if((this->row_ptr_arbiter() == x.row_ptr_arbiter()) &&
1036 (this->row_idx_arbiter() == x.row_idx_arbiter()) &&
1037 (this->col_idx_arbiter() == x.col_idx_arbiter()))
1038 return true;
1039
1040 const Memory::TypedView<IT_> row_ptr_a = this->row_ptr_view_r();
1041 const Memory::TypedView<IT_> row_idx_a = this->row_idx_view_r();
1042 const Memory::TypedView<IT_> col_idx_a = this->col_idx_view_r();
1043 const Memory::TypedView<IT_> row_ptr_b = x.row_ptr_view_r();
1044 const Memory::TypedView<IT_> row_idx_b = x.row_idx_view_r();
1045 const Memory::TypedView<IT_> col_idx_b = x.col_idx_view_r();
1046
1047 const Index nzr = this->num_nonzero_rows();
1048 for (Index i(0) ; i < nzr; ++i)
1049 {
1050 if(row_ptr_a[i] != row_ptr_b[i])
1051 return false;
1052 if(row_idx_a[i] != row_idx_b[i])
1053 return false;
1054 }
1055 if(row_ptr_a[nzr] != row_ptr_b[nzr])
1056 return false;
1057
1058 const Index nze = this->num_nzes();
1059 for (Index i(0) ; i < nze ; ++i)
1060 {
1061 if (col_idx_a[i] != col_idx_b[i])
1062 return false;
1063 }
1064
1065 return true;
1066 }
1067
1075 {
1076 XASSERTM(r.size() == this->num_rows(), "Vector size of r does not match!");
1077 XASSERTM(x.size() == this->num_cols(), "Vector size of x does not match!");
1078
1079 if(this->empty())
1080 return;
1081 else if(this->hollow())
1082 {
1083 r.format();
1084 return;
1085 }
1086
1087 XASSERTM(r.elements_arbiter() != x.elements_arbiter(), "Vector x and r must not share the same memory!");
1088
1089 TimeStamp ts_start;
1090
1091 Arch::MatVecMultCSCRDense::template exec<DT_, IT_>(r.elements_arbiter(), DT_(1), x.elements_arbiter(), Memory::Arbiter(),
1092 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1093 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(), false);
1094
1095 TimeStamp ts_stop;
1096
1097 Statistics::add_flops(this->num_nzes() * 2);
1098 Statistics::add_time_blas2(ts_stop.elapsed(ts_start));
1099 }
1100
1108 {
1109 XASSERTM(r.size() == this->num_cols(), "Vector size of r does not match!");
1110 XASSERTM(x.size() == this->num_rows(), "Vector size of x does not match!");
1111
1112 if(this->empty())
1113 return;
1114 else if(this->hollow())
1115 {
1116 r.format();
1117 return;
1118 }
1119
1120 XASSERTM(r.elements_arbiter() != x.elements_arbiter(), "Vector x and r must not share the same memory!");
1121
1122 TimeStamp ts_start;
1123
1124 Arch::MatVecMultCSCRDense::template exec<DT_, IT_>(r.elements_arbiter(), DT_(1), x.elements_arbiter(), Memory::Arbiter(),
1125 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1126 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(), true);
1127
1128 TimeStamp ts_stop;
1129
1130 Statistics::add_flops(this->num_nzes() * 2);
1131 Statistics::add_time_blas2(ts_stop.elapsed(ts_start));
1132 }
1133
1142 void apply(
1144 const DenseVector<DT_, IT_> & x,
1145 const DenseVector<DT_, IT_> & y,
1146 const DT_ alpha = DT_(1)) const
1147 {
1148 XASSERTM(r.size() == this->num_rows(), "Vector size of r does not match!");
1149 XASSERTM(x.size() == this->num_cols(), "Vector size of x does not match!");
1150 XASSERTM(y.size() == this->num_rows(), "Vector size of y does not match!");
1151
1152 if(this->empty())
1153 return;
1154 // we check alpha strictly for equality to 0, because testing if |alpha| < eps may
1155 // lead to false triggers if the matrix/vector contents are also < eps
1156 else if(this->hollow() || (alpha == DT_(0)))
1157 {
1158 r.copy(y);
1159 return;
1160 }
1161
1162 XASSERTM(r.elements_arbiter() != x.elements_arbiter(), "Vector x and r must not share the same memory!");
1163
1164 TimeStamp ts_start;
1165
1166 Arch::MatVecMultCSCRDense::template exec<DT_, IT_>(r.elements_arbiter(), alpha, x.elements_arbiter(), y.elements_arbiter(),
1167 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1168 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(), false);
1169
1170 TimeStamp ts_stop;
1171
1172 Statistics::add_flops( (this->num_nzes() + this->num_rows()) * 2 );
1173 Statistics::add_time_blas2(ts_stop.elapsed(ts_start));
1174 }
1175
1186 const DenseVector<DT_, IT_> & x,
1187 const DenseVector<DT_, IT_> & y,
1188 const DT_ alpha = DT_(1)) const
1189 {
1190 XASSERTM(r.size() == this->num_cols(), "Vector size of r does not match!");
1191 XASSERTM(x.size() == this->num_rows(), "Vector size of x does not match!");
1192 XASSERTM(y.size() == this->num_cols(), "Vector size of y does not match!");
1193
1194 if(this->empty())
1195 return;
1196 // we check alpha strictly for equality to 0, because testing if |alpha| < eps may
1197 // lead to false triggers if the matrix/vector contents are also < eps
1198 else if(this->hollow() || (alpha == DT_(0)))
1199 {
1200 r.copy(y);
1201 return;
1202 }
1203
1204 XASSERTM(r.elements_arbiter() != x.elements_arbiter(), "Vector x and r must not share the same memory!");
1205
1206 TimeStamp ts_start;
1207
1208 Arch::MatVecMultCSCRDense::template exec<DT_, IT_>(r.elements_arbiter(), alpha, x.elements_arbiter(), y.elements_arbiter(),
1209 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1210 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(), true);
1211
1212 TimeStamp ts_stop;
1213
1214 Statistics::add_flops( (this->num_nzes() + this->num_rows()) * 2 );
1215 Statistics::add_time_blas2(ts_stop.elapsed(ts_start));
1216 }
1218
1221 {
1222 return VectorTypeL(this->num_rows());
1223 }
1224
1226 VectorTypeR create_vector_r() const
1227 {
1228 return VectorTypeR(this->num_cols());
1229 }
1230
1241 void print(std::ostream & os, bool print_dense) const
1242 {
1243 if(this->empty())
1244 {
1245 os << "[]";
1246 return;
1247 }
1248
1249 const Memory::TypedView<IT_> row_ptr = this->row_ptr_view_r();
1250 const Memory::TypedView<IT_> row_idx = this->row_idx_view_r();
1251 const Memory::TypedView<IT_> col_idx = this->col_idx_view_r();
1252 const Memory::TypedView<DT_> val = this->val_view_r();
1253 const Index nrows = this->num_rows();
1254 const Index ncols = this->num_cols();
1255 const Index nzrows = this->num_nonzero_rows();
1256
1257 IT_ l = 0;
1258 os << "[\n";
1259 for (Index i(0) ; i < nzrows ; ++i)
1260 {
1261 if(print_dense)
1262 {
1263 // write leading zero rows
1264 for(; l < row_idx[i]; ++l)
1265 {
1266 os << "[";
1267 for(Index j = 0; j < ncols; ++j)
1268 os << " " << DT_(0);
1269 os << "]\n";
1270 }
1271 }
1272 else
1273 {
1274 // write row index
1275 os << row_idx[i] << ':';
1276 }
1277
1278 os << "[";
1279 IT_ k = 0;
1280 for(IT_ j = row_ptr[i]; j < row_ptr[i+1]; ++j)
1281 {
1282 // write leading zeros
1283 if(print_dense)
1284 {
1285 for(; k < col_idx[j]; ++k)
1286 os << " " << DT_(0);
1287 }
1288 else
1289 {
1290 os << " " << col_idx[j] << ':';
1291 }
1292
1293 // write value
1294 os << " " << val[j];
1295 ++k;
1296 }
1297
1298 // write trailing zeros
1299 if(print_dense)
1300 {
1301 for(; k < ncols; ++k)
1302 os << " " << DT_(0);
1303 ++l;
1304 }
1305 os << "]\n";
1306 }
1307
1308 // write trailing zero rows
1309 if(print_dense)
1310 {
1311 for(; l < nrows; ++l)
1312 {
1313 os << "[";
1314 for(Index j = 0; j < ncols; ++j)
1315 os << " " << DT_(0);
1316 os << "]\n";
1317 }
1318 }
1319
1320 os << "]";
1321 }
1322
1329 friend std::ostream & operator<< (std::ostream & os, const SparseMatrixCSCR & b)
1330 {
1331 b.print(os, true);
1332 return os;
1333 }
1334 }; //SparseMatrixCSCR
1335
1336#ifdef FEAT_EICKT
1337 extern template class SparseMatrixCSCR<float, std::uint32_t>;
1338 extern template class SparseMatrixCSCR<double, std::uint32_t>;
1339 extern template class SparseMatrixCSCR<float, std::uint64_t>;
1340 extern template class SparseMatrixCSCR<double, std::uint64_t>;
1341#endif
1342
1343 } // namespace LAFEM
1344} // 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.
Adjacency Graph implementation.
Definition: graph.hpp:34
Index * get_domain_ptr()
Returns the domain pointer array.
Definition: graph.hpp:359
Index * get_image_idx()
Returns the image node index array.
Definition: graph.hpp:374
Index get_num_indices() const
Returns the total number indices.
Definition: graph.hpp:390
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
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
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
Definition: container.hpp:236
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
Dense data vector class template.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
void copy(const VT_ &a)
Performs .
Index size() const
Returns the size of this vector, i.e. its number of entries.
Config class for serialize parameter.
Definition: container.hpp:50
Layout scheme for sparse matrix containers.
const std::vector< Memory::Arbiter > & get_indices() const
Returns a list of all Index arrays.
CSCR based sparse matrix.
bool same_layout(const SparseMatrixCSCR &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
SparseMatrixCSCR(FileMode mode, const String &filename)
Constructor.
const Memory::Arbiter & row_ptr_arbiter() const
Returns a reference to the row-pointer array arbiter.
SparseMatrixCSCR(Index rows, Index cols, Index nz_rows, Index nzes)
Constructor.
void convert(const MT_ &a)
Conversion method.
bool hollow() const
Checks whether the matrix has no non-zero entries, i.e. whether it is a null matrix.
SparseMatrixCSCR(const SparseLayout< IT_, layout_id > &layout_in)
Constructor.
Memory::Arbiter & row_idx_arbiter()
Returns a reference to the row-index array arbiter.
void clone(const SparseMatrixCSCR< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Index num_rows_raw() const
Returns the scalar matrix row count.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseLayout< IT_, layout_id > layout() const
Retrieve convenient sparse matrix layout object.
SparseMatrixCSCR(Index rows_in, Index columns_in)
Constructor.
void axpy(const SparseMatrixCSCR &x, const DT_ alpha=DT_(1))
Calculate .
Index num_nzes() const
Retrieve non zero element count.
void print(std::ostream &os, bool print_dense) const
Prints this sparse matrix in human-readable format into an output stream.
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
friend std::ostream & operator<<(std::ostream &os, const SparseMatrixCSCR &b)
SparseMatrixCSR streaming operator.
DenseVector< DT_, IT_ > VectorTypeL
Compatible L-vector type.
void copy(const SparseMatrixCSCR &x, bool full=false)
Performs .
const Memory::Arbiter & col_idx_arbiter() const
Returns a reference to the column-index array arbiter.
SparseMatrixCSCR & operator=(SparseMatrixCSCR &&other)
Move operator.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
static String name()
Returns a descriptive string.
void write_out(FileMode mode, std::ostream &file) const
Write out matrix to file.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
Memory::Arbiter & col_idx_arbiter()
Returns a reference to the column-index array arbiter.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
SparseMatrixCSCR(SparseMatrixCSCR &&other)
Move Constructor.
SparseMatrixCSCR(const Adjacency::Graph &graph)
Constructor.
bool empty() const
Checks whether the matrix is empty, i.e. whether it is a 0x0 matrix.
DenseVector< DT_, IT_ > VectorTypeR
Compatible R-vector type.
void convert(const SparseMatrixCSCR< DT2_, IT2_ > &other)
Conversion method.
DT_ norm_frobenius() const
Calculates the Frobenius norm of this matrix.
SparseMatrixCSCR(FileMode mode, std::istream &file)
Constructor.
void scale(const SparseMatrixCSCR &x, const DT_ alpha)
Calculate .
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
const Memory::Arbiter & row_idx_arbiter() const
Returns a reference to the row-index array arbiter.
SparseMatrixCSCR(std::vector< char > input)
Constructor.
SparseMatrixCSCR(const Index rows_in, const Index columns_in, DenseVector< IT_, IT_ > &row_ptr_in, DenseVector< IT_, IT_ > &row_idx_in, DenseVector< IT_, IT_ > &col_idx_in, DenseVector< DT_, IT_ > &val_in)
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
void read_from(FileMode mode, std::istream &file)
Read in matrix from stream.
static constexpr SparseLayoutId layout_id
Our used layout type.
Index num_cols() const
Retrieve matrix column count.
Memory::Arbiter & row_ptr_arbiter()
Returns a reference to the row-pointer array arbiter.
Index num_rows() const
Retrieve matrix row count.
Index num_nonzero_rows() const
Retrieve used matrix non zero row count.
SparseMatrixCSCR clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
Index num_cols_raw() const
Returns the scalar matrix column count.
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
SparseMatrixCSCR(const SparseMatrixCSR< DT_, IT_ > &csr, const VectorMirror< DT_, IT_ > &non_zero_rows)
Constructor.
DT_ ValueType
Value type, meaning the type of each 'block'.
const Memory::Arbiter & val_arbiter() const
Returns a reference to the values array arbiter.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
DT_ max_rel_diff(const SparseMatrixCSCR &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
SparseMatrixCSCR(const MT_ &other)
Constructor.
CSR based sparse matrix.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Handles vector prolongation, restriction and serialization.
Index num_indices() const
Returns the number of indices in the mirror.
Memory arbiter class.
Arbiter attach(std::size_t offset=0u, std::size_t size=full_size) const
Creates a new arbiter that is attached to the same memory block as this arbiter.
Typed memory view class.
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
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
Definition: base.hpp:105
SparseLayoutId
Definition: base.hpp:63
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
std::uint64_t Index
Index data type.