Skip to content

Commit 09466ba

Browse files
authored
Merge pull request #3973 from farscape-project/libmeshpetsccall
Refactor with LibmeshPetscCall
2 parents a5af2ed + fb0abb5 commit 09466ba

23 files changed

+917
-1685
lines changed

examples/eigenproblems/eigenproblems_ex4/eigenproblems_ex4.C

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -141,57 +141,41 @@ main(int argc, char ** argv)
141141
// Prints information about the system to the screen.
142142
equation_systems.print_info();
143143

144-
auto ierr = PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_type", "power");
145-
CHKERRQ(ierr);
146-
ierr = PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_update", "1");
147-
CHKERRQ(ierr);
148-
ierr = PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_nonlinear", "1");
149-
CHKERRQ(ierr);
150-
ierr = PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_max_it", "1");
151-
CHKERRQ(ierr);
152-
ierr = PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_snes_mf_operator", "1");
153-
CHKERRQ(ierr);
154-
ierr = PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_pc_type", "lu");
155-
CHKERRQ(ierr);
144+
LibmeshPetscCallQ(PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_type", "power"));
145+
LibmeshPetscCallQ(PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_update", "1"));
146+
LibmeshPetscCallQ(PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_nonlinear", "1"));
147+
LibmeshPetscCallQ(PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_max_it", "1"));
148+
LibmeshPetscCallQ(PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_snes_mf_operator", "1"));
149+
LibmeshPetscCallQ(PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-eps_power_pc_type", "lu"));
156150

157151
//
158152
// Set function/operator callback functions
159153
//
160154

161155
auto & A = static_cast<PetscShellMatrix<Number> &>(eigen_system.get_shell_matrix_A());
162156
auto Amat = A.mat();
163-
ierr = PetscObjectComposeFunction((PetscObject)Amat, "formFunction", form_functionA);
164-
CHKERRQ(ierr);
165-
ierr = PetscObjectComposeFunction((PetscObject)Amat, "formJacobian", form_matrixA);
166-
CHKERRQ(ierr);
157+
LibmeshPetscCallQ(PetscObjectComposeFunction((PetscObject)Amat, "formFunction", form_functionA));
158+
LibmeshPetscCallQ(PetscObjectComposeFunction((PetscObject)Amat, "formJacobian", form_matrixA));
167159

168160
auto & B = static_cast<PetscShellMatrix<Number> &>(eigen_system.get_shell_matrix_B());
169161
auto Bmat = B.mat();
170-
ierr = PetscObjectComposeFunction((PetscObject)Bmat, "formFunction", form_functionB);
171-
CHKERRQ(ierr);
162+
LibmeshPetscCallQ(PetscObjectComposeFunction((PetscObject)Bmat, "formFunction", form_functionB));
172163

173164
//
174165
// Set function/operator callback function contexts
175166
//
176167

177168
PetscContainer container;
178-
ierr = PetscContainerCreate(equation_systems.comm().get(), &container);
179-
CHKERRQ(ierr);
180-
ierr = PetscContainerSetPointer(container, &equation_systems);
181-
CHKERRQ(ierr);
182-
ierr = PetscObjectCompose((PetscObject)Amat, "formFunctionCtx", (PetscObject)container);
183-
CHKERRQ(ierr);
184-
ierr = PetscObjectCompose((PetscObject)Amat, "formJacobianCtx", (PetscObject)container);
185-
CHKERRQ(ierr);
186-
ierr = PetscObjectCompose((PetscObject)Bmat, "formFunctionCtx", (PetscObject)container);
187-
CHKERRQ(ierr);
188-
ierr = PetscContainerDestroy(&container);
189-
CHKERRQ(ierr);
169+
LibmeshPetscCallQ(PetscContainerCreate(equation_systems.comm().get(), &container));
170+
LibmeshPetscCallQ(PetscContainerSetPointer(container, &equation_systems));
171+
LibmeshPetscCallQ(PetscObjectCompose((PetscObject)Amat, "formFunctionCtx", (PetscObject)container));
172+
LibmeshPetscCallQ(PetscObjectCompose((PetscObject)Amat, "formJacobianCtx", (PetscObject)container));
173+
LibmeshPetscCallQ(PetscObjectCompose((PetscObject)Bmat, "formFunctionCtx", (PetscObject)container));
174+
LibmeshPetscCallQ(PetscContainerDestroy(&container));
190175

191176
// Set the initial space
192177
Vec initial_space;
193-
ierr = MatCreateVecs(Amat, &initial_space, nullptr);
194-
CHKERRQ(ierr);
178+
LibmeshPetscCallQ(MatCreateVecs(Amat, &initial_space, nullptr));
195179
PetscVector<Number> wrapped_initial_space(initial_space, equation_systems.comm());
196180
wrapped_initial_space.add(1);
197181
wrapped_initial_space.close();
@@ -221,8 +205,7 @@ main(int argc, char ** argv)
221205
else
222206
libMesh::out << "WARNING: Solver did not converge!\n" << nconv << std::endl;
223207

224-
ierr = VecDestroy(&initial_space);
225-
CHKERRQ(ierr);
208+
LibmeshPetscCallQ(VecDestroy(&initial_space));
226209

227210
// All done.
228211
return 0;
@@ -578,13 +561,11 @@ form_matrixA(SNES /*snes*/, Vec x, Mat jac, Mat pc, void * ctx)
578561

579562
PetscBool pisshell, jismffd;
580563

581-
auto ierr = PetscObjectTypeCompare((PetscObject)pc, MATSHELL, &pisshell);
582-
CHKERRQ(ierr);
564+
LibmeshPetscCallQ(PetscObjectTypeCompare((PetscObject)pc, MATSHELL, &pisshell));
583565
if (pisshell)
584566
libmesh_error_msg("Generic preconditioning requires that an explicit matrix representation of "
585567
"the preconditioner be formed");
586-
ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
587-
CHKERRQ(ierr);
568+
LibmeshPetscCallQ(PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd));
588569
if (!jismffd)
589570
libmesh_error_msg("The operator should be formed matrix free");
590571

@@ -714,10 +695,8 @@ form_matrixA(SNES /*snes*/, Vec x, Mat jac, Mat pc, void * ctx)
714695
}
715696

716697
// The MFFD Jac still must have assemble called on it
717-
ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
718-
CHKERRQ(ierr);
719-
ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
720-
CHKERRQ(ierr);
698+
LibmeshPetscCallQ(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
699+
LibmeshPetscCallQ(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
721700

722701
PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
723702
}

examples/systems_of_equations/systems_of_equations_ex6/systems_of_equations_ex6.C

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,8 @@ public:
100100

101101
virtual void configure_solver()
102102
{
103-
auto ierr = LIBMESH_PETSC_SUCCESS;
104-
ierr = KSPSetType (_petsc_linear_solver.ksp(), const_cast<KSPType>(KSPCG));
105-
CHKERRABORT(_petsc_linear_solver.comm().get(), ierr);
106-
107-
ierr = PCSetType (_petsc_linear_solver.pc(), const_cast<PCType>(PCBJACOBI));
108-
CHKERRABORT(_petsc_linear_solver.comm().get(), ierr);
103+
LibmeshPetscCall2(_petsc_linear_solver.comm(), KSPSetType(_petsc_linear_solver.ksp(), const_cast<KSPType>(KSPCG)));
104+
LibmeshPetscCall2(_petsc_linear_solver.comm(), PCSetType(_petsc_linear_solver.pc(), const_cast<PCType>(PCBJACOBI)));
109105
}
110106

111107
// The linear solver object that we are configuring

include/numerics/petsc_matrix_base.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
#include <cstring>
5050

5151
#define semiparallel_only() do { if (this->initialized()) { const char * mytype; \
52-
auto semiparallel_only_ierr = MatGetType(this->_mat,&mytype); \
53-
LIBMESH_CHKERR(semiparallel_only_ierr); \
52+
LibmeshPetscCall(MatGetType(this->_mat,&mytype)); \
5453
if (!strcmp(mytype, MATSEQAIJ)) \
5554
parallel_object_only(); } } while (0)
5655
#define exceptionless_semiparallel_only() do { if (this->initialized()) { const char * mytype; \

include/numerics/petsc_shell_matrix.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ template <typename T>
146146
inline
147147
numeric_index_type PetscShellMatrix<T>::m () const
148148
{
149-
PetscErrorCode ierr;
150149
PetscInt m;
151150

152-
ierr = MatGetSize(_mat, &m, nullptr);
153-
LIBMESH_CHKERR(ierr);
151+
LibmeshPetscCall(MatGetSize(_mat, &m, nullptr));
154152

155153
return m;
156154
}
@@ -161,11 +159,9 @@ template <typename T>
161159
inline
162160
numeric_index_type PetscShellMatrix<T>::n () const
163161
{
164-
PetscErrorCode ierr;
165162
PetscInt n;
166163

167-
ierr = MatGetSize(_mat, nullptr, &n);
168-
LIBMESH_CHKERR(ierr);
164+
LibmeshPetscCall(MatGetSize(_mat, nullptr, &n));
169165

170166
return n;
171167
}
@@ -175,11 +171,9 @@ template <typename T>
175171
inline
176172
numeric_index_type PetscShellMatrix<T>::local_m () const
177173
{
178-
PetscErrorCode ierr;
179174
PetscInt m;
180175

181-
ierr = MatGetLocalSize(_mat, &m, nullptr);
182-
LIBMESH_CHKERR(ierr);
176+
LibmeshPetscCall(MatGetLocalSize(_mat, &m, nullptr));
183177

184178
return m;
185179
}
@@ -190,11 +184,9 @@ template <typename T>
190184
inline
191185
numeric_index_type PetscShellMatrix<T>::local_n () const
192186
{
193-
PetscErrorCode ierr;
194187
PetscInt n;
195188

196-
ierr = MatGetLocalSize(_mat, nullptr, &n);
197-
LIBMESH_CHKERR(ierr);
189+
LibmeshPetscCall(MatGetLocalSize(_mat, nullptr, &n));
198190

199191
return n;
200192
}
@@ -207,8 +199,7 @@ void PetscShellMatrix<T>::get_diagonal (NumericVector<T> & dest) const
207199
// Make sure the NumericVector passed in is really a PetscVector
208200
PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
209201

210-
PetscErrorCode ierr = MatGetDiagonal(_mat, petsc_dest.vec());
211-
LIBMESH_CHKERR(ierr);
202+
LibmeshPetscCall(MatGetDiagonal(_mat, petsc_dest.vec()));
212203
}
213204

214205

include/numerics/petsc_solver_exception.h

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,33 @@ class PetscSolverException : public SolverException
7272

7373

7474

75-
// Macro which we call after every PETSc function that returns an error code.
76-
#define LIBMESH_CHKERR(ierr) \
77-
do { \
78-
if (ierr != 0) { \
79-
throw PetscSolverException(ierr); \
80-
} } while (0)
75+
// Macro which we call in functions returning a datatype convertible
76+
// to PetscErrorCode after every PETSc function that returns an error code.
77+
#define LIBMESH_CHKERRQ(ierr) \
78+
do { \
79+
if (ierr != 0) \
80+
throw libMesh::PetscSolverException(ierr); \
81+
} while (0)
8182

8283
// Two-argument CHKERR macro that takes both a comm and an error
8384
// code. When exceptions are enabled, the comm is not used for
84-
// anything, so we libmesh_ignore() it. This macro is useful when you
85-
// need to call LIBMESH_CHKERR but you're not in a ParallelObject.
86-
#define LIBMESH_CHKERR2(comm, ierr) \
85+
// anything, so we libmesh_ignore() it.
86+
#define LIBMESH_CHKERRA(comm, ierr) \
8787
do { \
8888
libmesh_ignore(comm); \
89-
LIBMESH_CHKERR(ierr); \
89+
LIBMESH_CHKERRQ(ierr); \
9090
} while (0)
9191

92+
// Remove me: for backward compatibility with MOOSE only
93+
#define LIBMESH_CHKERR(ierr) LIBMESH_CHKERRQ(ierr)
94+
#define LIBMESH_CHKERR2(comm, ierr) LIBMESH_CHKERRA(comm, ierr)
95+
9296
#else
9397

9498
// If we don't have exceptions enabled, just fall back on calling
95-
// PETSc's CHKERRABORT macro.
96-
#define LIBMESH_CHKERR(ierr) CHKERRABORT(this->comm().get(), ierr);
97-
98-
// Two argument version of the function above where you pass in the comm
99-
// instead of relying on it being available from the "this" pointer.
100-
#define LIBMESH_CHKERR2(comm, ierr) CHKERRABORT(comm.get(), ierr);
101-
102-
// Let's also be backwards-compatible with the old macro name.
103-
#define LIBMESH_CHKERRABORT(ierr) LIBMESH_CHKERR(ierr)
99+
// PETSc's CHKERRQ or CHKERRABORT macros.
100+
#define LIBMESH_CHKERRQ(ierr) CHKERRQ(ierr);
101+
#define LIBMESH_CHKERRA(comm, ierr) CHKERRABORT(comm, ierr);
104102

105103
#endif
106104

@@ -109,34 +107,46 @@ class PetscSolverException : public SolverException
109107
inline \
110108
void Function ## BeginEnd(const Parallel::Communicator & comm, const Args&... args) \
111109
{ \
112-
PetscErrorCode ierr = LIBMESH_PETSC_SUCCESS; \
110+
PetscErrorCode ierr = LIBMESH_PETSC_SUCCESS; \
113111
ierr = Function ## Begin(args...); \
114-
LIBMESH_CHKERR2(comm, ierr); \
112+
LIBMESH_CHKERRA(comm.get(), ierr); \
115113
ierr = Function ## End(args...); \
116-
LIBMESH_CHKERR2(comm, ierr); \
114+
LIBMESH_CHKERRA(comm.get(), ierr); \
117115
}
118116

119117
PETSC_BEGIN_END(VecScatter) // VecScatterBeginEnd
120118
PETSC_BEGIN_END(MatAssembly) // MatAssemblyBeginEnd
121119
PETSC_BEGIN_END(VecAssembly) // VecAssemblyBeginEnd
122120
PETSC_BEGIN_END(VecGhostUpdate) // VecGhostUpdateBeginEnd
123121

124-
#define LibmeshPetscCall(...) \
125-
do \
126-
{ \
127-
PetscErrorCode libmesh_petsc_call_ierr; \
128-
libmesh_petsc_call_ierr = __VA_ARGS__; \
129-
LIBMESH_CHKERR(libmesh_petsc_call_ierr); \
122+
// To use instead of PETSc idioms that'd call CHKERRQ or PetscCall().
123+
#define LibmeshPetscCallQ(...) \
124+
do \
125+
{ \
126+
PetscErrorCode libmesh_petsc_call_ierr; \
127+
libmesh_petsc_call_ierr = __VA_ARGS__; \
128+
LIBMESH_CHKERRQ(libmesh_petsc_call_ierr); \
130129
} while (0)
131130

132-
#define LibmeshPetscCall2(comm, ...) \
133-
do \
134-
{ \
135-
PetscErrorCode libmesh_petsc_call_ierr; \
136-
libmesh_petsc_call_ierr = __VA_ARGS__; \
137-
LIBMESH_CHKERR2(comm, libmesh_petsc_call_ierr); \
131+
// To use instead of PETSc idioms that'd call CHKERRABORT or PetscCallAbort().
132+
#define LibmeshPetscCallA(comm, ...) \
133+
do \
134+
{ \
135+
PetscErrorCode libmesh_petsc_call_ierr; \
136+
libmesh_petsc_call_ierr = __VA_ARGS__; \
137+
LIBMESH_CHKERRA(comm, libmesh_petsc_call_ierr); \
138138
} while (0)
139139

140+
// Shortcut for LibmeshPetscCallA for use within a ParallelObject, i.e. when
141+
// we can rely on the communicator being available from the "this" pointer.
142+
#define LibmeshPetscCall(...) \
143+
LibmeshPetscCallA(this->comm().get(), __VA_ARGS__)
144+
145+
// Shortcut for LibmeshPetscCallA for use when we have a Parallel::Communicator
146+
// available instead of just a bare MPI communicator.
147+
#define LibmeshPetscCall2(comm, ...) \
148+
LibmeshPetscCallA(comm.get(), __VA_ARGS__)
149+
140150
#ifdef LIBMESH_ENABLE_EXCEPTIONS
141151
#define LibmeshPetscCallExternal(func, ...) \
142152
do { \

0 commit comments

Comments
 (0)