1 /* Copyright (c) 2010-2017. 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");
22 long messages_count; /* - number of tasks */
23 long receivers_count; /* - number of receivers */
24 double msg_size; /* - communication cost in bytes */
27 explicit Sender(std::vector<std::string> args)
29 xbt_assert(args.size() == 4, "Expecting 3 parameters from the XML deployment file but got %zu", args.size());
30 messages_count = std::stol(args[1]);
31 msg_size = std::stod(args[2]);
32 receivers_count = std::stol(args[3]);
36 std::vector<simgrid::s4u::CommPtr> pending_comms;
38 /* Start dispatching all messages to receivers, in a round robin fashion */
39 for (int i = 0; i < messages_count; i++) {
41 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
42 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
43 std::string msgName = std::string("Message ") + std::to_string(i);
44 std::string* payload = new std::string(msgName); // copy the data we send:
45 // 'msgName' is not a stable storage location
46 XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
47 /* Create a communication representing the ongoing communication */
48 simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size);
49 /* Add this comm to the vector of all known comms */
50 pending_comms.push_back(comm);
53 /* Start sending messages to let the workers know that they should stop */
54 for (int i = 0; i < receivers_count; i++) {
55 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
56 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
57 std::string* payload = new std::string("finalize"); // Make a copy of the data we will send
59 simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0);
60 pending_comms.push_back(comm);
61 XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
63 XBT_INFO("Done dispatching all messages");
65 /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
66 while (not pending_comms.empty()) {
67 simgrid::s4u::CommPtr comm = pending_comms.back();
68 comm->wait(); // we could provide a timeout as a parameter
69 pending_comms.pop_back(); // remove it from the list
72 XBT_INFO("Goodbye now!");
76 /* Receiver actor expects 1 argument: its ID */
78 simgrid::s4u::MailboxPtr mbox;
81 explicit Receiver(std::vector<std::string> args)
83 xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
84 std::string mboxName = std::string("receiver-") + args[1];
85 mbox = simgrid::s4u::Mailbox::byName(mboxName);
90 XBT_INFO("Wait for my first message");
91 for (bool done = false; not done;) {
92 std::string* received = static_cast<std::string*>(mbox->get());
93 XBT_INFO("I got a '%s'.", received->c_str());
94 done = (*received == "finalize"); // If it's a finalize message, we're done
95 // Receiving the message was all we were supposed to do
101 int main(int argc, char *argv[])
103 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
105 simgrid::s4u::Engine e(&argc, argv);
106 e.registerFunction<Sender>("sender");
107 e.registerFunction<Receiver>("receiver");
109 e.loadPlatform(argv[1]);
110 e.loadDeployment(argv[2]);