Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/NGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,6 @@ int run_ngen(int argc, char* argv[], int mpi_num_procs, int mpi_rank) {
ss.str("");
std::ios::sync_with_stdio(false);

#if NGEN_WITH_PYTHON
// Start Python interpreter via the manager singleton
// Need to bind to a variable so that the underlying reference count
// is incremented, this essentially becomes the global reference to keep
// the interpreter alive till the end of `main`
auto _interp = utils::ngenPy::InterpreterUtil::getInstance();
// utils::ngenPy::InterpreterUtil::getInstance();
#endif // NGEN_WITH_PYTHON

// Pull a few "options" form the cli input, this is a temporary solution to CLI parsing!
// Use "positional args"
// arg 0 is program name
Expand Down Expand Up @@ -819,6 +810,8 @@ int main(int argc, char* argv[]) {
int mpi_num_procs = 1;
// Define in the non-MPI case so that we don't need to conditionally compile `if (mpi_rank == 0)`
int mpi_rank = 0;
// exit code result of the simulation run
int result;

#if NGEN_WITH_MPI
// initialize MPI if needed
Expand All @@ -832,15 +825,22 @@ int main(int argc, char* argv[]) {
// Need to bind to a variable so that the underlying reference count
// is incremented, this essentially becomes the global reference to keep
// the interpreter alive till the end of `main`
auto _interp = utils::ngenPy::InterpreterUtil::getInstance();
#endif // NGEN_WITH_PYTHON

int result = run_ngen(argc, argv, mpi_num_procs, mpi_rank);

#if NGEN_WITH_PYTHON
auto interp = utils::ngenPy::InterpreterUtil::getInstance();
try {
result = run_ngen(argc, argv, mpi_num_procs, mpi_rank);
} catch (...) {
// If any uncaught exception happens,
// explictly destroy the interpreter to ensure any
// python atexit registered actions will trigger.
interp.reset();
// Then, throw the original error.
throw;
}
// explicitly destroy the interpreter before calling MPI_Finalize
_interp.reset();
#endif
interp.reset();
#else
result = run_ngen(argc, argv, mpi_num_procs, mpi_rank);
#endif // NGEN_WITH_PYTHON

#if NGEN_WITH_MPI
MPI_Finalize();
Expand Down
3 changes: 1 addition & 2 deletions src/forcing/ForcingsEngineDataProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ void assert_forcings_engine_requirements()
{
// Check that the python module is installed.
{
auto interpreter_ = utils::ngenPy::InterpreterUtil::getInstance();
try {
auto mod = interpreter_->getModule(forcings_engine_python_module);
auto mod = utils::ngenPy::InterpreterUtil::getPyModule(forcings_engine_python_module);
auto cls = mod.attr(forcings_engine_python_class).cast<py::object>();
} catch(std::exception& e) {
std::string msg = "Failed to initialize ForcingsEngine: ForcingsEngine python module is not installed or is not properly configured. (" +
Expand Down
Loading