Skip to content

Commit

Permalink
Apply clang-format on C++ files
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-ballarin committed Nov 27, 2023
1 parent 50ec1be commit 2f3b57f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 79 deletions.
114 changes: 63 additions & 51 deletions rbnicsx/_cpp/rbnicsx/_backends/frobenius_inner_product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,77 @@

PetscReal rbnicsx::_backends::frobenius_inner_product(Mat a, Mat b)
{
PetscInt start_a, end_a, ncols_a, start_b, end_b, ncols_b;
PetscErrorCode ierr;
const PetscInt *cols_a, *cols_b;
const PetscScalar *vals_a, *vals_b;
PetscScalar sum(0.);
PetscInt start_a, end_a, ncols_a, start_b, end_b, ncols_b;
PetscErrorCode ierr;
const PetscInt *cols_a, *cols_b;
const PetscScalar *vals_a, *vals_b;
PetscScalar sum(0.);

ierr = MatGetOwnershipRange(a, &start_a, &end_a);
if (ierr != 0) petsc_error(ierr, __FILE__, "MatGetOwnershipRange");
ierr = MatGetOwnershipRange(a, &start_a, &end_a);
if (ierr != 0)
petsc_error(ierr, __FILE__, "MatGetOwnershipRange");
if (a != b)
{
ierr = MatGetOwnershipRange(b, &start_b, &end_b);
if (ierr != 0)
petsc_error(ierr, __FILE__, "MatGetOwnershipRange");
}
else
{
start_b = start_a;
end_b = end_a;
}
if (start_a != start_b)
petsc_error(ierr, __FILE__, "start_a != start_b");
if (end_a != end_b)
petsc_error(ierr, __FILE__, "end_a != end_b");

for (PetscInt i(start_a); i < end_a; i++)
{
ierr = MatGetRow(a, i, &ncols_a, &cols_a, &vals_a);
if (ierr != 0)
petsc_error(ierr, __FILE__, "MatGetRow");
if (a != b)
{
ierr = MatGetOwnershipRange(b, &start_b, &end_b);
if (ierr != 0) petsc_error(ierr, __FILE__, "MatGetOwnershipRange");
ierr = MatGetRow(b, i, &ncols_b, &cols_b, &vals_b);
if (ierr != 0)
petsc_error(ierr, __FILE__, "MatGetRow");
if (ncols_a != ncols_b)
petsc_error(ierr, __FILE__, "ncols_a != ncols_b");
}
else
{
start_b = start_a;
end_b = end_a;
ncols_b = ncols_a;
cols_b = cols_a;
vals_b = vals_a;
}
if (start_a != start_b) petsc_error(ierr, __FILE__, "start_a != start_b");
if (end_a != end_b) petsc_error(ierr, __FILE__, "end_a != end_b");

for (PetscInt i(start_a); i < end_a; i++)
for (PetscInt j(0); j < ncols_a; j++)
{
if (cols_a[j] != cols_b[j])
petsc_error(ierr, __FILE__, "cols_a[j] != cols_b[j]");
sum += vals_a[j] * vals_b[j];
}
if (a != b)
{
ierr = MatRestoreRow(b, i, &ncols_b, &cols_b, &vals_b);
if (ierr != 0)
petsc_error(ierr, __FILE__, "MatRestoreRow");
}
else
{
ierr = MatGetRow(a, i, &ncols_a, &cols_a, &vals_a);
if (ierr != 0) petsc_error(ierr, __FILE__, "MatGetRow");
if (a != b)
{
ierr = MatGetRow(b, i, &ncols_b, &cols_b, &vals_b);
if (ierr != 0) petsc_error(ierr, __FILE__, "MatGetRow");
if (ncols_a != ncols_b) petsc_error(ierr, __FILE__, "ncols_a != ncols_b");
}
else
{
ncols_b = ncols_a;
cols_b = cols_a;
vals_b = vals_a;
}
for (PetscInt j(0); j < ncols_a; j++)
{
if (cols_a[j] != cols_b[j]) petsc_error(ierr, __FILE__, "cols_a[j] != cols_b[j]");
sum += vals_a[j]*vals_b[j];
}
if (a != b)
{
ierr = MatRestoreRow(b, i, &ncols_b, &cols_b, &vals_b);
if (ierr != 0) petsc_error(ierr, __FILE__, "MatRestoreRow");
}
else
{
ncols_b = 0;
cols_b = NULL;
vals_b = NULL;
}
ierr = MatRestoreRow(a, i, &ncols_a, &cols_a, &vals_a);
if (ierr != 0) petsc_error(ierr, __FILE__, "MatRestoreRow");
ncols_b = 0;
cols_b = NULL;
vals_b = NULL;
}
ierr = MatRestoreRow(a, i, &ncols_a, &cols_a, &vals_a);
if (ierr != 0)
petsc_error(ierr, __FILE__, "MatRestoreRow");
}

PetscReal output(0.);
ierr = MPIU_Allreduce(&sum, &output, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) a));
if (ierr != 0) petsc_error(ierr, __FILE__, "MPIU_Allreduce");
return output;
PetscReal output(0.);
ierr = MPIU_Allreduce(&sum, &output, 1, MPIU_REAL, MPIU_SUM,
PetscObjectComm((PetscObject)a));
if (ierr != 0)
petsc_error(ierr, __FILE__, "MPIU_Allreduce");
return output;
}
8 changes: 4 additions & 4 deletions rbnicsx/_cpp/rbnicsx/_backends/frobenius_inner_product.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace rbnicsx::_backends
{
/// Frobenius inner product between two matrices, with code adapted from the
/// implementation of MatAXPY in PETSc.
PetscReal frobenius_inner_product(Mat a, Mat b);
}
/// Frobenius inner product between two matrices, with code adapted from the
/// implementation of MatAXPY in PETSc.
PetscReal frobenius_inner_product(Mat a, Mat b);
} // namespace rbnicsx::_backends
19 changes: 10 additions & 9 deletions rbnicsx/_cpp/rbnicsx/_backends/petsc_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

