1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* This example shows how to use simgrid::s4u::this_actor::wait() to wait for a given communication.
8 * As for the other asynchronous examples, the sender initiate all the messages it wants to send and
9 * pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occurs concurrently.
11 * The sender then loops until there is no ongoing communication.
14 #include "simgrid/s4u.hpp"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
21 static int sender(int argc, char** argv)
23 xbt_assert(argc == 4, "Expecting 3 parameters from the XML deployment file but got %d", argc);
24 long messages_count = std::stol(argv[1]); /* - number of tasks */
25 double msg_size = std::stol(argv[2]); /* - communication cost in bytes */
26 long receivers_count = std::stod(argv[3]); /* - number of receivers */
28 std::vector<simgrid::s4u::CommPtr> pending_comms;
30 /* Start dispatching all messages to receivers, in a round robin fashion */
31 for (int i = 0; i < messages_count; i++) {
33 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
34 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(mboxName);
35 std::string msgName = std::string("Message ") + std::to_string(i);
36 std::string* payload = new std::string(msgName); // copy the data we send:
37 // 'msgName' is not a stable storage location
38 XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
39 /* Create a communication representing the ongoing communication */
40 simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size);
41 /* Add this comm to the vector of all known comms */
42 pending_comms.push_back(comm);
45 /* Start sending messages to let the workers know that they should stop */
46 for (int i = 0; i < receivers_count; i++) {
47 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
48 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(mboxName);
49 std::string* payload = new std::string("finalize"); // Make a copy of the data we will send
51 simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0);
52 pending_comms.push_back(comm);
53 XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
55 XBT_INFO("Done dispatching all messages");
57 /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
58 while (not pending_comms.empty()) {
59 simgrid::s4u::CommPtr comm = pending_comms.back();
60 comm->wait(); // we could provide a timeout as a parameter
61 pending_comms.pop_back(); // remove it from the list
64 XBT_INFO("Goodbye now!");
68 /* Receiver actor expects 1 argument: its ID */
69 static int receiver(int argc, char** argv)
71 xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
72 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(std::string("receiver-") + argv[1]);
74 XBT_INFO("Wait for my first message");
75 for (bool cont = true; cont;) {
76 std::string* received = static_cast<std::string*>(mbox->get());
77 XBT_INFO("I got a '%s'.", received->c_str());
78 if (*received == "finalize")
79 cont = false; // If it's a finalize message, we're done.
85 int main(int argc, char *argv[])
87 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
89 simgrid::s4u::Engine e(&argc, argv);
90 e.register_function("sender", &sender);
91 e.register_function("receiver", &receiver);
93 e.load_platform(argv[1]);
94 e.load_deployment(argv[2]);