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>
44 template<
typename DT_,
typename IT_,
int size_>
45 struct BCSRVectorHelper
47 static_assert(size_ > 1,
"invalid block size");
48 typedef DenseVectorBlocked<DT_, IT_, size_> VectorType;
51 template<
typename DT_,
typename IT_>
52 struct BCSRVectorHelper<DT_, IT_, 1>
54 typedef DenseVector< DT_, IT_> VectorType;
81 template <
typename DT_,
typename IT_,
int block_height_,
int block_w
idth_>
84 static_assert(block_height_ > 0,
"invalid block size");
85 static_assert(block_width_ > 0,
"invalid block size");
103 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
107 template <
typename DataType2_,
typename IndexType2_>
111 typedef typename Intern::BCSRVectorHelper<DT_, IT_, block_height_>::VectorType
VectorTypeL;
113 typedef typename Intern::BCSRVectorHelper<DT_, IT_, block_width_>::VectorType
VectorTypeR;
115 static constexpr bool is_global =
false;
116 static constexpr bool is_local =
true;
134 template<
bool transposed_,
typename VecR_,
typename VecX_>
135 void _apply(VecR_& r,
const VecX_& x)
const
137 if constexpr(transposed_)
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!");
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!");
154 XASSERTM(r.elements_arbiter() != x.elements_arbiter(),
"Vector x and r must not share the same memory!");
157 FEAT_KERNEL_MARKER_START(
"BCSR-apply");
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());
164 FEAT_KERNEL_MARKER_STOP(
"BCSR-apply");
168 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
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
174 if constexpr(transposed_)
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!");
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!");
195 XASSERTM(r.elements_arbiter() != x.elements_arbiter(),
"Vector x and r must not share the same memory!");
198 FEAT_KERNEL_MARKER_START(
"BCSR-apply");
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());
205 FEAT_KERNEL_MARKER_STOP(
"BCSR-apply");
209 Statistics::add_time_reduction(ts_stop.elapsed(ts_start));
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());
286 this->
_indices.push_back(idx.attach());
300 SparseMatrixBCSR(graph.get_num_nodes_domain(), graph.get_num_nodes_image(), graph.get_num_indices())
309 FEAT_PRAGMA_OMP(parallel
for)
311 prow_ptr[i] = IT_(dom_ptr[i]);
313 FEAT_PRAGMA_OMP(parallel
for)
314 for(
Index i = 0; i < num_nnze; ++i)
315 pcol_idx[i] = IT_(img_idx[i]);
365 XASSERTM(val_in.
size() % (block_height_ * block_width_) == 0,
"input values size is not a multiple of container blocksize!");
386 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
389 this->deserialize<DT2_, IT2_>(input);
413 this->
move(std::forward<SparseMatrixBCSR>(other));
428 t.
clone(*
this, clone_mode);
440 template<
typename DT2_,
typename IT2_>
453 template <
typename DT2_,
typename IT2_>
459 template <
typename DT2_,
typename IT2_>
468 std::vector<IndexType> tmp_row_ptr(n_rows+1u);
469 std::vector<IndexType> tmp_col_ptr;
477 tmp_col_ptr.reserve(other.num_nzes());
480 std::set<IndexType> tmp_uni_col;
483 for(
Index b_row = 0; b_row < n_rows; ++b_row)
487 for(IT2_ idx = crs_row[l_row]; idx < crs_row[l_row+1]; ++idx)
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);});
500 Index elements_size = tmp_col_ptr.size();
511 this->
_indices.push_back(Memory::Arbiter(
sizeof(IT_) * (_rows() + 1)));
514 this->
_indices.push_back(Memory::Arbiter(
sizeof(IT_) * _nzes()));
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());
543 FEAT_PRAGMA_OMP(parallel
for)
544 for(
Index b_row =
Index(0); b_row < n_rows; ++b_row)
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());
596 this->
_indices.push_back(idx.attach());
611 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
627 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
641 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
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);
672 XABORTM(
"Filemode not supported!");
684 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
686 bin = std::ofstream::out;
688 char* buff =
nullptr;
694 file.open(filename.c_str(), bin);
696 XABORTM(
"Unable to open Matrix file " + filename);
725 file <<
"%%MatrixMarket matrix coordinate real general\n";
730 const IT_ end(trow_ptr[row + 1]);
731 for (IT_ i(trow_ptr[row]) ; i < end ; ++i)
733 const auto& block = tval[i];
734 for (
int y(0) ; y < block_height_ ; ++y)
736 for (
int x(0) ; x < block_width_ ; ++x)
738 file << (int(row) * block_height_) + y + 1 <<
" " << (
int(tcol_idx[i]) * block_width_) + x + 1 <<
" " << block[y][x] <<
"\n";
747 XABORTM(
"Filemode not supported!");
821 return this->
num_nzes() *
Index(block_height_ * block_width_);
877 return Memory::TypedView<ValueType>();
884 return Memory::TypedView<ValueType>();
885 return Memory::TypedView<ValueType>(this->
_elements.at(0).view(loc, acc));
891 return Memory::TypedView<DT_>();
898 return Memory::TypedView<DT_>();
905 return Memory::TypedView<DT_>();
912 return Memory::TypedView<DT_>();
913 return Memory::TypedView<DT_>(this->
_elements.at(0).view(loc, acc));
919 return Memory::TypedView<IT_>();
926 return Memory::TypedView<IT_>();
933 return Memory::TypedView<IT_>();
940 return Memory::TypedView<IT_>();
941 return Memory::TypedView<IT_>(this->
_indices.at(0).view(loc, acc));
947 return Memory::TypedView<IT_>();
954 return Memory::TypedView<IT_>();
961 return Memory::TypedView<IT_>();
968 return Memory::TypedView<IT_>();
969 return Memory::TypedView<IT_>(this->
_indices.at(1).view(loc, acc));
985 #ifdef FEAT_HAVE_CUDA
986 Arch::MatVecMultBCSRDense::init_workspace_cuda<DT_, IT_>(
1000 return "SparseMatrixBCSR";
1011 this->_copy_content(x, full);
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!");
1041 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
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!");
1064 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
1081 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1093 XASSERTM(row_norms.size() == this->num_rows(),
"Matrix/Vector dimension mismatch");
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);
1106 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1117 XASSERTM(row_norms.size() == this->num_rows(),
"Matrix/Vector dimension mismatch");
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);
1130 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1144 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes_raw(),
false,
true);
1147 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1163 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes_raw(),
true,
true);
1166 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1182 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes_raw(),
false,
false);
1185 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1201 DT_ result = Arch::MinMaxValueDense::template exec<DT_>(this->
val_arbiter(), this->
num_nzes_raw(),
true,
false);
1204 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1217 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1222 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->
val_arbiter(), x.
val_arbiter(), this->num_nzes_raw());
1225 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1260 for (
Index i(0) ; i <= n; ++i)
1262 if (row_ptr_a[i] != row_ptr_b[i])
1269 for (
Index i(0) ; i < nze ; ++i)
1271 if (col_idx_a[i] != col_idx_b[i])
1324 for (
Index i(0); i < txnonzeros; ++i)
1326 ++ptrow_ptr[ptxcol_idx[i] + 1];
1329 for (
Index i(1); i < txcolumns - 1; ++i)
1331 ptrow_ptr[i + 1] += ptrow_ptr[i];
1334 for (
Index i(0); i < txrows; ++i)
1336 for (IT_ k(ptxrow_ptr[i]); k < ptxrow_ptr[i+1]; ++k)
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);
1346 for (
Index i(txcolumns); i > 0; --i)
1348 ptrow_ptr[i] = ptrow_ptr[i - 1];
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!");
1373 Arch::ScaleRowBCSR::template exec<DT_, IT_>(this->
val_arbiter(), x.
val_arbiter(), this->col_idx_arbiter(),
1379 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
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!");
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_);
1400 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
1413 this->
template _apply<false>(r, x);
1426 this->
template _apply<true>(r, x);
1439 this->
template _apply<false>(r, x);
1452 this->
template _apply<true>(r, x);
1465 this->
template _apply<false>(r, x);
1478 this->
template _apply<true>(r, x);
1491 this->
template _apply<false>(r, x);
1504 this->
template _apply<true>(r, x);
1522 const DT_ alpha = DT_(1))
const
1524 this->
template _apply<false>(r, x, y, alpha);
1542 const DT_ alpha = DT_(1))
const
1544 this->
template _apply<true>(r, x, y, alpha);
1562 const DT_ alpha = DT_(1))
const
1564 this->
template _apply<false>(r, x, y, alpha);
1582 const DT_ alpha = DT_(1))
const
1584 this->
template _apply<true>(r, x, y, alpha);
1602 const DT_ alpha = DT_(1))
const
1604 this->
template _apply<false>(r, x, y, alpha);
1622 const DT_ alpha = DT_(1))
const
1624 this->
template _apply<true>(r, x, y, alpha);
1642 const DT_ alpha = DT_(1))
const
1644 this->
template _apply<false>(r, x, y, alpha);
1662 const DT_ alpha = DT_(1))
const
1664 this->
template _apply<true>(r, x, y, alpha);
1682 const DT_ alpha = DT_(1))
const
1684 this->
template _apply<false>(r, x, y, alpha);
1702 const DT_ alpha = DT_(1))
const
1704 this->
template _apply<true>(r, x, y, alpha);
1743 const DT_ alpha = DT_(1),
1744 const bool allow_incomplete =
false)
1747 XASSERT(block_height_ == block_width_);
1768 for(IT_ i(0); i < IT_(this->
num_rows()); ++i)
1771 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
1774 const IT_ k = col_idx_d[ik];
1777 for(IT_ kl(row_ptr_a[k]); kl < row_ptr_a[k+1]; ++kl)
1780 const IT_ l = col_idx_a[kl];
1791 IT_ ij = row_ptr_x[i];
1792 IT_ lj = row_ptr_b[l];
1793 while(lj < row_ptr_b[l+1])
1795 if(ij >= row_ptr_x[i+1])
1800 if(allow_incomplete)
1803 XABORTM(
"Incomplete output matrix structure");
1805 else if(col_idx_x[ij] == col_idx_b[lj])
1814 else if(col_idx_x[ij] < col_idx_b[lj])
1825 if(allow_incomplete)
1828 XABORTM(
"Incomplete output matrix structure");
1869 const DT_ alpha = DT_(1),
1870 const bool allow_incomplete =
false)
1873 XASSERT(block_height_ == block_width_);
1894 for(IT_ i(0); i < IT_(this->
num_rows()); ++i)
1897 for(IT_ ik(row_ptr_d[i]); ik < row_ptr_d[i+1]; ++ik)
1900 const IT_ k = col_idx_d[ik];
1903 for(IT_ kl(row_ptr_a[k]); kl < row_ptr_a[k+1]; ++kl)
1906 const IT_ l = col_idx_a[kl];
1909 ValueType omega = (alpha * data_d[ik]) * data_a[kl];
1915 IT_ ij = row_ptr_x[i];
1916 IT_ lj = row_ptr_b[l];
1917 while(lj < row_ptr_b[l+1])
1919 if(ij >= row_ptr_x[i+1])
1924 if(allow_incomplete)
1927 XABORTM(
"Incomplete output matrix structure");
1929 else if(col_idx_x[ij] == col_idx_b[lj])
1936 else if(col_idx_x[ij] < col_idx_b[lj])
1947 if(allow_incomplete)
1950 XABORTM(
"Incomplete output matrix structure");
1962 XASSERTM(lump.size() ==
num_rows(),
"lump vector size does not match matrix row count!");
1964 Arch::LumpingBCSR::template exec<DT_, IT_>(lump.elements_arbiter(), this->val_arbiter(),
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);
2011 if (perm_row.
size() == 0 && perm_col.
size() == 0)
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");
2018 IT_ * temp_row_ptr =
new IT_[
num_rows() + 1];
2019 IT_ * temp_col_idx =
new IT_[
num_nzes()];
2031 temp_row_ptr[0] = 0;
2034 Index row_size(row_ptr[perm_pos[row] + 1] - row_ptr[perm_pos[row]]);
2037 for (
Index i(new_start), j(row_ptr[perm_pos[row]]) ; i < new_start + row_size ; ++i, ++j)
2039 temp_col_idx[i] = col_idx[j];
2040 temp_val[i] = val[j];
2043 new_start += row_size;
2044 temp_row_ptr[row+1] = (IT_)new_start;
2056 col_idx[i] = (IT_)perm_pos[temp_col_idx[i]];
2059 delete[] temp_row_ptr;
2060 delete[] temp_col_idx;
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)
2072 swap_key = col_idx[i + offset];
2073 swap_val = val[i + offset];
2075 while (j > 0 && col_idx[j - 1 + offset] > swap_key)
2077 col_idx[j + offset] = col_idx[j - 1 + offset];
2078 val[j + offset] = val[j - 1 + offset];
2081 col_idx[j + offset] = swap_key;
2082 val[j + offset] = swap_val;
2130 const DT_ alpha = DT_(1))
const
2134 static constexpr IT_ bhd = IT_(BHD_);
2157 for(IT_ row(0); row < IT_(v.size()); ++row)
2160 for(IT_ pos_d(row_ptr_d[row]); pos_d < row_ptr_d[row+1]; ++pos_d)
2163 const IT_ col_d(col_idx_d[pos_d]);
2166 typename MatrixD::ValueType omega(0);
2167 for(IT_ i(0); i < bhd; ++i)
2169 for(IT_ j(0); j < bwd; ++j)
2171 omega[int(i)][int(j)] = alpha * data_d[bhd*bwd*pos_d + bwd*i + j] * data_a[bwd*col_d + j];
2177 IT_ pos_b(row_ptr_b[col_d]);
2178 while(pos_b < row_ptr_b[col_d+1])
2180 if(col_idx_b[pos_b] == row)
2183 for(IT_ i(0); i < bhd; ++i)
2185 for(IT_ j(0); j < bh; ++j)
2187 data_v[bhd*row + i] += omega[int(i)][int(j)]*data_b[bh*bw*pos_b + bw*j + i];
2206 VectorTypeR create_vector_r()
const
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_);
2233 template<
typename IT2_>
2234 Index get_row_col_indices(
const Index row, IT2_ *
const pcol_idx,
const IT2_ col_offset)
const
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_);
2241 const Index end =
Index(trow_ptr[trow + 1] - trow_ptr[trow]);
2242 for (
Index i(0); i < end; ++i)
2244 for (
Index ti(0); ti <
Index(block_width_); ++ti)
2246 pcol_idx[i *
Index(block_width_) + ti] = IT2_(tcol_idx[start + i]) * IT2_(block_width_) + IT2_(ti) + col_offset;
2249 return end *
Index(block_width_);
2263 template<
typename DT2_>
2264 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
2266 const Memory::TypedView<IT_> prow_ptr(this->row_ptr_view_r());
2267 const Memory::TypedView<ValueType> pval(this->val_view_r());
2269 const Index trow = row /
Index(block_height_);
2270 const int lrow = int(row %
Index(block_height_));
2273 const Index end =
Index(prow_ptr[trow + 1] - prow_ptr[trow]);
2274 for (
Index i(0); i < end; ++i)
2276 for (
int ti(0); ti < block_width_; ++ti)
2278 pvals[i *
Index(block_width_) +
Index(ti)] = DT2_(pval[start + i](lrow, ti));
2281 return end *
Index(block_width_);
2295 template<
typename DT2_>
2296 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
2298 const Memory::TypedView<IT_> prow_ptr(this->row_ptr_view_r());
2299 Memory::TypedView<ValueType> pval(this->val_view_rw());
2301 const Index trow = row /
Index(block_height_);
2302 const int lrow = int(row %
Index(block_height_));
2305 const Index end =
Index(prow_ptr[trow + 1] - prow_ptr[trow]);
2306 for (
Index i(0); i < end; ++i)
2308 for (
int ti(0); ti < block_width_; ++ti)
2310 pval[start + i](lrow, ti) = DT_(pvals[i *
Index(block_width_) +
Index(ti)]);
2313 return end *
Index(block_width_);
2327 void print(std::ostream & os,
bool print_dense)
const
2342 for (
Index i = 0 ; i < nrows ; ++i)
2348 for(IT_ j = row_ptr[i]; j < row_ptr[i+1]; ++j)
2353 for(; k < col_idx[j]; ++k)
2356 os <<
" " << DT_(0);
2361 os <<
" " << col_idx[j] <<
":[";
2366 os <<
" " << val[j][bi][bj];
2376 for(; k < ncols; ++k)
2379 os <<
" " << DT_(0);
2421 _row_ptr(matrix.row_ptr_view_r()),
2422 _col_idx(matrix.col_idx_view_r())
2444 XASSERTM(domain_node < _matrix.
num_rows(),
"Domain node index out of range");
2445 return &_col_idx.
get_r()[_row_ptr[domain_node]];
2451 XASSERTM(domain_node < _matrix.
num_rows(),
"Domain node index out of range");
2452 return &_col_idx.
get_r()[_row_ptr[domain_node + 1u]];
2456 Adjactor adjactor()
const
2458 return Adjactor(*
this);
2478 static constexpr IT_ _deadcode = ~IT_(0);
2493 _row_ptr_view(matrix.row_ptr_view_r()),
2494 _col_idx_view(matrix.col_idx_view_r()),
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))
2505 for(
int i(0); i < row_map.get_num_local_dofs(); ++i)
2508 const Index ix = row_map.get_index(i);
2511 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2513 _col_ptr_view[_col_idx_view(k)] = k;
2517 for(
int j(0); j < col_map.get_num_local_dofs(); ++j)
2520 const Index jx = col_map.get_index(j);
2523 ASSERTM(_col_ptr_view[jx] != _deadcode,
"invalid column index");
2526 _data_view[_col_ptr_view[jx]] += alpha * loc_mat[i][j];
2533 for(IT_ k(_row_ptr_view(ix)); k < _row_ptr_view(ix + 1); ++k)
2535 _col_ptr_view[_col_idx_view(k)] = _deadcode;
#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.
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.
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.
Index get_num_nodes_image() const
ImageIterator image_begin(Index domain_node) const
const IT_ * ImageIterator
ImageIterator typedef for Adjactor interface implementation.
Index get_num_nodes_domain() const
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.
IT_ IndexType
Our indextype.
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.
DT_ DataType
Our datatype.
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.
SparseMatrixBCSR()
Constructor.
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.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
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.
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 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.
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
CUDA_HOST_DEVICE void axpy(T_ &y, const T_ &x, const T_ &alpha)
Performs an AXPY of two scalars.
constexpr auto to_underlying(E e) noexcept
Cast enum to underlying type.
std::uint64_t Index
Index data type.