Skip to content

Commit

Permalink
Matching PetscFunctionReturn/PetscFunctionBegin
Browse files Browse the repository at this point in the history
Refs #27784
  • Loading branch information
lindsayad committed Jun 6, 2024
1 parent 24002af commit 07f72be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions framework/src/utils/PetscSupport.C
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ petscSetOptions(const PetscOptions & po, const SolverParams & solver_params)
PetscErrorCode
petscSetupOutput(CommandLine * cmd_line)
{
PetscFunctionBegin;
char code[10] = {45, 45, 109, 111, 111, 115, 101};
const std::vector<std::string> argv = cmd_line->getArguments();
for (const auto & arg : argv)
Expand All @@ -290,6 +291,7 @@ petscNonlinearConverged(SNES snes,
SNESConvergedReason * reason,
void * ctx)
{
PetscFunctionBegin;
FEProblemBase & problem = *static_cast<FEProblemBase *>(ctx);

// Let's be nice and always check PETSc error codes.
Expand Down
8 changes: 8 additions & 0 deletions framework/src/utils/SlepcSupport.C
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ PCApply_MoosePC(PC pc, Vec x, Vec y)
PetscContainer container;
PetscErrorCode ierr;

PetscFunctionBegin;
ierr = PCGetOperators(pc, &Amat, &Pmat);
CHKERRQ(ierr);
ierr = PetscObjectQuery((PetscObject)Pmat, "formFunctionCtx", (PetscObject *)&container);
Expand Down Expand Up @@ -1228,6 +1229,7 @@ PCSetUp_MoosePC(PC pc)
Mat Amat, Pmat;
PetscContainer container;

PetscFunctionBegin;
ierr = PCGetOperators(pc, &Amat, &Pmat);
CHKERRQ(ierr);
ierr = PetscObjectQuery((PetscObject)Pmat, "formFunctionCtx", (PetscObject *)&container);
Expand Down Expand Up @@ -1268,6 +1270,7 @@ mooseSlepcStoppingTest(EPS eps,
PetscErrorCode ierr;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);

PetscFunctionBegin;
ierr = EPSStoppingBasic(eps, its, max_it, nconv, nev, reason, NULL);
LIBMESH_CHKERR(ierr);

Expand All @@ -1291,6 +1294,7 @@ mooseSlepcEPSGetSNES(EPS eps, SNES * snes)
PetscErrorCode ierr;
PetscBool same, nonlinear;

PetscFunctionBegin;
ierr = PetscObjectTypeCompare((PetscObject)eps, EPSPOWER, &same);
LIBMESH_CHKERR(ierr);

Expand All @@ -1316,6 +1320,7 @@ mooseSlepcEPSSNESSetUpOptionPrefix(EPS eps)
SNES snes;
const char * prefix = nullptr;

PetscFunctionBegin;
ierr = mooseSlepcEPSGetSNES(eps, &snes);
LIBMESH_CHKERR(ierr);
// There is an extra "eps_power" in snes that users do not like it.
Expand All @@ -1338,6 +1343,7 @@ mooseSlepcEPSSNESSetCustomizePC(EPS eps)
KSP ksp;
PC pc;

PetscFunctionBegin;
// Get SNES from EPS
ierr = mooseSlepcEPSGetSNES(eps, &snes);
LIBMESH_CHKERR(ierr);
Expand All @@ -1360,6 +1366,7 @@ mooseSlepcEPSSNESKSPSetPCSide(FEProblemBase & problem, EPS eps)
SNES snes;
KSP ksp;

PetscFunctionBegin;
// Get SNES from EPS
ierr = mooseSlepcEPSGetSNES(eps, &snes);
LIBMESH_CHKERR(ierr);
Expand Down Expand Up @@ -1387,6 +1394,7 @@ mooseSlepcEPSMonitor(EPS eps,
auto ierr = (PetscErrorCode)0;
PetscScalar eigenr, eigeni;

PetscFunctionBegin;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(mctx);
auto & console = eigen_problem->console();

Expand Down
17 changes: 17 additions & 0 deletions modules/optimization/src/executioners/OptimizeSolve.C
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ OptimizeSolve::taoSolve()
// Petsc error code to be checked after each petsc call
auto ierr = (PetscErrorCode)0;

PetscFunctionBegin;
// Initialize tao object
ierr = TaoCreate(_my_comm.get(), &_tao);
CHKERRQ(ierr);
Expand Down Expand Up @@ -339,6 +340,7 @@ OptimizeSolve::monitor(Tao tao, void * ctx)
PetscInt its;
PetscReal f, gnorm, cnorm, xdiff;

PetscFunctionBegin;
auto ierr = TaoGetSolutionStatus(tao, &its, &f, &gnorm, &cnorm, &xdiff, &reason);
CHKERRQ(ierr);

Expand All @@ -351,6 +353,7 @@ OptimizeSolve::monitor(Tao tao, void * ctx)
PetscErrorCode
OptimizeSolve::objectiveFunctionWrapper(Tao /*tao*/, Vec x, Real * objective, void * ctx)
{
PetscFunctionBegin;
auto * solver = static_cast<OptimizeSolve *>(ctx);

libMesh::PetscVector<Number> param(x, solver->_my_comm);
Expand All @@ -364,6 +367,7 @@ PetscErrorCode
OptimizeSolve::objectiveAndGradientFunctionWrapper(
Tao /*tao*/, Vec x, Real * objective, Vec gradient, void * ctx)
{
PetscFunctionBegin;
auto * solver = static_cast<OptimizeSolve *>(ctx);

libMesh::PetscVector<Number> param(x, solver->_my_comm);
Expand All @@ -380,6 +384,7 @@ OptimizeSolve::objectiveAndGradientFunctionWrapper(
PetscErrorCode
OptimizeSolve::hessianFunctionWrapper(Tao /*tao*/, Vec x, Mat /*hessian*/, Mat /*pc*/, void * ctx)
{
PetscFunctionBegin;
// Define Hessian-vector multiplication routine
auto * solver = static_cast<OptimizeSolve *>(ctx);
libMesh::PetscVector<Number> param(x, solver->_my_comm);
Expand All @@ -394,6 +399,8 @@ PetscErrorCode
OptimizeSolve::applyHessianWrapper(Mat H, Vec s, Vec Hs)
{
void * ctx;

PetscFunctionBegin;
auto ierr = MatShellGetContext(H, &ctx);
CHKERRQ(ierr);

Expand All @@ -406,6 +413,7 @@ OptimizeSolve::applyHessianWrapper(Mat H, Vec s, Vec Hs)
PetscErrorCode
OptimizeSolve::variableBoundsWrapper(Tao tao, Vec /*xl*/, Vec /*xu*/, void * ctx)
{
PetscFunctionBegin;
auto * solver = static_cast<OptimizeSolve *>(ctx);

PetscErrorCode ierr = solver->variableBounds(tao);
Expand Down Expand Up @@ -456,6 +464,7 @@ OptimizeSolve::gradientFunction(libMesh::PetscVector<Number> & gradient)
PetscErrorCode
OptimizeSolve::applyHessian(libMesh::PetscVector<Number> & s, libMesh::PetscVector<Number> & Hs)
{
PetscFunctionBegin;
TIME_SECTION("applyHessian", 2, "Hessian forward/adjoint solve");
// What happens for material inversion when the Hessian
// is dependent on the parameters? Deal with it later???
Expand Down Expand Up @@ -492,6 +501,7 @@ OptimizeSolve::applyHessian(libMesh::PetscVector<Number> & s, libMesh::PetscVect
PetscErrorCode
OptimizeSolve::variableBounds(Tao tao)
{
PetscFunctionBegin;
unsigned int sz = _obj_function->getNumParams();

libMesh::PetscVector<Number> xl(_my_comm, sz);
Expand All @@ -512,6 +522,7 @@ OptimizeSolve::variableBounds(Tao tao)
PetscErrorCode
OptimizeSolve::equalityFunctionWrapper(Tao /*tao*/, Vec /*x*/, Vec ce, void * ctx)
{
PetscFunctionBegin;
// grab the solver
auto * solver = static_cast<OptimizeSolve *>(ctx);
libMesh::PetscVector<Number> eq_con(ce, solver->_my_comm);
Expand All @@ -525,6 +536,7 @@ PetscErrorCode
OptimizeSolve::equalityGradientFunctionWrapper(
Tao /*tao*/, Vec /*x*/, Mat gradient_e, Mat /*gradient_epre*/, void * ctx)
{
PetscFunctionBegin;
// grab the solver
auto * solver = static_cast<OptimizeSolve *>(ctx);
libMesh::PetscMatrix<Number> grad_eq(gradient_e, solver->_my_comm);
Expand All @@ -538,6 +550,7 @@ OptimizeSolve::equalityGradientFunctionWrapper(
PetscErrorCode
OptimizeSolve::inequalityFunctionWrapper(Tao /*tao*/, Vec /*x*/, Vec ci, void * ctx)
{
PetscFunctionBegin;
// grab the solver
auto * solver = static_cast<OptimizeSolve *>(ctx);
libMesh::PetscVector<Number> ineq_con(ci, solver->_my_comm);
Expand All @@ -551,6 +564,7 @@ PetscErrorCode
OptimizeSolve::inequalityGradientFunctionWrapper(
Tao /*tao*/, Vec /*x*/, Mat gradient_i, Mat /*gradient_ipre*/, void * ctx)
{
PetscFunctionBegin;
// grab the solver
auto * solver = static_cast<OptimizeSolve *>(ctx);
libMesh::PetscMatrix<Number> grad_ineq(gradient_i, solver->_my_comm);
Expand All @@ -566,6 +580,7 @@ OptimizeSolve::taoALCreate()
{
auto ierr = (PetscErrorCode)0;

PetscFunctionBegin;
if (_obj_function->getNumEqCons())
{
// Create equality vector
Expand Down Expand Up @@ -640,6 +655,8 @@ PetscErrorCode
OptimizeSolve::taoALDestroy()
{
auto ierr = (PetscErrorCode)0;

PetscFunctionBegin;
if (_obj_function->getNumEqCons())
{
ierr = VecDestroy(&_ce);
Expand Down
3 changes: 3 additions & 0 deletions modules/stochastic_tools/src/utils/GaussianProcessHandler.C
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ GaussianProcessHandler::tuneHyperParamsTAO(const RealEigenMatrix & training_para
PetscErrorCode ierr;
Tao tao;

PetscFunctionBegin;
_training_params = &training_params;
_training_data = &training_data;

Expand Down Expand Up @@ -219,6 +220,7 @@ GaussianProcessHandler::tuneHyperParamsTAO(const RealEigenMatrix & training_para
PetscErrorCode
GaussianProcessHandler::formInitialGuessTAO(Vec theta_vec)
{
PetscFunctionBegin;
libMesh::PetscVector<Number> theta(theta_vec, _tao_comm);
_covariance_function->buildHyperParamMap(_hyperparam_map, _hyperparam_vec_map);
mapToPetscVec(_tuning_data, _hyperparam_map, _hyperparam_vec_map, theta);
Expand All @@ -243,6 +245,7 @@ PetscErrorCode
GaussianProcessHandler::formFunctionGradientWrapper(
Tao tao, Vec theta_vec, PetscReal * f, Vec grad_vec, void * ptr)
{
PetscFunctionBegin;
GaussianProcessHandler * GP_ptr = (GaussianProcessHandler *)ptr;
GP_ptr->formFunctionGradient(tao, theta_vec, f, grad_vec);
PetscFunctionReturn(PETSC_SUCCESS);
Expand Down

0 comments on commit 07f72be

Please sign in to comment.