From 66840c9e4f4e264691fe57e60c8fc04d69e8ba91 Mon Sep 17 00:00:00 2001 From: Sophie Blondel Date: Wed, 29 Jan 2025 15:02:09 -0500 Subject: [PATCH] Free memory before starting a new loop (#206). --- xolotl/core/include/xolotl/core/network/Reaction.h | 5 +++-- xolotl/solver/src/PetscSolver.cpp | 9 +++++++++ xolotl/solver/src/monitor/PetscMonitor1D.cpp | 3 +++ xolotl/util/include/xolotl/util/Atomics.h | 6 +++--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/xolotl/core/include/xolotl/core/network/Reaction.h b/xolotl/core/include/xolotl/core/network/Reaction.h index 3e90fe81..cd8112bc 100644 --- a/xolotl/core/include/xolotl/core/network/Reaction.h +++ b/xolotl/core/include/xolotl/core/network/Reaction.h @@ -19,7 +19,8 @@ namespace core { namespace network { -template class ReactionNetwork; +template +class ReactionNetwork; /** * @brief General reaction class where * reactants become products with a given rate @@ -30,7 +31,7 @@ template class ReactionNetwork; template class Reaction { - friend class ReactionNetwork; + friend class ReactionNetwork; using Types = detail::ReactionNetworkTypes; using Props = detail::ReactionNetworkProperties; diff --git a/xolotl/solver/src/PetscSolver.cpp b/xolotl/solver/src/PetscSolver.cpp index 82fa4a22..1700f66b 100644 --- a/xolotl/solver/src/PetscSolver.cpp +++ b/xolotl/solver/src/PetscSolver.cpp @@ -177,6 +177,13 @@ PetscSolver::initialize(int loop, double time, DM oldDA, Vec oldC) PetscCallVoid(PetscInitialize(NULL, NULL, NULL, help)); petscInitializedHere = true; } + // Free memory + if (loop > 0) { + PetscCallVoid(PetscOptionsDestroy(&petscOptions)); + PetscCallVoid(VecDestroy(&C)); + PetscCallVoid(TSDestroy(&ts)); + PetscCallVoid(DMDestroy(&da)); + } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create the solver options @@ -358,7 +365,9 @@ PetscSolver::solve() while (reason == TS_CONVERGED_USER) { // The interface already initialized the first loop if (loopNumber > 0) { + PetscCallVoid(PetscOptionsPop()); initialize(loopNumber, time, oldDA, oldC); + PetscCallVoid(PetscOptionsPush(petscOptions)); } /* diff --git a/xolotl/solver/src/monitor/PetscMonitor1D.cpp b/xolotl/solver/src/monitor/PetscMonitor1D.cpp index 13fb9785..fa5a86a0 100644 --- a/xolotl/solver/src/monitor/PetscMonitor1D.cpp +++ b/xolotl/solver/src/monitor/PetscMonitor1D.cpp @@ -1951,6 +1951,9 @@ PetscMonitor1D::postEventFunction(TS ts, PetscInt nevents, PetscInt eventList[], if (nGridPoints > 0) { _solverHandler->setSurfaceOffset(nGridPoints); PetscCall(TSSetConvergedReason(ts, TS_CONVERGED_USER)); + + // Restore the solutionArray + PetscCall(DMDAVecRestoreArrayDOF(da, solution, &solutionArray)); PetscFunctionReturn(0); } } diff --git a/xolotl/util/include/xolotl/util/Atomics.h b/xolotl/util/include/xolotl/util/Atomics.h index 21bb5e8a..eb6e69f6 100644 --- a/xolotl/util/include/xolotl/util/Atomics.h +++ b/xolotl/util/include/xolotl/util/Atomics.h @@ -11,7 +11,7 @@ KOKKOS_INLINE_FUNCTION bool atomicCompareExchangeStrong(T* ptr, T expected, T desired) { - return expected == Kokkos::atomic_compare_exchange(ptr, expected, desired); -} -} + return expected == Kokkos::atomic_compare_exchange(ptr, expected, desired); } +} // namespace util +} // namespace xolotl