Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c948ebb77b35a25a6bc6677ff8d2d6134ebade9b
[simgrid.git] / examples / s4u / async-waitall / s4u_async-waitall.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
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
12
13 class sender {
14   long number_of_tasks             = 0; /* - Number of tasks      */
15   long receivers_count             = 0; /* - Number of workers    */
16
17 public:
18   explicit sender(std::vector<std::string> args)
19 {
20   xbt_assert(args.size()== 4, "This function expects 5 parameters from the XML deployment file");
21   number_of_tasks = std::stol(args[0]);
22   double task_comp_size = std::stod(args[1]);
23   double task_comm_size = std::stod(args[2]);
24   receivers_count = std::stol(args[3]);
25
26 }
27 void operator()()
28 {
29   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
30   simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[number_of_tasks + receivers_count] ;
31
32   for (int i = 0; i < number_of_tasks; i++) {
33     char mailbox[80];
34     char taskname[80];
35     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
36     snprintf(taskname,79, "Task_%d", i);
37     comms[i] = mbox->put_async((void*)taskname, 42);
38     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
39   }
40   for (int i = 0; i < receivers_count; i++) {
41     char mailbox[80];
42     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
43     comms[i + number_of_tasks] = mbox->put_async((void*)"finalize", 42);
44     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
45   }
46
47   /* Here we are waiting for the completion of all communications */
48   for (int i = 0; i < number_of_tasks + receivers_count; i++)
49     comms[i]->wait();
50
51   delete [] comms;
52   XBT_INFO("Goodbye now!");
53 }
54 };
55
56 class receiver {
57 public:
58   explicit receiver(std::vector<std::string> args)
59 {
60   xbt_assert(args.size() == 1,"This function expects 1 parameter from the XML deployment file");
61   int id = xbt_str_parse_int(args[0].c_str(), "Any process of this example must have a numerical name, not %s");
62   char mailbox[80];
63   snprintf(mailbox,79, "receiver-%d", id);
64
65   simgrid::s4u::this_actor::sleep_for(10.0);
66
67   XBT_INFO("I'm done. See you!");
68 }
69 void operator()()
70 {
71   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
72   while (1) {
73     XBT_INFO("Wait to receive a task");
74     void *received = NULL;
75     simgrid::s4u::CommPtr comm = mbox->get_async(&received);
76     comm->wait();
77     std::string* receivedStr = static_cast<std::string*>(received);
78     if (receivedStr->compare("finalize") == 0) {
79       delete receivedStr;
80       break;
81     }
82     double *comp_size = static_cast<double*>(received);
83     /*  - Otherwise, process the task */
84     simgrid::s4u::this_actor::execute(*comp_size);
85   }
86   XBT_INFO("I'm done. See you!");
87 }
88 };
89
90 int main(int argc, char *argv[])
91 {
92   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv); 
93
94   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
95              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
96
97   e->registerFunction<sender>("sender");   
98   e->registerFunction<receiver>("receiver"); 
99
100   e->loadDeployment(argv[2]); 
101   e->run();
102
103   return 0;
104 }