FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
sparse_matrix_bcsr.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/memory_arbiter.hpp>
14#include <kernel/util/statistics.hpp>
15#include <kernel/util/time_stamp.hpp>
16#include <kernel/util/tiny_algebra.hpp>
17#include <kernel/adjacency/graph.hpp>
18#include <kernel/lafem/forward.hpp>
19#include <kernel/lafem/container.hpp>
20#include <kernel/lafem/dense_vector.hpp>
21#include <kernel/lafem/dense_vector_blocked.hpp>
22#include <kernel/lafem/sparse_layout.hpp>
23#include <kernel/lafem/arch/axpy_dense.hpp>
24#include <kernel/lafem/arch/diagonal_bcsr.hpp>
25#include <kernel/lafem/arch/lumping_bcsr.hpp>
26#include <kernel/lafem/arch/matvecmult_bcsr_dense.hpp>
27#include <kernel/lafem/arch/norm2_dense.hpp>
28#include <kernel/lafem/arch/row_norm2_bcsr.hpp>
29#include <kernel/lafem/arch/scale_col_bcsr.hpp>
30#include <kernel/lafem/arch/scale_dense.hpp>
31#include <kernel/lafem/arch/scale_row_bcsr.hpp>
32
33// includes, system
34#include <fstream>
35#include <set>
36
37namespace FEAT
38{
39 namespace LAFEM
40 {
42 namespace Intern
43 {
44 template<typename DT_, typename IT_, int size_>
45 struct BCSRVectorHelper
46 {
47 static_assert(size_ > 1, "invalid block size");
48 typedef DenseVectorBlocked<DT_, IT_, size_> VectorType;
49 };
50
51 template<typename DT_, typename IT_>
52 struct BCSRVectorHelper<DT_, IT_, 1>
53 {
54 typedef DenseVector< DT_, IT_> VectorType;
55 };
56 } // namespace Intern
58
81 template <typename DT_, typename IT_, int block_height_, int block_width_>
82 class SparseMatrixBCSR : public Container<DT_, IT_>
83 {
84 static_assert(block_height_ > 0, "invalid block size");
85 static_assert(block_width_ > 0, "invalid block size");
86
87 public:
89 typedef DT_ DataType;
91 typedef IT_ IndexType;
93 static constexpr int block_height = block_height_;
95 static constexpr int block_width = block_width_;
98
101
103 template <typename DT2_ = DT_, typename IT2_ = IT_>
105
107 template <typename DataType2_, typename IndexType2_>
109
111 typedef typename Intern::BCSRVectorHelper<DT_, IT_, block_height_>::VectorType VectorTypeL;
113 typedef typename Intern::BCSRVectorHelper<DT_, IT_, block_width_>::VectorType VectorTypeR;
114
115 static constexpr bool is_global = false;
116 static constexpr bool is_local = true;
117
118 protected:
119 Index & _rows()
120 {
121 return this->_scalar_index.at(0);
122 }
123
124 Index & _cols()
125 {
126 return this->_scalar_index.at(1);
127 }
128
129 Index & _nzes()
130 {
131 return this->_scalar_index.at(2);
132 }
133
134 template<bool transposed_, typename VecR_, typename VecX_>
135 void _apply(VecR_& r, const VecX_& x) const
136 {
137 if constexpr(transposed_)
138 {
139 XASSERTM(r.size_raw() == this->num_cols_raw(), "Vector size of r does not match!");
140 XASSERTM(x.size_raw() == this->num_rows_raw(), "Vector size of x does not match!");
141 }
142 else
143 {
144 XASSERTM(r.size_raw() == this->num_rows_raw(), "Vector size of r does not match!");
145 XASSERTM(x.size_raw() == this->num_cols_raw(), "Vector size of x does not match!");
146 }
147
148 if (this->num_nzes() == Index(0))
149 {
150 r.format();
151 return;
152 }
153
154 XASSERTM(r.elements_arbiter() != x.elements_arbiter(), "Vector x and r must not share the same memory!");
155
156 TimeStamp ts_start;
157 FEAT_KERNEL_MARKER_START("BCSR-apply");
158
159 Arch::MatVecMultBCSRDense::template exec<DT_, IT_, block_height_, block_width_, transposed_>(
160 this->_backend_workspaces, r.elements_arbiter(),DT_(1), x.elements_arbiter(), Memory::Arbiter(),
161 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
162 this->num_rows(), this->num_cols(), this->num_nzes());
163
164 FEAT_KERNEL_MARKER_STOP("BCSR-apply");
165 TimeStamp ts_stop;
166
168 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
169 }
170
171 template<bool transposed_, typename VecR_, typename VecX_, typename VecY_>
172 void _apply(VecR_& r, const VecX_& x, const VecY_& y, const DT_ alpha) const
173 {
174 if constexpr(transposed_)
175 {
176 XASSERTM(r.size_raw() == this->num_cols_raw(), "Vector size of r does not match!");
177 XASSERTM(x.size_raw() == this->num_rows_raw(), "Vector size of x does not match!");
178 XASSERTM(y.size_raw() == this->num_cols_raw(), "Vector size of y does not match!");
179 }
180 else
181 {
182 XASSERTM(r.size_raw() == this->num_rows_raw(), "Vector size of r does not match!");
183 XASSERTM(x.size_raw() == this->num_cols_raw(), "Vector size of x does not match!");
184 XASSERTM(y.size_raw() == this->num_rows_raw(), "Vector size of y does not match!");
185 }
186
187 // we check alpha strictly for equality to 0, because testing if |alpha| < eps may
188 // lead to false triggers if the matrix/vector contents are also < eps
189 if((this->num_nzes() == Index(0)) || (alpha == DT_(0)))
190 {
191 r.copy(y);
192 return;
193 }
194
195 XASSERTM(r.elements_arbiter() != x.elements_arbiter(), "Vector x and r must not share the same memory!");
196
197 TimeStamp ts_start;
198 FEAT_KERNEL_MARKER_START("BCSR-apply");
199
200 Arch::MatVecMultBCSRDense::template exec<DT_, IT_, block_height_, block_width_, transposed_>(
201 this->_backend_workspaces, r.elements_arbiter(),alpha, x.elements_arbiter(), y.elements_arbiter(),
202 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
203 this->num_rows(), this->num_cols(), this->num_nzes());
204
205 FEAT_KERNEL_MARKER_STOP("BCSR-apply");
206 TimeStamp ts_stop;
207
209 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
210 }
211
212 public:
219 {
220 this->_scalar_index.push_back(0);
221 this->_scalar_index.push_back(0);
222 this->_scalar_index.push_back(0);
223 }
224
236 explicit SparseMatrixBCSR(Index rows_in, Index columns_in)
237 {
238 this->_scalar_index.push_back(rows_in);
239 this->_scalar_index.push_back(columns_in);
240 this->_scalar_index.push_back(0);
241 }
242
254 explicit SparseMatrixBCSR(Index rows_in, Index columns_in, Index nonzeros_in)
255 {
256 XASSERT(rows_in != Index(0) && columns_in != Index(0));
257
258 this->_scalar_index.push_back(rows_in);
259 this->_scalar_index.push_back(columns_in);
260 this->_scalar_index.push_back(nonzeros_in);
261
262 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * (_rows() + 1)));
263 this->_indices_size.push_back(_rows() + 1);
264
265 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * _nzes()));
266 this->_indices_size.push_back(_nzes());
267
268 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * num_nzes_raw()));
269 this->_elements_size.push_back(num_nzes_raw());
270 }
271
280 {
281 //this->_indices.assign(layout_in._indices.begin(), layout_in._indices.end());
282 this->_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
283 this->_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
284
285 for(const auto& idx : layout_in.get_indices())
286 this->_indices.push_back(idx.attach());
287
288 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * num_nzes_raw()));
289 this->_elements_size.push_back(num_nzes_raw());
290 }
291
299 explicit SparseMatrixBCSR(const Adjacency::Graph & graph) :
300 SparseMatrixBCSR(graph.get_num_nodes_domain(), graph.get_num_nodes_image(), graph.get_num_indices())
301 {
302 const Index num_nnze = graph.get_num_indices();
303 const Index num_rows = graph.get_num_nodes_domain();
304 const Index * dom_ptr(graph.get_domain_ptr());
305 const Index * img_idx(graph.get_image_idx());
306 Memory::TypedView<IT_> prow_ptr = this->row_ptr_view_w();
307 Memory::TypedView<IT_> pcol_idx = this->col_idx_view_w();
308
309 FEAT_PRAGMA_OMP(parallel for)
310 for(Index i = 0; i <= num_rows; ++i)
311 prow_ptr[i] = IT_(dom_ptr[i]);
312
313 FEAT_PRAGMA_OMP(parallel for)
314 for(Index i = 0; i < num_nnze; ++i)
315 pcol_idx[i] = IT_(img_idx[i]);
316 }
317
326 explicit SparseMatrixBCSR(FileMode mode, const String& filename)
327 {
328 this->read_from(mode, filename);
329 }
330
339 explicit SparseMatrixBCSR(FileMode mode, std::istream& file)
340 {
341 this->read_from(mode, file);
342 }
343
356 explicit SparseMatrixBCSR(const Index rows_in, const Index columns_in,
357 DenseVector<IT_, IT_> & row_ptr_in, DenseVector<IT_, IT_> & col_idx_in, DenseVector<DT_, IT_> & val_in)
358 {
360 XASSERT(row_ptr_in.size() > 0);
361 XASSERT(col_idx_in.size() > 0);
362 XASSERT(col_idx_in.size() * Index(block_height_ * block_width_) == val_in.size());
363
364 XASSERT(rows_in != Index(0) && columns_in != Index(0));
365 XASSERTM(val_in.size() % (block_height_ * block_width_) == 0, "input values size is not a multiple of container blocksize!");
366
367 this->_scalar_index.push_back(rows_in);
368 this->_scalar_index.push_back(columns_in);
369 this->_scalar_index.push_back(val_in.size() / Index(block_height_ * block_width_));
370
371 this->_indices.push_back(row_ptr_in.elements_arbiter().attach());
372 this->_indices_size.push_back(row_ptr_in.size());
373 this->_indices.push_back(col_idx_in.elements_arbiter().attach());
374 this->_indices_size.push_back(col_idx_in.size());
375 this->_elements.push_back(val_in.elements_arbiter().attach());
376 this->_elements_size.push_back(val_in.size());
377 }
378
386 template <typename DT2_ = DT_, typename IT2_ = IT_>
387 explicit SparseMatrixBCSR(std::vector<char> input)
388 {
389 this->deserialize<DT2_, IT2_>(input);
390 }
391
400 Container<DT_, IT_>(std::forward<SparseMatrixBCSR>(other))
401 {
402 }
403
412 {
413 this->move(std::forward<SparseMatrixBCSR>(other));
414 return *this;
415 }
416
426 {
428 t.clone(*this, clone_mode);
429 return t;
430 }
431
440 template<typename DT2_, typename IT2_>
442 {
443 Container<DT_, IT_>::clone(other, clone_mode);
444 }
445
453 template <typename DT2_, typename IT2_>
455 {
456 this->assign(other);
457 }
458
459 template <typename DT2_, typename IT2_>
460 void convert(const SparseMatrixCSR<DT2_, IT2_>& other)
461 {
462 this->clear();
463 // setup internal sizes
464 Index n_rows = Index(other.num_rows()) / Index(block_height);
465 Index n_cols = Index(other.num_cols()) / Index(block_width);
466
467 // temprorary arrays for row and column ptr
468 std::vector<IndexType> tmp_row_ptr(n_rows+1u);
469 std::vector<IndexType> tmp_col_ptr;
470
471 // ptrs of csr matrix
472 const Memory::TypedView<IT2_> crs_row = other.row_ptr_view_r();
473 const Memory::TypedView<IT2_> crs_col = other.col_idx_view_r();
474 const Memory::TypedView<DT2_> crs_val = other.val_view_r();
475
476 // reserve to sensical size, we will have at most used_element blocks
477 tmp_col_ptr.reserve(other.num_nzes());
478
479 // for each block row, gather which blocks should be actually allocated
480 std::set<IndexType> tmp_uni_col;
481
482 tmp_row_ptr[0] = IndexType(0);
483 for(Index b_row = 0; b_row < n_rows; ++b_row)
484 {
485 for(Index l_row = b_row*Index(block_height); l_row < (b_row+1)*Index(block_height); ++l_row)
486 {
487 for(IT2_ idx = crs_row[l_row]; idx < crs_row[l_row+1]; ++idx)
488 {
489 tmp_uni_col.insert(IndexType(crs_col[idx])/IndexType(block_width));
490 }
491 }
492 tmp_row_ptr[b_row+1] = IndexType(tmp_uni_col.size()) + tmp_row_ptr[b_row];
493 std::for_each(tmp_uni_col.begin(), tmp_uni_col.end(), [&](const auto& ele){tmp_col_ptr.push_back(ele);});
494 // std::printf("Brow %i: ", int(b_row));
495 // std::for_each(tmp_uni_col.begin(), tmp_uni_col.end(), [&](const auto& ele){std::cout << ele << ", ";});
496 // std::cout << "\n";
497 tmp_uni_col.clear();
498 }
499
500 Index elements_size = tmp_col_ptr.size();
501 // std::printf("Elements size %i\n", int(elements_size));
502 // std::printf("Tmp col ptr:\n");
503 // std::for_each(tmp_col_ptr.begin(), tmp_col_ptr.end(), [&](const auto& ele){std::cout << ele << ", ";});
504 // std::cout << "\n";
505
506 // allocate actual matrix data
507 this->_scalar_index.push_back(n_rows);
508 this->_scalar_index.push_back(n_cols);
509 this->_scalar_index.push_back(elements_size);
510
511 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * (_rows() + 1)));
512 this->_indices_size.push_back(_rows() + 1);
513
514 this->_indices.push_back(Memory::Arbiter(sizeof(IT_) * _nzes()));
515 this->_indices_size.push_back(_nzes());
516
517 this->_elements.push_back(Memory::Arbiter(sizeof(DT_) * num_nzes_raw()));
518 this->_elements_size.push_back(num_nzes_raw());
519
520 // format data
521 this->format();
522
523 Memory::TypedView<IT_> row_ptr(this->_indices.at(0).view(Memory::Location::main, Memory::Access::write));
524 Memory::TypedView<IT_> col_idx(this->_indices.at(1).view(Memory::Location::main, Memory::Access::write));
525 Memory::TypedView<DT_> val(this->_elements.at(0).view(Memory::Location::main, Memory::Access::write));
526
527 // now, fill up array data
528 std::copy(tmp_row_ptr.begin(), tmp_row_ptr.end(), row_ptr.get_w());
529 std::copy(tmp_col_ptr.begin(), tmp_col_ptr.end(), col_idx.get_w());
530 // std::printf("Row ptr:\n");
531 // std::for_each(row_ptr, row_ptr+n_rows+1, [&](const auto& ele){std::cout << ele << ", ";});
532 // std::cout << "\n";
533 // std::printf("Col ptr:\n");
534 // std::for_each(col, col+elements_size, [&](const auto& ele){std::cout << ele << ", ";});
535 // std::cout << "\n";
536
537 // clear temprorary arrays
538 tmp_row_ptr.clear();
539 tmp_col_ptr.clear();
540
541 // run through crs data and simply write to the correct location, this can be done
542 // in parallel
543 FEAT_PRAGMA_OMP(parallel for)
544 for(Index b_row = Index(0); b_row < n_rows; ++b_row)
545 {
546 for(Index l_row = Index(0); l_row < Index(block_height); ++l_row)
547 {
548 IndexType b_idx = row_ptr[b_row];
549 for(IT2_ idx = crs_row[b_row*Index(block_height) + l_row]; idx < crs_row[b_row*Index(block_height) + l_row + Index(1)]; ++idx)
550 {
551 //advance block row index until we find our current row
552 while(col_idx[b_idx] != IndexType(crs_col[idx]/block_width))
553 {
554 ++b_idx;
555 }
556 ASSERTM(col_idx[b_idx] == IndexType(crs_col[idx]/block_width), "Blocked Column Array does not fit csr columns");
557 // write data
558 val[b_idx*block_height*block_width + block_width*l_row + (crs_col[idx]%block_width)] = DT_(crs_val[idx]);
559 }
560 }
561 }
562 //done
563 }
564
572 void convert(const Adjacency::Graph & graph)
573 {
574 this->move(SparseMatrixBCSR(graph));
575 }
576
585 {
586 this->_elements.clear();
587 this->_indices.clear();
588 this->_elements_size.clear();
589 this->_indices_size.clear();
590 this->_scalar_index.clear();
591
592 this->_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
593 this->_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
594
595 for(const auto& idx : layout_in.get_indices())
596 this->_indices.push_back(idx.attach());
597
598 this->_elements.push_back(Memory::Arbiter(num_nzes_raw() * sizeof(DT_)));
599 this->_elements_size.push_back(num_nzes_raw());
600
601 return *this;
602 }
603
611 template <typename DT2_ = DT_, typename IT2_ = IT_>
612 void deserialize(std::vector<char> input)
613 {
614 this->template _deserialize<DT2_, IT2_>(FileMode::fm_bcsr, input);
615 }
616
627 template <typename DT2_ = DT_, typename IT2_ = IT_>
628 std::vector<char> serialize(const LAFEM::SerialConfig& config = SerialConfig()) const
629 {
630 return this->template _serialize<DT2_, IT2_>(FileMode::fm_bcsr, config);
631 }
632
639 void read_from(FileMode mode, const String& filename)
640 {
641 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
642 if(mode == FileMode::fm_mtx)
643 bin = std::ifstream::in;
644 std::ifstream file(filename.c_str(), bin);
645 if (! file.is_open())
646 XABORTM("Unable to open Matrix file " + filename);
647 read_from(mode, file);
648 file.close();
649 }
656 void read_from(FileMode mode, std::istream& file)
657 {
658 switch(mode)
659 {
661 /*case FileMode::fm_mtx:
662 * read_from_mtx(filename);
663 * break;*/
665 [[fallthrough]];
666
668 this->template _deserialize<double, std::uint64_t>(FileMode::fm_bcsr, file);
669 break;
670
671 default:
672 XABORTM("Filemode not supported!");
673 }
674 }
675
682 void write_out(FileMode mode, const String& filename) const
683 {
684 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
685 if(mode == FileMode::fm_mtx)
686 bin = std::ofstream::out;
687 std::ofstream file;
688 char* buff = nullptr;
689 if(mode == FileMode::fm_mtx)
690 {
691 buff = new char[LAFEM::file_out_stream_buffer_size];
692 file.rdbuf()->pubsetbuf(buff, LAFEM::file_out_stream_buffer_size);
693 }
694 file.open(filename.c_str(), bin);
695 if(! file.is_open())
696 XABORTM("Unable to open Matrix file " + filename);
697 write_out(mode, file);
698 file.close();
699 delete[] buff;
700 }
701
708 void write_out(FileMode mode, std::ostream& file) const
709 {
710 switch(mode)
711 {
713 [[fallthrough]];
714
716 this->template _serialize<double, std::uint64_t>(FileMode::fm_bcsr, file);
717 break;
718
719 case FileMode::fm_mtx:
720 {
721 const Memory::TypedView<IT_> trow_ptr = this->row_ptr_view_r();
722 const Memory::TypedView<IT_> tcol_idx = this->col_idx_view_r();
723 const Memory::TypedView<ValueType> tval = this->val_view_r();
724
725 file << "%%MatrixMarket matrix coordinate real general\n";
726 file << this->num_rows_raw() << " " << this->num_cols_raw() << " " << this->num_nzes_raw() << "\n";
727
728 for (Index row(0) ; row < num_rows() ; ++row)
729 {
730 const IT_ end(trow_ptr[row + 1]);
731 for (IT_ i(trow_ptr[row]) ; i < end ; ++i)
732 {
733 const auto& block = tval[i];
734 for (int y(0) ; y < block_height_ ; ++y)
735 {
736 for (int x(0) ; x < block_width_ ; ++x)
737 {
738 file << (int(row) * block_height_) + y + 1 << " " << (int(tcol_idx[i]) * block_width_) + x + 1 << " " << block[y][x] << "\n";
739 }
740 }
741 }
742 }
743 }
744 break;
745
746 default:
747 XABORTM("Filemode not supported!");
748 }
749 }
750
757 {
759 }
760
762 bool empty() const
763 {
764 return this->_scalar_index.empty() || (this->_scalar_index.at(0) == Index(0));
765 }
766
768 bool hollow() const
769 {
770 return this->_scalar_index.empty() || (this->_scalar_index.at(2) == Index(0));
771 }
772
780 {
781 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(0);
782 }
783
791 {
792 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(1);
793 }
794
802 {
803 return this->_scalar_index.empty() ? Index(0) : this->_scalar_index.at(2);
804 }
805
808 {
809 return this->num_rows() * Index(block_height_);
810 }
811
814 {
815 return this->num_cols() * Index(block_width_);
816 }
817
820 {
821 return this->num_nzes() * Index(block_height_ * block_width_);
822 }
823
826 {
827 return this->_indices.at(0);
828 }
829
832 {
833 return this->_indices.at(0);
834 }
835
838 {
839 return this->_indices.at(1);
840 }
841
844 {
845 return this->_indices.at(1);
846 }
847
850 {
851 return this->_elements.front();
852 }
853
856 {
857 return this->_elements.front();
858 }
859
861 {
862 if(this->_elements.empty())
864 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::read));
865 }
866
868 {
869 if(this->_elements.empty())
871 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::write));
872 }
873
874 Memory::TypedView<ValueType> val_view_rw(Memory::Location loc = Memory::Location::main)
875 {
876 if(this->_elements.empty())
877 return Memory::TypedView<ValueType>();
878 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, Memory::Access::read_write));
879 }
880
881 Memory::TypedView<ValueType> val_view(Memory::Location loc, Memory::Access acc)
882 {
883 if(this->_elements.empty())
884 return Memory::TypedView<ValueType>();
885 return Memory::TypedView<ValueType>(this->_elements.at(0).view(loc, acc));
886 }
887
888 Memory::TypedView<DT_> val_view_raw_r(Memory::Location loc = Memory::Location::main) const
889 {
890 if(this->_elements.empty())
891 return Memory::TypedView<DT_>();
892 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::read));
893 }
894
895 Memory::TypedView<DT_> val_view_raw_w(Memory::Location loc = Memory::Location::main)
896 {
897 if(this->_elements.empty())
898 return Memory::TypedView<DT_>();
899 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::write));
900 }
901
902 Memory::TypedView<DT_> val_view_raw_rw(Memory::Location loc = Memory::Location::main)
903 {
904 if(this->_elements.empty())
905 return Memory::TypedView<DT_>();
906 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, Memory::Access::read_write));
907 }
908
909 Memory::TypedView<DT_> val_view_dt(Memory::Location loc, Memory::Access acc)
910 {
911 if(this->_elements.empty())
912 return Memory::TypedView<DT_>();
913 return Memory::TypedView<DT_>(this->_elements.at(0).view(loc, acc));
914 }
915
916 Memory::TypedView<IT_> row_ptr_view_r(Memory::Location loc = Memory::Location::main) const
917 {
918 if(this->_indices.empty())
919 return Memory::TypedView<IT_>();
920 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read));
921 }
922
923 Memory::TypedView<IT_> row_ptr_view_w(Memory::Location loc = Memory::Location::main)
924 {
925 if(this->_indices.empty())
926 return Memory::TypedView<IT_>();
927 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::write));
928 }
929
930 Memory::TypedView<IT_> row_ptr_view_rw(Memory::Location loc = Memory::Location::main)
931 {
932 if(this->_indices.empty())
933 return Memory::TypedView<IT_>();
934 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, Memory::Access::read_write));
935 }
936
937 Memory::TypedView<IT_> row_ptr_view(Memory::Location loc, Memory::Access acc)
938 {
939 if(this->_indices.empty())
940 return Memory::TypedView<IT_>();
941 return Memory::TypedView<IT_>(this->_indices.at(0).view(loc, acc));
942 }
943
944 Memory::TypedView<IT_> col_idx_view_r(Memory::Location loc = Memory::Location::main) const
945 {
946 if(this->_indices.empty())
947 return Memory::TypedView<IT_>();
948 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, Memory::Access::read));
949 }
950
951 Memory::TypedView<IT_> col_idx_view_w(Memory::Location loc = Memory::Location::main)
952 {
953 if(this->_indices.empty())
954 return Memory::TypedView<IT_>();
955 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, Memory::Access::write));
956 }
957
958 Memory::TypedView<IT_> col_idx_view_rw(Memory::Location loc = Memory::Location::main)
959 {
960 if(this->_indices.empty())
961 return Memory::TypedView<IT_>();
962 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, Memory::Access::read_write));
963 }
964
965 Memory::TypedView<IT_> col_idx_view(Memory::Location loc, Memory::Access acc)
966 {
967 if(this->_indices.empty())
968 return Memory::TypedView<IT_>();
969 return Memory::TypedView<IT_>(this->_indices.at(1).view(loc, acc));
970 }
971
983 void init_workspace() const
984 {
985 #ifdef FEAT_HAVE_CUDA
986 Arch::MatVecMultBCSRDense::init_workspace_cuda<DT_, IT_>(
987 this->_backend_workspaces[to_underlying(BackendIdx::cuda)],
988 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
989 this->num_rows(), this->num_cols(), this->num_nzes(), block_height_, block_width_, false);
990 #endif
991 }
992
998 static String name()
999 {
1000 return "SparseMatrixBCSR";
1001 }
1002
1009 void copy(const SparseMatrixBCSR & x, bool full = false)
1010 {
1011 this->_copy_content(x, full);
1012 }
1013
1016
1025 void axpy(const SparseMatrixBCSR & x, const DT_ alpha = DT_(1))
1026 {
1027 XASSERTM(x.num_rows() == this->num_rows(), "Matrix rows do not match!");
1028 XASSERTM(x.num_cols() == this->num_cols(), "Matrix columns do not match!");
1029 XASSERTM(x.num_nzes() == this->num_nzes(), "Matrix nonzeros do not match!");
1030
1031 if(this->empty())
1032 return;
1033
1034 TimeStamp ts_start;
1035
1036 Arch::AxpyDense::template exec<DT_>(this->val_arbiter(), alpha, x.val_arbiter(), this->num_nzes_raw());
1037
1038 TimeStamp ts_stop;
1039
1041 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
1042 }
1043
1050 void scale(const SparseMatrixBCSR & x, const DT_ alpha)
1051 {
1052 XASSERTM(x.num_rows() == this->num_rows(), "Row count does not match!");
1053 XASSERTM(x.num_cols() == this->num_cols(), "Column count does not match!");
1054 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
1055
1056 if(this->empty())
1057 return;
1058
1059 TimeStamp ts_start;
1060 Arch::ScaleDense::template exec<DT_>(this->val_arbiter(), x.val_arbiter(), alpha, this->num_nzes_raw());
1061 TimeStamp ts_stop;
1062
1064 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
1065 }
1066
1072 DT_ norm_frobenius() const
1073 {
1074 XASSERT(!this->empty());
1075
1076 TimeStamp ts_start;
1077 DT_ result = Arch::Norm2Dense::template exec<DT_>(this->val_arbiter(), this->num_nzes_raw());
1078 TimeStamp ts_stop;
1079
1081 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1082 return result;
1083 }
1084
1091 void row_norm2(VectorTypeL& row_norms) const
1092 {
1093 XASSERTM(row_norms.size() == this->num_rows(), "Matrix/Vector dimension mismatch");
1094
1095 if(this->empty())
1096 return;
1097
1098 TimeStamp ts_start;
1099
1100 Arch::RowNorm2BCSR::template exec<DT_, IT_>(row_norms.elements_arbiter(), this->val_arbiter(),
1101 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows(), block_height_, block_width, false);
1102
1103 TimeStamp ts_stop;
1104
1106 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1107 }
1108
1115 void row_norm2sqr(VectorTypeL& row_norms) const
1116 {
1117 XASSERTM(row_norms.size() == this->num_rows(), "Matrix/Vector dimension mismatch");
1118
1119 if(this->empty())
1120 return;
1121
1122 TimeStamp ts_start;
1123
1124 Arch::RowNorm2BCSR::template exec<DT_, IT_>(row_norms.elements_arbiter(), this->val_arbiter(),
1125 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows(), block_height_, block_width, true);
1126
1127 TimeStamp ts_stop;
1128
1130 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1131 }
1132
1139 {
1140 XASSERT(!this->empty());
1141
1142 TimeStamp ts_start;
1143
1144 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->val_arbiter(), this->num_nzes_raw(), false, true);
1145
1146 TimeStamp ts_stop;
1147 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1148
1149 return result;
1150 }
1151
1158 {
1159 XASSERT(!this->empty());
1160
1161 TimeStamp ts_start;
1162
1163 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->val_arbiter(), this->num_nzes_raw(), true, true);
1164
1165 TimeStamp ts_stop;
1166 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1167
1168 return result;
1169 }
1170
1176 DT_ max_element() const
1177 {
1178 XASSERT(!this->empty());
1179
1180 TimeStamp ts_start;
1181
1182 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->val_arbiter(), this->num_nzes_raw(), false, false);
1183
1184 TimeStamp ts_stop;
1185 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1186
1187 return result;
1188 }
1189
1195 DT_ min_element() const
1196 {
1197 XASSERT(!this->empty());
1198
1199 TimeStamp ts_start;
1200
1201 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->val_arbiter(), this->num_nzes_raw(), true, false);
1202
1203 TimeStamp ts_stop;
1204 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1205
1206 return result;
1207 }
1208
1215 DT_ max_rel_diff(const SparseMatrixBCSR& x) const
1216 {
1217 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
1218 XASSERT(!this->empty());
1219
1220 TimeStamp ts_start;
1221
1222 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->val_arbiter(), x.val_arbiter(), this->num_nzes_raw());
1223
1224 TimeStamp ts_stop;
1225 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
1226
1227 return result;
1228 }
1229
1238 bool same_layout(const SparseMatrixBCSR& x) const
1239 {
1240 if (this->num_rows() != x.num_rows())
1241 return false;
1242 if (this->num_cols() != x.num_cols())
1243 return false;
1244 if (this->num_nzes() != x.num_nzes())
1245 return false;
1246
1247 if(this->num_nzes() == Index(0))
1248 return true;
1249
1250 // check if the arbiters for row_ptr and col_idx are the same
1251 if((this->row_ptr_arbiter() == x.row_ptr_arbiter()) && (this->col_idx_arbiter() == x.col_idx_arbiter()))
1252 return true;
1253
1254 const Memory::TypedView<IT_> row_ptr_a = this->row_ptr_view_r();
1255 const Memory::TypedView<IT_> col_idx_a = this->col_idx_view_r();
1256 const Memory::TypedView<IT_> row_ptr_b = x.row_ptr_view_r();
1257 const Memory::TypedView<IT_> col_idx_b = x.col_idx_view_r();
1258
1259 const Index n = this->num_rows();
1260 for (Index i(0) ; i <= n; ++i)
1261 {
1262 if (row_ptr_a[i] != row_ptr_b[i])
1263 {
1264 return false;
1265 }
1266 }
1267
1268 const Index nze = this->num_nzes();
1269 for (Index i(0) ; i < nze ; ++i)
1270 {
1271 if (col_idx_a[i] != col_idx_b[i])
1272 {
1273 return false;
1274 }
1275 }
1276
1277 return true;
1278 }
1279
1288 {
1290 x_t.transpose(*this);
1291 return x_t;
1292 }
1293
1300 {
1302 if (x.num_nzes() == 0)
1303 {
1304 this->move(SparseMatrixBCSR(x.num_rows(), x.num_cols()));
1305 return;
1306 }
1307
1308 const Index txrows(x.num_rows());
1309 const Index txcolumns(x.num_cols());
1310 const Index txnonzeros(x.num_nzes());
1311
1312 const Memory::TypedView<IT_> ptxcol_idx(x.col_idx_view_r());
1313 const Memory::TypedView<IT_> ptxrow_ptr(x.row_ptr_view_r());
1314 const Memory::TypedView<typename XType::ValueType> ptxval(x.val_view_r());
1315
1316 DenseVector<IT_, IT_> tcol_idx(txnonzeros);
1317 DenseVector<DT_, IT_> tval(txnonzeros * block_height_ * block_width_);
1318 DenseVector<IT_, IT_> trow_ptr(txcolumns + 1, IT_(0));
1319
1320 Memory::TypedView<IT_> ptrow_ptr(trow_ptr.elements_view_rw());
1321 Memory::TypedView<IT_> ptcol_idx(tcol_idx.elements_view_w());
1322 Memory::TypedView<ValueType> ptval(std::forward<Memory::View&&>(tval.elements_view_w()));
1323
1324 for (Index i(0); i < txnonzeros; ++i)
1325 {
1326 ++ptrow_ptr[ptxcol_idx[i] + 1];
1327 }
1328
1329 for (Index i(1); i < txcolumns - 1; ++i)
1330 {
1331 ptrow_ptr[i + 1] += ptrow_ptr[i];
1332 }
1333
1334 for (Index i(0); i < txrows; ++i)
1335 {
1336 for (IT_ k(ptxrow_ptr[i]); k < ptxrow_ptr[i+1]; ++k)
1337 {
1338 const IT_ l(ptxcol_idx[k]);
1339 const IT_ j(ptrow_ptr[l]);
1340 ptval[j].set_transpose(ptxval[k]);
1341 ptcol_idx[j] = IT_(i);
1342 ++ptrow_ptr[l];
1343 }
1344 }
1345
1346 for (Index i(txcolumns); i > 0; --i)
1347 {
1348 ptrow_ptr[i] = ptrow_ptr[i - 1];
1349 }
1350 ptrow_ptr[0] = 0;
1351
1352 this->move(SparseMatrixBCSR<DT_, IT_, block_height_, block_width_> (txcolumns, txrows, trow_ptr, tcol_idx, tval));
1353 }
1354
1362 {
1363 XASSERTM(x.num_rows() == this->num_rows(), "Row count does not match!");
1364 XASSERTM(x.num_cols() == this->num_cols(), "Column count does not match!");
1365 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
1366 XASSERTM(s.size() == this->num_rows(), "Vector size does not match!");
1367
1368 if(this->empty())
1369 return;
1370
1371 TimeStamp ts_start;
1372
1373 Arch::ScaleRowBCSR::template exec<DT_, IT_>(this->val_arbiter(), x.val_arbiter(), this->col_idx_arbiter(),
1374 this->row_ptr_arbiter(), s.elements_arbiter(), this->num_rows(), this->num_cols(), this->num_nzes(), block_height, block_width_);
1375
1376 TimeStamp ts_stop;
1377
1379 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
1380 }
1381
1382 void scale_cols(const SparseMatrixBCSR & x, const DenseVectorBlocked<DT_,IT_, block_width_> & s)
1383 {
1384 XASSERTM(x.num_rows() == this->num_rows(), "Row count does not match!");
1385 XASSERTM(x.num_cols() == this->num_cols(), "Column count does not match!");
1386 XASSERTM(x.num_nzes() == this->num_nzes(), "Nonzero count does not match!");
1387 XASSERTM(s.size() == this->num_cols(), "Vector size does not match!");
1388
1389 if(this->empty())
1390 return;
1391
1392 TimeStamp ts_start;
1393
1394 Arch::ScaleColBCSR::template exec<DT_, IT_>(this->val_arbiter(), x.val_arbiter(), this->col_idx_arbiter(),
1395 this->row_ptr_arbiter(), s.elements_arbiter(), this->num_rows(), this->num_cols(), this->num_nzes(), block_height_, block_width_);
1396
1397 TimeStamp ts_stop;
1398
1400 Statistics::add_time_axpy(ts_stop.elapsed(ts_start));
1401 }
1402
1412 {
1413 this->template _apply<false>(r, x);
1414 }
1415
1425 {
1426 this->template _apply<true>(r, x);
1427 }
1428
1438 {
1439 this->template _apply<false>(r, x);
1440 }
1441
1451 {
1452 this->template _apply<true>(r, x);
1453 }
1454
1464 {
1465 this->template _apply<false>(r, x);
1466 }
1467
1477 {
1478 this->template _apply<true>(r, x);
1479 }
1480
1490 {
1491 this->template _apply<false>(r, x);
1492 }
1493
1503 {
1504 this->template _apply<true>(r, x);
1505 }
1506
1518 void apply(
1520 const DenseVector< DT_, IT_> & x,
1521 const DenseVector< DT_, IT_> & y,
1522 const DT_ alpha = DT_(1)) const
1523 {
1524 this->template _apply<false>(r, x, y, alpha);
1525 }
1526
1540 const DenseVector< DT_, IT_> & x,
1541 const DenseVector< DT_, IT_> & y,
1542 const DT_ alpha = DT_(1)) const
1543 {
1544 this->template _apply<true>(r, x, y, alpha);
1545 }
1546
1558 void apply(
1560 const DenseVector< DT_, IT_> & x,
1562 const DT_ alpha = DT_(1)) const
1563 {
1564 this->template _apply<false>(r, x, y, alpha);
1565 }
1566
1580 const DenseVector< DT_, IT_> & x,
1582 const DT_ alpha = DT_(1)) const
1583 {
1584 this->template _apply<true>(r, x, y, alpha);
1585 }
1586
1598 void apply(
1601 const DenseVector< DT_, IT_> & y,
1602 const DT_ alpha = DT_(1)) const
1603 {
1604 this->template _apply<false>(r, x, y, alpha);
1605 }
1606
1621 const DenseVector< DT_, IT_> & y,
1622 const DT_ alpha = DT_(1)) const
1623 {
1624 this->template _apply<true>(r, x, y, alpha);
1625 }
1626
1638 void apply(
1642 const DT_ alpha = DT_(1)) const
1643 {
1644 this->template _apply<false>(r, x, y, alpha);
1645 }
1646
1662 const DT_ alpha = DT_(1)) const
1663 {
1664 this->template _apply<true>(r, x, y, alpha);
1665 }
1666
1678 void apply(
1681 const DenseVector< DT_, IT_> & y,
1682 const DT_ alpha = DT_(1)) const
1683 {
1684 this->template _apply<false>(r, x, y, alpha);
1685 }
1686
1701 const DenseVector< DT_, IT_> & y,
1702 const DT_ alpha = DT_(1)) const
1703 {
1704 this->template _apply<true>(r, x, y, alpha);
1705 }
1706
1743 const DT_ alpha = DT_(1),
1744 const bool allow_incomplete = false)
1745 {
1746 // validate matrix dimensions
1747 XASSERT(block_height_ == block_width_);
1748 XASSERT(this->num_rows() == d.num_rows());
1749 XASSERT(d.num_cols() == a.num_rows());
1750 XASSERT(a.num_cols() == b.num_rows());
1751 XASSERT(b.num_cols() == this->num_cols());
1752
1753 // fetch matrix arrays:
1754 Memory::TypedView<ValueType> data_x = this->val_view_rw();
1755 const Memory::TypedView<ValueType> data_d = d.val_view_r();
1756 const Memory::TypedView<ValueType> data_a = a.val_view_r();
1757 const Memory::TypedView<ValueType> data_b = b.val_view_r();
1758 const Memory::TypedView<IT_> row_ptr_x = this->row_ptr_view_r();
1759 const Memory::TypedView<IT_> col_idx_x = this->col_idx_view_r();
1760 const Memory::TypedView<IT_> row_ptr_d = d.row_ptr_view_r();
1761 const Memory::TypedView<IT_> col_idx_d = d.col_idx_view_r();
1762 const Memory::TypedView<IT_> row_ptr_a = a.row_ptr_view_r();
1763 const Memory::TypedView<IT_> col_idx_a = a.col_idx_view_r();
1764 const Memory::TypedView<IT_> row_ptr_b = b.row_ptr_view_r();
1765 const Memory::TypedView<IT_> col_idx_b = b.col_idx_view_r();
1766
1767 // loop over all rows of D and X, resp.
1768 for(IT_ i(0); i < IT_(this->num_rows()); ++i)
1769 {
1770 // loop over all non-zeros D_ik in row i of D
1771 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
1772 {
1773 // get column index k
1774 const IT_ k = col_idx_d[ik];
1775
1776 // loop over all non-zeros A_kl in row k of A
1777 for(IT_ kl(row_ptr_a[k]); kl < row_ptr_a[k+1]; ++kl)
1778 {
1779 // get column index l
1780 const IT_ l = col_idx_a[kl];
1781
1782 // pre-compute factor (alpha * D_ik * A_kl)
1783 ValueType omega;
1784 omega.set_mat_mat_mult(data_d[ik], data_a[kl]);
1785 omega *= alpha;
1786
1787 // loop over all non-zeros B_lj in row j of B and
1788 // loop over all non-zeros X_ij in row i of X and
1789 // perform a "sparse axpy" of B_l onto X_i, i.e.:
1790 // X_i. += (alpha * D_ik * A_kl) * B_l.
1791 IT_ ij = row_ptr_x[i];
1792 IT_ lj = row_ptr_b[l];
1793 while(lj < row_ptr_b[l+1])
1794 {
1795 if(ij >= row_ptr_x[i+1])
1796 {
1797 // we have reached the end of row X_i, but there is at least
1798 // one entry in row B_l left, so the pattern of X is incomplete
1799 // We let the caller decide whether this is a valid case or not:
1800 if(allow_incomplete)
1801 break; // continue with next row
1802 else
1803 XABORTM("Incomplete output matrix structure");
1804 }
1805 else if(col_idx_x[ij] == col_idx_b[lj])
1806 {
1807 // okay: B_lj contributes to X_ij
1808 ValueType temp;
1809 temp.set_mat_mat_mult(omega, data_b[lj]);
1810 data_x[ij] += temp;
1811 ++ij;
1812 ++lj;
1813 }
1814 else if(col_idx_x[ij] < col_idx_b[lj])
1815 {
1816 // entry X_ij exists, but B_lj is missing:
1817 // this is a perfectly valid case, so continue with the next non-zero of X_i
1818 ++ij;
1819 }
1820 else //if(col_idx_x[ij] > col_idx_b[lj])
1821 {
1822 // If we come out here, then the sparsity pattern of X is incomplete:
1823 // B_lj is meant to be added onto X_ij, but the entry X_ij is missing
1824 // We let the caller decide whether this is a valid case or not:
1825 if(allow_incomplete)
1826 ++lj;
1827 else
1828 XABORTM("Incomplete output matrix structure");
1829 }
1830 }
1831 }
1832 }
1833 }
1834 }
1835
1869 const DT_ alpha = DT_(1),
1870 const bool allow_incomplete = false)
1871 {
1872 // validate matrix dimensions
1873 XASSERT(block_height_ == block_width_);
1874 XASSERT(this->num_rows() == d.num_rows());
1875 XASSERT(d.num_cols() == a.num_rows());
1876 XASSERT(a.num_cols() == b.num_rows());
1877 XASSERT(b.num_cols() == this->num_cols());
1878
1879 // fetch matrix arrays:
1880 Memory::TypedView<ValueType> data_x = this->val_view_rw();
1881 const Memory::TypedView<DT_> data_d = d.val_view_r();
1882 const Memory::TypedView<ValueType> data_a = a.val_view_r();
1883 const Memory::TypedView<DT_> data_b = b.val_view_r();
1884 const Memory::TypedView<IT_> row_ptr_x = this->row_ptr_view_r();
1885 const Memory::TypedView<IT_> col_idx_x = this->col_idx_view_r();
1886 const Memory::TypedView<IT_> row_ptr_d = d.row_ptr_view_r();
1887 const Memory::TypedView<IT_> col_idx_d = d.col_idx_view_r();
1888 const Memory::TypedView<IT_> row_ptr_a = a.row_ptr_view_r();
1889 const Memory::TypedView<IT_> col_idx_a = a.col_idx_view_r();
1890 const Memory::TypedView<IT_> row_ptr_b = b.row_ptr_view_r();
1891 const Memory::TypedView<IT_> col_idx_b = b.col_idx_view_r();
1892
1893 // loop over all rows of D and X, resp.
1894 for(IT_ i(0); i < IT_(this->num_rows()); ++i)
1895 {
1896 // loop over all non-zeros D_ik in row i of D
1897 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
1898 {
1899 // get column index k
1900 const IT_ k = col_idx_d[ik];
1901
1902 // loop over all non-zeros A_kl in row k of A
1903 for(IT_ kl(row_ptr_a[k]); kl < row_ptr_a[k+1]; ++kl)
1904 {
1905 // get column index l
1906 const IT_ l = col_idx_a[kl];
1907
1908 // pre-compute factor (alpha * D_ik * A_kl)
1909 ValueType omega = (alpha * data_d[ik]) * data_a[kl];
1910
1911 // loop over all non-zeros B_lj in row j of B and
1912 // loop over all non-zeros X_ij in row i of X and
1913 // perform a "sparse axpy" of B_l onto X_i, i.e.:
1914 // X_i. += (alpha * D_ik * A_kl) * B_l.
1915 IT_ ij = row_ptr_x[i];
1916 IT_ lj = row_ptr_b[l];
1917 while(lj < row_ptr_b[l+1])
1918 {
1919 if(ij >= row_ptr_x[i+1])
1920 {
1921 // we have reached the end of row X_i, but there is at least
1922 // one entry in row B_l left, so the pattern of X is incomplete
1923 // We let the caller decide whether this is a valid case or not:
1924 if(allow_incomplete)
1925 break; // continue with next row
1926 else
1927 XABORTM("Incomplete output matrix structure");
1928 }
1929 else if(col_idx_x[ij] == col_idx_b[lj])
1930 {
1931 // okay: B_lj contributes to X_ij
1932 Tiny::axpy(data_x[ij], omega, data_b[lj]);
1933 ++ij;
1934 ++lj;
1935 }
1936 else if(col_idx_x[ij] < col_idx_b[lj])
1937 {
1938 // entry X_ij exists, but B_lj is missing:
1939 // this is a perfectly valid case, so continue with the next non-zero of X_i
1940 ++ij;
1941 }
1942 else //if(col_idx_x[ij] > col_idx_b[lj])
1943 {
1944 // If we come out here, then the sparsity pattern of X is incomplete:
1945 // B_lj is meant to be added onto X_ij, but the entry X_ij is missing
1946 // We let the caller decide whether this is a valid case or not:
1947 if(allow_incomplete)
1948 ++lj;
1949 else
1950 XABORTM("Incomplete output matrix structure");
1951 }
1952 }
1953 }
1954 }
1955 }
1956 }
1958
1960 void lump_rows(VectorTypeL& lump) const
1961 {
1962 XASSERTM(lump.size() == num_rows(), "lump vector size does not match matrix row count!");
1963
1964 Arch::LumpingBCSR::template exec<DT_, IT_>(lump.elements_arbiter(), this->val_arbiter(),
1965 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows(), block_height, block_width);
1966 }
1967
1978 {
1979 VectorTypeL lump = create_vector_l();
1980 lump_rows(lump);
1981 return lump;
1982 }
1983
1990 void extract_diag(VectorTypeL & diag) const
1991 {
1992 Arch::DiagonalBCSR::template exec<DT_, IT_>(diag.elements_arbiter(), this->val_arbiter(),
1993 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows(), block_height);
1994 }
1995
2002 {
2003 VectorTypeL diag = create_vector_l();
2004 extract_diag(diag);
2005 return diag;
2006 }
2007
2010 {
2011 if (perm_row.size() == 0 && perm_col.size() == 0)
2012 return;
2013
2014 XASSERTM(perm_row.size() == this->num_rows(), "Container rows does not match permutation size");
2015 XASSERTM(perm_col.size() == this->num_cols(), "Container columns does not match permutation size");
2016
2017 // http://de.mathworks.com/help/matlab/math/sparse-matrix-operations.html#f6-13070
2018 IT_ * temp_row_ptr = new IT_[num_rows() + 1];
2019 IT_ * temp_col_idx = new IT_[num_nzes()];
2020 ValueType * temp_val = new ValueType[num_nzes()];
2021
2022 Memory::TypedView<IT_> row_ptr = this->row_ptr_view_rw();
2023 Memory::TypedView<IT_> col_idx = this->col_idx_view_rw();
2024 Memory::TypedView<ValueType> val = this->val_view_rw();
2025
2026 Index * perm_pos;
2027 perm_pos = perm_row.get_perm_pos();
2028
2029 //permute rows from source to temp_*
2030 Index new_start(0);
2031 temp_row_ptr[0] = 0;
2032 for (Index row(0) ; row < this->num_rows() ; ++row)
2033 {
2034 Index row_size(row_ptr[perm_pos[row] + 1] - row_ptr[perm_pos[row]]);
2035
2036 //iterate over all elements in single one new and old row
2037 for (Index i(new_start), j(row_ptr[perm_pos[row]]) ; i < new_start + row_size ; ++i, ++j)
2038 {
2039 temp_col_idx[i] = col_idx[j];
2040 temp_val[i] = val[j];
2041 }
2042
2043 new_start += row_size;
2044 temp_row_ptr[row+1] = (IT_)new_start;
2045 }
2046
2047 //use inverse col permutation as lookup table: i -> new location of i
2048 Adjacency::Permutation perm_col_inv = perm_col.inverse();
2049 perm_pos = perm_col_inv.get_perm_pos();
2050
2051 //permute columns from temp_* to source
2052 Memory::memcopy_main(row_ptr.get_w(), temp_row_ptr, (num_rows() + 1) * sizeof(IT_));
2053 Memory::memcopy_main(val.get_w(), temp_val, num_nzes() * sizeof(ValueType));
2054 for (Index i(0) ; i < num_nzes() ; ++i)
2055 {
2056 col_idx[i] = (IT_)perm_pos[temp_col_idx[i]];
2057 }
2058
2059 delete[] temp_row_ptr;
2060 delete[] temp_col_idx;
2061 delete[] temp_val;
2062
2063 //sort columns in every row by column index
2064 IT_ swap_key;
2065 ValueType swap_val;
2066 for (Index row(0) ; row < num_rows() ; ++row)
2067 {
2068 Index offset(row_ptr[row]);
2069 Index row_size(row_ptr[row+1] - row_ptr[row]);
2070 for (Index i(1), j ; i < row_size ; ++i)
2071 {
2072 swap_key = col_idx[i + offset];
2073 swap_val = val[i + offset];
2074 j = i;
2075 while (j > 0 && col_idx[j - 1 + offset] > swap_key)
2076 {
2077 col_idx[j + offset] = col_idx[j - 1 + offset];
2078 val[j + offset] = val[j - 1 + offset];
2079 --j;
2080 }
2081 col_idx[j + offset] = swap_key;
2082 val[j + offset] = swap_val;
2083 }
2084 }
2085 }
2086
2125 template<int BHD_>
2130 const DT_ alpha = DT_(1)) const
2131 {
2133
2134 static constexpr IT_ bhd = IT_(BHD_);
2135 static constexpr IT_ bwd = IT_(block_height);
2136
2137 static constexpr IT_ bh = IT_(block_height);
2138 static constexpr IT_ bw = IT_(block_width);
2139
2140 // Check matrix dimensions
2141 XASSERT(v.size() == d.num_rows());
2142 XASSERT(d.num_cols() == a.size());
2143 XASSERT(a.size() == this->num_rows());
2144
2145 // Fetch matrix arrays
2146 // Use POD here to avoid hassle with 1x1 matrices and stuff
2147 Memory::TypedView<DT_> data_v(v.elements_view_raw_rw());
2148 const Memory::TypedView<DT_> data_d = d.val_view_raw_r();
2149 const Memory::TypedView<DT_> data_a = a.elements_view_raw_r();
2150 const Memory::TypedView<DT_> data_b = this->val_view_raw_r();
2151 const Memory::TypedView<IT_> row_ptr_d = d.row_ptr_view_r();
2152 const Memory::TypedView<IT_> col_idx_d = d.col_idx_view_r();
2153 const Memory::TypedView<IT_> row_ptr_b = this->row_ptr_view_r();
2154 const Memory::TypedView<IT_> col_idx_b = this->col_idx_view_r();
2155
2156 // Loop over all rows i of v
2157 for(IT_ row(0); row < IT_(v.size()); ++row)
2158 {
2159 // For all non-zeros D_ik in row i of D
2160 for(IT_ pos_d(row_ptr_d[row]); pos_d < row_ptr_d[row+1]; ++pos_d)
2161 {
2162 // Get column index k
2163 const IT_ col_d(col_idx_d[pos_d]);
2164
2165 // Pre-compute block factor (alpha * D_ik * A_kk)
2166 typename MatrixD::ValueType omega(0);
2167 for(IT_ i(0); i < bhd; ++i)
2168 {
2169 for(IT_ j(0); j < bwd; ++j)
2170 {
2171 omega[int(i)][int(j)] = alpha * data_d[bhd*bwd*pos_d + bwd*i + j] * data_a[bwd*col_d + j];
2172 }
2173 }
2174
2175 // Loop over all non-zero blocks B_kj in row j of B and perform a "sparse axpy" of B_l onto v_i, i.e.:
2176 // v_i += alpha * (D_ik * A_kk) * B_k.
2177 IT_ pos_b(row_ptr_b[col_d]);
2178 while(pos_b < row_ptr_b[col_d+1])
2179 {
2180 if(col_idx_b[pos_b] == row)
2181 {
2182 // B_kj contributes to v_i here
2183 for(IT_ i(0); i < bhd; ++i)
2184 {
2185 for(IT_ j(0); j < bh; ++j)
2186 {
2187 data_v[bhd*row + i] += omega[int(i)][int(j)]*data_b[bh*bw*pos_b + bw*j + i];
2188 }
2189 }
2190 break;
2191 }
2192 ++pos_b;
2193 }
2194 }
2195 }
2196 }
2197
2199 // Returns a new compatible L-Vector.
2200 VectorTypeL create_vector_l() const
2201 {
2202 return VectorTypeL(this->num_rows());
2203 }
2204
2205 // Returns a new compatible R-Vector.
2206 VectorTypeR create_vector_r() const
2207 {
2208 return VectorTypeR(this->num_cols());
2209 }
2210
2212 Index row_degree(const Index row) const
2213 {
2214 const Index trow = row / Index(block_height_);
2215 const Memory::TypedView<IT_> prow_ptr(this->row_ptr_view_r());
2216 return Index(prow_ptr[trow + 1] - prow_ptr[trow]) * Index(block_width_);
2217 }
2218
2233 template<typename IT2_>
2234 Index get_row_col_indices(const Index row, IT2_ * const pcol_idx, const IT2_ col_offset) const
2235 {
2236 const Memory::TypedView<IT_> trow_ptr(this->row_ptr_view_r());
2237 const Memory::TypedView<IT_> tcol_idx(this->col_idx_view_r());
2238 const Index trow = row / Index(block_height_);
2239
2240 const Index start = Index(trow_ptr[trow]);
2241 const Index end = Index(trow_ptr[trow + 1] - trow_ptr[trow]);
2242 for (Index i(0); i < end; ++i)
2243 {
2244 for (Index ti(0); ti < Index(block_width_); ++ti)
2245 {
2246 pcol_idx[i * Index(block_width_) + ti] = IT2_(tcol_idx[start + i]) * IT2_(block_width_) + IT2_(ti) + col_offset;
2247 }
2248 }
2249 return end * Index(block_width_);
2250 }
2251
2263 template<typename DT2_>
2264 Index get_row_values(const Index row, DT2_ * const pvals) const
2265 {
2266 const Memory::TypedView<IT_> prow_ptr(this->row_ptr_view_r());
2267 const Memory::TypedView<ValueType> pval(this->val_view_r());
2268
2269 const Index trow = row / Index(block_height_);
2270 const int lrow = int(row % Index(block_height_));
2271
2272 const Index start = Index(prow_ptr[trow]);
2273 const Index end = Index(prow_ptr[trow + 1] - prow_ptr[trow]);
2274 for (Index i(0); i < end; ++i)
2275 {
2276 for (int ti(0); ti < block_width_; ++ti)
2277 {
2278 pvals[i * Index(block_width_) + Index(ti)] = DT2_(pval[start + i](lrow, ti));
2279 }
2280 }
2281 return end * Index(block_width_);
2282 }
2283
2295 template<typename DT2_>
2296 Index set_row_values(const Index row, const DT2_ * const pvals)
2297 {
2298 const Memory::TypedView<IT_> prow_ptr(this->row_ptr_view_r());
2299 Memory::TypedView<ValueType> pval(this->val_view_rw());
2300
2301 const Index trow = row / Index(block_height_);
2302 const int lrow = int(row % Index(block_height_));
2303
2304 const Index start = Index(prow_ptr[trow]);
2305 const Index end = Index(prow_ptr[trow + 1] - prow_ptr[trow]);
2306 for (Index i(0); i < end; ++i)
2307 {
2308 for (int ti(0); ti < block_width_; ++ti)
2309 {
2310 pval[start + i](lrow, ti) = DT_(pvals[i * Index(block_width_) + Index(ti)]);
2311 }
2312 }
2313 return end * Index(block_width_);
2314 }
2316
2327 void print(std::ostream & os, bool print_dense) const
2328 {
2329 if(this->empty())
2330 {
2331 os << "[]";
2332 return;
2333 }
2334
2335 const Memory::TypedView<IT_> row_ptr = this->row_ptr_view_r();
2336 const Memory::TypedView<IT_> col_idx = this->col_idx_view_r();
2337 const Memory::TypedView<ValueType> val = this->val_view_r();
2338 const Index nrows = this->num_rows();
2339 const Index ncols = this->num_cols();
2340
2341 os << "[\n";
2342 for (Index i = 0 ; i < nrows ; ++i)
2343 {
2344 for(int bi = 0; bi < block_height; ++bi)
2345 {
2346 os << "[";
2347 IT_ k = 0;
2348 for(IT_ j = row_ptr[i]; j < row_ptr[i+1]; ++j)
2349 {
2350 // write leading zeros
2351 if(print_dense)
2352 {
2353 for(; k < col_idx[j]; ++k)
2354 {
2355 for(int bj = 0; bj < block_width; ++bj)
2356 os << " " << DT_(0);
2357 }
2358 }
2359 else
2360 {
2361 os << " " << col_idx[j] << ":[";
2362 }
2363
2364 // write value
2365 for(int bj = 0; bj < block_width; ++bj)
2366 os << " " << val[j][bi][bj];
2367 ++k;
2368
2369 if(!print_dense)
2370 os << ']';
2371 }
2372
2373 // write trailing zeros
2374 if(print_dense)
2375 {
2376 for(; k < ncols; ++k)
2377 {
2378 for(int bj = 0; bj < block_width; ++bj)
2379 os << " " << DT_(0);
2380 }
2381 }
2382 os << "]\n";
2383 }
2384 }
2385 os << "]";
2386 }
2387
2394 friend std::ostream & operator<< (std::ostream & os, const SparseMatrixBCSR & b)
2395 {
2396 b.print(os, true);
2397 return os;
2398 }
2399
2400
2401 public:
2408 {
2409 public:
2411 typedef const IT_* ImageIterator;
2412
2413 private:
2414 const SparseMatrixBCSR& _matrix;
2415 const Memory::TypedView<IT_> _row_ptr;
2416 const Memory::TypedView<IT_> _col_idx;
2417
2418 public:
2419 explicit Adjactor(const SparseMatrixBCSR& matrix) :
2420 _matrix(matrix),
2421 _row_ptr(matrix.row_ptr_view_r()),
2422 _col_idx(matrix.col_idx_view_r())
2423 {
2424 }
2425
2426 Adjactor(const Adjactor&) = delete;
2427 Adjactor& operator=(const Adjactor&) = delete;
2428
2431 {
2432 return _matrix.num_rows();
2433 }
2434
2437 {
2438 return _matrix.num_cols();
2439 }
2440
2442 inline ImageIterator image_begin(Index domain_node) const
2443 {
2444 XASSERTM(domain_node < _matrix.num_rows(), "Domain node index out of range");
2445 return &_col_idx.get_r()[_row_ptr[domain_node]];
2446 }
2447
2449 inline ImageIterator image_end(Index domain_node) const
2450 {
2451 XASSERTM(domain_node < _matrix.num_rows(), "Domain node index out of range");
2452 return &_col_idx.get_r()[_row_ptr[domain_node + 1u]];
2453 }
2454 }; // class Adjactor
2455
2456 Adjactor adjactor() const
2457 {
2458 return Adjactor(*this);
2459 }
2460
2469 {
2470 public:
2472 typedef DT_ DataType;
2473 typedef IT_ IndexType;
2475
2476 private:
2477#ifdef DEBUG
2478 static constexpr IT_ _deadcode = ~IT_(0);
2479#endif
2480 Index _num_rows;
2481 Index _num_cols;
2482 Memory::Arbiter _col_ptr_arbiter;
2483 Memory::TypedView<IT_> _row_ptr_view;
2484 Memory::TypedView<IT_> _col_idx_view;
2485 Memory::TypedView<IT_> _col_ptr_view;
2487
2488 public:
2489 explicit ScatterAxpy(MatrixType& matrix) :
2490 _num_rows(matrix.num_rows()),
2491 _num_cols(matrix.num_cols()),
2492 _col_ptr_arbiter(_num_cols * sizeof(IT_), Memory::Location::main, Memory::Init::format_to_one),
2493 _row_ptr_view(matrix.row_ptr_view_r()),
2494 _col_idx_view(matrix.col_idx_view_r()),
2495 _col_ptr_view(_col_ptr_arbiter.view(Memory::Location::main, Memory::Access::read_write)),
2497 {
2498 }
2499
2500 template<typename LocalMatrix_, typename RowMapping_, typename ColMapping_>
2501 void operator()(const LocalMatrix_& loc_mat, const RowMapping_& row_map,
2502 const ColMapping_& col_map, DT_ alpha = DT_(1))
2503 {
2504 // loop over all local row entries
2505 for(int i(0); i < row_map.get_num_local_dofs(); ++i)
2506 {
2507 // fetch row index
2508 const Index ix = row_map.get_index(i);
2509
2510 // build column pointer for this row entry contribution
2511 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2512 {
2513 _col_ptr_view[_col_idx_view(k)] = k;
2514 }
2515
2516 // loop over all local column entries
2517 for(int j(0); j < col_map.get_num_local_dofs(); ++j)
2518 {
2519 // fetch column index
2520 const Index jx = col_map.get_index(j);
2521
2522 // ensure that the column pointer is valid for this index
2523 ASSERTM(_col_ptr_view[jx] != _deadcode, "invalid column index");
2524
2525 // incorporate data into global matrix
2526 _data_view[_col_ptr_view[jx]] += alpha * loc_mat[i][j];
2527
2528 // continue with next column entry
2529 }
2530
2531#ifdef DEBUG
2532 // reformat column-pointer array
2533 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2534 {
2535 _col_ptr_view[_col_idx_view(k)] = _deadcode;
2536 }
2537#endif
2538 // continue with next row entry
2539 }
2540 }
2541 }; // class ScatterAxpy
2542 }; // class SparseMatrixBCSR
2543 } // namespace LAFEM
2544} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#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
Index size() const
returns the size of the permutation
Index * get_perm_pos()
returns the permute-position array
Permutation inverse() const
Computes the inverse permutation.
Container base class.
Definition: container.hpp:224
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 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
Blocked Dense data vector class template.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Dense data vector class template.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Index size() const
Returns the size of this vector, i.e. its number of entries.
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.
Adjactor Wrapper for SparseMatrixBCSR.
ImageIterator image_begin(Index domain_node) const
const IT_ * ImageIterator
ImageIterator typedef for Adjactor interface implementation.
ImageIterator image_end(Index domain_node) const
Scatter-Axpy operation for SparseMatrixBCSR.
CSR based blocked sparse matrix.
static constexpr SparseLayoutId layout_id
Our used layout type.
void apply(DenseVectorBlocked< DT_, IT_, block_height_ > &r, const DenseVectorBlocked< DT_, IT_, block_width_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void scale(const SparseMatrixBCSR &x, const DT_ alpha)
Calculate .
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
SparseMatrixBCSR & operator=(SparseMatrixBCSR &&other)
Move operator.
SparseMatrixBCSR(Index rows_in, Index columns_in, Index nonzeros_in)
Constructor.
void apply(DenseVector< DT_, IT_ > &r, const DenseVectorBlocked< DT_, IT_, block_width_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
DT_ min_element() const
Retrieve the minimum value of this matrix.
SparseMatrixBCSR< DT_, IT_, block_width_, block_height_ > transpose() const
Calculate .
Index num_rows() const
Retrieve matrix row count.
static constexpr int block_width
Our block width.
SparseMatrixBCSR(const Index rows_in, const Index columns_in, DenseVector< IT_, IT_ > &row_ptr_in, DenseVector< IT_, IT_ > &col_idx_in, DenseVector< DT_, IT_ > &val_in)
Constructor.
void clone(const SparseMatrixBCSR< DT2_, IT2_, block_height_, block_width_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Index num_rows_raw() const
Returns the scalar matrix row count.
SparseMatrixBCSR(SparseMatrixBCSR &&other)
Move Constructor.
void add_trace_double_mat_mult(typename LAFEM::SparseMatrixBCSR< DT_, IT_, BHD_, block_height >::VectorTypeL &v, const LAFEM::SparseMatrixBCSR< DT_, IT_, BHD_, block_height > &d, const LAFEM::DenseVectorBlocked< DT_, IT_, block_height > &a, const DT_ alpha=DT_(1)) const
Adds the trace of a double matrix multiplication to a vector.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
bool empty() const
Checks whether the matrix is empty, i.e. whether it is a 0x0 matrix.
void row_norm2sqr(VectorTypeL &row_norms) const
Computes the square of the 2-norm for every row.
void apply(DenseVectorBlocked< DT_, IT_, block_height_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVectorBlocked< DT_, IT_, block_height_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void init_workspace() const
Inits persistent workspace.
Tiny::Matrix< DataType, block_height, block_width > ValueType
Value type, meaning the type of each block.
void convert(const SparseMatrixBCSR< DT2_, IT2_, block_height_, block_width_ > &other)
Conversion method.
void scale_rows(const SparseMatrixBCSR &x, const DenseVectorBlocked< DT_, IT_, block_height_ > &s)
Calculate .
void convert(const Adjacency::Graph &graph)
Conversion method.
SparseLayout< IT_, layout_id > layout() const
Retrieve convenient sparse matrix layout object.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
static constexpr int block_height
Our block height.
void copy(const SparseMatrixBCSR &x, bool full=false)
Performs .
SparseMatrixBCSR(const SparseLayout< IT_, layout_id > &layout_in)
Constructor.
void lump_rows(VectorTypeL &lump) const
void apply(DenseVectorBlocked< DT_, IT_, block_height_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVectorBlocked< DT_, IT_, block_height_ > &x) const
Calculate .
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVectorBlocked< DT_, IT_, block_height_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
bool hollow() const
Checks whether the matrix has no non-zero entries, i.e. whether it is a null matrix.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
Index num_nzes() const
Retrieve non zero element count.
Intern::BCSRVectorHelper< DT_, IT_, block_width_ >::VectorType VectorTypeR
Compatible R-vector type.
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.
Intern::BCSRVectorHelper< DT_, IT_, block_height_ >::VectorType VectorTypeL
Compatible L-vector type.
void permute(Adjacency::Permutation &perm_row, Adjacency::Permutation &perm_col)
Permute matrix rows and columns according to the given Permutations.
Memory::Arbiter & row_ptr_arbiter()
Returns a reference to the row-pointer array arbiter.
void apply(DenseVector< DT_, IT_ > &r, const DenseVectorBlocked< DT_, IT_, block_width_ > &x) const
Calculate .
VectorTypeL lump_rows() const
Returns the lumped rows vector.
bool same_layout(const SparseMatrixBCSR &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
DT_ max_abs_element() const
Retrieve the absolute maximum value of this matrix.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
DT_ max_rel_diff(const SparseMatrixBCSR &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
void apply_transposed(DenseVectorBlocked< DT_, IT_, block_width_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
DT_ norm_frobenius() const
Calculates the Frobenius norm of this matrix.
void apply_transposed(DenseVectorBlocked< DT_, IT_, block_width_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVectorBlocked< DT_, IT_, block_width_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void apply(DenseVectorBlocked< DT_, IT_, block_height_ > &r, const DenseVectorBlocked< DT_, IT_, block_width_ > &x) const
Calculate .
SparseMatrixBCSR(FileMode mode, std::istream &file)
Constructor.
void axpy(const SparseMatrixBCSR &x, const DT_ alpha=DT_(1))
Calculate .
void apply_transposed(DenseVectorBlocked< DT_, IT_, block_width_ > &r, const DenseVectorBlocked< DT_, IT_, block_height_ > &x) const
Calculate .
static String name()
Returns a descriptive string.
void write_out(FileMode mode, std::ostream &file) const
Write out matrix to file.
SparseMatrixBCSR(FileMode mode, const String &filename)
Constructor.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
void apply_transposed(DenseVectorBlocked< DT_, IT_, block_width_ > &r, const DenseVectorBlocked< DT_, IT_, block_height_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseMatrixBCSR(std::vector< char > input)
Constructor.
void add_double_mat_product(const LAFEM::SparseMatrixBCSR< DT_, IT_, block_height_, block_width_ > &d, const LAFEM::SparseMatrixBCSR< DT_, IT_, block_height_, block_width_ > &a, const LAFEM::SparseMatrixBCSR< DT_, IT_, block_height_, block_width_ > &b, const DT_ alpha=DT_(1), const bool allow_incomplete=false)
Adds a double-matrix product onto this matrix.
void read_from(FileMode mode, std::istream &file)
Read in matrix from stream.
const Memory::Arbiter & col_idx_arbiter() const
Returns a reference to the column-index array arbiter.
void apply(DenseVectorBlocked< DT_, IT_, block_height_ > &r, const DenseVectorBlocked< DT_, IT_, block_width_ > &x, const DenseVectorBlocked< DT_, IT_, block_height_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
const Memory::Arbiter & row_ptr_arbiter() const
Returns a reference to the row-pointer array arbiter.
SparseMatrixBCSR clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
friend std::ostream & operator<<(std::ostream &os, const SparseMatrixBCSR &b)
SparseMatrixBCSR streaming operator.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseMatrixBCSR(Index rows_in, Index columns_in)
Constructor.
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
Index num_cols() const
Retrieve matrix column count.
DT_ max_element() const
Retrieve the maximum value of this matrix.
Memory::Arbiter & col_idx_arbiter()
Returns a reference to the column-index array arbiter.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this matrix.
void add_double_mat_product(const LAFEM::SparseMatrixCSR< DT_, IT_ > &d, const LAFEM::SparseMatrixBCSR< DT_, IT_, block_height_, block_width_ > &a, const LAFEM::SparseMatrixCSR< DT_, IT_ > &b, const DT_ alpha=DT_(1), const bool allow_incomplete=false)
Adds a double-matrix product onto this matrix.
void transpose(const SparseMatrixBCSR< DT_, IT_, block_width_, block_height_ > &x)
Calculate .
VectorTypeL extract_diag() const
extract main diagonal vector from matrix
const Memory::Arbiter & val_arbiter() const
Returns a reference to the values array arbiter.
void row_norm2(VectorTypeL &row_norms) const
Computes the 2-norm for every row.
Index num_cols_raw() const
Returns the scalar matrix column count.
void apply_transposed(DenseVectorBlocked< DT_, IT_, block_width_ > &r, const DenseVectorBlocked< DT_, IT_, block_height_ > &x, const DenseVectorBlocked< DT_, IT_, block_width_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseMatrixBCSR(const Adjacency::Graph &graph)
Constructor.
CSR based sparse matrix.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
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.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
Typed memory view class.
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.
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 Matrix class template.
CUDA_HOST_DEVICE Matrix & set_mat_mat_mult(const Matrix< T_, m_, la_, sma_, sna_ > &a, const Matrix< T_, lb_, n_, smb_, snb_ > &b)
Sets this matrix to the algebraic matrix-product of two other matrices.
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
Definition: base.hpp:105
SparseLayoutId
Definition: base.hpp:63
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.
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.
@ format_to_one
allocate and format all bits to one
CUDA_HOST_DEVICE void axpy(T_ &y, const T_ &x, const T_ &alpha)
Performs an AXPY of two scalars.
FEAT namespace.
Definition: adjactor.hpp:12
constexpr auto to_underlying(E e) noexcept
Cast enum to underlying type.
std::uint64_t Index
Index data type.