Skip to content

Commit 05b0fbd

Browse files
committed
Hypre interface: Clang tidying
Many small things. Replaced some raw pointers with shared_ptr. Added `modernize-use-ranges` to `.clangd`
1 parent f4d64c9 commit 05b0fbd

3 files changed

Lines changed: 57 additions & 55 deletions

File tree

‎.clangd‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ Diagnostics:
22
MissingIncludes: Strict
33
ClangTidy:
44
FastCheckFilter: None
5+
Add: [modernize-use-ranges]
56
Includes:
67
IgnoreHeader: ["adios2/.*", "bits/.*", "cpptrace/.*", "petsc.*"]

‎include/bout/hypre_interface.hxx‎

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@
1515
#include "bout/field2d.hxx"
1616
#include "bout/globalindexer.hxx"
1717
#include "bout/hyprelib.hxx"
18+
#include "bout/msg_stack.hxx"
1819
#include "bout/options.hxx"
1920
#include "bout/output.hxx"
2021
#include "bout/paralleltransform.hxx"
2122
#include "bout/region.hxx"
23+
#include "bout/unused.hxx"
2224
#include "bout/utils.hxx"
2325

2426
#include "HYPRE.h"
2527
#include "HYPRE_IJ_mv.h"
2628
#include "HYPRE_parcsr_ls.h"
2729
#include "HYPRE_parcsr_mv.h"
2830
#include "HYPRE_utilities.h"
29-
#include "_hypre_utilities.h"
3031

3132
#include <algorithm>
3233
#include <cmath>
3334
#include <iterator>
3435
#include <memory>
36+
#include <utility>
3537
#include <vector>
3638

