1 /* Copyright (c) 2010-2019. 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::Activity::wait_until() and
7 * simgrid::s4u::Activity::wait_for() on a given communication.
9 * It is very similar to the async-wait example, but the sender initially
10 * does some waits that are too short before doing an infinite wait.
13 #include "simgrid/s4u.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_waituntil, "Messages specific for this s4u example");
20 static int sender(int argc, char** argv)
22 xbt_assert(argc == 4, "Expecting 3 parameters from the XML deployment file but got %d", argc);
23 long messages_count = std::stol(argv[1]); /* - number of tasks */
24 double msg_size = std::stol(argv[2]); /* - communication cost in bytes */
25 long receivers_count = std::stod(argv[3]); /* - number of receivers */
27 std::vector<simgrid::s4u::CommPtr> pending_comms;
29 /* Start dispatching all messages to receivers, in a round robin fashion */
30 for (int i = 0; i < messages_count; i++) {
32 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
33 simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(mboxName);
34 std::string msgName = std::string("Message ") + std::to_string(i);
35 std::string* payload = new std::string(msgName); // copy the data we send:
36 // 'msgName' is not a stable storage location
37 XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
38 /* Create a communication representing the ongoing communication */
39 simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size);
40 /* Add this comm to the vector of all known comms */
41 pending_comms.push_back(comm);
44 /* Start sending messages to let the workers know that they should stop */
45 for (int i = 0; i < receivers_count; i++) {
46 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
47 simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(mboxName);
48 std::string* payload = new std::string("finalize"); // Make a copy of the data we will send
50 simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0);
51 pending_comms.push_back(comm);
52 XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
54 XBT_INFO("Done dispatching all messages");
56 /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
57 while (not pending_comms.empty()) {
58 simgrid::s4u::CommPtr comm = pending_comms.back();
60 pending_comms.pop_back(); // remove it from the list
63 XBT_INFO("Goodbye now!");
67 /* Receiver actor expects 1 argument: its ID */
68 static int receiver(int argc, char** argv)
70 xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
71 simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(std::string("receiver-") + argv[1]);
73 XBT_INFO("Wait for my first message");
74 for (bool cont = true; cont;) {
75 std::string* received = static_cast<std::string*>(mbox->get());
76 XBT_INFO("I got a '%s'.", received->c_str());
77 if (*received == "finalize")
78 cont = false; // If it's a finalize message, we're done.
84 int main(int argc, char* argv[])
86 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
88 simgrid::s4u::Engine e(&argc, argv);
89 e.register_function("sender", &sender);
90 e.register_function("receiver", &receiver);
92 e.load_platform(argv[1]);
93 e.load_deployment(argv[2]);