Skip to content

Commit

Permalink
Adding LoadBalancer::AbstractFactory (c.f. UT-CHG#61)
Browse files Browse the repository at this point in the history
Abstract factory manages the respective load balancer factories, which
are at the moment `LoadBalancer::Random`. At the moment, the rebalance
requests are getting properly issued. The next step will be building a
proper world model, and actually issuing migrates.
  • Loading branch information
bremerm31 committed Jun 27, 2018
1 parent 9c3b56c commit 1b1f6e5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 36 deletions.
13 changes: 11 additions & 2 deletions source/simulation/hpx_simulation_unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "../communication/hpx_communicator.hpp"

#include "writer.hpp"
#include "load_balancer/base_model.hpp"
#include "load_balancer/abstract_load_balancer_factory.hpp"

template <typename ProblemType>
class HPXSimulationUnit : public hpx::components::simple_component_base<HPXSimulationUnit<ProblemType>> {
Expand All @@ -19,6 +21,7 @@ class HPXSimulationUnit : public hpx::components::simple_component_base<HPXSimul
typename ProblemType::ProblemMeshType mesh;
typename ProblemType::ProblemInputType problem_input;
HPXCommunicator communicator;
std::unique_ptr<LoadBalancer::SubmeshModel> submesh_model = nullptr;

public:
HPXSimulationUnit() = default;
Expand All @@ -32,7 +35,11 @@ class HPXSimulationUnit : public hpx::components::simple_component_base<HPXSimul
this->communicator = HPXCommunicator(input.mesh_file_name.substr(0, input.mesh_file_name.find_last_of('.')) + ".dbmd",
locality_id,
submesh_id);

this->submesh_model = LoadBalancer::AbstractFactory::create_submesh_model<ProblemType>(locality_id,submesh_id);
assert(this->submesh_model);
if ( locality_id == 0 && submesh_id == 0 ) {
std::cout << "Making submesh model on locality = 0 submesh_id = 0" << std::endl;
}
ProblemType::initialize_problem_parameters(this->problem_input);

input.ReadMesh();
Expand Down Expand Up @@ -134,6 +141,8 @@ void HPXSimulationUnit<ProblemType>::Parse() {

template <typename ProblemType>
hpx::future<void> HPXSimulationUnit<ProblemType>::Stage() {
this->Parse();

if (this->writer.WritingVerboseLog()) {
this->writer.GetLogFile() << "Current (time, stage): (" << this->stepper.get_t_at_curr_stage() << ','
<< this->stepper.get_stage() << ')' << std::endl << "Starting work before receive"
Expand Down Expand Up @@ -263,7 +272,6 @@ hpx::future<void> HPXSimulationUnit<ProblemType>::Step() {
hpx::future<void> step_future = hpx::make_ready_future();

for (uint stage = 0; stage < this->stepper.get_num_stages(); stage++) {
this->Parse();

step_future = step_future.then([this](auto&&) {
return this->Stage();
Expand All @@ -275,6 +283,7 @@ hpx::future<void> HPXSimulationUnit<ProblemType>::Step() {
}

return step_future.then([this](auto&&) {
this->submesh_model->InStep(0,0);
return this->SwapStates();
});
}
Expand Down
19 changes: 19 additions & 0 deletions source/simulation/load_balancer/abstract_load_balancer_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef ABSTRACT_LOAD_BALANCER_FACTORY_HPP
#define ABSTRACT_LOAD_BALANCER_FACTORY_HPP

#include "base_model.hpp"

namespace LoadBalancer {
class AbstractFactory {
public:
template <typename ProblemType>
static hpx::future<void> initialize_locality_and_world_models(const uint locality_id);

template <typename ProblemType>
static void reset_locality_and_world_models();

template <typename ProblemType>
static std::unique_ptr<SubmeshModel> create_submesh_model(uint locality_id, uint submesh_id);
};
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ABSTRACT_LOAD_BALANCER_FACTORY_IMPL_HPP
#define ABSTRACT_LOAD_BALANCER_FACTORY_IMPL_HPP

#include "abstract_load_balancer_factory.hpp"
#include "random.hpp"


namespace LoadBalancer {
template <typename ProblemType>
hpx::future<void> AbstractFactory::initialize_locality_and_world_models(const uint locality_id) {
return Random<ProblemType>::initialize_locality_and_world_models(locality_id);
}

template <typename ProblemType>
void AbstractFactory::reset_locality_and_world_models() {
return Random<ProblemType>::reset_locality_and_world_models();
}

template <typename ProblemType>
std::unique_ptr<SubmeshModel> AbstractFactory::create_submesh_model(uint locality_id, uint submesh_id) {
return Random<ProblemType>::create_submesh_model(locality_id, submesh_id);
}
}
#endif
25 changes: 25 additions & 0 deletions source/simulation/load_balancer/base_model.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef LOAD_BALANCER_BASE_MODEL_HPP
#define LOAD_BALANCER_BASE_MODEL_HPP

namespace LoadBalancer {
class SubmeshModel {
public:
SubmeshModel() = default;
SubmeshModel(uint locality_id, uint submesh_id)
: locality_id(locality_id), submesh_id(submesh_id) {}

virtual void InStep(uint64_t compute_cost, uint64_t memory_cost) {
std::cout << "Firing base_model::Instep()\n";
}

template <typename Archive>
void serialize(Archive& ar, unsigned) {
ar & locality_id & submesh_id;
}
HPX_SERIALIZATION_POLYMORPHIC(SubmeshModel);

protected:
uint locality_id, submesh_id;
};
}
#endif
16 changes: 0 additions & 16 deletions source/simulation/load_balancer/base_models.hpp

This file was deleted.

16 changes: 3 additions & 13 deletions source/simulation/load_balancer/load_balancer_headers.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
#ifndef SIMULATION_LOAD_BALANCER_HPP
#define SIMULATION_LOAD_BALANCER_HPP

#include "base_models.hpp"
#include "base_model.hpp"
#include "random.hpp"

namespace LoadBalancer {
template <typename ProblemType>
hpx::future<void> initialize_locality_and_world_models(const uint locality_id) {
return Random<ProblemType>::initialize_locality_and_world_models(locality_id);
}

template <typename ProblemType>
void reset_locality_and_world_models() {
Random<ProblemType>::reset_locality_and_world_models();
}
}
#include "abstract_load_balancer_factory.hpp" //added for clarity
#include "abstract_load_balancer_factory_impl.hpp"

#define DGSWEMV2_REGISTER_LOAD_BALANCERS(ProblemType)\
DGSWEMV2_REGISTER_RANDOM_LOAD_BALANCER(ProblemType);
Expand Down
28 changes: 24 additions & 4 deletions source/simulation/load_balancer/random.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LOAD_BALANCER_RANDOM_HPP
#define LOAD_BALANCER_RANDOM_HPP
#include "../../utilities/heartbeat.hpp"
#include "base_models.hpp"
#include "base_model.hpp"
#include "../hpx_simulation_unit.hpp"

#include <cstdlib>
Expand Down Expand Up @@ -36,21 +36,27 @@ class WorldModelClient final :
WorldModelClient(hpx::future<hpx::id_type>&&id) : BaseType(std::move(id)) {}

void MigrateOneSubmesh() {
using ActionType = typename WorldModel<ProblemType>::MigrateOneSubmesh;
using ActionType = typename WorldModel<ProblemType>::MigrateOneSubmeshAction;
hpx::apply<ActionType>(this->get_id());
}
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename ProblemType>
class SubmeshModel final : public LoadBalancer::SubmeshModel {
class SubmeshModel : public LoadBalancer::SubmeshModel {
public:
using BaseType = LoadBalancer::SubmeshModel;

SubmeshModel() = default;
SubmeshModel(const std::chrono::duration<double>& rebalance_period,
uint locality_id, uint submesh_id);

void InStep(uint64_t compute_cost, uint64_t memory_cost);

template <typename Archive>
void serialize(Archive& ar, unsigned);
HPX_SERIALIZATION_POLYMORPHIC(SubmeshModel<ProblemType>);

private:
Utilities::HeartBeat beat;
};
Expand All @@ -66,6 +72,7 @@ struct Random {
static WorldModelClient world_model_client;
static hpx::future<void> initialize_locality_and_world_models(const uint locality_id);
static void reset_locality_and_world_models();
static std::unique_ptr<LoadBalancer::SubmeshModel> create_submesh_model(uint locality_id, uint submesh_id);
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -98,10 +105,19 @@ SubmeshModel<ProblemType>::SubmeshModel(const std::chrono::duration<double>& reb

template <typename ProblemType>
void SubmeshModel<ProblemType>::InStep(uint64_t, uint64_t) {
if ( locality_id == 0 && submesh_id == 0 && beat.Thump() ) {
if ( this->locality_id == 0 && this->submesh_id == 0 && this->beat.Thump() ) {
std::cout << "In submeshmode->Instep()\n";
assert(Random<ProblemType>::world_model_client);
Random<ProblemType>::world_model_client.MigrateOneSubmesh();
}
}

template <typename ProblemType>
template <typename Archive>
void SubmeshModel<ProblemType>::serialize(Archive& ar, unsigned) {
ar & hpx::serialization::base_object<LoadBalancer::SubmeshModel>(*this);
ar & beat;
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -128,7 +144,11 @@ hpx::future<void> Random<ProblemType>::initialize_locality_and_world_models(cons
template <typename ProblemType>
void Random<ProblemType>::reset_locality_and_world_models() {
Random<ProblemType>::world_model_client = WorldModelClient();
}

template <typename ProblemType>
std::unique_ptr<LoadBalancer::SubmeshModel> Random<ProblemType>::create_submesh_model(uint locality_id, uint submesh_id) {
return std::make_unique<Random<ProblemType>::SubmeshModel>(std::chrono::duration<double>(2.), locality_id, submesh_id);
}
}
#endif
4 changes: 3 additions & 1 deletion source/simulation/simulation_RKDG/rkdg_simulation_hpx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ HPXSimulation<ProblemType>::HPXSimulation(const std::string& input_string) {
const uint locality_id = hpx::get_locality_id();
const hpx::naming::id_type here = hpx::find_here();

hpx::future<void> lb_future = LoadBalancer::AbstractFactory::initialize_locality_and_world_models<ProblemType>(locality_id);

InputParameters<typename ProblemType::ProblemInputType> input(input_string);

hpx::future<void> lb_future = LoadBalancer::initialize_locality_and_world_models<ProblemType>(locality_id);
Expand Down Expand Up @@ -107,7 +109,7 @@ hpx::future<void> HPXSimulation<ProblemType>::Run() {
}
}
return hpx::when_all(simulation_futures).then([](auto&&) {
LoadBalancer::reset_locality_and_world_models<ProblemType>();
LoadBalancer::AbstractFactory::reset_locality_and_world_models<ProblemType>();
});
}

Expand Down

0 comments on commit 1b1f6e5

Please sign in to comment.