22struct is_integral_constant : std::false_type
25template <std::
integral U, U N>
26struct is_integral_constant<std::integral_constant<U, N>> : std::true_type
34concept BlockSizeArg = std::integral<std::remove_cvref_t<T>>
35 or is_integral_constant<std::remove_cvref_t<T>>::value;
67template <
int BS0,
int BS1,
typename OP,
typename U,
typename V,
typename W,
68 typename X,
typename Y>
69void insert_csr(U&& data,
const V& cols,
const W& row_ptr,
const X& x,
70 const Y& xrows,
const Y& xcols, OP op,
71 [[maybe_unused]]
typename Y::value_type num_rows)
73 const std::size_t nc = xcols.size();
74 assert(x.size() == xrows.size() * xcols.size() * BS0 * BS1);
75 for (std::size_t r = 0; r < xrows.size(); ++r)
79 using T =
typename X::value_type;
80 const T* xr = x.data() + r * nc * BS0 * BS1;
84 throw std::runtime_error(
"Local row out of range");
87 auto cit0 = std::next(cols.begin(), row_ptr[row]);
88 auto cit1 = std::next(cols.begin(), row_ptr[row + 1]);
89 for (std::size_t c = 0; c < nc; ++c)
92 auto it = std::lower_bound(cit0, cit1, xcols[c]);
93 if (it == cit1 or *it != xcols[c])
94 throw std::runtime_error(
"Entry not in sparsity");
96 std::size_t d = std::ranges::distance(cols.begin(), it);
97 std::size_t di = d * BS0 * BS1;
98 std::size_t xi = c * BS1;
99 assert(di < data.size());
100 for (
int i = 0; i < BS0; ++i)
102 for (
int j = 0; j < BS1; ++j)
103 op(data[di + j], xr[xi + j]);
132template <
int BS0,
int BS1,
typename OP,
typename U,
typename V,
typename W,
133 typename X,
typename Y>
135 const Y& xrows,
const Y& xcols, OP op,
136 [[maybe_unused]]
typename Y::value_type num_rows)
138 const std::size_t nc = xcols.size();
139 assert(x.size() == xrows.size() * xcols.size() * BS0 * BS1);
140 for (std::size_t r = 0; r < xrows.size(); ++r)
143 auto row = xrows[r] * BS0;
146 throw std::runtime_error(
"Local row out of range");
149 for (
int i = 0; i < BS0; ++i)
151 using T =
typename X::value_type;
152 const T* xr = x.data() + (r * BS0 + i) * nc * BS1;
155 auto cit0 = std::next(cols.begin(), row_ptr[row + i]);
156 auto cit1 = std::next(cols.begin(), row_ptr[row + i + 1]);
157 for (std::size_t c = 0; c < nc; ++c)
160 auto it = std::lower_bound(cit0, cit1, xcols[c] * BS1);
161 if (it == cit1 or *it != xcols[c] * BS1)
162 throw std::runtime_error(
"Entry not in sparsity");
164 std::size_t d = std::ranges::distance(cols.begin(), it);
165 assert(d < data.size());
166 std::size_t xi = c * BS1;
167 for (
int j = 0; j < BS1; ++j)
168 op(data[d + j], xr[xi + j]);
193template <
typename OP,
typename U,
typename V,
typename W,
typename X,
196 const X& x,
const Y& xrows,
const Y& xcols, OP op,
197 [[maybe_unused]]
typename Y::value_type num_rows,
200 const std::size_t nc = xcols.size();
201 const int nbs = bs0 * bs1;
203 assert(x.size() == xrows.size() * xcols.size());
204 for (std::size_t r = 0; r < xrows.size(); ++r)
207 auto rdiv = std::div(xrows[r], bs0);
208 using T =
typename X::value_type;
209 const T* xr = x.data() + r * nc;
212 if (rdiv.quot >= num_rows)
213 throw std::runtime_error(
"Local row out of range");
216 auto cit0 = std::next(cols.begin(), row_ptr[rdiv.quot]);
217 auto cit1 = std::next(cols.begin(), row_ptr[rdiv.quot + 1]);
218 for (std::size_t c = 0; c < nc; ++c)
221 auto cdiv = std::div(xcols[c], bs1);
222 auto it = std::lower_bound(cit0, cit1, cdiv.quot);
223 if (it == cit1 or *it != cdiv.quot)
224 throw std::runtime_error(
"Entry not in sparsity");
226 std::size_t d = std::ranges::distance(cols.begin(), it);
227 std::size_t di = d * nbs + rdiv.rem * bs1 + cdiv.rem;
228 assert(di < data.size());
260void spmv(std::span<const T> values, std::span<const std::int64_t> row_begin,
261 std::span<const std::int64_t> row_end,
262 std::span<const std::int32_t> indices, std::span<const T> x,
263 std::span<T> y, BlockSizeArg
auto bs0, BlockSizeArg
auto bs1)
265 assert(row_begin.size() == row_end.size());
269 for (
decltype(+bs0) k0 = 0; k0 < bs0; ++k0)
271 for (std::size_t i = 0; i < row_begin.size(); i++)
274 for (std::int64_t j = row_begin[i]; j < row_end[i]; j++)
276 for (
decltype(+bs1) k1 = 0; k1 < bs1; ++k1)
278 vi += values[j * bs0 * bs1 + k0 * bs1 + k1]
279 * x[indices[j] * bs1 + k1];
283 y[i * bs0 + k0] += vi;
320void spmvT(std::span<const T> values, std::span<const std::int64_t> row_begin,
321 std::span<const std::int64_t> row_end,
322 std::span<const std::int32_t> indices, std::span<const T> x,
323 std::span<T> y, BlockSizeArg
auto bs0, BlockSizeArg
auto bs1)
325 assert(row_begin.size() == row_end.size());
328 for (
decltype(+bs0) k0 = 0; k0 < bs0; ++k0)
330 for (std::size_t i = 0; i < row_begin.size(); i++)
332 const T xval = x[i * bs0 + k0];
333 for (std::int64_t j = row_begin[i]; j < row_end[i]; j++)
335 for (
decltype(+bs1) k1 = 0; k1 < bs1; ++k1)
337 y[indices[j] * bs1 + k1]
338 += values[j * bs0 * bs1 + k0 * bs1 + k1] * xval;
Fetch the rows of B that correspond to the ghost columns of A.
Definition matmul.h:36
void insert_csr(U &&data, const V &cols, const W &row_ptr, const X &x, const Y &xrows, const Y &xcols, OP op, typename Y::value_type num_rows)
Incorporate data into a CSR matrix.
Definition matrix_csr_impl.h:69
void spmv(std::span< const T > values, std::span< const std::int64_t > row_begin, std::span< const std::int64_t > row_end, std::span< const std::int32_t > indices, std::span< const T > x, std::span< T > y, BlockSizeArg auto bs0, BlockSizeArg auto bs1)
Sparse matrix-vector product implementation.
Definition matrix_csr_impl.h:260
void insert_blocked_csr(U &&data, const V &cols, const W &row_ptr, const X &x, const Y &xrows, const Y &xcols, OP op, typename Y::value_type num_rows)
Incorporate blocked data with given block sizes into a non-blocked MatrixCSR.
Definition matrix_csr_impl.h:134
void insert_nonblocked_csr(U &&data, const V &cols, const W &row_ptr, const X &x, const Y &xrows, const Y &xcols, OP op, typename Y::value_type num_rows, int bs0, int bs1)
Incorporate non-blocked data into a blocked matrix (data block size=1).
Definition matrix_csr_impl.h:195
void spmvT(std::span< const T > values, std::span< const std::int64_t > row_begin, std::span< const std::int64_t > row_end, std::span< const std::int32_t > indices, std::span< const T > x, std::span< T > y, BlockSizeArg auto bs0, BlockSizeArg auto bs1)
Sparse matrix-vector transpose product implementation.
Definition matrix_csr_impl.h:320
Linear algebra interface.
Definition dolfinx_la.h:7