From: Martin Quinson Date: Sun, 22 Apr 2018 20:51:40 +0000 (+0200) Subject: further snake_case s4u::Engine X-Git-Tag: v3.20~335 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/3721210a272c9593a3bccd52f63178403b5729e3 further snake_case s4u::Engine --- diff --git a/examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp b/examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp index 16b693cc60..19668e4155 100644 --- a/examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp +++ b/examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp @@ -39,9 +39,9 @@ int main(int argc, char* argv[]) "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]); - e.load_platform(argv[1]); /* - Load the platform description */ - e.registerFunction("sleeper"); - e.load_deployment(argv[2]); /* - Deploy the sleeper processes with explicit start/kill times */ + e.load_platform(argv[1]); /* Load the platform description */ + e.register_actor("sleeper"); + e.load_deployment(argv[2]); /* Deploy the sleeper processes with explicit start/kill times */ e.run(); /* - Run the simulation */ diff --git a/examples/s4u/actor-yield/s4u-actor-yield.cpp b/examples/s4u/actor-yield/s4u-actor-yield.cpp index 8fc130c6fe..bd80f70c3e 100644 --- a/examples/s4u/actor-yield/s4u-actor-yield.cpp +++ b/examples/s4u/actor-yield/s4u-actor-yield.cpp @@ -36,8 +36,8 @@ int main(int argc, char* argv[]) "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]); - e.load_platform(argv[1]); /* - Load the platform description */ - e.register_function("yielder"); + e.load_platform(argv[1]); /* Load the platform description */ + e.register_actor("yielder"); /* Register the class representing the actors */ e.load_deployment(argv[2]); diff --git a/examples/s4u/app-bittorrent/s4u-bittorrent.cpp b/examples/s4u/app-bittorrent/s4u-bittorrent.cpp index 66bcd81b16..2ef35e10b7 100644 --- a/examples/s4u/app-bittorrent/s4u-bittorrent.cpp +++ b/examples/s4u/app-bittorrent/s4u-bittorrent.cpp @@ -19,14 +19,14 @@ int main(int argc, char* argv[]) e.load_platform(argv[1]); + /* Install our extension on all existing hosts */ HostBittorrent::EXTENSION_ID = simgrid::s4u::Host::extension_create(); - std::vector list = simgrid::s4u::Engine::getInstance()->get_all_hosts(); for (auto const& host : list) host->extension_set(new HostBittorrent(host)); - e.register_function("tracker"); - e.register_function("peer"); + e.register_actor("tracker"); + e.register_actor("peer"); e.load_deployment(argv[2]); e.run(); diff --git a/examples/s4u/app-masterworker/s4u-app-masterworker.cpp b/examples/s4u/app-masterworker/s4u-app-masterworker.cpp index 47420c8a30..643f6d4572 100644 --- a/examples/s4u/app-masterworker/s4u-app-masterworker.cpp +++ b/examples/s4u/app-masterworker/s4u-app-masterworker.cpp @@ -89,10 +89,10 @@ int main(int argc, char* argv[]) "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]); - e.load_platform(argv[1]); /** - Load the platform description */ - e.register_function("master"); - e.register_function("worker"); /** - Register the function to be executed by the processes */ - e.load_deployment(argv[2]); /** - Deploy the application */ + e.load_platform(argv[1]); /* Load the platform description */ + e.register_actor("master"); /* Register the class representing the actors */ + e.register_actor("worker"); + e.load_deployment(argv[2]); /* Deploy the application */ e.run(); /** - Run the simulation */ diff --git a/examples/s4u/async-wait/s4u-async-wait.cpp b/examples/s4u/async-wait/s4u-async-wait.cpp index c8a344c548..b2f5a11a7f 100644 --- a/examples/s4u/async-wait/s4u-async-wait.cpp +++ b/examples/s4u/async-wait/s4u-async-wait.cpp @@ -18,93 +18,77 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example"); -class Sender { - long messages_count; /* - number of tasks */ - long receivers_count; /* - number of receivers */ - double msg_size; /* - communication cost in bytes */ - -public: - explicit Sender(std::vector args) - { - xbt_assert(args.size() == 4, "Expecting 3 parameters from the XML deployment file but got %zu", args.size()); - messages_count = std::stol(args[1]); - msg_size = std::stod(args[2]); - receivers_count = std::stol(args[3]); +static int sender(int argc, char** argv) +{ + xbt_assert(argc == 4, "Expecting 3 parameters from the XML deployment file but got %d", argc); + long messages_count = std::stol(argv[1]); /* - number of tasks */ + double msg_size = std::stol(argv[2]); /* - communication cost in bytes */ + long receivers_count = std::stod(argv[3]); /* - number of receivers */ + + std::vector pending_comms; + + /* Start dispatching all messages to receivers, in a round robin fashion */ + for (int i = 0; i < messages_count; i++) { + + std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count); + simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName); + std::string msgName = std::string("Message ") + std::to_string(i); + std::string* payload = new std::string(msgName); // copy the data we send: + // 'msgName' is not a stable storage location + XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str()); + /* Create a communication representing the ongoing communication */ + simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size); + /* Add this comm to the vector of all known comms */ + pending_comms.push_back(comm); } - void operator()() - { - std::vector pending_comms; - - /* Start dispatching all messages to receivers, in a round robin fashion */ - for (int i = 0; i < messages_count; i++) { - - std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count); - simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName); - std::string msgName = std::string("Message ") + std::to_string(i); - std::string* payload = new std::string(msgName); // copy the data we send: - // 'msgName' is not a stable storage location - XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str()); - /* Create a communication representing the ongoing communication */ - simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size); - /* Add this comm to the vector of all known comms */ - pending_comms.push_back(comm); - } - - /* Start sending messages to let the workers know that they should stop */ - for (int i = 0; i < receivers_count; i++) { - std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count); - simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName); - std::string* payload = new std::string("finalize"); // Make a copy of the data we will send - - simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0); - pending_comms.push_back(comm); - XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count); - } - XBT_INFO("Done dispatching all messages"); - - /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */ - while (not pending_comms.empty()) { - simgrid::s4u::CommPtr comm = pending_comms.back(); - comm->wait(); // we could provide a timeout as a parameter - pending_comms.pop_back(); // remove it from the list - } - - XBT_INFO("Goodbye now!"); + + /* Start sending messages to let the workers know that they should stop */ + for (int i = 0; i < receivers_count; i++) { + std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count); + simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName); + std::string* payload = new std::string("finalize"); // Make a copy of the data we will send + + simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0); + pending_comms.push_back(comm); + XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count); } -}; + XBT_INFO("Done dispatching all messages"); -/* Receiver actor expects 1 argument: its ID */ -class Receiver { - simgrid::s4u::MailboxPtr mbox; - -public: - explicit Receiver(std::vector args) - { - xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size()); - std::string mboxName = std::string("receiver-") + args[1]; - mbox = simgrid::s4u::Mailbox::byName(mboxName); + /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */ + while (not pending_comms.empty()) { + simgrid::s4u::CommPtr comm = pending_comms.back(); + comm->wait(); // we could provide a timeout as a parameter + pending_comms.pop_back(); // remove it from the list } - void operator()() - { - XBT_INFO("Wait for my first message"); - for (bool cont = true; cont;) { - std::string* received = static_cast(mbox->get()); - XBT_INFO("I got a '%s'.", received->c_str()); - cont = (*received != "finalize"); // If it's a finalize message, we're done - // Receiving the message was all we were supposed to do - delete received; - } + XBT_INFO("Goodbye now!"); + return 0; +} + +/* Receiver actor expects 1 argument: its ID */ +static int receiver(int argc, char** argv) +{ + xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc); + simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(std::string("receiver-") + argv[1]); + + XBT_INFO("Wait for my first message"); + for (bool cont = true; cont;) { + std::string* received = static_cast(mbox->get()); + XBT_INFO("I got a '%s'.", received->c_str()); + if (*received == "finalize") + cont = false; // If it's a finalize message, we're done. + delete received; } -}; + return 0; +} int main(int argc, char *argv[]) { xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); simgrid::s4u::Engine e(&argc, argv); - e.registerFunction("sender"); - e.registerFunction("receiver"); + e.register_function("sender", &sender); + e.register_function("receiver", &receiver); e.load_platform(argv[1]); e.load_deployment(argv[2]); diff --git a/examples/s4u/async-waitall/s4u-async-waitall.cpp b/examples/s4u/async-waitall/s4u-async-waitall.cpp index 4d2c49c77b..a6762bc75d 100644 --- a/examples/s4u/async-waitall/s4u-async-waitall.cpp +++ b/examples/s4u/async-waitall/s4u-async-waitall.cpp @@ -99,8 +99,8 @@ int main(int argc, char *argv[]) xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); simgrid::s4u::Engine e(&argc, argv); - e.registerFunction("sender"); - e.registerFunction("receiver"); + e.register_actor("sender"); + e.register_actor("receiver"); e.load_platform(argv[1]); e.load_deployment(argv[2]); diff --git a/examples/s4u/async-waitany/s4u-async-waitany.cpp b/examples/s4u/async-waitany/s4u-async-waitany.cpp index 79d0ebf209..32b53e3739 100644 --- a/examples/s4u/async-waitany/s4u-async-waitany.cpp +++ b/examples/s4u/async-waitany/s4u-async-waitany.cpp @@ -115,8 +115,8 @@ int main(int argc, char *argv[]) xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); simgrid::s4u::Engine e(&argc, argv); - e.register_function("sender"); - e.register_function("receiver"); + e.register_actor("sender"); + e.register_actor("receiver"); e.load_platform(argv[1]); e.load_deployment(argv[2]); diff --git a/examples/s4u/dht-chord/s4u-dht-chord.cpp b/examples/s4u/dht-chord/s4u-dht-chord.cpp index 1b261349ca..4376b7bf96 100644 --- a/examples/s4u/dht-chord/s4u-dht-chord.cpp +++ b/examples/s4u/dht-chord/s4u-dht-chord.cpp @@ -64,9 +64,9 @@ int main(int argc, char* argv[]) e.load_platform(options[0]); - chord_init(); + chord_init(); // FIXME: inline me - e.register_function("node"); + e.register_actor("node"); e.load_deployment(options[1]); e.run(); diff --git a/examples/s4u/replay-comm/s4u-replay-comm.cpp b/examples/s4u/replay-comm/s4u-replay-comm.cpp index 8605a10f34..1340ea9163 100644 --- a/examples/s4u/replay-comm/s4u-replay-comm.cpp +++ b/examples/s4u/replay-comm/s4u-replay-comm.cpp @@ -97,8 +97,8 @@ int main(int argc, char* argv[]) e.load_platform(argv[1]); e.register_default(&simgrid::xbt::replay_runner); - e.registerFunction("p0"); - e.registerFunction("p1"); + e.register_actor("p0"); + e.register_actor("p1"); e.load_deployment(argv[2]); /* Action registration */ diff --git a/examples/s4u/replay-storage/s4u-replay-storage.cpp b/examples/s4u/replay-storage/s4u-replay-storage.cpp index f1a76f2855..d793ea17e2 100644 --- a/examples/s4u/replay-storage/s4u-replay-storage.cpp +++ b/examples/s4u/replay-storage/s4u-replay-storage.cpp @@ -113,7 +113,7 @@ int main(int argc, char* argv[]) e.load_platform(argv[1]); e.register_default(&simgrid::xbt::replay_runner); - e.registerFunction("p0"); + e.register_actor("p0"); e.load_deployment(argv[2]); /* Action registration */ diff --git a/include/simgrid/s4u/Engine.hpp b/include/simgrid/s4u/Engine.hpp index 52224c13b8..44eab9d746 100644 --- a/include/simgrid/s4u/Engine.hpp +++ b/include/simgrid/s4u/Engine.hpp @@ -42,6 +42,8 @@ public: /** Registers the main function of an actor that will be launched from the deployment file */ void register_function(const char* name, int (*code)(int, char**)); + // FIXME: provide a register_function(std::string, void (*code)(int, char**)) and deprecate the int returning one + // FIXME: provide a register_function(std::string, std::vector) /** Registers a function as the default main function of actors * @@ -50,6 +52,23 @@ public: */ void register_default(int (*code)(int, char**)); + template void register_actor(const char* name) + { + simgrid::simix::register_function(name, [](std::vector args) { + return simgrid::simix::ActorCode([args] { + F code(std::move(args)); + code(); + }); + }); + } + + template void register_actor(const char* name, F code) + { + simgrid::simix::register_function(name, [code](std::vector args) { + return simgrid::simix::ActorCode([code, args] { code(std::move(args)); }); + }); + } + /** @brief Load a deployment file and launch the actors that it contains */ void load_deployment(const char* deploy); @@ -100,23 +119,6 @@ public: void netpointRegister(simgrid::kernel::routing::NetPoint * card); void netpointUnregister(simgrid::kernel::routing::NetPoint * card); - template void register_actor(const char* name) - { - simgrid::simix::register_function(name, [](std::vector args) { - return simgrid::simix::ActorCode([args] { - F code(std::move(args)); - code(); - }); - }); - } - - template void register_actor(const char* name, F code) - { - simgrid::simix::register_function(name, [code](std::vector args) { - return simgrid::simix::ActorCode([code, args] { code(std::move(args)); }); - }); - } - /** Returns whether SimGrid was initialized yet -- mostly for internal use */ static bool isInitialized(); @@ -142,6 +144,17 @@ public: { register_default(code); } + template + XBT_ATTRIB_DEPRECATED_v323("Please use Engine::register_actor()") void registerFunction(const char* name) + { + register_actor(name); + } + template + XBT_ATTRIB_DEPRECATED_v323("Please use Engine::register_actor()") void registerFunction(const char* name, F code) + { + register_actor(name, code); + } + XBT_ATTRIB_DEPRECATED_v323("Please use Engine::load_deployment()") void loadDeployment(const char* deploy) { load_deployment(deploy); diff --git a/include/simgrid/s4u/Mailbox.hpp b/include/simgrid/s4u/Mailbox.hpp index 34326a70b1..c7438ef0f7 100644 --- a/include/simgrid/s4u/Mailbox.hpp +++ b/include/simgrid/s4u/Mailbox.hpp @@ -176,7 +176,7 @@ public: CommPtr get_async(void** data); /** Blocking data reception */ - void* get(); + void* get(); // FIXME: make a typed template version /** Blocking data reception with timeout */ void* get(double timeout); }; diff --git a/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp b/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp index c953820fb5..ab82939ac0 100644 --- a/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp +++ b/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp @@ -182,7 +182,7 @@ int main(int argc, char* argv[]) } xbt_assert(argSend.front().size() == argRecv.front().size(), "Sender and receiver spec must be of the same size"); - std::vector hosts = e.getAllHosts(); + std::vector hosts = e.get_all_hosts(); simgrid::s4u::Actor::create("sender", hosts[0], sender, argSend); simgrid::s4u::Actor::create("recver", hosts[1], receiver, argRecv);