23struct is_integral_constant : std::false_type
26template <std::
integral U, U N>
27struct is_integral_constant<std::integral_constant<U, N>> : std::true_type
35concept BlockSizeArg = std::integral<std::remove_cvref_t<T>>
36 or is_integral_constant<std::remove_cvref_t<T>>::value;
68template <
int BS0,
int BS1,
typename OP,
typename U,
typename V,
typename W,
69 typename X,
typename Y>
70void insert_csr(U&& data,
const V& cols,
const W& row_ptr,
const X& x,
71 const Y& xrows,
const Y& xcols, OP op,
72 [[maybe_unused]]
typename Y::value_type num_rows)
74 const std::size_t nc = xcols.size();
75 assert(x.size() == xrows.size() * xcols.size() * BS0 * BS1);
76 for (std::size_t r = 0; r < xrows.size(); ++r)
80 using T =
typename X::value_type;
81 const T* xr = x.data() + r * nc * BS0 * BS1;
85 throw std::runtime_error(
"Local row out of range");
88 auto cit0 = std::next(cols.begin(), row_ptr[row]);
89 auto cit1 = std::next(cols.begin(), row_ptr[row + 1]);
90 for (std::size_t c = 0; c < nc; ++c)
93 auto it = std::lower_bound(cit0, cit1, xcols[c]);
94 if (it == cit1 or *it != xcols[c])
95 throw std::runtime_error(
"Entry not in sparsity");
97 std::size_t d = std::ranges::distance(cols.begin(), it);
98 std::size_t di = d * BS0 * BS1;
99 std::size_t xi = c * BS1;
100 assert(di < data.size());
101 for (
int i = 0; i < BS0; ++i)
103 for (
int j = 0; j < BS1; ++j)
104 op(data[di + j], xr[xi + j]);
133template <
int BS0,
int BS1,
typename OP,
typename U,
typename V,
typename W,
134 typename X,
typename Y>
136 const Y& xrows,
const Y& xcols, OP op,
137 [[maybe_unused]]
typename Y::value_type num_rows)
139 const std::size_t nc = xcols.size();
140 assert(x.size() == xrows.size() * xcols.size() * BS0 * BS1);
141 for (std::size_t r = 0; r < xrows.size(); ++r)
144 auto row = xrows[r] * BS0;
147 throw std::runtime_error(
"Local row out of range");
150 for (
int i = 0; i < BS0; ++i)
152 using T =
typename X::value_type;
153 const T* xr = x.data() + (r * BS0 + i) * nc * BS1;
156 auto cit0 = std::next(cols.begin(), row_ptr[row + i]);
157 auto cit1 = std::next(cols.begin(), row_ptr[row + i + 1]);
158 for (std::size_t c = 0; c < nc; ++c)
161 auto it = std::lower_bound(cit0, cit1, xcols[c] * BS1);
162 if (it == cit1 or *it != xcols[c] * BS1)
163 throw std::runtime_error(
"Entry not in sparsity");
165 std::size_t d = std::ranges::distance(cols.begin(), it);
166 assert(d < data.size());
167 std::size_t xi = c * BS1;
168 for (
int j = 0; j < BS1; ++j)
169 op(data[d + j], xr[xi + j]);
194template <
typename OP,
typename U,
typename V,
typename W,
typename X,
197 const X& x,
const Y& xrows,
const Y& xcols, OP op,
198 [[maybe_unused]]
typename Y::value_type num_rows,
201 const std::size_t nc = xcols.size();
202 const int nbs = bs0 * bs1;
204 assert(x.size() == xrows.size() * xcols.size());
205 for (std::size_t r = 0; r < xrows.size(); ++r)
208 auto rdiv = std::div(xrows[r], bs0);
209 using T =
typename X::value_type;
210 const T* xr = x.data() + r * nc;
213 if (rdiv.quot >= num_rows)
214 throw std::runtime_error(
"Local row out of range");
217 auto cit0 = std::next(cols.begin(), row_ptr[rdiv.quot]);
218 auto cit1 = std::next(cols.begin(), row_ptr[rdiv.quot + 1]);
219 for (std::size_t c = 0; c < nc; ++c)
222 auto cdiv = std::div(xcols[c], bs1);
223 auto it = std::lower_bound(cit0, cit1, cdiv.quot);
224 if (it == cit1 or *it != cdiv.quot)
225 throw std::runtime_error(
"Entry not in sparsity");
227 std::size_t d = std::ranges::distance(cols.begin(), it);
228 std::size_t di = d * nbs + rdiv.rem * bs1 + cdiv.rem;
229 assert(di < data.size());
261void spmv(std::span<const T> values, std::span<const std::int64_t> row_begin,
262 std::span<const std::int64_t> row_end,
263 std::span<const std::int32_t> indices, std::span<const T> x,
264 std::span<T> y, BlockSizeArg
auto bs0, BlockSizeArg
auto bs1)
266 assert(row_begin.size() == row_end.size());
270 for (
decltype(+bs0) k0 = 0; k0 < bs0; ++k0)
272 for (std::size_t i = 0; i < row_begin.size(); i++)
275 for (std::int64_t j = row_begin[i]; j < row_end[i]; j++)
277 for (
decltype(+bs1) k1 = 0; k1 < bs1; ++k1)
279 vi += values[j * bs0 * bs1 + k0 * bs1 + k1]
280 * x[indices[j] * bs1 + k1];
284 y[i * bs0 + k0] += vi;
321void spmvT(std::span<const T> values, std::span<const std::int64_t> row_begin,
322 std::span<const std::int64_t> row_end,
323 std::span<const std::int32_t> indices, std::span<const T> x,
324 std::span<T> y, BlockSizeArg
auto bs0, BlockSizeArg
auto bs1)
326 assert(row_begin.size() == row_end.size());
329 for (
decltype(+bs0) k0 = 0; k0 < bs0; ++k0)
331 for (std::size_t i = 0; i < row_begin.size(); i++)
333 const T xval = x[i * bs0 + k0];
334 for (std::int64_t j = row_begin[i]; j < row_end[i]; j++)
336 for (
decltype(+bs1) k1 = 0; k1 < bs1; ++k1)
338 y[indices[j] * bs1 + k1]
339 += 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:70
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:261
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:135
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:196
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:321
Linear algebra interface.
Definition dolfinx_la.h:7