• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / 18300593117

06 Oct 2025 10:47PM UTC coverage: 74.862% (-0.2%) from 75.031%
18300593117

push

github

web-flow
Merge pull request #759 from wdhawkins/performance

Sweep performance optimizations

294 of 302 new or added lines in 15 files covered. (97.35%)

334 existing lines in 80 files now uncovered.

17788 of 23761 relevant lines covered (74.86%)

61852783.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/framework/data_types/sparse_matrix/sparse_matrix.h
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#pragma once
5

6
#include "framework/math/math.h"
7

8
namespace opensn
9
{
10

11
/**
12
 * Sparse matrix utility. This is a basic CSR type sparse matrix which allows efficient matrix
13
 * storage and multiplication. It is not intended for solving linear systems (use PETSc for that
14
 * instead). It was originally developed for the transfer matrices of transport cross sections.
15
 */
16
class SparseMatrix
17
{
18
private:
19
  /// Maximum number of rows for this matrix
20
  size_t row_size_;
21
  /// Maximum number of columns for this matrix
22
  size_t col_size_;
23

24
public:
25
  /// rowI_indices[i] is a vector indices j for the non-zero columns.
26
  std::vector<std::vector<size_t>> rowI_indices;
27
  /// rowI_values[i] corresponds to column indices and contains the non-zero value.
28
  std::vector<std::vector<double>> rowI_values;
29

30
public:
31
  /// Constructor with number of rows and columns constructor.
32
  SparseMatrix(size_t num_rows, size_t num_cols);
33
  /// Copy constructor.
34
  SparseMatrix(const SparseMatrix& matrix);
35

36
  size_t GetNumRows() const { return row_size_; }
5,117,002✔
37
  size_t GetNumCols() const { return col_size_; }
38

39
  /// Inserts a value into the matrix.
40
  void Insert(size_t i, size_t j, double value);
41

42
  /// Inserts-Adds a value into the matrix with duplicate check.
43
  void InsertAdd(size_t i, size_t j, double value);
44

45
  /**
46
   * Returns the value in the matrix at the given location. This is a rather inefficient routine.
47
   * Use the columns and values rather than directly this function.
48
   */
49
  double GetValueIJ(size_t i, size_t j) const;
50

51
  /// Sets the diagonal of the matrix using a vector.
52
  void SetDiagonal(const std::vector<double>& diag);
53

54
  /// Sorts the column indices of each row for faster lookup.
55
  void Compress();
56

57
  /// Prints the sparse matrix to string.
58
  std::string PrintStr() const;
59

60
private:
61
  /// Constructor with number of rows constructor.
62
  void CheckInitialized() const;
63

64
public:
65
  virtual ~SparseMatrix() = default;
5,973✔
66

67
public:
68
  struct EntryReference
69
  {
70
    const size_t& row_index;
71
    const size_t& column_index;
72
    double& value;
73

74
    EntryReference(const size_t& row_id, const size_t& column_id, double& value)
3,785✔
75
      : row_index(row_id), column_index(column_id), value(value)
3,785✔
76
    {
77
    }
3,785✔
78
  };
79

80
  struct ConstEntryReference
81
  {
82
  public:
83
    const size_t& row_index;
84
    const size_t& column_index;
85
    const double& value;
86

87
    ConstEntryReference(const size_t& row_id, const size_t& column_id, const double& value)
88
      : row_index(row_id), column_index(column_id), value(value)
89
    {
90
    }
91
  };
92

93
  class RowIteratorContext
94
  {
95
  private:
96
    const std::vector<size_t>& ref_col_ids_;
97
    std::vector<double>& ref_col_vals_;
98
    const size_t ref_row_;
99

100
  public:
101
    RowIteratorContext(SparseMatrix& matrix, size_t ref_row)
102
      : ref_col_ids_(matrix.rowI_indices[ref_row]),
103
        ref_col_vals_(matrix.rowI_values[ref_row]),
104
        ref_row_(ref_row)
105
    {
106
    }
107

108
    class RowIterator
109
    {
110
    private:
111
      RowIteratorContext& context_;
112
      size_t ref_entry_;
113

114
    public:
115
      RowIterator(RowIteratorContext& context, size_t ref_entry)
698✔
116
        : context_{context}, ref_entry_{ref_entry}
698✔
117
      {
118
      }
698✔
119

120
      RowIterator operator++()
3,773✔
121
      {
122
        RowIterator i = *this;
3,773✔
123
        ref_entry_++;
3,773✔
124
        return i;
3,773✔
125
      }
126
      RowIterator operator++(int)
127
      {
128
        ref_entry_++;
129
        return *this;
130
      }
131

132
      EntryReference operator*()
3,773✔
133
      {
134
        return {
135
          context_.ref_row_, context_.ref_col_ids_[ref_entry_], context_.ref_col_vals_[ref_entry_]};
3,773✔
136
      }
137

138
      bool operator==(const RowIterator& rhs) const { return ref_entry_ == rhs.ref_entry_; }
139
      bool operator!=(const RowIterator& rhs) const { return ref_entry_ != rhs.ref_entry_; }
4,122✔
140
    };
141

142
    RowIterator begin() { return {*this, 0}; }
349✔
143
    RowIterator end() { return {*this, ref_col_vals_.size()}; }
349✔
144
  };
4,946✔
145

349✔
146
  RowIteratorContext Row(size_t row_id);
349✔
147

349✔
148
  class ConstRowIteratorContext
349✔
149
  {
349✔
150
  private:
151
    const std::vector<size_t>& ref_col_ids_;
152
    const std::vector<double>& ref_col_vals_;
153
    const size_t ref_row_;
154

155
  public:
156
    ConstRowIteratorContext(const SparseMatrix& matrix, size_t ref_row)
2,147,483,647✔
157
      : ref_col_ids_(matrix.rowI_indices[ref_row]),
2,147,483,647✔
158
        ref_col_vals_(matrix.rowI_values[ref_row]),
2,147,483,647✔
159
        ref_row_(ref_row)
2,147,483,647✔
160
    {
161
    }
2,147,483,647✔
162

163
    class ConstRowIterator
164
    {
165
    private:
166
      const ConstRowIteratorContext& context_;
167
      size_t ref_entry_;
168

169
    public:
170
      ConstRowIterator(const ConstRowIteratorContext& context, size_t ref_entry)
171
        : context_(context), ref_entry_{ref_entry}
172
      {
173
      }
174

175
      ConstRowIterator operator++()
176
      {
177
        ConstRowIterator i = *this;
178
        ref_entry_++;
179
        return i;
180
      }
181
      ConstRowIterator operator++(int)
182
      {
183
        ref_entry_++;
184
        return *this;
185
      }
186

187
      ConstEntryReference operator*()
188
      {
189
        return {
190
          context_.ref_row_, context_.ref_col_ids_[ref_entry_], context_.ref_col_vals_[ref_entry_]};
191
      }
192

193
      bool operator==(const ConstRowIterator& rhs) const { return ref_entry_ == rhs.ref_entry_; }
194
      bool operator!=(const ConstRowIterator& rhs) const { return ref_entry_ != rhs.ref_entry_; }
195
    };
196

197
    ConstRowIterator begin() const { return {*this, 0}; }
198
    ConstRowIterator end() const { return {*this, ref_col_vals_.size()}; }
199
  };
200

201
  ConstRowIteratorContext Row(size_t row_id) const;
202

203
  /// Iterator to loop over all matrix entries.
204
  class EntriesIterator
205
  {
206
  private:
207
    SparseMatrix& sp_matrix;
208
    size_t ref_row_;
209
    size_t ref_col_;
210

211
  public:
212
    explicit EntriesIterator(SparseMatrix& context, size_t row)
2✔
213
      : sp_matrix{context}, ref_row_{row}, ref_col_(0)
2✔
214
    {
215
    }
2✔
216

2,147,483,647✔
217
    void Advance()
2,147,483,647✔
218
    {
2,147,483,647✔
219
      ref_col_++;
2,147,483,647✔
220
      if (ref_col_ >= sp_matrix.rowI_indices[ref_row_].size())
2,147,483,647✔
221
      {
2,147,483,647✔
222
        ref_row_++;
2,147,483,647✔
223
        ref_col_ = 0;
2,147,483,647✔
224
        while ((ref_row_ < sp_matrix.row_size_) and (sp_matrix.rowI_indices[ref_row_].empty()))
2,147,483,647✔
225
          ref_row_++;
2,147,483,647✔
226
      }
2,147,483,647✔
227
    }
2,147,483,647✔
228

2,147,483,647✔
229
    EntriesIterator operator++()
2,147,483,647✔
230
    {
2,147,483,647✔
231
      EntriesIterator i = *this;
12✔
232
      Advance();
12✔
233
      return i;
12✔
234
    }
4✔
235
    EntriesIterator operator++(int)
4✔
236
    {
4✔
UNCOV
237
      Advance();
×
238
      return *this;
12✔
239
    }
12✔
240

12✔
241
    EntryReference operator*()
12✔
242
    {
12✔
243
      return {ref_row_,
12✔
244
              sp_matrix.rowI_indices[ref_row_][ref_col_],
12✔
245
              sp_matrix.rowI_values[ref_row_][ref_col_]};
12✔
246
    }
12✔
247
    bool operator==(const EntriesIterator& rhs) const
248
    {
249
      return (ref_row_ == rhs.ref_row_) and (ref_col_ == rhs.ref_col_);
250
    }
251
    bool operator!=(const EntriesIterator& rhs) const
13✔
252
    {
253
      return (ref_row_ != rhs.ref_row_) or (ref_col_ != rhs.ref_col_);
13✔
254
    }
255
  };
256

257
  EntriesIterator begin();
258
  EntriesIterator end();
259
};
260

261
} // namespace opensn
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc