Arcane  4.1.11.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
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 private:
184
185 IndexType m_end = -1;
186 };
187
188 public:
189
190 CSRRowRangeIterator() = default;
191
192 private:
193
194 constexpr CSRRowRangeIterator(IndexType* row_indexes, IndexType index)
195 : m_row_indexes(row_indexes)
196 , m_index(index)
197 {}
198
199 public:
200
201 constexpr CSRRow<IndexType> operator*() const
202 {
203 return CSRRow(m_row_indexes[m_index], m_row_indexes[m_index + 1]);
204 }
205 constexpr ThatClass& operator++()
206 {
207 ++m_index;
208 return (*this);
209 }
210 constexpr ThatClass operator++(int)
211 {
212 return ThatClass(m_row_indexes, m_index++);
213 }
214 friend bool operator!=(const CSRRowRangeIterator& a, const Sentinel& b)
215 {
216 return a.m_index != b.m_end;
217 }
218
219 private:
220
221 IndexType* m_row_indexes = nullptr;
222 IndexType m_index = -1;
223};
224
225/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
230template <typename IndexType_>
231class CSRRowRange
232{
233 template <typename ValueType_, typename ColumnType_, typename RowIndexType_>
234 friend class CSRMatrixView;
235
236 public:
237
238 using IndexType = IndexType_;
239 using IteratorType = CSRRowRangeIterator<IndexType>;
240 using SentinelType = IteratorType::Sentinel;
241
242 public:
243
244 CSRRowRange() = default;
245
246 private:
247
248 constexpr ARCCORE_HOST_DEVICE CSRRowRange(IndexType* row_indexes, IndexType begin, IndexType end)
249 : m_row_indexes(row_indexes)
250 , m_begin(begin)
251 , m_end(end)
252 {}
253
254 public:
255
256 constexpr IteratorType begin() const
257 {
258 return IteratorType(m_row_indexes, m_begin);
259 }
260 constexpr SentinelType end() const
261 {
262 return SentinelType(m_end);
263 }
264
265 private:
266
267 IndexType* m_row_indexes = nullptr;
268 IndexType m_begin = -1;
269 IndexType m_end = -1;
270};
271
272/*---------------------------------------------------------------------------*/
273/*---------------------------------------------------------------------------*/
277template <typename ValueType_, typename ColumnType_, typename RowIndexType_>
278class CSRMatrixView
279{
280 public:
281
282 typedef ValueType_ value_type;
283 typedef ValueType_ val_type;
284 typedef ColumnType_ col_type;
285 using ptr_type = RowIndexType_;
286 using RowIndexType = RowIndexType_;
287 using ColumnSpanType = SpanChooser<ColumnType_,sizeof(RowIndexType_)>::SpanType;
288 using ValueSpanType = SpanChooser<ValueType_,sizeof(RowIndexType_)>::SpanType;
289
290 public:
291
292 CSRMatrixView() = default;
293
294 CSRMatrixView(Int32 nb_row, RowIndexType nb_non_zero, ptr_type* ptr_range, col_type* col_range, val_type* val_range)
295 : m_values(val_range)
296 , m_row_indexes(ptr_range)
297 , m_columns(col_range)
298 , m_nb_row(nb_row)
299 , m_nb_non_zero(nb_non_zero)
300 {
301 }
302
303 public:
304
306 constexpr Int32 nbRow() const noexcept { return m_nb_row; }
308 constexpr RowIndexType nbNonZero() const noexcept { return m_nb_non_zero; }
309
311 constexpr Int32 nbNonZeroForRow(Int32 row) const
312 {
313 ARCCORE_CHECK_AT(row, m_nb_row);
314 return m_row_indexes[row + 1] - m_row_indexes[row];
315 }
316
317 SmallSpan<RowIndexType> rowIndexes() const noexcept { return { m_row_indexes, m_nb_row + 1 }; }
318 ColumnSpanType columns() const noexcept { return { m_columns, m_nb_non_zero }; }
319 ValueSpanType values() const noexcept { return { m_values, m_nb_non_zero }; }
320
321 public:
322
323 [[nodiscard]] constexpr CSRRow<RowIndexType> rowRange(Int32 row) const
324 {
325 auto begin = m_row_indexes[row];
326 auto end = m_row_indexes[row + 1];
327 return { begin, end };
328 }
331 {
332 return CSRRowRange<RowIndexType>(m_row_indexes, 0, m_nb_row);
333 }
334
335 constexpr CSRRowRange<RowIndexType> subrows(RowIndexType begin, RowIndexType size) const
336 {
337 return CSRRowRange<RowIndexType>(m_row_indexes, begin, begin + size);
338 }
339
340 public:
341
342 constexpr ptr_type ptr(Int32 i) const
343 {
344 ARCCORE_CHECK_AT(i, m_nb_row + 1);
345 return m_row_indexes[i];
346 }
347 constexpr col_type col(RowIndexType i) const
348 {
349 ARCCORE_CHECK_AT(i, m_nb_non_zero);
350 return m_columns[i];
351 }
352 constexpr val_type val(RowIndexType i) const
353 {
354 ARCCORE_CHECK_AT(i, m_nb_non_zero);
355 return m_values[i];
356 }
357
358 ptr_type& ptr(Int32 i)
359 {
360 ARCCORE_CHECK_AT(i, m_nb_row + 1);
361 return m_row_indexes[i];
362 }
363 col_type& col(RowIndexType i)
364 {
365 ARCCORE_CHECK_AT(i, m_nb_non_zero);
366 return m_columns[i];
367 }
368 val_type& val(RowIndexType i)
369 {
370 ARCCORE_CHECK_AT(i, m_nb_non_zero);
371 return m_values[i];
372 }
373
375 constexpr val_type& value(CSRRowColumnIndex<RowIndexType> rc_index) const
376 {
377 return m_values[rc_index];
378 }
379
380 constexpr col_type column(CSRRowColumnIndex<RowIndexType> rc_index) const
381 {
382 return m_columns[rc_index];
383 }
384
385 private:
386
387 val_type* m_values = nullptr;
388 RowIndexType* m_row_indexes = nullptr;
389 col_type* m_columns = nullptr;
390 Int32 m_nb_row = 0;
391 RowIndexType m_nb_non_zero = 0;
392};
393
394/*---------------------------------------------------------------------------*/
395/*---------------------------------------------------------------------------*/
396
397} // namespace Arcane::Alina
398
399/*---------------------------------------------------------------------------*/
400/*---------------------------------------------------------------------------*/
401
402#endif
Types et fonctions associés aux 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.
Vue d'un tableau d'éléments de type T.
Definition Span.h:801
Vue d'un tableau d'éléments de type T.
Definition Span.h:633
std::int32_t Int32
Type entier signé sur 32 bits.