From 3386cb2452dc4e2d3a0b58989e0e2bc445c3f4bb Mon Sep 17 00:00:00 2001 From: Kevin Piotrkowski Date: Sun, 12 Aug 2018 18:34:01 -0300 Subject: [PATCH] Cover with a test Mailbox::ready() method introduced in commit 1ed0e64dc405fbb627ff8a7d7b70ac4ff81cd489 --- examples/s4u/CMakeLists.txt | 5 +- examples/s4u/async-ready/s4u-async-ready.cpp | 110 ++++++++++++++++++ examples/s4u/async-ready/s4u-async-ready.tesh | 91 +++++++++++++++ .../s4u/async-ready/s4u-async-ready_d.xml | 23 ++++ 4 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 examples/s4u/async-ready/s4u-async-ready.cpp create mode 100644 examples/s4u/async-ready/s4u-async-ready.tesh create mode 100644 examples/s4u/async-ready/s4u-async-ready_d.xml diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index c33de9ee87..2185d6aaab 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -3,7 +3,7 @@ foreach (example actor-create actor-daemon actor-join actor-kill actor-lifetime actor-migration actor-suspend actor-yield app-chainsend app-pingpong app-token-ring - async-wait async-waitany async-waitall + async-wait async-waitany async-waitall async-ready cloud-capping cloud-migration cloud-simple energy-exec energy-boot energy-link energy-vm engine-filtering @@ -76,6 +76,7 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actor-create/s4u-a ${CMAKE_CURRENT_SOURCE_DIR}/async-waitany/s4u-async-waitany_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/async-waitall/s4u-async-waitall_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/async-wait/s4u-async-wait_d.xml + ${CMAKE_CURRENT_SOURCE_DIR}/async-ready/s4u-ready-wait_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/dht-chord/s4u-dht-chord_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/s4u-dht-kademlia_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/energy-boot/platform_boot.xml @@ -95,7 +96,7 @@ set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/replay-comm/s4u-re foreach(example actor-create actor-daemon actor-join actor-kill actor-lifetime actor-migration actor-suspend actor-yield app-bittorrent app-chainsend app-masterworkers app-pingpong app-token-ring - async-wait async-waitall async-waitany + async-wait async-waitall async-waitany async-ready cloud-capping cloud-migration cloud-simple dht-chord dht-kademlia energy-exec energy-boot energy-link energy-vm diff --git a/examples/s4u/async-ready/s4u-async-ready.cpp b/examples/s4u/async-ready/s4u-async-ready.cpp new file mode 100644 index 0000000000..c23158b1ee --- /dev/null +++ b/examples/s4u/async-ready/s4u-async-ready.cpp @@ -0,0 +1,110 @@ +/* Copyright (c) 2010-2018. 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 use simgrid::s4u::Mailbox::ready() to check for completed communications. + * + * We have a number of peers which send and receive messages in two phases: + * -> sending phase: each one of them sends a number of messages to the others followed + * by a single "finalize" message. + * -> receiving phase: each one of them receives all the available messages that reached + * their corresponding mailbox until it has all the needed "finalize" + * messages to know that no more work needs to be done. + * + * To avoid doing a wait() over the ongoing communications, each peer makes use of the + * simgrid::s4u::Mailbox::ready() method. If it returns true then a following get() will fetch the + * message immediately, if not the peer will sleep for a fixed amount of time before checking again. + * + */ + +#include "simgrid/s4u.hpp" +#include +#include +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_ready, "Messages specific for this s4u example"); + +static int peer(int argc, char** argv) +{ + xbt_assert(argc == 5, "Expecting 4 parameters from the XML deployment file but got %d", argc); + int my_id = std::stoi(argv[1]); /* - my id */ + long messages_count = std::stol(argv[2]); /* - number of tasks */ + double msg_size = std::stol(argv[3]); /* - communication cost in bytes */ + long peers_count = std::stod(argv[4]); /* - number of peers */ + + /* Set myself as the persistent receiver of my mailbox so that messages start flowing to me as soon as they are put into it */ + simgrid::s4u::MailboxPtr my_mbox = simgrid::s4u::Mailbox::by_name(std::string("peer-") + std::to_string(my_id)); + my_mbox->set_receiver(simgrid::s4u::Actor::self()); + + std::vector pending_comms; + + /* Start dispatching all messages to peers others that myself */ + for (int i = 0; i < messages_count; i++) { + for (int peer_id = 0; peer_id < peers_count; peer_id++) { + if (peer_id != my_id) { + std::string mboxName = std::string("peer-") + std::to_string(peer_id); + simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(mboxName); + std::string msgName = std::string("Message ") + std::to_string(i) + std::string(" from peer ") + std::to_string(my_id); + 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 */ + pending_comms.push_back(mbox->put_async(payload, msg_size)); + } + } + } + + /* Start sending messages to let peers know that they should stop */ + for (int peer_id = 0; peer_id < peers_count; peer_id++) { + if (peer_id != my_id) { + std::string mboxName = std::string("peer-") + std::to_string(peer_id); + simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(mboxName); + std::string* payload = new std::string("finalize"); // Make a copy of the data we will send + pending_comms.push_back(mbox->put_async(payload, msg_size)); + XBT_INFO("Send 'finalize' to 'peer-%d'", peer_id); + } + } + XBT_INFO("Done dispatching all messages"); + + /* Retrieve all the messages other peers have been sending to me until I receive all the corresponding "Finalize" messages */ + int pending_finalize_messages = peers_count - 1; + while (pending_finalize_messages > 0) { + if (my_mbox->ready()) { + double start = simgrid::s4u::Engine::get_clock(); + std::string* received = static_cast(my_mbox->get()); + double waiting_time = simgrid::s4u::Engine::get_clock() - start; + xbt_assert(waiting_time == 0, "Expecting the waiting time to be 0 because the communication was supposedly ready, but got %f instead", waiting_time); + XBT_INFO("I got a '%s'.", received->c_str()); + if (*received == "finalize") { + pending_finalize_messages--; + } + delete received; + } else { + XBT_INFO("Nothing ready to consume yet, I better sleep for a while"); + simgrid::s4u::this_actor::sleep_for(.01); + } + } + + XBT_INFO("I'm done, just waiting for my peers to receive the messages before exiting"); + simgrid::s4u::Comm::wait_all(&pending_comms); + + XBT_INFO("Goodbye now!"); + 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.register_function("peer", &peer); + + e.load_platform(argv[1]); + e.load_deployment(argv[2]); + + e.run(); + + return 0; +} diff --git a/examples/s4u/async-ready/s4u-async-ready.tesh b/examples/s4u/async-ready/s4u-async-ready.tesh new file mode 100644 index 0000000000..008c23cc97 --- /dev/null +++ b/examples/s4u/async-ready/s4u-async-ready.tesh @@ -0,0 +1,91 @@ +#!/usr/bin/env tesh + +p Test1 Peer sending and receiving + +$ $SG_TEST_EXENV ${bindir:=.}/s4u-async-ready ${platfdir}/small_platform_fatpipe.xml s4u-async-ready_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:peer@Tremblay) Send 'Message 0 from peer 0' to 'peer-1' +> [ 0.000000] (2:peer@Ruby) Send 'Message 0 from peer 1' to 'peer-0' +> [ 0.000000] (1:peer@Tremblay) Send 'Message 0 from peer 0' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Send 'Message 0 from peer 1' to 'peer-2' +> [ 0.000000] (3:peer@Perl) Send 'finalize' to 'peer-0' +> [ 0.000000] (1:peer@Tremblay) Send 'Message 1 from peer 0' to 'peer-1' +> [ 0.000000] (2:peer@Ruby) Send 'Message 1 from peer 1' to 'peer-0' +> [ 0.000000] (3:peer@Perl) Send 'finalize' to 'peer-1' +> [ 0.000000] (3:peer@Perl) Done dispatching all messages +> [ 0.000000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.000000] (1:peer@Tremblay) Send 'Message 1 from peer 0' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Send 'Message 1 from peer 1' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Send 'Message 2 from peer 1' to 'peer-0' +> [ 0.000000] (1:peer@Tremblay) Send 'finalize' to 'peer-1' +> [ 0.000000] (2:peer@Ruby) Send 'Message 2 from peer 1' to 'peer-2' +> [ 0.000000] (1:peer@Tremblay) Send 'finalize' to 'peer-2' +> [ 0.000000] (1:peer@Tremblay) Done dispatching all messages +> [ 0.000000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.000000] (2:peer@Ruby) Send 'Message 3 from peer 1' to 'peer-0' +> [ 0.000000] (2:peer@Ruby) Send 'Message 3 from peer 1' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Send 'Message 4 from peer 1' to 'peer-0' +> [ 0.000000] (2:peer@Ruby) Send 'Message 4 from peer 1' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Send 'Message 5 from peer 1' to 'peer-0' +> [ 0.000000] (2:peer@Ruby) Send 'Message 5 from peer 1' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Send 'finalize' to 'peer-0' +> [ 0.000000] (2:peer@Ruby) Send 'finalize' to 'peer-2' +> [ 0.000000] (2:peer@Ruby) Done dispatching all messages +> [ 0.000000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.010000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.010000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.010000] (1:peer@Tremblay) I got a 'Message 0 from peer 1'. +> [ 0.010000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.020000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.020000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.020000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.030000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.030000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.030000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.040000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.040000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.040000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.050000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.050000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.050000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.060000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.060000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.060000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.070000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.070000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.070000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.080000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.080000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.080000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.090000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.090000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.090000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.100000] (1:peer@Tremblay) Nothing ready to consume yet, I better sleep for a while +> [ 0.100000] (3:peer@Perl) Nothing ready to consume yet, I better sleep for a while +> [ 0.100000] (2:peer@Ruby) Nothing ready to consume yet, I better sleep for a while +> [ 0.110000] (2:peer@Ruby) I got a 'Message 0 from peer 0'. +> [ 0.110000] (3:peer@Perl) I got a 'Message 0 from peer 0'. +> [ 0.110000] (1:peer@Tremblay) I got a 'finalize'. +> [ 0.110000] (2:peer@Ruby) I got a 'finalize'. +> [ 0.110000] (3:peer@Perl) I got a 'Message 0 from peer 1'. +> [ 0.110000] (1:peer@Tremblay) I got a 'Message 1 from peer 1'. +> [ 0.110000] (2:peer@Ruby) I got a 'Message 1 from peer 0'. +> [ 0.110000] (3:peer@Perl) I got a 'Message 1 from peer 0'. +> [ 0.110000] (1:peer@Tremblay) I got a 'Message 2 from peer 1'. +> [ 0.110000] (2:peer@Ruby) I got a 'finalize'. +> [ 0.110000] (2:peer@Ruby) I'm done, just waiting for my peers to receive the messages before exiting +> [ 0.110000] (3:peer@Perl) I got a 'Message 1 from peer 1'. +> [ 0.110000] (1:peer@Tremblay) I got a 'Message 3 from peer 1'. +> [ 0.110000] (3:peer@Perl) I got a 'finalize'. +> [ 0.110000] (1:peer@Tremblay) I got a 'Message 4 from peer 1'. +> [ 0.110000] (3:peer@Perl) I got a 'Message 2 from peer 1'. +> [ 0.110000] (1:peer@Tremblay) I got a 'Message 5 from peer 1'. +> [ 0.110000] (3:peer@Perl) I got a 'Message 3 from peer 1'. +> [ 0.110000] (1:peer@Tremblay) I got a 'finalize'. +> [ 0.110000] (1:peer@Tremblay) I'm done, just waiting for my peers to receive the messages before exiting +> [ 0.110000] (3:peer@Perl) I got a 'Message 4 from peer 1'. +> [ 0.110000] (3:peer@Perl) I got a 'Message 5 from peer 1'. +> [ 0.110000] (3:peer@Perl) I got a 'finalize'. +> [ 0.110000] (3:peer@Perl) I'm done, just waiting for my peers to receive the messages before exiting +> [ 0.110000] (3:peer@Perl) Goodbye now! +> [ 0.110000] (1:peer@Tremblay) Goodbye now! +> [ 0.110000] (2:peer@Ruby) Goodbye now! diff --git a/examples/s4u/async-ready/s4u-async-ready_d.xml b/examples/s4u/async-ready/s4u-async-ready_d.xml new file mode 100644 index 0000000000..e7e48c2d93 --- /dev/null +++ b/examples/s4u/async-ready/s4u-async-ready_d.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + -- 2.20.1