Arcane  4.2.2.0
Developer documentation
Loading...
Searching...
No Matches
CSRMatrixView.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* CSRMatrixView.h (C) 2000-2026 */
9/* */
10/* View of a sparse matrix stored in CSR (Compressed Sparse Row) format. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_CSRMATRIXVIEW_H
13#define ARCCORE_ALINA_CSRMATRIXVIEW_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/alina/AlinaGlobal.h"
18
19#include "arccore/base/Span.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane::Alina
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
32template <typename IndexType_>
33class CSRRowColumnIndex
34{
35 public:
36
37 using IndexType = IndexType_;
38
39 public:
40
41 CSRRowColumnIndex() = default;
42 explicit constexpr ARCCORE_HOST_DEVICE CSRRowColumnIndex(IndexType index)
43 : m_index(index)
44 {}
45
46 public:
47
48 [[nodiscard]] constexpr ARCCORE_HOST_DEVICE IndexType value() const { return m_index; }
49 constexpr ARCCORE_HOST_DEVICE operator IndexType() const { return m_index; }
50
51 private:
52
53 IndexType m_index = -1;
54};
55
56/*---------------------------------------------------------------------------*/
57/*---------------------------------------------------------------------------*/
61template <typename IndexType_>
62class CSRRowColumnIterator
63{
64 template <typename T> friend class CSRRow;
65 using IndexType = IndexType_;
66
67 public:
68
69 CSRRowColumnIterator() = default;
70
71 private:
72
73 explicit constexpr CSRRowColumnIterator(IndexType index)
74 : m_index(index)
75 {}
76
77 public:
78
79 constexpr CSRRowColumnIndex<IndexType> operator*() const { return CSRRowColumnIndex(m_index); }
80 constexpr void operator++() { ++m_index; }
81
82 friend constexpr bool
83 operator!=(const CSRRowColumnIterator& lhs, const CSRRowColumnIterator& rhs)
84 {
85 return lhs.m_index != rhs.m_index;
86 }
87 constexpr bool isValid() const { return m_index != (-1); }
88
89 private:
90
91 IndexType m_index = -1;
92};
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
99template <typename IndexType_>
100class CSRRow
101{
102 template <typename V, typename C, typename R>
103 friend class CSRMatrixView;
104 template <typename T>
105 friend class CSRRowRangeIterator;
106
107 public:
108
109 using IndexType = IndexType_;
110 using IteratorType = CSRRowColumnIterator<IndexType>;
111
112 public:
113
114 CSRRow() = default;
115
116 private:
117
118 constexpr CSRRow(IndexType begin, IndexType end)
119 : m_begin(begin)
120 , m_end(end)
121 {}
122
123 public:
124
125 constexpr IteratorType begin() const { return IteratorType(m_begin); }
126 constexpr IteratorType end() const { return IteratorType(m_end); }
127
128 private:
129
130 IndexType m_begin = -1;
131 IndexType m_end = -1;
132};
133
134template <typename T, size_t IntType>
136
137template<typename T>
138class SpanChooser<T,4>
139{
140 public:
141 using SpanType = SmallSpan<T>;
142};
143
144template <typename T>
145class SpanChooser<T, 8>
146{
147 public:
148
149 using SpanType = Span<T>;
150};
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
157template <typename IndexType_>
158class CSRRowRangeIterator
159{
160 template <typename V, typename C, typename R>
161 friend class CSRMatrixView;
162 template <typename T>
163 friend class CSRRowRange;
164
165 public:
166
167 using IndexType = IndexType_;
168 using ThatClass = CSRRowRangeIterator<IndexType>;
169
170 public:
171
173 class Sentinel
174 {
175 friend CSRRowRangeIterator<IndexType>;
176
177 public:
178
179 explicit Sentinel(IndexType v)
180 : m_end(v)
181 {}
182
183 public:
184
185 constexpr IndexType end() const { return m_end; }
186
187 private:
188
189 IndexType m_end = -1;
190 };
191
192 public:
193
194 CSRRowRangeIterator() = default;
195
196 private:
197
198 constexpr CSRRowRangeIterator(IndexType* row_indexes, IndexType index)
199 : m_row_indexes(row_indexes)
200 , m_index(index)
201 {}
202
203 public:
204
205 constexpr CSRRow<IndexType> operator*() const
206 {
207 return CSRRow(m_row_indexes[m_index], m_row_indexes[m_index + 1]);
208 }
209 constexpr ThatClass& operator++()
210 {
211 ++m_index;
212 return (*this);
213 }
214 constexpr ThatClass operator++(int)
215 {
216 return ThatClass(m_row_indexes, m_index++);
217 }
218 friend bool operator!=(const CSRRowRangeIterator& a, const Sentinel& b)
219 {
220 return a.m_index != b.end();
221 }
222
223 private:
224
225 IndexType* m_row_indexes = nullptr;
226 IndexType m_index = -1;
227};
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
234template <typename IndexType_>
235class CSRRowRange
236{
237 template <typename ValueType_, typename ColumnType_, typename RowIndexType_>
238 friend class CSRMatrixView;
239
240 public:
241
242 using IndexType = IndexType_;
243 using IteratorType = CSRRowRangeIterator<IndexType>;
244 using SentinelType = IteratorType::Sentinel;
245
246 public:
247
248 CSRRowRange() = default;
249
250 private:
251
252 constexpr ARCCORE_HOST_DEVICE CSRRowRange(IndexType* row_indexes, IndexType begin, IndexType end)
253 : m_row_indexes(row_indexes)
254 , m_begin(begin)
255 , m_end(end)
256 {}
257
258 public:
259
260 constexpr IteratorType begin() const
261 {
262 return IteratorType(m_row_indexes, m_begin);
263 }
264 constexpr SentinelType end() const
265 {
266 return SentinelType(m_end);
267 }
268
269 private:
270
271 IndexType* m_row_indexes = nullptr;
272 IndexType m_begin = -1;
273 IndexType m_end = -1;
274};
275
276/*---------------------------------------------------------------------------*/
277/*---------------------------------------------------------------------------*/
281template <typename ValueType_, typename ColumnType_, typename RowIndexType_>
282class CSRMatrixView
283{
284 public:
285
286 typedef ValueType_ value_type;
287 typedef ValueType_ val_type;
288 typedef ColumnType_ col_type;
289 using ptr_type = RowIndexType_;
290 using RowIndexType = RowIndexType_;
291 using ColumnSpanType = SpanChooser<ColumnType_,sizeof(RowIndexType_)>::SpanType;
292 using ValueSpanType = SpanChooser<ValueType_,sizeof(RowIndexType_)>::SpanType;
293
294 public:
295
296 CSRMatrixView() = default;
297
298 CSRMatrixView(Int32 nb_row, RowIndexType nb_non_zero, ptr_type* ptr_range, col_type* col_range, val_type* val_range)
299 : m_values(val_range)
300 , m_row_indexes(ptr_range)
301 , m_columns(col_range)
302 , m_nb_row(nb_row)
303 , m_nb_non_zero(nb_non_zero)
304 {
305 }
306
307 public:
308
310 constexpr Int32 nbRow() const noexcept { return m_nb_row; }
312 constexpr RowIndexType nbNonZero() const noexcept { return m_nb_non_zero; }
313
315 constexpr Int32 nbNonZeroForRow(Int32 row) const
316 {
317 ARCCORE_CHECK_AT(row, m_nb_row);
318 return m_row_indexes[row + 1] - m_row_indexes[row];
319 }
320
321 SmallSpan<RowIndexType> rowIndexes() const noexcept { return { m_row_indexes, m_nb_row + 1 }; }
322 ColumnSpanType columns() const noexcept { return { m_columns, m_nb_non_zero }; }
323 ValueSpanType values() const noexcept { return { m_values, m_nb_non_zero }; }
324
325 public:
326
327 [[nodiscard]] constexpr CSRRow<RowIndexType> rowRange(Int32 row) const
328 {
329 auto begin = m_row_indexes[row];
330 auto end = m_row_indexes[row + 1];
331 return { begin, end };
332 }
335 {
336 return CSRRowRange<RowIndexType>(m_row_indexes, 0, m_nb_row);
337 }
338
339 constexpr CSRRowRange<RowIndexType> subrows(RowIndexType begin, RowIndexType size) const
340 {
341 return CSRRowRange<RowIndexType>(m_row_indexes, begin, begin + size);
342 }
343
344 public:
345
346 constexpr ptr_type ptr(Int32 i) const
347 {
348 ARCCORE_CHECK_AT(i, m_nb_row + 1);
349 return m_row_indexes[i];
350 }
351 constexpr col_type col(RowIndexType i) const
352 {
353 ARCCORE_CHECK_AT(i, m_nb_non_zero);
354 return m_columns[i];
355 }
356 constexpr val_type val(RowIndexType i) const
357 {
358 ARCCORE_CHECK_AT(i, m_nb_non_zero);
359 return m_values[i];
360 }
361
362 ptr_type& ptr(Int32 i)
363 {
364 ARCCORE_CHECK_AT(i, m_nb_row + 1);
365 return m_row_indexes[i];
366 }
367 col_type& col(RowIndexType i)
368 {
369 ARCCORE_CHECK_AT(i, m_nb_non_zero);
370 return m_columns[i];
371 }
372 val_type& val(RowIndexType i)
373 {
374 ARCCORE_CHECK_AT(i, m_nb_non_zero);
375 return m_values[i];
376 }
377
379 constexpr val_type& value(CSRRowColumnIndex<RowIndexType> rc_index) const
380 {
381 return m_values[rc_index];
382 }
383
384 constexpr col_type column(CSRRowColumnIndex<RowIndexType> rc_index) const
385 {
386 return m_columns[rc_index];
387 }
388
389 private:
390
391 val_type* m_values = nullptr;
392 RowIndexType* m_row_indexes = nullptr;
393 col_type* m_columns = nullptr;
394 Int32 m_nb_row = 0;
395 RowIndexType m_nb_non_zero = 0;
396};
397
398/*---------------------------------------------------------------------------*/
399/*---------------------------------------------------------------------------*/
400
401} // namespace Arcane::Alina
402
403/*---------------------------------------------------------------------------*/
404/*---------------------------------------------------------------------------*/
405
406#endif
Types and functions associated with the classes SpanImpl, SmallSpan and Span.
constexpr col_type column(CSRRowColumnIndex< RowIndexType > rc_index) const
Value of the matrix for the given RowColumnIndex rc_index.
constexpr CSRRowRange< RowIndexType > rows() const
Range of all rows of the matrix.
constexpr val_type & value(CSRRowColumnIndex< RowIndexType > rc_index) const
Value of the matrix for the given RowColumnIndex rc_index.
constexpr CSRRowRange< RowIndexType > subrows(RowIndexType begin, RowIndexType size) const
Range of rows from interval [begin,begin+size[.
constexpr Int32 nbRow() const noexcept
Number of row.
constexpr RowIndexType nbNonZero() const noexcept
Number of non-zero in the matrix.
constexpr Int32 nbNonZeroForRow(Int32 row) const
Number of non-zero for the row row.
Index in the RowColumn list of a CSR Matrix.
Represents an iterator over the columns of a CSRRow.
Sentinel to detect end of iteration.
Iterator over rows of a CSR Matrix.
Represents a range of rows of a CSR Matrix.
Represents a row of a CSR Matrix.
View of an array of elements of type T.
Definition Span.h:803
View of an array of elements of type T.
Definition Span.h:633
std::int32_t Int32
Signed integer type of 32 bits.