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"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
21 long messages_count; /* - number of tasks */
22 long receivers_count; /* - number of receivers */
23 double msg_size; /* - communication cost in bytes */
26 explicit Sender(std::vector<std::string> args)
28 xbt_assert(args.size() == 4, "Expecting 3 parameters from the XML deployment file but got %zu", args.size());
29 messages_count = std::stol(args[1]);
30 msg_size = std::stod(args[2]);
31 receivers_count = std::stol(args[3]);
35 std::vector<simgrid::s4u::CommPtr> pending_comms;
37 /* Start dispatching all messages to receivers, in a round robin fashion */
38 for (int i = 0; i < messages_count; i++) {
40 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
41 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
42 std::string msgName = std::string("Message ") + std::to_string(i);
43 char* payload = xbt_strdup(msgName.c_str()); // copy the data we send: 'msgName' is not a stable storage location
45 XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
46 /* Create a communication representing the ongoing communication */
47 simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, msg_size);
48 /* Add this comm to the vector of all known comms */
49 pending_comms.push_back(comm);
52 /* Start sending messages to let the workers know that they should stop */
53 for (int i = 0; i < receivers_count; i++) {
54 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
55 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
56 char* payload = xbt_strdup("finalize"); // Make a copy of the data we will send
58 simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0);
59 pending_comms.push_back(comm);
60 XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
62 XBT_INFO("Done dispatching all messages");
64 /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
65 while (not pending_comms.empty()) {
66 simgrid::s4u::CommPtr comm = pending_comms.back();
67 comm->wait(); // we could provide a timeout as a parameter
68 pending_comms.pop_back(); // remove it from the list
71 XBT_INFO("Goodbye now!");
75 /* Receiver actor expects 1 argument: its ID */
77 simgrid::s4u::MailboxPtr mbox;
80 explicit Receiver(std::vector<std::string> args)
82 xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
83 std::string mboxName = std::string("receiver-") + args[1];
84 mbox = simgrid::s4u::Mailbox::byName(mboxName);
89 XBT_INFO("Wait for my first message");
91 char* received = static_cast<char*>(mbox->get());
92 XBT_INFO("I got a '%s'.", received);
93 if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
97 /* Otherwise receiving the message was all we were supposed to do */
103 int main(int argc, char *argv[])
105 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
107 simgrid::s4u::Engine e(&argc, argv);
108 e.registerFunction<Sender>("sender");
109 e.registerFunction<Receiver>("receiver");
111 e.loadPlatform(argv[1]);
112 e.loadDeployment(argv[2]);