Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
26f711ec02bc7706c6e362313e33f1c9f28c49bd
[simgrid.git] / examples / s4u / async-waitany / s4u_async-waitany.cpp
1 /* Copyright (c) 2010-2016. 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 #include "simgrid/s4u.hpp"
7 #include "xbt/str.h"  
8 #include <cstdlib>
9 #include <iostream>
10 #include <vector>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
13
14 class sender {
15   long number_of_tasks             = 0; /* - Number of tasks      */
16   long receivers_count             = 0; /* - Number of workers    */
17   int diff_com                     = 0;
18
19 public:
20   explicit sender(std::vector<std::string> args)
21 {
22   xbt_assert(args.size()== 5, "This function expects 5 parameters from the XML deployment file");
23   number_of_tasks = std::stol(args[0]);
24   double task_comp_size = std::stod(args[1]);
25   double task_comm_size = std::stod(args[2]);
26   receivers_count = std::stol(args[3]);
27   diff_com = std::stoi(args[4]);
28
29 }
30 void operator()()
31 {
32   std::vector<simgrid::s4u::CommPtr> comms;
33   simgrid::s4u::CommPtr comm;
34   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
35   /* First pack the communications in the dynar */
36   for (int i = 0; i < number_of_tasks; i++) {
37     double coef = (diff_com == 0) ? 1 : (i + 1);
38     char mailbox[80];
39     char taskname[80];
40     snprintf(mailbox,79, "receiver-%ld", (i % receivers_count));
41     snprintf(taskname,79, "Task_%d", i);
42     comm = mbox->put_async((void*)taskname, 42.0);
43     comms.push_back(comm);
44   }
45
46   /* Here we are waiting for the completion of all communications */
47   while (!comms.empty()) {
48     comm=comms.back();
49     comms.pop_back();
50     comm->wait();
51   }
52   comms.clear();
53   comm = nullptr;
54
55   /* Here we are waiting for the completion of all tasks */
56   for (int i = 0; i < receivers_count; i++) {
57     void* received = nullptr;
58     simgrid::s4u::CommPtr comm = mbox->get_async(&received);
59     comm->wait();
60     comm = nullptr;
61   }
62   XBT_INFO("Goodbye now!");
63 }
64 };
65
66 class receiver {
67   int id                     = 0;
68   int task_amount            = 0;
69 public:
70   explicit receiver(std::vector<std::string> args)
71 {
72   xbt_assert(args.size() == 2, "This function expects 2 parameters from the XML deployment file");
73   id = std::stoi(args[0]);
74   task_amount = std::stoi(args[1]);
75 }
76 void operator()()
77 {
78   void *received; 
79   std::vector<simgrid::s4u::CommPtr> comms;
80   simgrid::s4u::CommPtr comm;
81   simgrid::s4u::MailboxPtr mbox;
82   
83   simgrid::s4u::this_actor::sleep_for(10.0);
84   for (int i = 0; i < task_amount; i++) {
85     XBT_INFO("Wait to receive task %d", i);
86     received = NULL;
87     comm = mbox->get_async(&received);
88     comms.push_back(comm);
89   }
90
91   /* Here we are waiting for the receiving of all communications */
92   while (!comms.empty()) {
93     // returns the rank of the comm that just ended. Remove it.
94     comm=comms.back();
95     comms.pop_back();
96     comm->wait();
97   }
98   comms.clear();
99   comm = nullptr;
100   /* Here we tell to sender that all tasks are done */
101   simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
102   XBT_INFO("I'm done. See you!");
103 }
104 };
105
106 int main(int argc, char *argv[])
107 {
108   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);  
109
110    e->registerFunction<sender>("sender");   
111    e->registerFunction<receiver>("receiver");  
112   
113   e->loadDeployment(argv[2]); 
114   e->run();
115
116   delete e;
117   return 0;
118 }