Logo AND Algorithmique Numérique Distribuée

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