#include <rbnicsx/_backends/petsc_error.h>

void rbnicsx::_backends::petsc_error(int error_code, std::string filename, std::string petsc_function)
void rbnicsx::_backends::petsc_error(int error_code, std::string filename,
std::string petsc_function)
{
// Fetch PETSc error description
const char* desc;
PetscErrorMessage(error_code, &desc, nullptr);
// Fetch PETSc error description
const char* desc;
PetscErrorMessage(error_code, &desc, nullptr);

// Log detailed error info
throw std::runtime_error(
"Failed to successfully call PETSc function '" + petsc_function + "' in '" + filename + "'. "
+ "PETSc error code is: " + std ::to_string(error_code) + ", " + std::string(desc)
);
// Log detailed error info
throw std::runtime_error(
"Failed to successfully call PETSc function '" + petsc_function + "' in '"
+ filename + "'. " + "PETSc error code is: " + std ::to_string(error_code)
+ ", " + std::string(desc));
}
7 changes: 4 additions & 3 deletions rbnicsx/_cpp/rbnicsx/_backends/petsc_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace rbnicsx::_backends
{
/// Print error message for PETSc calls that return an error
void petsc_error(int error_code, std::string filename, std::string petsc_function);
}
/// Print error message for PETSc calls that return an error
void petsc_error(int error_code, std::string filename,
std::string petsc_function);
} // namespace rbnicsx::_backends
12 changes: 6 additions & 6 deletions rbnicsx/_cpp/rbnicsx/wrappers/_backends.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

#include <nanobind/nanobind.h>

#include <rbnicsx/_backends/petsc_casters.h>
#include <rbnicsx/_backends/frobenius_inner_product.h>
#include <rbnicsx/_backends/petsc_casters.h>

namespace nb = nanobind;

namespace rbnicsx_wrappers
{
void _backends(nb::module_& m)
{
m.def("frobenius_inner_product", &rbnicsx::_backends::frobenius_inner_product,
"Frobenius inner product between PETSc Mat objects.");
}
void _backends(nb::module_& m)
{
m.def("frobenius_inner_product", &rbnicsx::_backends::frobenius_inner_product,
"Frobenius inner product between PETSc Mat objects.");
}
} // namespace rbnicsx_wrappers
13 changes: 7 additions & 6 deletions rbnicsx/_cpp/rbnicsx/wrappers/rbnicsx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ namespace nb = nanobind;

namespace rbnicsx_wrappers
{
void _backends(nb::module_& m);
void _backends(nb::module_& m);
}

NB_MODULE(rbnicsx_cpp, m)
{
// Create module for C++ wrappers
m.doc() = "RBniCSx Python interface";
// Create module for C++ wrappers
m.doc() = "RBniCSx Python interface";

// Create internal backends submodule
nb::module_ _backends = m.def_submodule("_backends", "Internal backends module");
rbnicsx_wrappers::_backends(_backends);
// Create internal backends submodule
nb::module_ _backends
= m.def_submodule("_backends", "Internal backends module");
rbnicsx_wrappers::_backends(_backends);
}

0 comments on commit 2f3b57f

Please sign in to comment.