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/adjacency/graph.hpp>
17#include <kernel/adjacency/permutation.hpp>
18#include <kernel/lafem/forward.hpp>
19#include <kernel/lafem/container.hpp>
20#include <kernel/lafem/dense_vector.hpp>
21#include <kernel/lafem/sparse_layout.hpp>
22#include <kernel/lafem/arch/axpy_dense.hpp>
23#include <kernel/lafem/arch/diagonal_csr.hpp>
24#include <kernel/lafem/arch/lumping_csr.hpp>
25#include <kernel/lafem/arch/matvecmult_csr_block.hpp>
26#include <kernel/lafem/arch/matvecmult_csr_dense.hpp>
27#include <kernel/lafem/arch/norm2_dense.hpp>
28#include <kernel/lafem/arch/row_norm2_csr.hpp>
29#include <kernel/lafem/arch/scale_col_csr.hpp>
30#include <kernel/lafem/arch/scale_dense.hpp>
31#include <kernel/lafem/arch/scale_row_csr.hpp>
60 template <
typename DT_,
typename IT_ = Index>
77 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
81 template <
typename DataType2_,
typename IndexType2_>
176 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
177 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
180 this->
_indices.push_back(idx.attach());
193 template <
typename MT_>
207 SparseMatrixCSR(graph.get_num_nodes_domain(), graph.get_num_nodes_image(), graph.get_num_indices())
217 FEAT_PRAGMA_OMP(parallel
for)
219 prow_ptr[i] = IT_(dom_ptr[i]);
221 FEAT_PRAGMA_OMP(parallel
for)
222 for(
Index i = 0; i < num_nnze; ++i)
223 pcol_idx[i] = IT_(img_idx[i]);
293 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
296 this->deserialize<DT2_, IT2_>(input);
320 this->
move(std::forward<SparseMatrixCSR>(other));
336 t.
clone(*
this, clone_mode);
348 template<
typename DT2_,
typename IT2_>
361 template <
typename DT2_,
typename IT2_>
386 template <
typename DT2_,
typename IT2_>
395 if (other.num_nzes() == 0)
411 const Index nbands(other.num_bands());
412 const Index nrows(other.num_rows());
413 const Index ncols(other.num_cols());
420 while (k < nbands && coffsets[k] + 1 < nrows)
427 for (
Index i(k + 1); i > 0;)
432 for (
Index j(nbands + 1); j > 0;)
453 for (
Index l(start); l < end; ++l)
455 for (
Index a(i); a < j; ++a)
457 val[ue] = cval[a * nrows + l];
458 col_idx[ue] = IT_(l + coffsets[a] + 1 - nrows);
474 template <
typename DT2_,
typename IT2_,
int block_height_,
int block_w
idth_>
483 if (other.num_nzes_raw() == 0)
500 const Index brows = other.num_rows();
503 for (
Index orow(0), ait(0) ; orow < brows ; ++orow)
505 for (
int row(0) ; row < block_height_ ; ++row)
507 for(
Index ocol(brow_ptr[orow]) ; ocol < brow_ptr[orow + 1] ; ++ocol)
509 for (
int col(0) ; col < block_width_ ; ++col, ++ait)
511 val[ait] = bval[ocol](row,col);
512 col_idx[ait] = bcol_idx[ocol] * IT_(block_width_) + IT_(col);
515 row_ptr[orow *
Index(block_height_) +
Index(row) + 1] = IT_(ait);
527 template <
typename MT_>
532 const Index arows(a.num_rows_raw());
533 const Index acols(a.num_cols_raw());
534 const Index anzes(a.num_nzes_raw());
543 for (
Index i(0); i < arows; ++i)
545 row_ptr[i + 1] = row_ptr[i] + IT_(a.row_degree(i));
548 for (
Index i(0); i < arows; ++i)
550 a.get_row_col_indices(i, &col_idx[row_ptr[i]], IT_(0));
551 a.get_row_values(i, &val[row_ptr[i]]);
558 this->
move(std::move(csr));
571 template <
typename MT_>
574 XASSERT(source.num_rows_raw() == this->num_rows());
575 XASSERT(source.num_cols_raw() == this->num_cols());
576 XASSERT(source.num_nzes_raw() == this->num_nzes());
582 for (
Index i(0); i < nrows; ++i)
584 source.get_row_values(i, &val[row_ptr[i]]);
598 template <
typename MT_>
601 XASSERT(target.num_rows_raw() == this->num_rows());
602 XASSERT(target.num_cols_raw() == this->num_cols());
603 XASSERT(target.num_nzes_raw() == this->num_nzes());
609 for (
Index i(0); i < nrows; ++i)
611 target.set_row_values(i, &val[row_ptr[i]]);
630 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
631 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
634 this->
_indices.push_back(idx.attach());
650 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
666 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
680 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
682 bin = std::ifstream::in;
683 std::ifstream file(filename.c_str(), bin);
684 if (! file.is_open())
685 XABORTM(
"Unable to open Matrix file " + filename);
707 std::map<IT_, std::map<IT_, DT_> > entries;
711 std::getline(file, line);
712 const bool general((line.find(
"%%MatrixMarket matrix coordinate real general") != String::npos) ?
true :
false);
713 const bool symmetric((line.find(
"%%MatrixMarket matrix coordinate real symmetric") != String::npos) ?
true :
false);
715 if (
symmetric ==
false && general ==
false)
717 XABORTM(
"Input-file is not a compatible mtx-file");
722 std::getline(file,line);
724 XABORTM(
"Input-file is empty");
726 String::size_type begin(line.find_first_not_of(
" "));
727 if (line.at(begin) !=
'%')
731 String::size_type begin(line.find_first_not_of(
" "));
732 line.erase(0, begin);
733 String::size_type end(line.find_first_of(
" "));
734 String srow(line, 0, end);
738 begin = line.find_first_not_of(
" ");
739 line.erase(0, begin);
740 end = line.find_first_of(
" ");
741 String scol(line, 0, end);
750 std::getline(file, line);
754 String::size_type begin(line.find_first_not_of(
" "));
755 line.erase(0, begin);
756 String::size_type end(line.find_first_of(
" "));
757 String srow(line, 0, end);
758 IT_ row((IT_)atol(srow.c_str()));
762 begin = line.find_first_not_of(
" ");
763 line.erase(0, begin);
764 end = line.find_first_of(
" ");
765 String scol(line, 0, end);
766 IT_ col((IT_)atol(scol.c_str()));
770 begin = line.find_first_not_of(
" ");
771 line.erase(0, begin);
772 end = line.find_first_of(
" ");
773 String sval(line, 0, end);
774 DT_ tval((DT_)atof(sval.c_str()));
776 entries[IT_(row)].insert(std::pair<IT_, DT_>(col, tval));
780 entries[IT_(col)].insert(std::pair<IT_, DT_>(row, tval));
799 for (
auto row : entries)
801 trow_ptr[row_idx] = idx;
802 for (
auto col : row.second)
804 tcol_idx[idx] = col.first;
805 tval[idx] = col.second;
825 XABORTM(
"Filemode not supported!");
841 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
843 bin = std::ofstream::out;
845 char* buff =
nullptr;
851 file.open(filename.c_str(), bin);
853 XABORTM(
"Unable to open Matrix file " + filename);
888 file <<
"%%MatrixMarket matrix coordinate real symmetric\n";
889 std::vector<IT_> rowv;
890 std::vector<IT_> colv;
891 std::vector<DT_> valv;
895 const IT_ end(trow_ptr[row + 1]);
896 for (IT_ i(trow_ptr[row]) ; i < end ; ++i)
898 const IT_ col(tcol_idx[i]);
901 rowv.push_back(IT_(row + 1));
902 colv.push_back(col + 1);
903 valv.push_back(tval[i]);
907 file << this->
num_rows() <<
" " << this->
num_cols() <<
" " << valv.size() <<
"\n";
908 for (
Index i(0) ; i < valv.size() ; ++i)
910 file << rowv.at(i) <<
" " << colv.at(i) <<
" " << valv.at(i) <<
"\n";
915 file <<
"%%MatrixMarket matrix coordinate real general\n";
920 const IT_ end(trow_ptr[row + 1]);
921 for (IT_ i(trow_ptr[row]) ; i < end ; ++i)
923 file << row + 1 <<
" " << tcol_idx[i] + 1 <<
" " << tval[i] <<
"\n";
931 XABORTM(
"Filemode not supported!");
1058 return Memory::TypedView<DT_>();
1065 return Memory::TypedView<DT_>();
1066 return Memory::TypedView<DT_>(this->
_elements.at(0).view(loc, acc));
1072 return Memory::TypedView<IT_>();
1079 return Memory::TypedView<IT_>();
1086 return Memory::TypedView<IT_>();
1093 return Memory::TypedView<IT_>();
1094 return Memory::TypedView<IT_>(this->
_indices.at(0).view(loc, acc));
1100 return Memory::TypedView<IT_>();
1107 return Memory::TypedView<IT_>();
1114 return Memory::TypedView<IT_>();
1121 return Memory::TypedView<IT_>();
1122 return Memory::TypedView<IT_>(this->
_indices.at(1).view(loc, acc));
1141 if (row_ptr[row+1] == row_ptr[row])
1144 Index temp = col_idx[row_ptr[row+1]-1] - col_idx[row_ptr[row]] + 1;
1186 if (row_ptr[row+1] == row_ptr[row])
1189 if (row_ptr[row+1] > 0)
1191 Index temp1 = col_idx[row_ptr[row+1]-1];
1198 Index temp2 = col_idx[row_ptr[row]];
1220 #ifdef FEAT_HAVE_CUDA
1221 Arch::MatVecMultCSRDense::init_workspace_cuda<DT_, IT_>(
1251 return "SparseMatrixCSR";
1262 this->_copy_content(x, full);
1279 XASSERTM(x.
num_cols() == this->num_cols(),
"Matrix columns do not match!");
1280 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Matrix nonzeros do not match!");
1292 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
1304 XASSERTM(x.
num_cols() == this->num_cols(),
"Column count does not match!");
1305 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1317 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
1336 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1349 ASSERTM(row_norms.
size() == this->num_rows(),
"Matrix/Vector dimension mismatch");
1356 Arch::RowNorm2CSR::template exec<DT_, IT_>(row_norms.
elements_arbiter(), this->val_arbiter(),
1357 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows(),
false);
1362 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1373 ASSERTM(row_norms.
size() == this->num_rows(),
"Matrix/Vector dimension mismatch");
1380 Arch::RowNorm2CSR::template exec<DT_, IT_>(row_norms.
elements_arbiter(), this->val_arbiter(),
1381 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows(),
true);
1386 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1400 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes(),
false,
true);
1403 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1419 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes(),
true,
true);
1422 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1438 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes(),
false,
false);
1441 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1457 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes(),
true,
false);
1460 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1476 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1481 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->
val_arbiter(), x.
val_arbiter(), this->num_nzes());
1484 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1519 for (
Index i(0) ; i <= n; ++i)
1521 if (row_ptr_a[i] != row_ptr_b[i])
1528 for (
Index i(0) ; i < nze ; ++i)
1530 if (col_idx_a[i] != col_idx_b[i])
1550 if(txnonzeros <=
Index(0))
1564 for (
Index i(0); i <= txcolumns; ++i)
1566 ptrow_ptr[i] = IT_(0);
1569 for (
Index i(0); i < txnonzeros; ++i)
1571 ++ptrow_ptr[ptxcol_idx[i] + 1];
1574 for (
Index i(1); i < txcolumns; ++i)
1576 ptrow_ptr[i + 1] += ptrow_ptr[i];
1579 for (
Index i(0); i < txrows; ++i)
1581 for (IT_ k(ptxrow_ptr[i]); k < ptxrow_ptr[i+1]; ++k)
1583 const IT_ l(ptxcol_idx[k]);
1584 const IT_ j(ptrow_ptr[l]);
1585 ptval[j] = ptxval[k];
1586 ptcol_idx[j] = IT_(i);
1591 for (
Index i(txcolumns); i > 0; --i)
1593 ptrow_ptr[i] = ptrow_ptr[i - 1];
1620 XASSERTM(x.
num_cols() == this->num_cols(),
"Column count does not match!");
1621 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1622 XASSERTM(s.
size() == this->num_rows(),
"Vector size does not match!");
1629 Arch::ScaleRowCSR::template exec<DT_, IT_>(this->
val_arbiter(), x.
val_arbiter(), this->col_idx_arbiter(),
1630 this->row_ptr_arbiter(), s.
elements_arbiter(), this->num_rows(), this->num_cols(), this->num_nzes());
1635 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
1647 XASSERTM(x.
num_cols() == this->num_cols(),
"Column count does not match!");
1648 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1649 XASSERTM(s.
size() == this->num_cols(),
"Vector size does not match!");
1656 Arch::ScaleColCSR::template exec<DT_, IT_>(this->
val_arbiter(), x.
val_arbiter(), this->col_idx_arbiter(),
1657 this->row_ptr_arbiter(), s.
elements_arbiter(), this->num_rows(), this->num_cols(), this->num_nzes());
1662 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
1675 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
1676 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
1691 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
1692 this->num_rows(), this->num_cols(), this->num_nzes(),
false);
1697 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1710 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
1711 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
1726 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
1727 this->num_rows(), this->num_cols(), this->num_nzes(),
true);
1731 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1744 template<
int block_size_>
1747 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
1748 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
1763 Memory::Arbiter(), this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
1764 this->num_rows(), this->num_cols(), this->num_nzes(),
false);
1769 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1787 const DT_ alpha = DT_(1))
const
1789 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
1790 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
1791 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
1797 else if((this->
hollow()) || (alpha == DT_(0)))
1808 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
1809 this->num_rows(), this->num_cols(), this->num_nzes(),
false);
1814 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1832 const DT_ alpha = DT_(1))
const
1834 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
1835 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
1836 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
1842 else if((this->
hollow()) || (alpha == DT_(0)))
1853 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
1854 this->num_rows(), this->num_cols(), this->num_nzes(),
true);
1858 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1874 template<
int block_size_>
1879 const DT_ alpha = DT_(1))
const
1881 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
1882 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
1883 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
1889 else if((this->
hollow()) || (alpha == DT_(0)))
1900 y.
elements_arbiter(), this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(),
1901 this->num_rows(), this->num_cols(), this->num_nzes(),
false);
1906 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1945 const DT_ alpha = DT_(1),
1946 const bool allow_incomplete =
false)
1969 for(IT_ i(0); i < IT_(this->
num_rows()); ++i)
1972 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
1975 const IT_ k = col_idx_d[ik];
1978 for(IT_ kl(row_ptr_a[k]); kl < row_ptr_a[k+1]; ++kl)
1981 const IT_ l = col_idx_a[kl];
1984 const DT_ omega = alpha * data_d[ik] * data_a[kl];
1990 IT_ ij = row_ptr_x[i];
1991 IT_ lj = row_ptr_b[l];
1992 while(lj < row_ptr_b[l+1])
1994 if(ij >= row_ptr_x[i+1])
1999 if(allow_incomplete)
2002 XABORTM(
"Incomplete output matrix structure");
2004 else if(col_idx_x[ij] == col_idx_b[lj])
2007 data_x[ij] += omega * data_b[lj];
2011 else if(col_idx_x[ij] < col_idx_b[lj])
2022 if(allow_incomplete)
2025 XABORTM(
"Incomplete output matrix structure");
2072 const DT_ alpha = DT_(1),
2073 const bool allow_incomplete =
false)
2094 for(IT_ i(0); i < IT_(this->
num_rows()); ++i)
2097 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
2100 const IT_ k = col_idx_d[ik];
2103 const DT_ omega = alpha * data_d[ik] * data_a[k];
2109 IT_ ij = row_ptr_x[i];
2110 IT_ kj = row_ptr_b[k];
2111 while(kj < row_ptr_b[k+1])
2113 if(ij >= row_ptr_x[i+1])
2118 if(allow_incomplete)
2121 XABORTM(
"Incomplete output matrix structure");
2123 else if(col_idx_x[ij] == col_idx_b[kj])
2126 data_x[ij] += omega * data_b[kj];
2130 else if(col_idx_x[ij] < col_idx_b[kj])
2141 if(allow_incomplete)
2144 XABORTM(
"Incomplete output matrix structure");
2191 const DT_ alpha = DT_(1),
2192 const bool allow_incomplete =
false)
2213 for(IT_ i(0); i < IT_(this->
num_rows()); ++i)
2216 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
2219 const IT_ k = col_idx_d[ik];
2223 for(
int l = 0; l < bs_; ++l)
2224 omega[l] = alpha * data_d[ik](0,l) * data_a[k][l];
2230 IT_ ij = row_ptr_x[i];
2231 IT_ kj = row_ptr_b[k];
2232 while(kj < row_ptr_b[k+1])
2234 if(ij >= row_ptr_x[i+1])
2239 if(allow_incomplete)
2242 XABORTM(
"Incomplete output matrix structure");
2244 else if(col_idx_x[ij] == col_idx_b[kj])
2247 for(
int l = 0; l < bs_; ++l)
2248 data_x[ij] += omega[l] * data_b[kj](l,0);
2252 else if(col_idx_x[ij] < col_idx_b[kj])
2263 if(allow_incomplete)
2266 XABORTM(
"Incomplete output matrix structure");
2307 const DT_ alpha = DT_(1),
2308 const bool allow_incomplete =
false)
2327 for(IT_ i(0); i < IT_(this->
num_rows()); ++i)
2330 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
2333 const IT_ k = col_idx_d[ik];
2336 const DT_ omega = alpha * data_d[ik];
2342 IT_ ij = row_ptr_x[i];
2343 IT_ kj = row_ptr_b[k];
2344 while(kj < row_ptr_b[k+1])
2346 if(ij >= row_ptr_x[i+1])
2351 if(allow_incomplete)
2354 XABORTM(
"Incomplete output matrix structure");
2356 else if(col_idx_x[ij] == col_idx_b[kj])
2359 data_x[ij] += omega * data_b[kj];
2363 else if(col_idx_x[ij] < col_idx_b[kj])
2374 if(allow_incomplete)
2377 XABORTM(
"Incomplete output matrix structure");
2392 Arch::LumpingCSR::template exec<DT_, IT_>(lump.
elements_arbiter(), this->val_arbiter(),
2393 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows());
2423 Arch::DiagonalCSR::template exec<DT_, IT_>(diag.
elements_arbiter(), this->val_arbiter(),
2424 this->col_idx_arbiter(), this->row_ptr_arbiter(), this->num_rows());
2442 if (perm_row.
size() == 0 && perm_col.
size() == 0)
2445 XASSERTM(perm_row.
size() == this->num_rows(),
"Container rows does not match permutation size");
2446 XASSERTM(perm_col.
size() == this->num_cols(),
"Container columns does not match permutation size");
2449 IT_ * temp_row_ptr =
new IT_[
num_rows() + 1];
2450 IT_ * temp_col_idx =
new IT_[
num_nzes()];
2451 DT_ * temp_val =
new DT_[
num_nzes()];
2461 temp_row_ptr[0] = 0;
2464 Index row_size(row_ptr[perm_pos[row] + 1] - row_ptr[perm_pos[row]]);
2467 for (
Index i(new_start), j(row_ptr[perm_pos[row]]) ; i < new_start + row_size ; ++i, ++j)
2469 temp_col_idx[i] = col_idx[j];
2470 temp_val[i] = val[j];
2473 new_start += row_size;
2474 temp_row_ptr[row+1] = (IT_)new_start;
2486 col_idx[i] = (IT_)perm_pos[temp_col_idx[i]];
2489 delete[] temp_row_ptr;
2490 delete[] temp_col_idx;
2498 Index offset(row_ptr[row]);
2499 Index row_size(row_ptr[row+1] - row_ptr[row]);
2500 for (
Index i(1), j ; i < row_size ; ++i)
2502 swap_key = col_idx[i + offset];
2503 swap_val = val[i + offset];
2505 while (j > 0 && col_idx[j - 1 + offset] > swap_key)
2507 col_idx[j + offset] = col_idx[j - 1 + offset];
2508 val[j + offset] = val[j - 1 + offset];
2511 col_idx[j + offset] = swap_key;
2512 val[j + offset] = swap_val;
2531 std::vector<IT_> row_entries(this->
num_rows(), IT_(0));
2534 for (
Index el(row_ptr[row]) ; el < row_ptr[row + 1] ; ++el)
2538 row_entries.at(row) += IT_(1);
2544 for (
auto& n : row_entries)
2561 nrp[row + 1] = nrp[row] + row_entries.at(row);
2565 row_entries.clear();
2575 for (
Index el(row_ptr[row]) ; el < row_ptr[row + 1] ; ++el)
2579 nci[counter] = col_idx[el];
2580 nval[counter] = val[el];
2611 return Index(prow_ptr[row + 1] - prow_ptr[row]);
2628 template<
typename IT2_>
2635 const Index end =
Index(trow_ptr[row + 1] - trow_ptr[row]);
2636 for (
Index i(0); i < end; ++i)
2638 pcol_idx[i] = IT2_(tcol_idx[start + i]) + col_offset;
2654 template<
typename DT2_>
2661 const Index end =
Index(prow_ptr[row + 1] - prow_ptr[row]);
2662 for (
Index i(0); i < end; ++i)
2664 pvals[i] = DT2_(pval[start + i]);
2680 template<
typename DT2_>
2687 const Index end =
Index(prow_ptr[row + 1] - prow_ptr[row]);
2688 for (
Index i(0); i < end; ++i)
2690 pval[start + i] = DT_(pvals[i]);
2705 void print(std::ostream & os,
bool print_dense)
const
2720 for (
Index i(0) ; i < nrows ; ++i)
2724 for(IT_ j = row_ptr[i]; j < row_ptr[i+1]; ++j)
2729 for(; k < col_idx[j]; ++k)
2730 os <<
" " << DT_(0);
2734 os <<
" " << col_idx[j] <<
':';
2738 os <<
" " << val[j];
2745 for(; k < ncols; ++k)
2746 os <<
" " << DT_(0);
2785 _row_ptr(matrix.row_ptr_view_r()),
2786 _col_idx(matrix.col_idx_view_r())
2808 XASSERTM(domain_node < _matrix.
num_rows(),
"Domain node index out of range");
2809 return &_col_idx.
get_r()[_row_ptr[domain_node]];
2815 XASSERTM(domain_node < _matrix.
num_rows(),
"Domain node index out of range");
2816 return &_col_idx.
get_r()[_row_ptr[domain_node + 1u]];
2829 typedef DT_ DataType;
2830 typedef IT_ IndexType;
2834 static constexpr IT_ _deadcode = ~IT_(0);
2836 const Index _num_rows;
2837 const Index _num_cols;
2849 _row_ptr_view(matrix.row_ptr_view_r()),
2850 _col_idx_view(matrix.col_idx_view_r()),
2856 template<
typename LocalMatrix_,
typename RowMapping_,
typename ColMapping_>
2857 void operator()(
const LocalMatrix_& loc_mat,
const RowMapping_& row_map,
2858 const ColMapping_& col_map, DT_ alpha = DT_(1))
2864 for(
int i(0); i < row_map.get_num_local_dofs(); ++i)
2867 const Index ix = row_map.get_index(i);
2870 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2872 _col_ptr_view[_col_idx_view(k)] = k;
2876 for(
int j(0); j < col_map.get_num_local_dofs(); ++j)
2879 const Index jx = col_map.get_index(j);
2882 ASSERTM(_col_ptr_view[jx] != _deadcode,
"invalid column index");
2885 _data_view[_col_ptr_view[jx]] += alpha * loc_mat[i][j];
2892 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2894 _col_ptr_view[_col_idx_view(k)] = _deadcode;
2911 typedef DT_ DataType;
2912 typedef IT_ IndexType;
2916 static constexpr IT_ _deadcode = ~IT_(0);
2918 const Index _num_rows;
2919 const Index _num_cols;
2931 _row_ptr_view(matrix.row_ptr_view_r()),
2932 _col_idx_view(matrix.col_idx_view_r()),
2934 _data_view(matrix.val_view_r())
2938 template<
typename LocalMatrix_,
typename RowMapping_,
typename ColMapping_>
2939 void operator()(LocalMatrix_& loc_mat,
const RowMapping_& row_map,
const ColMapping_& col_map, DT_ alpha = DT_(1))
2945 for(
int i(0); i < row_map.get_num_local_dofs(); ++i)
2948 const Index ix = row_map.get_index(i);
2951 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2953 _col_ptr_view[_col_idx_view(k)] = k;
2957 for(
int j(0); j < col_map.get_num_local_dofs(); ++j)
2960 const Index jx = col_map.get_index(j);
2963 ASSERTM(_col_ptr_view[jx] != _deadcode,
"invalid column index");
2966 loc_mat[i][j] += alpha * _data_view[_col_ptr_view[jx]];
2973 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2975 _col_ptr_view[_col_idx_view(k)] = _deadcode;
2991 extern template class SparseMatrixCSR<float, std::uint32_t>;
2992 extern template class SparseMatrixCSR<double, std::uint32_t>;
2993 extern template class SparseMatrixCSR<float, std::uint64_t>;
2994 extern template class SparseMatrixCSR<double, std::uint64_t>;
#define XABORTM(msg)
Abortion macro definition with custom message.
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Adjacency Graph implementation.
Index * get_domain_ptr()
Returns the domain pointer array.
Index * get_image_idx()
Returns the image node index array.
Index get_num_indices() const
Returns the total number indices.
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.
std::array< std::shared_ptr< void >, to_underlying(BackendIdx::num_backends)> _backend_workspaces
List of backend workspaces.
void format()
Reset all elements of the container to a given value or zero if missing.
void move(Container &&source)
Assignment move operation.
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
virtual void clear()
Free all allocated arrays.
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Blocked Dense data vector class template.
void copy(const DenseVectorBlocked &x, bool full=false)
Performs .
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.
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.
Layout scheme for sparse matrix containers.
const std::vector< Memory::Arbiter > & get_indices() const
Returns a list of all Index arrays.
CSR based blocked sparse matrix.
Index num_rows() const
Retrieve matrix row count.
Index num_rows_raw() const
Returns the scalar matrix row count.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
Index num_cols() const
Retrieve matrix column count.
Index num_cols_raw() const
Returns the scalar matrix column count.
Index num_nzes() const
Retrieve non zero element count.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Adjactor Wrapper for SparseMatrixCSR.
Index get_num_nodes_domain() const
Index get_num_nodes_image() const
ImageIterator image_begin(Index domain_node) const
ImageIterator image_end(Index domain_node) const
const IT_ * ImageIterator
ImageIterator typedef for Adjactor interface implementation.
Gather-Axpy operation for SparseMatrixCSR.
Scatter-Axpy operation for SparseMatrixCSR.
void row_norm2(VectorTypeL &row_norms) const
Computes the 2-norm for every row.
void copy_to(MT_ &target) const
Copies the values of this matrix into the target matrix.
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
const Memory::Arbiter & col_idx_arbiter() const
Returns a reference to the column-index array arbiter.
void add_double_mat_product(const LAFEM::SparseMatrixCSR< DT_, IT_ > &d, const LAFEM::DenseVector< DT_, IT_ > &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.
DT_ norm_frobenius() const
Calculates the Frobenius norm of this matrix.
SparseMatrixCSR & operator=(SparseMatrixCSR &&other)
Move operator.
void bandwidth_row(Index &bandw, Index &bandw_i) const
Retrieve maximum bandwidth among all rows.
void radius_row(Index &radius, Index &radius_i) const
Retrieve maximum bandwidth among all columns.
bool hollow() const
Checks whether the matrix has no non-zero entries, i.e. whether it is a null matrix.
void scale(const SparseMatrixCSR &x, const DT_ alpha)
Calculate .
DenseVector< DT_, IT_ > VectorTypeL
Compatible L-vector type.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void clone(const SparseMatrixCSR< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
void copy(const SparseMatrixCSR &x, bool full=false)
Performs .
DT_ DataType
Our datatype.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
SparseMatrixCSR transpose() const
Calculate .
Index get_row_values(const Index row, DT2_ *const pvals) const
Extracts the values for a given row.
DT_ ValueType
Value type, meaning the type of each 'block'.
SparseMatrixCSR(const Adjacency::Graph &graph)
Constructor.
void add_mat_mat_product(const LAFEM::SparseMatrixCSR< DT_, IT_ > &d, const LAFEM::SparseMatrixCSR< DT_, IT_ > &b, const DT_ alpha=DT_(1), const bool allow_incomplete=false)
Adds a matrix-matrix product onto this matrix.
DT_ max_element() const
Retrieve the maximum value of this matrix.
DT_ max_abs_element() const
Retrieve the absolute maximum value of this matrix.
void add_double_mat_product(const LAFEM::SparseMatrixCSR< DT_, IT_ > &d, const LAFEM::SparseMatrixCSR< DT_, IT_ > &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 permute(const Adjacency::Permutation &perm_row, const Adjacency::Permutation &perm_col)
Permute matrix rows and columns according to the given Permutations.
void convert(const SparseMatrixCSR< DT2_, IT2_ > &other)
Conversion method.
void axpy(const SparseMatrixCSR &x, const DT_ alpha=DT_(1))
Calculate .
SparseMatrixCSR(SparseMatrixCSR &&other)
Move Constructor.
VectorTypeL extract_diag() const
extract main diagonal vector from matrix
void row_norm2sqr(VectorTypeL &row_norms) const
Computes the square of the 2-norm for every row.
void lump_rows(VectorTypeL &lump) const
SparseMatrixCSR(std::vector< char > input)
Constructor.
DT_ max_rel_diff(const SparseMatrixCSR &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
static constexpr bool is_global
this is not a global container
SparseMatrixCSR(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 convert(const MT_ &a)
Conversion method.
void apply(DenseVectorBlocked< DT_, IT_, block_size_ > &r, const DenseVectorBlocked< DT_, IT_, block_size_ > &x, const DenseVectorBlocked< DT_, IT_, block_size_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseMatrixCSR(FileMode mode, std::istream &file)
Constructor.
void apply(DenseVectorBlocked< DT_, IT_, block_size_ > &r, const DenseVectorBlocked< DT_, IT_, block_size_ > &x) const
Calculate .
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
void write_out(FileMode mode, const String &filename, bool symmetric=false) const
Write out matrix to file.
DT_ min_element() const
Retrieve the minimum value of this matrix.
SparseMatrixCSR(Index rows_in, Index columns_in)
Constructor.
const Memory::Arbiter & val_arbiter() const
Returns a reference to the values array arbiter.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
bool empty() const
Checks whether the matrix is empty, i.e. whether it is a 0x0 matrix.
Index set_row_values(const Index row, const DT2_ *const pvals)
Overwrites the values for a given row.
DenseVector< DT_, IT_ > VectorTypeR
Compatible R-vector type.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
SparseMatrixCSR clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
SparseMatrixCSR(Index rows_in, Index columns_in, Index nonzeros_in)
Constructor.
void init_workspace() const
Inits persistent workspace.
Memory::Arbiter & col_idx_arbiter()
Returns a reference to the column-index array arbiter.
Index num_nzes() const
Retrieve non zero element count.
SparseLayout< IT_, layout_id > layout() const
Retrieve convenient sparse matrix layout object.
DT_ min_abs_element() const
Retrieve the absolute minimum value of this matrix.
void print(std::ostream &os, bool print_dense) const
Prints this sparse matrix in human-readable format into an output stream.
void convert(const SparseMatrixBCSR< DT2_, IT2_, block_height_, block_width_ > &other)
Conversion method.
void add_double_mat_product(const LAFEM::SparseMatrixBCSR< DT_, IT_, 1, bs_ > &d, const LAFEM::DenseVectorBlocked< DT_, IT_, bs_ > &a, const LAFEM::SparseMatrixBCSR< DT_, IT_, bs_, 1 > &b, const DT_ alpha=DT_(1), const bool allow_incomplete=false)
Adds a double-matrix product onto this matrix.
void scale_rows(const SparseMatrixCSR &x, const DenseVector< DT_, IT_ > &s)
Calculate .
VectorTypeL lump_rows() const
Returns the lumped rows vector.
static constexpr SparseLayoutId layout_id
Our used layout type.
Index num_cols_raw() const
Returns the scalar matrix column count.
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 read_from(FileMode mode, std::istream &file)
Read in matrix from stream.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
void copy(const MT_ &source)
Copies the values of the input matrix into this matrix.
Index get_row_col_indices(const Index row, IT2_ *const pcol_idx, const IT2_ col_offset) const
Extracts the column indices for a given row.
bool same_layout(const SparseMatrixCSR &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
SparseMatrixCSR(FileMode mode, const String &filename)
Constructor.
void shrink(DT_ eps)
Shrink matrix and drop small values.
void scale_cols(const SparseMatrixCSR &x, const DenseVector< DT_, IT_ > &s)
Calculate .
SparseMatrixCSR()
Constructor.
void write_out(FileMode mode, std::ostream &file, bool symmetric=false) const
Write out matrix to file.
static constexpr bool is_local
this is a local container
const Memory::Arbiter & row_ptr_arbiter() const
Returns a reference to the row-pointer array arbiter.
SparseMatrixCSR(const SparseLayout< IT_, layout_id > &layout_in)
Constructor.
Index num_rows_raw() const
Returns the scalar matrix row count.
void transpose(const SparseMatrixCSR &x)
Calculate .
IT_ IndexType
Our indextype.
friend std::ostream & operator<<(std::ostream &os, const SparseMatrixCSR &b)
SparseMatrixCSR streaming operator.
void convert(const Adjacency::Graph &graph)
Conversion method.
void convert(const SparseMatrixBanded< DT2_, IT2_ > &other)
Conversion method.
Index row_degree(const Index row) const
Returns the number of NNZ-elements of the selected row.
SparseMatrixCSR(const MT_ &other)
Constructor.
static String name()
Retrieve maximum radius among all column.
Memory::Arbiter & row_ptr_arbiter()
Returns a reference to the row-pointer array arbiter.
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.
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.
void release() const
Releases this view.
static void add_flops(Index flops)
Add an amount of flops to the global flop counter.
String class implementation.
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
Tiny Vector class template.
constexpr std::size_t file_out_stream_buffer_size
OutStreamBufferSize.
@ symmetric
matrix with symmetric structure and values
T_ abs(T_ x)
Returns the absolute value.
T_ min(T_ a, T_ b)
Returns the minimum of two values.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
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)
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
@ format_to_one
allocate and format all bits to one
constexpr auto to_underlying(E e) noexcept
Cast enum to underlying type.
std::uint64_t Index
Index data type.