Skip to content

Commit

Permalink
Enable building without MPI
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonhagland committed Dec 1, 2024
1 parent fc23d55 commit a500868
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
21 changes: 14 additions & 7 deletions CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ list (APPEND MAIN_SOURCE_FILES
opm/simulators/flow/partitionCells.cpp
opm/simulators/flow/RSTConv.cpp
opm/simulators/flow/RegionPhasePVAverage.cpp
opm/simulators/flow/ReservoirCoupling.cpp
opm/simulators/flow/ReservoirCouplingMaster.cpp
opm/simulators/flow/ReservoirCouplingSlave.cpp
opm/simulators/flow/ReservoirCouplingSpawnSlaves.cpp
opm/simulators/flow/SimulatorReportBanners.cpp
opm/simulators/flow/SimulatorSerializer.cpp
opm/simulators/flow/SolutionContainers.cpp
Expand Down Expand Up @@ -843,9 +839,6 @@ list (APPEND PUBLIC_HEADER_FILES
opm/simulators/flow/priVarsPacking.hpp
opm/simulators/flow/RSTConv.hpp
opm/simulators/flow/RegionPhasePVAverage.hpp
opm/simulators/flow/ReservoirCouplingMaster.hpp
opm/simulators/flow/ReservoirCouplingSlave.hpp
opm/simulators/flow/ReservoirCouplingSpawnSlaves.hpp
opm/simulators/flow/SimulatorFullyImplicitBlackoil.hpp
opm/simulators/flow/SimulatorReportBanners.hpp
opm/simulators/flow/SimulatorSerializer.hpp
Expand Down Expand Up @@ -1164,3 +1157,17 @@ if(dune-alugrid_FOUND)
examples/fracture_discretefracture.cpp
)
endif()
if(USE_MPI)
list (APPEND MAIN_SOURCE_FILES
opm/simulators/flow/ReservoirCoupling.cpp
opm/simulators/flow/ReservoirCouplingMaster.cpp
opm/simulators/flow/ReservoirCouplingSlave.cpp
opm/simulators/flow/ReservoirCouplingSpawnSlaves.cpp
)
list (APPEND PUBLIC_HEADER_FILES
opm/simulators/flow/ReservoirCoupling.hpp
opm/simulators/flow/ReservoirCouplingMaster.hpp
opm/simulators/flow/ReservoirCouplingSlave.hpp
opm/simulators/flow/ReservoirCouplingSpawnSlaves.hpp
)
endif()
11 changes: 11 additions & 0 deletions opm/simulators/flow/FlowMain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <opm/simulators/flow/FlowUtils.hpp>
#include <opm/simulators/flow/SimulatorFullyImplicitBlackoil.hpp>

#if HAVE_MPI
#define RESERVOIR_COUPLING_ENABLED
#endif
#if HAVE_DUNE_FEM
#include <dune/fem/misc/mpimanager.hh>
#else
Expand Down Expand Up @@ -360,15 +363,23 @@ namespace Opm {
// Callback that will be called from runSimulatorInitOrRun_().
int runSimulatorRunCallback_()
{
#ifdef RESERVOIR_COUPLING_ENABLED
SimulatorReport report = simulator_->run(*simtimer_, this->argc_, this->argv_);
#else
SimulatorReport report = simulator_->run(*simtimer_);
#endif
runSimulatorAfterSim_(report);
return report.success.exit_status;
}

// Callback that will be called from runSimulatorInitOrRun_().
int runSimulatorInitCallback_()
{
#ifdef RESERVOIR_COUPLING_ENABLED
simulator_->init(*simtimer_, this->argc_, this->argv_);
#else
simulator_->init(*simtimer_);
#endif
return EXIT_SUCCESS;
}

Expand Down
9 changes: 7 additions & 2 deletions opm/simulators/flow/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ namespace Opm {
Main::Main(int argc, char** argv, bool ownMPI)
: argc_(argc), argv_(argv), ownMPI_(ownMPI)
{
#if HAVE_MPI
maybeSaveReservoirCouplingSlaveLogFilename_();
#endif
if (ownMPI_) {
initMPI();
}
Expand Down Expand Up @@ -128,6 +130,7 @@ Main::~Main()
#endif
}

#if HAVE_MPI
void Main::maybeSaveReservoirCouplingSlaveLogFilename_()
{
// If first command line argument is "--slave-log-file=<filename>",
Expand All @@ -150,7 +153,8 @@ void Main::maybeSaveReservoirCouplingSlaveLogFilename_()
}
}
}

#endif
#if HAVE_MPI
void Main::maybeRedirectReservoirCouplingSlaveOutput_() {
if (!this->reservoirCouplingSlaveOutputFilename_.empty()) {
std::string filename = this->reservoirCouplingSlaveOutputFilename_
Expand All @@ -171,6 +175,7 @@ void Main::maybeRedirectReservoirCouplingSlaveOutput_() {
close(fd);
}
}
#endif

void Main::setArgvArgc_(const std::string& filename)
{
Expand Down Expand Up @@ -204,9 +209,9 @@ void Main::initMPI()
FlowGenericVanguard::setCommunication(std::make_unique<Parallel::Communication>());

handleTestSplitCommunicatorCmdLine_();
maybeRedirectReservoirCouplingSlaveOutput_();

#if HAVE_MPI
maybeRedirectReservoirCouplingSlaveOutput_();
if (test_split_comm_ && FlowGenericVanguard::comm().size() > 1) {
int world_rank = FlowGenericVanguard::comm().rank();
int color = (world_rank == 0);
Expand Down
2 changes: 2 additions & 0 deletions opm/simulators/flow/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,9 @@ class Main
// To demonstrate run with non_world_comm
bool test_split_comm_ = false;
bool isSimulationRank_ = true;
#if HAVE_MPI
std::string reservoirCouplingSlaveOutputFilename_{};
#endif
#if HAVE_DAMARIS
bool enableDamarisOutput_ = false;
#endif
Expand Down
23 changes: 17 additions & 6 deletions opm/simulators/flow/SimulatorFullyImplicitBlackoil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <opm/common/ErrorMacros.hpp>

#if HAVE_MPI
#define RESERVOIR_COUPLING_ENABLED
#endif
#ifdef RESERVOIR_COUPLING_ENABLED
#include <opm/input/eclipse/Schedule/ResCoup/ReservoirCouplingInfo.hpp>
#include <opm/input/eclipse/Schedule/ResCoup/MasterGroup.hpp>
#include <opm/input/eclipse/Schedule/ResCoup/Slaves.hpp>
Expand Down Expand Up @@ -206,9 +209,15 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
/// \param[in,out] timer governs the requested reporting timesteps
/// \param[in,out] state state of reservoir: pressure, fluxes
/// \return simulation report, with timing data
#ifdef RESERVOIR_COUPLING_ENABLED
SimulatorReport run(SimulatorTimer& timer, int argc, char** argv)
{
init(timer, argc, argv);
#else
SimulatorReport run(SimulatorTimer& timer)
{
init(timer);
#endif
// Make cache up to date. No need for updating it in elementCtx.
// NB! Need to be at the correct step in case of restart
simulator_.setEpisodeIndex(timer.currentStepNum());
Expand All @@ -223,7 +232,7 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
return finalize();
}

#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
// This method should only be called if slave mode (i.e. Parameters::Get<Parameters::Slave>())
// is false. We try to determine if this is a normal flow simulation or a reservoir
// coupling master. It is a normal flow simulation if the schedule does not contain
Expand Down Expand Up @@ -256,12 +265,11 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
}
#endif

#ifdef RESERVOIR_COUPLING_ENABLED
// NOTE: The argc and argv will be used when launching a slave process
void init(SimulatorTimer &timer, int argc, char** argv)
{
#if HAVE_MPI
auto slave_mode = Parameters::Get<Parameters::Slave>();
FlowGenericVanguard::comm().barrier();
if (slave_mode) {
this->reservoirCouplingSlave_ =
std::make_unique<ReservoirCouplingSlave>(
Expand All @@ -283,6 +291,9 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
);
}
}
#else
void init(SimulatorTimer &timer)
{
#endif
simulator_.setEpisodeIndex(-1);

Expand All @@ -306,7 +317,7 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
else {
adaptiveTimeStepping_ = std::make_unique<TimeStepper>(unitSystem, max_next_tstep, terminalOutput_);
}
#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
if (this->reservoirCouplingSlave_) {
adaptiveTimeStepping_->setReservoirCouplingSlave(this->reservoirCouplingSlave_.get());
}
Expand Down Expand Up @@ -424,7 +435,7 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
return false;
};
tuningUpdater();
#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
if (this->reservoirCouplingMaster_) {
this->reservoirCouplingMaster_->maybeSpawnSlaveProcesses(timer.currentStepNum());
}
Expand Down Expand Up @@ -696,7 +707,7 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
std::unique_ptr<TimeStepper> adaptiveTimeStepping_;


#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
bool slaveMode_{false};
std::unique_ptr<ReservoirCouplingMaster> reservoirCouplingMaster_{nullptr};
std::unique_ptr<ReservoirCouplingSlave> reservoirCouplingSlave_{nullptr};
Expand Down
14 changes: 10 additions & 4 deletions opm/simulators/timestepping/AdaptiveTimeStepping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <opm/simulators/timestepping/TimeStepControlInterface.hpp>

#if HAVE_MPI
#define RESERVOIR_COUPLING_ENABLED
#endif
#ifdef RESERVOIR_COUPLING_ENABLED
#include <opm/simulators/flow/ReservoirCoupling.hpp>
#include <opm/simulators/flow/ReservoirCouplingMaster.hpp>
#include <opm/simulators/flow/ReservoirCouplingSlave.hpp>
Expand Down Expand Up @@ -92,8 +95,9 @@ class AdaptiveTimeStepping
{
private:
using Scalar = GetPropType<TypeTag, Properties::Scalar>;
#ifdef RESERVOIR_COUPLING_ENABLED
using TimePoint = ReservoirCoupling::TimePoint;

#endif
template <class Solver>
class SolutionTimeErrorSolverWrapper : public RelativeChangeInterface
{
Expand Down Expand Up @@ -131,10 +135,10 @@ class AdaptiveTimeStepping
void maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double originalTimeStep);
bool maybeUpdateTuning_() const;
double maxTimeStep_() const;
SimulatorReport runStepOriginal_();
#ifdef RESERVOIR_COUPLING_ENABLED
ReservoirCouplingMaster& reservoirCouplingMaster_();
ReservoirCouplingSlave& reservoirCouplingSlave_();
SimulatorReport runStepOriginal_();
#if HAVE_MPI
SimulatorReport runStepReservoirCouplingMaster_();
SimulatorReport runStepReservoirCouplingSlave_();
#endif
Expand Down Expand Up @@ -221,8 +225,10 @@ class AdaptiveTimeStepping
bool operator==(const AdaptiveTimeStepping<TypeTag>& rhs);

static void registerParameters();
#ifdef RESERVOIR_COUPLING_ENABLED
void setReservoirCouplingMaster(ReservoirCouplingMaster *reservoir_coupling_master);
void setReservoirCouplingSlave(ReservoirCouplingSlave *reservoir_coupling_slave);
#endif
void setSuggestedNextStep(const double x);
double suggestedNextStep() const;

Expand Down Expand Up @@ -280,7 +286,7 @@ class AdaptiveTimeStepping

//! < shut problematic wells when time step size in days are less than this
double min_time_step_before_shutting_problematic_wells_;
#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
ReservoirCouplingMaster *reservoir_coupling_master_ = nullptr;
ReservoirCouplingSlave *reservoir_coupling_slave_ = nullptr;
#endif
Expand Down
12 changes: 7 additions & 5 deletions opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ registerParameters()
detail::registerAdaptiveParameters();
}

#ifdef RESERVOIR_COUPLING_ENABLED
template<class TypeTag>
void
AdaptiveTimeStepping<TypeTag>::
Expand All @@ -161,6 +162,7 @@ setReservoirCouplingSlave(ReservoirCouplingSlave *reservoir_coupling_slave)
{
this->reservoir_coupling_slave_ = reservoir_coupling_slave;
}
#endif

/** \brief step method that acts like the solver::step method
in a sub cycle of time steps
Expand Down Expand Up @@ -487,7 +489,7 @@ SimulatorReport
AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
run()
{
#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
if (isReservoirCouplingSlave_() && reservoirCouplingSlave_().activated()) {
return runStepReservoirCouplingSlave_();
}
Expand Down Expand Up @@ -576,7 +578,7 @@ runStepOriginal_()
return substepIteration.run();
}

#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
template <class TypeTag>
template <class Solver>
ReservoirCouplingMaster&
Expand All @@ -587,7 +589,7 @@ reservoirCouplingMaster_()
}
#endif

#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
template <class TypeTag>
template <class Solver>
ReservoirCouplingSlave&
Expand All @@ -598,7 +600,7 @@ reservoirCouplingSlave_()
}
#endif

#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
template <class TypeTag>
template <class Solver>
SimulatorReport
Expand Down Expand Up @@ -645,7 +647,7 @@ runStepReservoirCouplingMaster_()
}
#endif

#if HAVE_MPI
#ifdef RESERVOIR_COUPLING_ENABLED
template <class TypeTag>
template <class Solver>
SimulatorReport
Expand Down

0 comments on commit a500868

Please sign in to comment.