Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1bb5aa2d391d455bcf940883c80b8a32cae32fb
[simgrid.git] / examples / s4u / async-wait / s4u-async-wait.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 /* This example shows how to start many asynchronous communications,
7  * and block on them later.
8  */
9
10 #include "simgrid/s4u.hpp"
11 #include "xbt/str.h"
12 #include <cstdlib>
13 #include <iostream>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
16
17 /* Main function of the Sender process */
18 class sender {
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;
23   
24 public:
25   explicit sender(std::vector<std::string> args)
26 {
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]);
31 }
32 void operator()()
33 {
34   std::vector<simgrid::s4u::CommPtr>* pending_comms = new std::vector<simgrid::s4u::CommPtr>();
35
36   /* Start dispatching all messages to receivers, in a round robin fashion */
37   for (int i = 0; i < messages_count; i++) {
38     
39     std::string mbox_name = std::string("receiver-") + std::to_string(i % receivers_count);
40     mbox = simgrid::s4u::Mailbox::byName(mbox_name);
41     
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
45
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);
49   }
50
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
57     }
58   }
59
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++) {
62     char mailbox[80];
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);
67   }
68
69   XBT_INFO("Goodbye now!");
70
71 }
72 };
73
74 /* Receiver process expects 1 arguments: its ID */
75 class receiver {
76   simgrid::s4u::MailboxPtr mbox;
77   
78 public:
79   explicit receiver(std::vector<std::string> args)
80   {
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);
84 }
85
86 void operator()()
87 {
88   while (1) {
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 */
93       xbt_free(received);
94       break;
95     }
96   }
97 }
98 };
99
100 int main(int argc, char *argv[])
101 {
102   simgrid::s4u::Engine e(&argc, argv);
103
104   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
105
106   e.registerFunction<sender>("sender");
107   e.registerFunction<receiver>("receiver");
108
109   e.loadPlatform(argv[1]);
110   e.loadDeployment(argv[2]);
111   e.run();
112
113   return 0;
114 }