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 start many asynchronous communications,
7 * and block on them later.
10 #include "simgrid/s4u.hpp"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
17 /* Main function of the Sender process */
19 long messages_count; /* - number of tasks */
20 long receivers_count; /* - number of receivers */
21 double msg_size; /* - communication cost in bytes */
22 simgrid::s4u::MailboxPtr mbox;
25 explicit sender(std::vector<std::string> args)
27 xbt_assert(args.size() == 4, "The sender function expects 3 arguments from the XML deployment file");
28 messages_count = std::stol(args[1]);
29 msg_size = std::stod(args[2]);
30 receivers_count = std::stol(args[3]);
34 std::vector<simgrid::s4u::CommPtr>* pending_comms = new std::vector<simgrid::s4u::CommPtr>();
36 /* Start dispatching all messages to receivers, in a round robin fashion */
37 for (int i = 0; i < messages_count; i++) {
39 std::string mbox_name = std::string("receiver-") + std::to_string(i % receivers_count);
40 mbox = simgrid::s4u::Mailbox::byName(mbox_name);
42 /* Create a communication representing the ongoing communication */
43 std::string msgName = std::string("Message ") + std::to_string(i);
44 char* payload = xbt_strdup(msgName.c_str()); // copy the data we send: 'msgName' is not a stable storage location
46 simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, msg_size);
47 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
48 pending_comms->push_back(comm);
51 /* Now that all comms are in flight, wait for all of them (one after the other) */
52 for (int i = 0; i < messages_count; i++) {
53 while (not pending_comms->empty()) {
54 simgrid::s4u::CommPtr comm = pending_comms->back();
55 comm->wait(); // we could provide a timeout as a parameter
56 pending_comms->pop_back(); // remove it from the list
60 /* Start sending messages to let the workers know that they should stop (in a synchronous way) */
61 for (int i = 0; i < receivers_count; i++) {
63 char* payload = xbt_strdup("finalize");
64 snprintf(mailbox, 79, "receiver-%d", i);
65 mbox->put((void*)payload, 0); // instantaneous message (payload size is 0) sent in a synchronous way (with put)
66 XBT_INFO("Send to receiver-%d finalize", i);
69 XBT_INFO("Goodbye now!");
74 /* Receiver process expects 1 arguments: its ID */
76 simgrid::s4u::MailboxPtr mbox;
79 explicit receiver(std::vector<std::string> args)
81 xbt_assert(args.size() == 2, "The receiver function takes a unique parameter from the XML deployment file");
82 std::string mbox_name = std::string("receiver-") + args[1];
83 mbox = simgrid::s4u::Mailbox::byName(mbox_name);
89 XBT_INFO("Wait to receive a task");
90 char* received = static_cast<char*>(mbox->get());
91 XBT_INFO("I got a '%s'.", received);
92 if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
100 int main(int argc, char *argv[])
102 simgrid::s4u::Engine e(&argc, argv);
104 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
106 e.registerFunction<sender>("sender");
107 e.registerFunction<receiver>("receiver");
109 e.loadPlatform(argv[1]);
110 e.loadDeployment(argv[2]);