1 /* Copyright (c) 2010-2021. 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 comm-wait example, but the sender initially
10 * does some waits that are too short before doing an infinite wait.
13 #include "simgrid/s4u.hpp"
17 namespace sg4 = simgrid::s4u;
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_waituntil, "Messages specific for this s4u example");
21 static void 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 messages */
25 long msg_size = std::stol(argv[2]); /* - message size in bytes */
26 long receivers_count = std::stol(argv[3]); /* - number of receivers */
28 std::vector<sg4::CommPtr> pending_comms;
30 /* Start dispatching all messages to receivers, in a round robin fashion */
31 for (int i = 0; i < messages_count; i++) {
32 std::string mboxName = std::string("receiver-") + std::to_string(i % receivers_count);
33 sg4::Mailbox* mbox = sg4::Mailbox::by_name(mboxName);
34 std::string msgName = std::string("Message ") + std::to_string(i);
35 auto* 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 sg4::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 sg4::Mailbox* mbox = sg4::Mailbox::by_name(mboxName);
49 auto* payload = new std::string("finalize"); // Make a copy of the data we will send
51 sg4::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 sg4::CommPtr comm = pending_comms.back();
61 pending_comms.pop_back(); // remove it from the list
64 XBT_INFO("Goodbye now!");
67 /* Receiver actor expects 1 argument: its ID */
68 static void receiver(int argc, char** argv)
70 xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
71 sg4::Mailbox* mbox = sg4::Mailbox::by_name(std::string("receiver-") + argv[1]);
73 XBT_INFO("Wait for my first message");
74 for (bool cont = true; cont;) {
75 auto received = mbox->get_unique<std::string>();
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.
82 int main(int argc, char* argv[])
84 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
86 sg4::Engine e(&argc, argv);
87 e.register_function("sender", &sender);
88 e.register_function("receiver", &receiver);
90 e.load_platform(argv[1]);
91 e.load_deployment(argv[2]);