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

Open-Sn / opensn / #5

02 Apr 2026 01:04PM UTC coverage: 74.858% (-1.2%) from 76.085%
#5

push

web-flow
Merge pull request #1009 from andrsd/unit-tests

Removing `opensn-test`

20988 of 28037 relevant lines covered (74.86%)

65922489.9 hits per line

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

33.33
/framework/data_types/parallel_vector/parallel_vector.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
#include "mpicpp-lite/mpicpp-lite.h"
8
#include "petscvec.h"
9
#include <cstdint>
10

11
namespace mpi = mpicpp_lite;
12

13
namespace opensn
14
{
15

16
enum class VecOpType : short
17
{
18
  SET_VALUE = 1,
19
  ADD_VALUE = 2
20
};
21

22
/// An abstract implementation of a parallel vector.
23
class ParallelVector
24
{
25
public:
26
  /**
27
   * Initialize a parallel vector with the given local and global sizes with the given communicator
28
   * whose entries are set to zero.
29
   */
30
  ParallelVector(uint64_t local_size, uint64_t global_size, const mpi::Communicator& communicator);
31

32
  /// Copy constructor.
33
  ParallelVector(const ParallelVector& other);
34

35
  /// Move constructor.
36
  ParallelVector(ParallelVector&& other) noexcept;
37

38
  /**
39
   * Creates a copy of the vector datastructures AND values. This routine requires no
40
   * communication.
41
   */
42
  virtual std::unique_ptr<ParallelVector> MakeCopy() const = 0;
43

44
  /**
45
   * Creates a copy of the vector datastructures but NOT the values. The values are defaulted to
46
   * zero. This routine requires no communication.
47
   */
48
  virtual std::unique_ptr<ParallelVector> MakeClone() const = 0;
49

50
  /**
51
   * Returns a direct pointer to the memory array used internally by the vector to store its owned
52
   * elements
53
   */
54
  virtual double* GetData() = 0;
55

56
  /**
57
   * Returns a direct const pointer to the memory array used internally by the vector to store its
58
   * owned elements
59
   */
60
  virtual const double* GetData() const = 0;
61

62
  /// Return the size of the locally owned portion of the parallel vector.
63
  uint64_t GetLocalSize() const { return local_size_; }
78,227✔
64

65
  /// Return the global size of the parallel vector.
66
  uint64_t GetGlobalSize() const { return global_size_; }
×
67

68
  /**
69
   * Read only accessor to the entry at the given local index of the local vector.
70
   *
71
   * \note This accessor allows access to all locally stored elements, including any data beyond
72
   *       local_size_ that may exist in derived classes.
73
   */
74
  virtual double operator[](uint64_t local_id) const = 0;
75

76
  /**
77
   * Read/write accessor to the entry at the given local index of the local vector.
78
   *
79
   * \note This accessor allows access to all locally stored elements, including any data beyond
80
   *       local_size_ that may exist in derived classes.
81
   */
82
  virtual double& operator[](uint64_t local_id) = 0;
83

84
  /// Return a vector containing the locally owned data.
85
  virtual std::vector<double> MakeLocalVector() = 0;
86

87
  /// Set the entries of the locally owned portion of the parallel vector to the given value.
88
  virtual void Set(double value) = 0;
89

90
  /// Set the entries of the locally owned portion of the parallel vector to the given STL vector.
91
  virtual void Set(const std::vector<double>& local_vector) = 0;
92

93
  /**
94
   * Copies a contiguous block of data from the source STL vector to the current vector starting at
95
   * local_offset. The input STL vector must have exactly num_values entries.
96
   */
97
  virtual void
98
  BlockSet(const std::vector<double>& y, uint64_t local_offset, uint64_t num_values) = 0;
99

100
  /// Sets the local values of one vector equal to another. The sizes must be compatible.
101
  virtual void CopyLocalValues(const ParallelVector& y) = 0;
102

103
  /**
104
   * Sets the local values of the vector equal to that of the PETSc vector. The sizes must be
105
   * compatible.
106
   */
107
  virtual void CopyLocalValues(Vec y) = 0;
108

109
  /**
110
   * Copies a contiguous block of local data (num_values entries) from the source vector (starting
111
   * at y_offset) to the current vector starting at local_offset.
112
   */
113
  virtual void BlockCopyLocalValues(const ParallelVector& y,
114
                                    uint64_t y_offset,
115
                                    uint64_t local_offset,
116
                                    uint64_t num_values) = 0;
117

118
  /**
119
   * Copies a contiguous block of local data (num_values entries) from the source vector (starting
120
   * at y_offset) to the current vector starting at local_offset. PETSc flavor.
121
   */
122
  virtual void
123
  BlockCopyLocalValues(Vec y, uint64_t y_offset, uint64_t local_offset, uint64_t num_values) = 0;
124

125
  /**
126
   * Define a set or add operation for the given global id-value pair
127
   *
128
   * This routine adds the global id-value pair to the set operation cache, which upon execution of
129
   * Assemble, communicates the operations to the appropriate process.
130
   */
131
  virtual void SetValue(uint64_t global_id, double value, VecOpType op_type) = 0;
132

133
  /**
134
   * Group multiple operations into a single call.
135
   *
136
   * This routine goes through the given global id-value pairs and calls SetValue for each.
137
   */
138
  virtual void SetValues(const std::vector<uint64_t>& global_ids,
139
                         const std::vector<double>& values,
140
                         VecOpType op_type) = 0;
141

142
  /// In place adding of vectors. The sizes must be compatible.
143
  virtual void operator+=(const ParallelVector& y) = 0;
144

145
  /// Adds a vector multiplied by scalar a, x=x+a*y. Optimized for a=1.0 and -1.0
146
  virtual void PlusAY(const ParallelVector& y, double a) = 0;
147

148
  /// Performs x = a*x + y with the current vector being x.
149
  virtual void AXPlusY(double a, const ParallelVector& y) = 0;
150

151
  /// Scales a vector by a scalar
152
  virtual void Scale(double a) = 0;
153

154
  /// Adds a constant scalar value to all the entries of the vector
155
  virtual void Shift(double a) = 0;
156

157
  /// Returns the specified norm of the vector.
158
  virtual double ComputeNorm(NormType norm_type) const = 0;
159

160
  /**
161
   * Communicate all operations stored within the operation cache to the corresponding processes
162
   * that own the respective global indices, and apply the operations.
163
   *
164
   * This routine clears the respective operation cache once completed.
165
   */
166
  virtual void Assemble() = 0;
167

168
  /// Print the local vectors to stings.
169
  virtual std::string PrintStr() const = 0;
170

171
  /**
172
   * Communicate the current ghost entries, if applicable, to all other processes to update the
173
   * locally stored ghost data.
174
   */
175
  virtual void CommunicateGhostEntries() {};
×
176

177
  virtual ~ParallelVector() = default;
178

179
protected:
180
  const uint64_t local_size_;
181
  const uint64_t global_size_;
182

183
  const int location_id_;
184
  const int process_count_;
185
  const mpi::Communicator& comm_;
186
};
187

188
} // 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