Logo AND Algorithmique Numérique Distribuée

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