Skip to content

Commit

Permalink
Send slave start dates to master
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonhagland committed Sep 9, 2024
1 parent e0df4c1 commit 4fe8ce0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
32 changes: 32 additions & 0 deletions opm/simulators/flow/ReservoirCouplingMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ ReservoirCouplingMaster::ReservoirCouplingMaster(
// Public methods
// ------------------

void ReservoirCouplingMaster::receiveSimulationStartDateFromSlaves() {
this->slave_start_dates_.resize(this->num_slaves_);
if (this->comm_.rank() == 0) {
// Ensure that std::time_t is of type long since we are sending it over MPI with MPI_LONG
static_assert(std::is_same<std::time_t, long>::value, "std::time_t is not of type long");
for (unsigned int i = 0; i < this->master_slave_comm_.size(); i++) {
std::time_t start_date;
int result = MPI_Recv(
&start_date,
/*count=*/1,
/*datatype=*/MPI_LONG,
/*source_rank=*/0,
/*tag=*/static_cast<int>(MessageTag::SimulationStartDate),
*this->master_slave_comm_[i].get(),
MPI_STATUS_IGNORE
);
if (result != MPI_SUCCESS) {
OPM_THROW(std::runtime_error, "Failed to receive simulation start date from slave process");
}
this->slave_start_dates_[i] = start_date;
OpmLog::info(
fmt::format(
"Received simulation start date from slave process with name: {}. "
"Start date: {}", this->slave_names_[i], start_date
)
);
}
}
this->comm_.broadcast(this->slave_start_dates_.data(), this->num_slaves_, /*emitter_rank=*/0);
}

// NOTE: This functions is executed for all ranks, but only rank 0 will spawn
// the slave processes
void ReservoirCouplingMaster::spawnSlaveProcesses([[maybe_unused]]int argc, char **argv) {
Expand Down Expand Up @@ -101,6 +132,7 @@ void ReservoirCouplingMaster::spawnSlaveProcesses([[maybe_unused]]int argc, char
);
this->master_slave_comm_.push_back(std::move(master_slave_comm));
this->slave_names_.push_back(slave_name);
this->num_slaves_++;
}
}

Expand Down
10 changes: 6 additions & 4 deletions opm/simulators/flow/ReservoirCouplingMaster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ namespace Opm {

class ReservoirCouplingMaster {
public:
using MPI_Comm_Ptr = ReservoirCoupling::MPI_Comm_Ptr;
using MessageTag = ReservoirCoupling::MessageTag;

ReservoirCouplingMaster(const Parallel::Communication &comm, const Schedule &schedule);

void spawnSlaveProcesses(int argc, char **argv);

using MPI_Comm_Ptr = ReservoirCoupling::MPI_Comm_Ptr;
void receiveSimulationStartDateFromSlaves();

private:
std::vector<char *> getSlaveArgv(
Expand All @@ -52,9 +53,10 @@ class ReservoirCouplingMaster {

const Parallel::Communication &comm_;
const Schedule& schedule_;
// MPI communicators for the slave processes
std::vector<MPI_Comm_Ptr> master_slave_comm_;
std::size_t num_slaves_ = 0; // Initially zero, will be updated in spawnSlaveProcesses()
std::vector<MPI_Comm_Ptr> master_slave_comm_; // MPI communicators for the slave processes
std::vector<std::string> slave_names_;
std::vector<std::time_t> slave_start_dates_;
};

} // namespace Opm
Expand Down
33 changes: 23 additions & 10 deletions opm/simulators/flow/ReservoirCouplingSlave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,30 @@ ReservoirCouplingSlave::ReservoirCouplingSlave(
) :
comm_{comm},
schedule_{schedule}
{ }
{
this->slave_master_comm_ = MPI_Comm_Ptr(new MPI_Comm(MPI_COMM_NULL));
MPI_Comm_get_parent(this->slave_master_comm_.get());
if (*(this->slave_master_comm_) == MPI_COMM_NULL) {
OPM_THROW(std::runtime_error, "Slave process is not spawned by a master process");
}
}

void ReservoirCouplingSlave::sendSimulationStartDateToMasterProcess() {
// TODO: Implement this function next

//this->slave_master_comm_ = MPI_Comm_Ptr(new MPI_Comm(MPI_COMM_NULL));
//MPI_Comm_get_parent(this->slave_master_comm_.get());
//if (*(this->slave_master_comm_) == MPI_COMM_NULL) {
// OPM_THROW(std::runtime_error, "Slave process is not spawned by a master process");
//}
//MPI_Send(&start_date, 1, MPI_INT, 0, 0, parentcomm);
OpmLog::info("Sent simulation start date to master process");

if (this->comm_.rank() == 0) {
// Ensure that std::time_t is of type long since we are sending it over MPI with MPI_LONG
static_assert(std::is_same<std::time_t, long>::value, "std::time_t is not of type long");
std::time_t start_date = this->schedule_.getStartTime();
MPI_Send(
&start_date,
/*count=*/1,
/*datatype=*/MPI_LONG,
/*dest_rank=*/0,
/*tag=*/static_cast<int>(MessageTag::SimulationStartDate),
*this->slave_master_comm_
);
OpmLog::info("Sent simulation start date to master process from rank 0");
}
}

} // namespace Opm
5 changes: 3 additions & 2 deletions opm/simulators/flow/ReservoirCouplingSlave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef OPM_RESERVOIR_COUPLING_SLAVE_HPP
#define OPM_RESERVOIR_COUPLING_SLAVE_HPP

#include <opm/simulators/flow/ReservoirCouplingMaster.hpp>
#include <opm/simulators/flow/ReservoirCoupling.hpp>
#include <opm/input/eclipse/Schedule/Schedule.hpp>
#include <opm/simulators/utils/ParallelCommunication.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
Expand All @@ -33,7 +33,8 @@ namespace Opm {

class ReservoirCouplingSlave {
public:
using MPI_Comm_Ptr = ReservoirCouplingMaster::MPI_Comm_Ptr;
using MPI_Comm_Ptr = ReservoirCoupling::MPI_Comm_Ptr;
using MessageTag = ReservoirCoupling::MessageTag;

ReservoirCouplingSlave(const Parallel::Communication &comm, const Schedule &schedule);
void sendSimulationStartDateToMasterProcess();
Expand Down
1 change: 1 addition & 0 deletions opm/simulators/flow/SimulatorFullyImplicitBlackoil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class SimulatorFullyImplicitBlackoil : private SerializableSim
this->schedule()
);
this->reservoirCouplingMaster_->spawnSlaveProcesses(argc, argv);
this->reservoirCouplingMaster_->receiveSimulationStartDateFromSlaves();
}
}
#endif
Expand Down

0 comments on commit 4fe8ce0

Please sign in to comment.