Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
almost converted
[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     comms.pop_back();
45   }
46   comms.clear();
47
48   /* Here we are waiting for the completion of all tasks */
49   for (int i = 0; i < receivers_count; i++) {
50     void* received = nullptr;
51     simgrid::s4u::CommPtr comm = mbox->get_async(&received);
52     comm->wait();
53     comm = nullptr;
54   }
55
56   XBT_INFO("Goodbye now!");
57 }
58 };
59
60 class receiver {
61 public:
62   explicit receiver(std::vector<std::string> args)
63 {
64   xbt_assert(args.size() == 2, "This function expects 2 parameters from the XML deployment file");
65   int id = xbt_str_parse_int(args[0].c_str(), "ID should be numerical, not %s");
66   int task_amount = xbt_str_parse_int(args[1].c_str(), "Invalid amount of tasks: %s");
67   void *received; 
68   std::vector<simgrid::s4u::CommPtr> comms;
69   simgrid::s4u::CommPtr comm;
70   simgrid::s4u::MailboxPtr mbox;
71   
72   simgrid::s4u::this_actor::sleep_for(10.0);
73   for (int i = 0; i < task_amount; i++) {
74     XBT_INFO("Wait to receive task %d", i);
75     received = NULL;
76     comm = mbox->get_async(&received);
77     comms.push_back(comm);
78   }
79
80   /* Here we are waiting for the receiving of all communications */
81   while (!comms.empty()) {
82     // returns the rank of the comm that just ended. Remove it.
83     comm=comms.back();
84     comms.pop_back();
85     // simgrid::s4u::this_actor::execute(comm);
86   }
87   comms.clear();
88   comm = nullptr;
89
90   /* Here we tell to sender that all tasks are done */
91   simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
92   XBT_INFO("I'm done. See you!");
93 }
94
95 int main(int argc, char *argv[])
96 {
97   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);  
98
99   e->loadDeployment(argv[2]); 
100  
101   e->registerFunction<sender>("sender");   
102   // e->registerFunction<receiver>("receiver");  
103
104   e->run();
105
106   delete e;
107   return 0;
108 }