Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a3ac331ba853aa599ef30433f445c1e3c93633ca
[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 public:
15   explicit sender(std::vector<std::string> args)
16 {
17   xbt_assert(args.size()== 4, "This function expects 5 parameters from the XML deployment file");
18   long number_of_tasks = xbt_str_parse_int(args[0].c_str(), "Invalid amount of tasks: %s");
19   double task_comp_size = xbt_str_parse_double(args[1].c_str(), "Invalid computational size: %s");
20   double task_comm_size = xbt_str_parse_double(args[2].c_str(), "Invalid communication size: %s");
21   long receivers_count = xbt_str_parse_int(args[3].c_str(), "Invalid amount of receivers: %s");
22
23   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
24   simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[number_of_tasks + receivers_count] ;
25
26   for (int i = 0; i < number_of_tasks; i++) {
27     char mailbox[80];
28     char taskname[80];
29     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
30     snprintf(taskname,79, "Task_%d", i);
31     comms[i] = mbox->put_async((void*)taskname, 42);
32     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
33   }
34   for (int i = 0; i < receivers_count; i++) {
35     char mailbox[80];
36     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
37
38     comms[i + number_of_tasks] = mbox->put_async((void*)"finalize", 42);
39     
40     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
41   }
42
43   /* Here we are waiting for the completion of all communications */
44   for (int i = 0; i < number_of_tasks + receivers_count; i++)
45     comms[i]->wait();
46
47   delete [] comms;
48 }
49 void operator()()
50 {
51   XBT_INFO("Goodbye now!");
52 }
53 };
54
55 class receiver {
56 public:
57   explicit receiver(std::vector<std::string> args)
58 {
59   xbt_assert(args.size() == 1,"This function expects 1 parameter from the XML deployment file");
60   int id = xbt_str_parse_int(args[0].c_str(), "Any process of this example must have a numerical name, not %s");
61   void *received;
62   char mailbox[80];
63   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
64   snprintf(mailbox,79, "receiver-%d", id);
65
66   simgrid::s4u::this_actor::sleep_for(10.0);
67   while (1) {
68     XBT_INFO("Wait to receive a task");
69     received = NULL;
70     simgrid::s4u::CommPtr comm = mbox->get_async(&received);
71     comm->wait();
72     if (strcmp(received, "finalize") == 0) {
73       delete received;
74       break;
75     }
76
77     XBT_INFO("Processing \"%s\"", received);
78     // MSG_task_execute(task);
79     XBT_INFO("\"%s\" done", recieved);
80     delete recieved;
81   }
82   XBT_INFO("I'm done. See you!");
83 }
84 void operator()()
85 {
86   simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
87   XBT_INFO("I'm done. See you!");
88 }
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
101   e->loadDeployment(argv[2]); 
102   e->run();
103
104   return 0;
105 }