DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
matrix_csr_impl.h
1// Copyright (C) 2021-2023 Garth N. Wells and Chris N. Richardson
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include <concepts>
10#include <numeric>
11#include <span>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
16namespace dolfinx::la
17{
18namespace impl
19{
21template <typename T>
22struct is_integral_constant : std::false_type
23{
24};
25template <std::integral U, U N>
26struct is_integral_constant<std::integral_constant<U, N>> : std::true_type
27{
28};
29
33template <typename T>
34concept BlockSizeArg = std::integral<std::remove_cvref_t<T>>
35 or is_integral_constant<std::remove_cvref_t<T>>::value;
37
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)
72{
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)
76 {
77 // Row index and current data row
78 auto row = xrows[r];
79 using T = typename X::value_type;
80 const T* xr = x.data() + r * nc * BS0 * BS1;
81
82#ifndef NDEBUG
83 if (row >= num_rows)
84 throw std::runtime_error("Local row out of range");
85#endif
86 // Columns indices for row
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)
90 {
91 // Find position of column index
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");
95
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)
101 {
102 for (int j = 0; j < BS1; ++j)
103 op(data[di + j], xr[xi + j]);
104 di += BS1;
105 xi += nc * BS1;
106 }
107 }
108 }
109}
110
132template <int BS0, int BS1, typename OP, typename U, typename V, typename W,
133 typename X, typename Y>
134void insert_blocked_csr(U&& data, const V& cols, const W& row_ptr, const X& x,
135 const Y& xrows, const Y& xcols, OP op,
136 [[maybe_unused]] typename Y::value_type num_rows)
137{
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)
141 {
142 // Row index and current data row
143 auto row = xrows[r] * BS0;
144#ifndef NDEBUG
145 if (row >= num_rows)
146 throw std::runtime_error("Local row out of range");
147#endif
148
149 for (int i = 0; i < BS0; ++i)
150 {
151 using T = typename X::value_type;
152 const T* xr = x.data() + (r * BS0 + i) * nc * BS1;
153
154 // Columns indices for row
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)
158 {
159 // Find position of column index
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");
163
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]);
169 }
170 }
171 }
172}
173
193template <typename OP, typename U, typename V, typename W, typename X,
194 typename Y>
195void insert_nonblocked_csr(U&& data, const V& cols, const W& row_ptr,
196 const X& x, const Y& xrows, const Y& xcols, OP op,
197 [[maybe_unused]] typename Y::value_type num_rows,
198 int bs0, int bs1)
199{
200 const std::size_t nc = xcols.size();
201 const int nbs = bs0 * bs1;
202
203 assert(x.size() == xrows.size() * xcols.size());
204 for (std::size_t r = 0; r < xrows.size(); ++r)
205 {
206 // Row index and current data row
207 auto rdiv = std::div(xrows[r], bs0);
208 using T = typename X::value_type;
209 const T* xr = x.data() + r * nc;
210
211#ifndef NDEBUG
212 if (rdiv.quot >= num_rows)
213 throw std::runtime_error("Local row out of range");
214#endif
215 // Columns indices for row
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)
219 {
220 // Find position of column index
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");
225
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());
229 op(data[di], xr[c]);
230 }
231 }
232}
233
259template <typename T>
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)
264{
265 assert(row_begin.size() == row_end.size());
266 // Block layout: row-major within each block. The element at row-offset
267 // k0 (∈ [0, bs0)) and column-offset k1 (∈ [0, bs1)) of block entry j
268 // is stored at values[j * bs0 * bs1 + k0 * bs1 + k1].
269 for (decltype(+bs0) k0 = 0; k0 < bs0; ++k0)
270 {
271 for (std::size_t i = 0; i < row_begin.size(); i++)
272 {
273 T vi{0};
274 for (std::int64_t j = row_begin[i]; j < row_end[i]; j++)
275 {
276 for (decltype(+bs1) k1 = 0; k1 < bs1; ++k1)
277 {
278 vi += values[j * bs0 * bs1 + k0 * bs1 + k1]
279 * x[indices[j] * bs1 + k1];
280 }
281 }
282
283 y[i * bs0 + k0] += vi;
284 }
285 }
286}
287
319template <typename T>
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)
324{
325 assert(row_begin.size() == row_end.size());
326
327 // Block layout: row-major within each block.
328 for (decltype(+bs0) k0 = 0; k0 < bs0; ++k0)
329 {
330 for (std::size_t i = 0; i < row_begin.size(); i++)
331 {
332 const T xval = x[i * bs0 + k0];
333 for (std::int64_t j = row_begin[i]; j < row_end[i]; j++)
334 {
335 for (decltype(+bs1) k1 = 0; k1 < bs1; ++k1)
336 {
337 y[indices[j] * bs1 + k1]
338 += values[j * bs0 * bs1 + k0 * bs1 + k1] * xval;
339 }
340 }
341 }
342 }
343}
344
345} // namespace impl
346} // namespace dolfinx::la
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