From c771a55e40d2a41d607a0da1b67cd1f7d546ccc8 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Sun, 12 Dec 2010 23:51:22 +0100 Subject: [PATCH] Wip++... * add hostdata * add process::rev_neigh * implement process::send --- Makefile | 1 + NOTES | 9 +--- TODO | 7 ++++ communicator.cpp | 23 ++++------ communicator.h | 14 ++++--- cost_func.cpp | 3 +- hostdata.cpp | 44 ++++++++++++++++++++ hostdata.h | 33 +++++++++++++++ load_balance.h | 13 ------ main.cpp | 11 ++--- misc.h | 4 +- neighbor.cpp | 10 ++--- neighbor.h | 27 ++++++------ options.cpp | 3 +- process.cpp | 106 ++++++++++++++++++++++++++++++++++++++++------- process.h | 20 +++++++-- 16 files changed, 239 insertions(+), 89 deletions(-) create mode 100644 TODO create mode 100644 hostdata.cpp create mode 100644 hostdata.h delete mode 100644 load_balance.h diff --git a/Makefile b/Makefile index 61fab37..0f52ab6 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ SETLOCALVERSION := ./setlocalversion SRC.loba := main.cpp \ communicator.cpp \ cost_func.cpp \ + hostdata.cpp \ misc.cpp \ neighbor.cpp \ options.cpp \ diff --git a/NOTES b/NOTES index 489006c..fbb82a3 100644 --- a/NOTES +++ b/NOTES @@ -3,11 +3,9 @@ Process parameters: initial_load [neighbors...] Communications: - - two channels per host: normal and low_latency + - two channels per host: control and data -How shall we manage link failures ? - -Process model (?) +Process model while (there is something to do) { compute some task; @@ -23,6 +21,3 @@ Process model (?) nothing more to do. - how to manage link failures? - - - shall we retrieve pending tasks? - : Ideally, why shall. How? By using some acknowledgment? diff --git a/TODO b/TODO new file mode 100644 index 0000000..1f235b5 --- /dev/null +++ b/TODO @@ -0,0 +1,7 @@ +* implement loba_* algorithms (start with some trivial one) +* add loba algorithm selection (-a number ?) + +* implement automatic process topology + (line, ring, star, btree, clique, hypercube, etc..) +* implement automatic platform generation + (number of hosts, all connected, constant bandwidth/latency) diff --git a/communicator.cpp b/communicator.cpp index 0c64b5b..4b54161 100644 --- a/communicator.cpp +++ b/communicator.cpp @@ -1,9 +1,10 @@ +#include "communicator.h" + #include #include #include #include #include -#include "communicator.h" #include "simgrid_features.h" #include "misc.h" @@ -21,20 +22,14 @@ std::string message::to_string() } communicator::communicator() + : host((hostdata* )MSG_host_get_data(MSG_host_self())) + , ctrl_task(NULL) + , ctrl_comm(NULL) + , ctrl_close_is_last(false) + , data_task(NULL) + , data_comm(NULL) + , data_close_is_last(false) { - const char* hostname = MSG_host_get_name(MSG_host_self()); - - ctrl_mbox = hostname; - ctrl_mbox += "_ctrl"; - ctrl_task = NULL; - ctrl_comm = NULL; - ctrl_close_is_last = false; - - data_mbox = hostname; - data_mbox += "_data"; - data_task = NULL; - data_comm = NULL; - data_close_is_last = false; } communicator::~communicator() diff --git a/communicator.h b/communicator.h index 1e01677..4e0757a 100644 --- a/communicator.h +++ b/communicator.h @@ -6,6 +6,7 @@ #include #include #include +#include "hostdata.h" class message { public: @@ -38,23 +39,24 @@ public: void next_close_on_data_is_last(); private: + // Myself + const hostdata* host; + // List of pending send communications std::list sent_comm; // Control channel for receiving - std::string ctrl_mbox; - msg_comm_t ctrl_comm; m_task_t ctrl_task; + msg_comm_t ctrl_comm; bool ctrl_close_is_last; // Data channel for receiving - std::string data_mbox; - msg_comm_t data_comm; m_task_t data_task; + msg_comm_t data_comm; bool data_close_is_last; - const char* get_ctrl_mbox() const { return ctrl_mbox.c_str(); } - const char* get_data_mbox() const { return data_mbox.c_str(); } + const char* get_ctrl_mbox() const { return host->get_ctrl_mbox(); } + const char* get_data_mbox() const { return host->get_data_mbox(); } static void comm_push_in_dynar(xbt_dynar_t dynar, msg_comm_t comm); static bool comm_test_n_destroy(msg_comm_t comm); diff --git a/cost_func.cpp b/cost_func.cpp index 23e6a53..d102fd9 100644 --- a/cost_func.cpp +++ b/cost_func.cpp @@ -1,9 +1,10 @@ +#include "cost_func.h" + #include #include #include #include #include -#include "cost_func.h" cost_func::cost_func(const char* param) { diff --git a/hostdata.cpp b/hostdata.cpp new file mode 100644 index 0000000..96831f0 --- /dev/null +++ b/hostdata.cpp @@ -0,0 +1,44 @@ +#include "hostdata.h" + +#include +#include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu); + +hostdata* hostdata::instances = NULL; + +void hostdata::create() +{ + int nhosts = MSG_get_host_number(); + m_host_t* host_list = MSG_get_host_table(); + VERB1("Got %d hosts.", nhosts); + for (int i = 0; i < nhosts; i++) { + hostdata* h = new hostdata(host_list[i]); + MSG_host_set_data(host_list[i], h); + VERB2("Host #%d named \"%s\".", i, h->get_name()); + h->next = instances; + instances = h; + } + xbt_free(host_list); +} + +void hostdata::destroy() +{ + while (instances) { + hostdata* h = instances; + instances = h->next; + delete h; + } +} + +hostdata::hostdata(m_host_t host) + : next(NULL) + , name(MSG_host_get_name(host)) + , ctrl_mbox(std::string(name) + "_ctrl") + , data_mbox(std::string(name) + "_data") +{ +} + +hostdata::~hostdata() +{ +} diff --git a/hostdata.h b/hostdata.h new file mode 100644 index 0000000..82e133f --- /dev/null +++ b/hostdata.h @@ -0,0 +1,33 @@ +#ifndef HOSTDATA_H +#define HOSTDATA_H + +#include +#include + +class hostdata { +public: + static void create(); + static void destroy(); + + hostdata(m_host_t host); + ~hostdata(); + + const char* get_name() const { return name; } + const char* get_ctrl_mbox() const { return ctrl_mbox.c_str(); } + const char* get_data_mbox() const { return data_mbox.c_str(); } + +private: + // linked list of hostdata's, used by create/destroy + static hostdata* instances; + hostdata* next; + + const char* name; + std::string ctrl_mbox; + std::string data_mbox; +}; + +#endif // !HOSTDATA_H + +// Local variables: +// mode: c++ +// End: diff --git a/load_balance.h b/load_balance.h deleted file mode 100644 index d524aa7..0000000 --- a/load_balance.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef LOAD_BALANCE_H -#define LOAD_BALANCE_H - -class loba { -public: - -}; - -#endif // !LOAD_BALANCE_H - -// Local variables: -// mode: c++ -// End: diff --git a/main.cpp b/main.cpp index d48bd87..a3b790d 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "hostdata.h" #include "misc.h" #include "options.h" #include "process.h" @@ -71,14 +72,7 @@ int main(int argc, char* argv[]) // Create the platform and the application. MSG_create_environment(opt::platform_file); - if (LOG_ISENABLED(xbt_log_priority_verbose)) { - int n = MSG_get_host_number(); - m_host_t* h = MSG_get_host_table(); - VERB1("Got %d hosts.", n); - for (int i = 0; i < n; i++) - VERB2("Host #%d named \"%s\".", i, MSG_host_get_name(h[i])); - xbt_free(h); - } + hostdata::create(); MSG_launch_application(opt::application_file); exit_status = EXIT_FAILURE_SIMU; // ===== @@ -103,6 +97,7 @@ int main(int argc, char* argv[]) } // Clean the MSG simulation. + hostdata::destroy(); res = MSG_clean(); if (res != MSG_OK) { ERROR1("MSG_clean() failed with status %#x", res); diff --git a/misc.h b/misc.h index 4bae95a..aee128e 100644 --- a/misc.h +++ b/misc.h @@ -10,12 +10,12 @@ #define LOG_ISENABLED(priority) \ (_XBT_LOG_ISENABLEDV((*_XBT_LOGV(default)), (priority))) +/* Returns c-string "s" if n > 1, empty string "" otherwise. */ +#define ESSE(n) ((n) > 1 ? misc::str_esse : misc::str_nil) namespace misc { extern const char str_esse[]; extern const char str_nil[]; } -/* Returns c-string "s" if n > 1, empty string "" otherwise. */ -#define ESSE(n) ((n) > 1 ? misc::str_esse : misc::str_nil) #endif // !MISC_H diff --git a/neighbor.cpp b/neighbor.cpp index 8715396..3340f8f 100644 --- a/neighbor.cpp +++ b/neighbor.cpp @@ -1,14 +1,14 @@ #include "neighbor.h" +#include +#include + neighbor::neighbor(const char* hostname) - : name(hostname) - , ctrl_mbox(hostname) - , data_mbox(hostname) + : host((hostdata* )MSG_host_get_data(MSG_get_host_by_name(hostname))) , load(std::numeric_limits::infinity()) , debt(0.0) + , to_send(0.0) { - ctrl_mbox += "_ctrl"; - data_mbox += "_data"; } neighbor::~neighbor() diff --git a/neighbor.h b/neighbor.h index 4bd05a9..7700cb3 100644 --- a/neighbor.h +++ b/neighbor.h @@ -1,31 +1,34 @@ #ifndef NEIGHBOR_H #define NEIGHBOR_H -#include -#include +#include +#include "hostdata.h" class neighbor { public: neighbor(const char* hostname); ~neighbor(); - const char* get_name() const { return name.c_str(); } - const char* get_ctrl_mbox() const { return ctrl_mbox.c_str(); } - const char* get_data_mbox() const { return data_mbox.c_str(); } + const char* get_name() const { return host->get_name(); } + const char* get_ctrl_mbox() const { return host->get_ctrl_mbox(); } + const char* get_data_mbox() const { return host->get_data_mbox(); } - double get_load() const { return load; } - void set_load(double l) { load = l; } + double get_load() const { return load; } + void set_load(double amount) { load = amount; } - double get_debt() const { return debt; } - void set_debt(double d) { debt = d; } + double get_debt() const { return debt; } + void set_debt(double amount) { debt = amount; } + + double get_to_send() const { return to_send; } + void set_to_send(double amount) { to_send = amount; } private: - std::string name; - std::string ctrl_mbox; - std::string data_mbox; + const hostdata* host; double load; double debt; + + double to_send; }; #endif // !NEIGHBOR_H diff --git a/options.cpp b/options.cpp index a16023a..61861f0 100644 --- a/options.cpp +++ b/options.cpp @@ -1,10 +1,11 @@ +#include "options.h" + #include // strrchr #include #include #include #include // getopt #include -#include "options.h" #include "misc.h" XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu); diff --git a/process.cpp b/process.cpp index bc71b9d..1a9066d 100644 --- a/process.cpp +++ b/process.cpp @@ -1,3 +1,5 @@ +#include "process.h" + #include #include #include @@ -7,7 +9,6 @@ #include #include "misc.h" #include "options.h" -#include "process.h" XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu); @@ -17,6 +18,11 @@ process::process(int argc, char* argv[]) throw std::invalid_argument("bad or missing initial load"); neigh.assign(argv + 2, argv + argc); + + std::for_each(neigh.begin(), neigh.end(), + std::tr1::bind(&process::insert_neighbor_in_map, + this, std::tr1::placeholders::_1)); + expected_load = load; ctrl_close_pending = data_close_pending = neigh.size(); @@ -46,12 +52,11 @@ process::process(int argc, char* argv[]) int process::run() { bool one_more = true; - unsigned iter = 0; INFO1("Initial load: %g", load); VERB0("Starting..."); + iter = 0; while (one_more) { - bool close_received; ++iter; if (opt::log_rate && iter % opt::log_rate == 0) { @@ -64,13 +69,15 @@ int process::run() } compute(); - close_received = !receive(false); - /* - * compute load balancing; - * send tasks to neighbors; - */ + bool close_received = !receive(false); + + if (opt::bookkeeping) + expected_load -= load_balance(expected_load); + else + load -= load_balance(load); + send(); comm.flush(false); if (opt::exit_on_close && close_received) @@ -106,6 +113,61 @@ void process::compute() MSG_task_destroy(task); } +double process::load_balance(double /*my_load*/) +{ + return 0.0; +} + +void process::send1_no_bookkeeping(neighbor& nb) +{ + comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load)); + double load_to_send = nb.get_to_send(); + if (load_to_send > 0.0) { + comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send)); + nb.set_to_send(0.0); + } +} + +void process::send1_bookkeeping(neighbor& nb) +{ + comm.send(nb.get_ctrl_mbox(), new message(message::INFO, expected_load)); + double load_to_send; + double new_debt; + double debt_to_send = nb.get_to_send(); + if (debt_to_send > 0.0) { + comm.send(nb.get_ctrl_mbox(), + new message(message::CREDIT, debt_to_send)); + nb.set_to_send(0.0); + new_debt = nb.get_debt() + debt_to_send; + } else { + new_debt = nb.get_debt(); + } + if (load <= new_debt) { + load_to_send = load; + nb.set_debt(new_debt - load_to_send); + load = 0.0; + } else { + load_to_send = new_debt; + nb.set_debt(0.0); + load -= load_to_send; + } + if (load_to_send > 0.0) + comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send)); +} + +void process::send() +{ + // fixme: shall we send data at all iterations? + if (opt::bookkeeping) { + std::for_each(neigh.begin(), neigh.end(), + std::tr1::bind(&process::send1_bookkeeping, + this, std::tr1::placeholders::_1)); + } else { + std::for_each(neigh.begin(), neigh.end(), + std::tr1::bind(&process::send1_no_bookkeeping, + this, std::tr1::placeholders::_1)); + } +} // Returns false if a CLOSE message was received. bool process::receive(bool wait_for_close) @@ -118,10 +180,11 @@ bool process::receive(bool wait_for_close) DEBUG2("received %s from %s", msg->to_string().c_str(), MSG_host_get_name(from)); switch (msg->get_type()) { - case message::INFO: - // fixme: update neighbor - // need a map m_host_t -> neighbor& + case message::INFO: { + neighbor* n = rev_neigh[from]; + n->set_load(msg->get_amount()); break; + } case message::CREDIT: expected_load += msg->get_amount(); break; @@ -146,15 +209,19 @@ bool process::receive(bool wait_for_close) return result; } +void process::finalize1(neighbor& nb) +{ + comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0)); + comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0)); +} + void process::finalize() { DEBUG2("send CLOSE to %d neighbor%s.", (int )neigh.size(), ESSE(neigh.size())); - std::vector::iterator n; - for (n = neigh.begin() ; n != neigh.end() ; ++n) { - comm.send(n->get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0)); - comm.send(n->get_data_mbox(), new message(message::DATA_CLOSE, 0.0)); - } + std::for_each(neigh.begin(), neigh.end(), + std::tr1::bind(&process::finalize1, + this, std::tr1::placeholders::_1)); DEBUG2("wait for CLOSE from %d neighbor%s.", (int )neigh.size(), ESSE(neigh.size())); @@ -180,6 +247,13 @@ void process::print_loads(e_xbt_log_priority_t logp) LOG1(logp, "Neighbor loads: %s", oss.str().c_str()); } +void process::insert_neighbor_in_map(neighbor& nb) +{ + neighbor* nbp = &nb; + m_host_t host = MSG_get_host_by_name(nb.get_name()); + rev_neigh.insert(std::make_pair(host, nbp)); +} + // Local variables: // mode: c++ // End: diff --git a/process.h b/process.h index 3baabbf..a49c6ef 100644 --- a/process.h +++ b/process.h @@ -1,8 +1,9 @@ #ifndef PROCESS_H #define PROCESS_H +#include #include -#include +#include #include "communicator.h" #include "neighbor.h" @@ -13,18 +14,29 @@ public: int run(); private: - communicator comm; std::vector neigh; - double load; - double expected_load; + std::map rev_neigh; + communicator comm; int ctrl_close_pending; int data_close_pending; + unsigned iter; + + double load; + double expected_load; + void compute(); + virtual double load_balance(double my_load); + void send1_no_bookkeeping(neighbor& nb); + void send1_bookkeeping(neighbor& nb); + void send(); bool receive(bool wait_for_close); + void finalize1(neighbor& nb); void finalize(); void print_loads(e_xbt_log_priority_t logp = xbt_log_priority_info); + + void insert_neighbor_in_map(neighbor& nb); }; #endif // !PROCESS_H -- 2.39.5