3739
// BOUT_ENUM_CLASS does not work inside namespaces
@@ -48,7 +50,7 @@ namespace bout {
4850

4951
namespace {
5052
int checkHypreError(int error) {
51-
if (error) {
53+
if (error != 0) {
5254
// Aaron Fisher warns that it is possible that the error code is non-zero even
5355
// though a solve was successful. If this occurs, we might want to make this a
5456
// warning, or otherwise investigate further.
@@ -217,7 +219,7 @@ class HypreVector {
217219
HYPRE_Complex* V{nullptr};
218220
HYPRE_Complex* workV{nullptr};
219221
bool cache_current{false};
220-
HypreLib hyprelib{};
222+
HypreLib hyprelib;
221223

222224
public:
223225
static_assert(bout::utils::is_Field_v<T>, "HypreVector only works with Fields");
@@ -241,7 +243,7 @@ public:
241243
HypreVector(const HypreVector<T>&) = delete;
242244
auto operator=(const HypreVector<T>&) = delete;
243245

244-
HypreVector(HypreVector<T>&& other)
246+
HypreVector(HypreVector<T>&& other) noexcept
245247
: comm(other.comm), jlower(other.jlower), jupper(other.jupper), vsize(other.vsize),
246248
indexConverter(other.indexConverter), location(other.location),
247249
initialised(other.initialised), have_indices(other.have_indices), I(other.I),
@@ -312,7 +314,8 @@ public:
312314
}
313315

314316
/// Construct a vector with given index set, but don't set any values
315-
explicit HypreVector(IndexerPtr<T> indConverter) : indexConverter(indConverter) {
317+
explicit HypreVector(IndexerPtr<T> indConverter)
318+
: indexConverter(indConverter), location(CELL_LOC::centre) {
316319
Mesh& mesh = *indConverter->getMesh();
317320
const MPI_Comm comm =
318321
std::is_same_v<T, FieldPerp> ? mesh.getXZcomm() : BoutComm::get();
@@ -325,7 +328,6 @@ public:
325328
checkHypreError(HYPRE_IJVectorSetObjectType(hypre_vector, HYPRE_PARCSR));
326329
checkHypreError(HYPRE_IJVectorInitialize(hypre_vector));
327330
initialised = true;
328-
location = CELL_LOC::centre;
329331
HypreMalloc(I, vsize * sizeof(HYPRE_BigInt));
330332
HypreMalloc(V, vsize * sizeof(HYPRE_Complex));
331333
HypreMalloc(workV, vsize * sizeof(HYPRE_Complex));
@@ -546,13 +548,13 @@ class HypreMatrix {
546548
ParallelTransform* parallel_transform{nullptr};
547549
bool assembled{false};
548550
HYPRE_BigInt num_rows;
549-
std::vector<HYPRE_BigInt>* I;
550-
std::vector<std::vector<HYPRE_BigInt>>* J;
551-
std::vector<std::vector<HYPRE_Complex>>* V;
551+
std::shared_ptr<std::vector<HYPRE_BigInt>> I;
552+
std::shared_ptr<std::vector<std::vector<HYPRE_BigInt>>> J;
553+
std::shared_ptr<std::vector<std::vector<HYPRE_Complex>>> V;
552554
/// Enable reduction of boundary equations before assembling the Hypre matrix.
553555
bool use_boundary_elimination{false};
554556
BoundaryEliminationPtr boundary_elimination;
555-
HypreLib hyprelib{};
557+
HypreLib hyprelib;
556558

557559
// todo also take care of I,J,V
558560
struct MatrixDeleter {
@@ -575,8 +577,8 @@ public:
575577
index_converter(other.index_converter), location(other.location),
576578
initialised(other.initialised), yoffset(other.yoffset),
577579
parallel_transform(other.parallel_transform), assembled(other.assembled),
578-
num_rows(other.num_rows), I(other.I), J(other.J), V(other.V),
579-
use_boundary_elimination(other.use_boundary_elimination),
580+
num_rows(other.num_rows), I(std::move(other.I)), J(std::move(other.J)),
581+
V(std::move(other.V)), use_boundary_elimination(other.use_boundary_elimination),
580582
boundary_elimination(other.boundary_elimination) {
581583
std::swap(hypre_matrix, other.hypre_matrix);
582584
std::swap(parallel_matrix, other.parallel_matrix);
@@ -595,9 +597,9 @@ public:
595597
parallel_transform = other.parallel_transform;
596598
assembled = other.assembled;
597599
num_rows = other.num_rows;
598-
I = other.I;
599-
J = other.J;
600-
V = other.V;
600+
I = std::move(other.I);
601+
J = std::move(other.J);
602+
V = std::move(other.V);
601603
use_boundary_elimination = other.use_boundary_elimination;
602604
boundary_elimination = other.boundary_elimination;
603605
return *this;
@@ -608,27 +610,21 @@ public:
608610
///
609611
/// note: preallocate not currently used, but here to match PetscMatrix interface
610612
explicit HypreMatrix(IndexerPtr<T> indConverter, bool UNUSED(preallocate) = true)
611-
: hypre_matrix(new HYPRE_IJMatrix, MatrixDeleter{}), index_converter(indConverter) {
613+
: ilower(indConverter->getGlobalStart()),
614+
iupper(ilower + indConverter->size() - 1), // inclusive end
615+
hypre_matrix(new HYPRE_IJMatrix, MatrixDeleter{}), index_converter(indConverter),
616+
num_rows(iupper - ilower + 1),
617+
I(std::make_shared<std::vector<HYPRE_BigInt>>(num_rows)),
618+
J(std::make_shared<std::vector<std::vector<HYPRE_BigInt>>>(num_rows)),
619+
V(std::make_shared<std::vector<std::vector<HYPRE_Complex>>>(num_rows)) {
612620
Mesh* mesh = indConverter->getMesh();
613621
const MPI_Comm comm =
614622
std::is_same_v<T, FieldPerp> ? mesh->getXZcomm() : BoutComm::get();
615623
parallel_transform = &mesh->getCoordinates()->getParallelTransform();
616624

617-
ilower = indConverter->getGlobalStart();
618-
iupper = ilower + indConverter->size() - 1; // inclusive end
619-
num_rows = iupper - ilower + 1;
620-
621-
I = new std::vector<HYPRE_BigInt>;
622-
J = new std::vector<std::vector<HYPRE_BigInt>>;
623-
V = new std::vector<std::vector<HYPRE_Complex>>;
624-
(*I).resize(num_rows);
625-
(*J).resize(num_rows);
626-
(*V).resize(num_rows);
627625
for (HYPRE_BigInt i = 0; i < num_rows; ++i) {
628626
(*I)[i] = ilower + i;
629-
(*J)[i].resize(0);
630627
(*J)[i].reserve(10);
631-
(*V)[i].resize(0);
632628
(*V)[i].reserve(10);
633629
}
634630

@@ -659,8 +655,8 @@ public:
659655
Element(HypreMatrix<T>& matrix_, HYPRE_BigInt row_, HYPRE_BigInt column_,
660656
std::vector<HYPRE_BigInt> positions_ = {},
661657
std::vector<BoutReal> weights_ = {})
662-
: hypre_matrix(matrix_.get()), row(row_), column(column_), positions(positions_),
663-
weights(weights_) {
658+
: hypre_matrix(matrix_.get()), row(row_), column(column_),
659+
positions(std::move(positions_)), weights(std::move(weights_)) {
664660
#if CHECK > 2
665661
for (const auto val : weights) {
666662
ASSERT3(std::isfinite(val));
@@ -1032,7 +1028,7 @@ public:
10321028
const HYPRE_Complex operator_value = elimination_state.interior_values->data[i];
10331029
elimination_state.interior_values->data[i] = alpha * operator_value;
10341030
elimination_state.boundary_values->data[i] =
1035-
alpha * operator_value
1031+
(alpha * operator_value)
10361032
+ (y_boundary_values != nullptr ? beta * y_boundary_values->data[i] : 0.0);
10371033
}
10381034
}
@@ -1073,7 +1069,7 @@ private:
10731069
HYPRE_Solver precon;
10741070
bool solver_setup{false};
10751071
HYPRE_SOLVER_TYPE solver_type = HYPRE_SOLVER_TYPE::gmres;
1076-
HypreLib hyprelib{};
1072+
HypreLib hyprelib;
10771073

10781074
// Basically a manual vtable for the various implementations of the
10791075
// HYPRE solver methods. By storing function pointers and setting
@@ -1092,14 +1088,13 @@ private:
10921088
decltype(HYPRE_ParCSRGMRESSolve)* solverSolve{nullptr};
10931089

10941090
public:
1095-
HypreSystem(Mesh& mesh, Options& options) {
1091+
HypreSystem(Mesh& mesh, Options& options)
1092+
: comm(std::is_same_v<T, FieldPerp> ? mesh.getXZcomm() : BoutComm::get()) {
10961093
solver_type = options["hypre_solver_type"]
10971094
.doc("Type of solver to use when solving Hypre system. Possible "
10981095
"values are: gmres, bicgstab, pcg")
10991096
.withDefault(HYPRE_SOLVER_TYPE::bicgstab);
11001097

1101-
comm = std::is_same_v<T, FieldPerp> ? mesh.getXZcomm() : BoutComm::get();
1102-
11031098
auto print_level =
11041099
options["hypre_print_level"]
11051100
.doc("Verbosity for Hypre solver. Integer from 0 (silent) to 4 "

‎src/sys/hypre_interface.cxx‎

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33

44
#if BOUT_HAS_HYPRE
55

6+
#include <HYPRE_utilities.h>
7+
8+
#include "bout/boutexception.hxx"
69
#include "bout/hypre_interface.hxx"
710

11+
#include <algorithm>
12+
#include <iterator>
13+
#include <memory>
814
#include <numeric>
915
#include <unordered_map>
16+
#include <vector>
1017

1118
namespace bout {
1219

@@ -15,9 +22,8 @@ BoundaryElimination::BoundaryElimination(HYPRE_Int nrows, HYPRE_Int* ncols,
1522
HYPRE_BigInt* cols, HYPRE_Complex* values,
1623
HYPRE_Int nb, HYPRE_Int* bi_array)
1724
: nb(nb) {
18-
HYPRE_Int* row_indexes;
1925
const auto find_local_row = [nrows, rows](HYPRE_BigInt row) -> HYPRE_Int {
20-
auto row_position = std::lower_bound(rows, rows + nrows, row);
26+
auto* row_position = std::lower_bound(rows, rows + nrows, row);
2127
if ((row_position == rows + nrows) || (*row_position != row)) {
2228
throw BoutException("Could not find local row {} while constructing boundary "
2329
"elimination data",
@@ -27,6 +33,7 @@ BoundaryElimination::BoundaryElimination(HYPRE_Int nrows, HYPRE_Int* ncols,
2733
};
2834

2935
// Create the row_indexes array
36+
HYPRE_Int* row_indexes{nullptr};
3037
HypreMalloc(row_indexes, sizeof(HYPRE_Int) * nrows);
3138
row_indexes[0] = 0;
3239
for (HYPRE_Int i = 1; i < nrows; i++) {
@@ -71,7 +78,7 @@ BoundaryElimination::BoundaryElimination(HYPRE_Int nrows, HYPRE_Int* ncols,
7178
// Get boundary equation information and adjust boundary equations
7279
HYPRE_Int i = bi_array[bnum];
7380
const HYPRE_Int binum = find_local_row(i);
74-
HYPRE_Int bcoeffnum = row_indexes[binum];
81+
const HYPRE_Int bcoeffnum = row_indexes[binum];
7582
HYPRE_Complex bii{0.0}, bij{0.0};
7683
HYPRE_BigInt j = -1;
7784

@@ -148,11 +155,11 @@ BoundaryElimination::BoundaryElimination(HYPRE_Int nrows, HYPRE_Int* ncols,
148155

149156
na = static_cast<HYPRE_Int>(aknums.size());
150157
HypreMalloc(aoffset_array, sizeof(HYPRE_Int) * (nb + 1));
151-
std::copy(aoffsets.begin(), aoffsets.end(), aoffset_array);
158+
std::ranges::copy(aoffsets, aoffset_array);
152159
HypreMalloc(aknum_array, sizeof(HYPRE_Int) * na);
153-
std::copy(aknums.begin(), aknums.end(), aknum_array);
160+
std::ranges::copy(aknums, aknum_array);
154161
HypreMalloc(aki_array, sizeof(HYPRE_Complex) * na);
155-
std::copy(akis.begin(), akis.end(), aki_array);
162+
std::ranges::copy(akis, aki_array);
156163

157164
std::vector<HYPRE_Int> dependency_depth(nb, -1);
158165
const auto get_depth = [&dependency_depth, this](const auto& self,
@@ -174,15 +181,14 @@ BoundaryElimination::BoundaryElimination(HYPRE_Int nrows, HYPRE_Int* ncols,
174181

175182
expansion_order.resize(nb);
176183
std::iota(expansion_order.begin(), expansion_order.end(), 0);
177-
std::sort(expansion_order.begin(), expansion_order.end(),
178-
[&dependency_depth](HYPRE_Int lhs, HYPRE_Int rhs) {
179-
if (dependency_depth[lhs] != dependency_depth[rhs]) {
180-
return dependency_depth[lhs] < dependency_depth[rhs];
181-
}
182-
return lhs < rhs;
183-
});
184+
std::ranges::sort(expansion_order, [&dependency_depth](HYPRE_Int lhs, HYPRE_Int rhs) {
185+
if (dependency_depth[lhs] != dependency_depth[rhs]) {
186+
return dependency_depth[lhs] < dependency_depth[rhs];
187+
}
188+
return lhs < rhs;
189+
});
184190
reduction_order = expansion_order;
185-
std::reverse(reduction_order.begin(), reduction_order.end());
191+
std::ranges::reverse(reduction_order);
186192

187193
// Set return arguments
188194
*row_indexes_ptr = row_indexes;
@@ -216,9 +222,9 @@ BCValuesPtr BoundaryElimination::reduceRightHandSideInPlace(HYPRE_Complex* rhs)
216222
// Allocate array to store boundary row values
217223
BCValuesPtr brhs = copyBoundaryRowValues(rhs);
218224

219-
for (HYPRE_Int bnum : reduction_order) {
225+
for (const HYPRE_Int bnum : reduction_order) {
220226
for (HYPRE_Int anum = aoffset_array[bnum]; anum < aoffset_array[bnum + 1]; anum++) {
221-
HYPRE_Int aknum = aknum_array[anum];
227+
const HYPRE_Int aknum = aknum_array[anum];
222228
rhs[aknum] -= aki_array[anum] * brhs->data[bnum] / bii_array[bnum];
223229
}
224230
if (bdep_array[bnum] >= 0) {
@@ -232,9 +238,9 @@ BCValuesPtr BoundaryElimination::reduceRightHandSideInPlace(HYPRE_Complex* rhs)
232238
void BoundaryElimination::expandSolutionInPlace(BCValuesPtr brhs,
233239
HYPRE_Complex* solution) const {
234240

235-
for (HYPRE_Int bnum : expansion_order) {
236-
HYPRE_Int binum = binum_array[bnum];
237-
HYPRE_Int bjnum = bjnum_array[bnum];
241+
for (const HYPRE_Int bnum : expansion_order) {
242+
const HYPRE_Int binum = binum_array[bnum];
243+
const HYPRE_Int bjnum = bjnum_array[bnum];
238244
solution[binum] =
239245
(brhs->data[bnum] - bij_array[bnum] * solution[bjnum]) / bii_array[bnum];
240246
}
@@ -245,7 +251,7 @@ void BoundaryElimination::expandMatvecResultInPlace(BCValuesPtr boundary_operato
245251
HYPRE_Complex* result) const {
246252
for (HYPRE_Int bnum = 0; bnum < nb; bnum++) {
247253
for (HYPRE_Int anum = aoffset_array[bnum]; anum < aoffset_array[bnum + 1]; anum++) {
248-
HYPRE_Int aknum = aknum_array[anum];
254+
const HYPRE_Int aknum = aknum_array[anum];
249255
result[aknum] +=
250256
aki_array[anum] * boundary_operator_values->data[bnum] / bii_array[bnum];
251257
}

0 commit comments

Comments
 (0)