From d54ca2b555fc67dfb608b5c6e87e1af04f7b15db Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 10 Jul 2023 02:17:05 +0200 Subject: [PATCH] Convert an example to the new activity sets --- MANIFEST.in | 4 +- examples/cpp/CMakeLists.txt | 4 +- .../s4u-activityset-waitall.cpp | 55 +++++++++++ .../s4u-activityset-waitall.tesh | 7 ++ .../cpp/comm-waitall/s4u-comm-waitall.cpp | 94 ------------------- .../cpp/comm-waitall/s4u-comm-waitall.tesh | 21 ----- include/simgrid/s4u/ActivitySet.hpp | 4 +- src/s4u/s4u_ActivitySet.cpp | 1 + 8 files changed, 70 insertions(+), 120 deletions(-) create mode 100644 examples/cpp/activityset-waitall/s4u-activityset-waitall.cpp create mode 100644 examples/cpp/activityset-waitall/s4u-activityset-waitall.tesh delete mode 100644 examples/cpp/comm-waitall/s4u-comm-waitall.cpp delete mode 100644 examples/cpp/comm-waitall/s4u-comm-waitall.tesh diff --git a/MANIFEST.in b/MANIFEST.in index f4d801b2bf..aeeda8568b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -122,6 +122,8 @@ include examples/c/synchro-semaphore/synchro-semaphore.c include examples/c/synchro-semaphore/synchro-semaphore.tesh include examples/cpp/activityset-testany/s4u-activityset-testany.cpp include examples/cpp/activityset-testany/s4u-activityset-testany.tesh +include examples/cpp/activityset-waitall/s4u-activityset-waitall.cpp +include examples/cpp/activityset-waitall/s4u-activityset-waitall.tesh include examples/cpp/activityset-waitany/s4u-activityset-waitany.cpp include examples/cpp/activityset-waitany/s4u-activityset-waitany.tesh include examples/cpp/actor-create/s4u-actor-create.cpp @@ -194,8 +196,6 @@ include examples/cpp/comm-throttling/s4u-comm-throttling.cpp include examples/cpp/comm-throttling/s4u-comm-throttling.tesh include examples/cpp/comm-wait/s4u-comm-wait.cpp include examples/cpp/comm-wait/s4u-comm-wait.tesh -include examples/cpp/comm-waitall/s4u-comm-waitall.cpp -include examples/cpp/comm-waitall/s4u-comm-waitall.tesh include examples/cpp/comm-waituntil/s4u-comm-waituntil.cpp include examples/cpp/comm-waituntil/s4u-comm-waituntil.tesh include examples/cpp/dag-comm/s4u-dag-comm.cpp diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index 1bada049a4..0a57bf9086 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -153,12 +153,12 @@ endif() # Deal with each example -foreach (example activityset-testany activityset-waitany +foreach (example activityset-testany activityset-waitany activityset-waitall actor-create actor-daemon actor-exiting actor-join actor-kill actor-lifetime actor-migrate actor-suspend actor-yield actor-stacksize app-bittorrent app-chainsend app-token-ring battery-degradation battery-simple battery-energy - comm-pingpong comm-ready comm-suspend comm-wait comm-waitall comm-waituntil + comm-pingpong comm-ready comm-suspend comm-wait comm-waituntil comm-dependent comm-host2host comm-failure comm-throttling cloud-capping cloud-migration cloud-simple dag-comm dag-from-json-simple dag-from-dax-simple dag-from-dax dag-from-dot-simple dag-from-dot dag-failure dag-io dag-scheduling dag-simple dag-tuto diff --git a/examples/cpp/activityset-waitall/s4u-activityset-waitall.cpp b/examples/cpp/activityset-waitall/s4u-activityset-waitall.cpp new file mode 100644 index 0000000000..176e50fcd6 --- /dev/null +++ b/examples/cpp/activityset-waitall/s4u-activityset-waitall.cpp @@ -0,0 +1,55 @@ +/* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" +#include +#include +#include +namespace sg4 = simgrid::s4u; + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_activity_waittany, "Messages specific for this s4u example"); + +static void bob() +{ + sg4::Mailbox* mbox = sg4::Mailbox::by_name("mbox"); + const sg4::Disk* disk = sg4::Host::current()->get_disks().front(); + std::string* payload; + + XBT_INFO("Create my asynchronous activities"); + auto exec = sg4::this_actor::exec_async(5e9); + auto comm = mbox->get_async(&payload); + auto io = disk->read_async(3e8); + + sg4::ActivitySet pending_activities({boost::dynamic_pointer_cast(exec), + boost::dynamic_pointer_cast(comm), + boost::dynamic_pointer_cast(io)}); + + XBT_INFO("Wait for asynchrounous activities to complete, all in one shot."); + pending_activities.wait_all(); + + XBT_INFO("All activities are completed."); + delete payload; +} + +static void alice() +{ + auto* payload = new std::string("Message"); + XBT_INFO("Send '%s'", payload->c_str()); + sg4::Mailbox::by_name("mbox")->put(payload, 6e8); +} + +int main(int argc, char* argv[]) +{ + sg4::Engine e(&argc, argv); + + e.load_platform(argv[1]); + + sg4::Actor::create("bob", e.host_by_name("bob"), bob); + sg4::Actor::create("alice", e.host_by_name("alice"), alice); + + e.run(); + + return 0; +} diff --git a/examples/cpp/activityset-waitall/s4u-activityset-waitall.tesh b/examples/cpp/activityset-waitall/s4u-activityset-waitall.tesh new file mode 100644 index 0000000000..6f0142951d --- /dev/null +++ b/examples/cpp/activityset-waitall/s4u-activityset-waitall.tesh @@ -0,0 +1,7 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-activityset-waitall ${platfdir}/hosts_with_disks.xml "--log=root.fmt:[%7.6r]%e[%5a]%e%m%n" +> [0.000000] [alice] Send 'Message' +> [0.000000] [ bob] Create my asynchronous activities +> [0.000000] [ bob] Wait for asynchrounous activities to complete, all in one shot. +> [5.197828] [ bob] All activities are completed. diff --git a/examples/cpp/comm-waitall/s4u-comm-waitall.cpp b/examples/cpp/comm-waitall/s4u-comm-waitall.cpp deleted file mode 100644 index 5c3f863626..0000000000 --- a/examples/cpp/comm-waitall/s4u-comm-waitall.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */ - -/* This program is free software; you can redistribute it and/or modify it - * under the terms of the license (GNU LGPL) which comes with this package. */ - -/* This example shows how to block on the completion of a set of communications. - * - * As for the other asynchronous examples, the sender initiate all the messages it wants to send and - * pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occur concurrently. - * - * The sender then blocks until all ongoing communication terminate, using simgrid::s4u::Comm::wait_all() - * - */ - -#include "simgrid/s4u.hpp" -#include -#include -#include -namespace sg4 = simgrid::s4u; - -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_waitall, "Messages specific for this s4u example"); - -static void sender(unsigned int messages_count, unsigned int receivers_count, long msg_size) -{ - if (messages_count == 0 || receivers_count == 0) { - XBT_WARN("Sender has nothing to do. Bail out!"); - return; - } - // sphinx-doc: init-begin (this line helps the doc to build; ignore it) - /* Vector in which we store all ongoing communications */ - std::vector pending_comms; - - /* Make a vector of the mailboxes to use */ - std::vector mboxes; - for (unsigned int i = 0; i < receivers_count; i++) - mboxes.push_back(sg4::Mailbox::by_name("receiver-" + std::to_string(i))); - // sphinx-doc: init-end - - /* Start dispatching all messages to receivers, in a round robin fashion */ - for (unsigned int i = 0; i < messages_count; i++) { - std::string msg_content = "Message " + std::to_string(i); - // Copy the data we send: the 'msg_content' variable is not a stable storage location. - // It will be destroyed when this actor leaves the loop, ie before the receiver gets it - auto* payload = new std::string(msg_content); - - XBT_INFO("Send '%s' to '%s'", msg_content.c_str(), mboxes[i % receivers_count]->get_cname()); - - /* Create a communication representing the ongoing communication, and store it in pending_comms */ - sg4::CommPtr comm = mboxes[i % receivers_count]->put_async(payload, msg_size); - pending_comms.push_back(comm); - } - - /* Start sending messages to let the workers know that they should stop */ // sphinx-doc: put-begin - for (unsigned int i = 0; i < receivers_count; i++) { - XBT_INFO("Send 'finalize' to 'receiver-%u'", i); - sg4::CommPtr comm = mboxes[i]->put_async(new std::string("finalize"), 0); - pending_comms.push_back(comm); - } - XBT_INFO("Done dispatching all messages"); - - /* Now that all message exchanges were initiated, wait for their completion in one single call */ - sg4::Comm::wait_all(pending_comms); - // sphinx-doc: put-end - - XBT_INFO("Goodbye now!"); -} - -/* Receiver actor expects 1 argument: its ID */ -static void receiver(int id) -{ - sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver-" + std::to_string(id)); - XBT_INFO("Wait for my first message"); - for (bool cont = true; cont;) { - auto received = mbox->get_unique(); - 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 - } -} - -int main(int argc, char* argv[]) -{ - sg4::Engine e(&argc, argv); - - e.load_platform(argv[1]); - - sg4::Actor::create("sender", e.host_by_name("Tremblay"), sender, 5, 2, 1e6); - sg4::Actor::create("receiver", e.host_by_name("Ruby"), receiver, 0); - sg4::Actor::create("receiver", e.host_by_name("Perl"), receiver, 1); - - e.run(); - - return 0; -} diff --git a/examples/cpp/comm-waitall/s4u-comm-waitall.tesh b/examples/cpp/comm-waitall/s4u-comm-waitall.tesh deleted file mode 100644 index cdf73659e3..0000000000 --- a/examples/cpp/comm-waitall/s4u-comm-waitall.tesh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env tesh - -$ ${bindir:=.}/s4u-comm-waitall ${platfdir}/small_platform_fatpipe.xml "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n" -> [ 0.000000] (2:receiver@Ruby) Wait for my first message -> [ 0.000000] (3:receiver@Perl) Wait for my first message -> [ 0.000000] (1:sender@Tremblay) Send 'Message 0' to 'receiver-0' -> [ 0.000000] (1:sender@Tremblay) Send 'Message 1' to 'receiver-1' -> [ 0.000000] (1:sender@Tremblay) Send 'Message 2' to 'receiver-0' -> [ 0.000000] (1:sender@Tremblay) Send 'Message 3' to 'receiver-1' -> [ 0.000000] (1:sender@Tremblay) Send 'Message 4' to 'receiver-0' -> [ 0.000000] (1:sender@Tremblay) Send 'finalize' to 'receiver-0' -> [ 0.000000] (1:sender@Tremblay) Send 'finalize' to 'receiver-1' -> [ 0.000000] (1:sender@Tremblay) Done dispatching all messages -> [ 0.004022] (2:receiver@Ruby) I got a 'Message 0'. -> [ 0.004022] (3:receiver@Perl) I got a 'Message 1'. -> [ 0.008043] (2:receiver@Ruby) I got a 'Message 2'. -> [ 0.008043] (3:receiver@Perl) I got a 'Message 3'. -> [ 0.009995] (3:receiver@Perl) I got a 'finalize'. -> [ 0.012065] (2:receiver@Ruby) I got a 'Message 4'. -> [ 0.014016] (2:receiver@Ruby) I got a 'finalize'. -> [ 0.014016] (1:sender@Tremblay) Goodbye now! diff --git a/include/simgrid/s4u/ActivitySet.hpp b/include/simgrid/s4u/ActivitySet.hpp index cf3cd4cbdc..8f9e6ed718 100644 --- a/include/simgrid/s4u/ActivitySet.hpp +++ b/include/simgrid/s4u/ActivitySet.hpp @@ -42,9 +42,11 @@ public: * * On timeout, an exception is raised, and the completed activities remain in the set. Use test_any() to retrieve * them. + * + * When no timeout occures, the set is emptied. */ void wait_all_for(double timeout); - /** Wait for the completion of all activities in the set */ + /** Wait for the completion of all activities in the set. The set is emptied afterward. */ void wait_all() { wait_all_for(-1); } /** Returns the first terminated activity if any, or ActivityPtr(nullptr) if no activity is terminated */ ActivityPtr test_any(); diff --git a/src/s4u/s4u_ActivitySet.cpp b/src/s4u/s4u_ActivitySet.cpp index 6b628388b0..f7eb3f49e5 100644 --- a/src/s4u/s4u_ActivitySet.cpp +++ b/src/s4u/s4u_ActivitySet.cpp @@ -32,6 +32,7 @@ void ActivitySet::wait_all_for(double timeout) for (const auto& act : activities_) act->wait_until(deadline); } + activities_.clear(); } ActivityPtr ActivitySet::test_any() -- 2.20.1