Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s4u-async-wait is complete
[simgrid.git] / examples / s4u / async-wait / s4u-async-wait.cpp
1 /* Copyright (c) 2010-2017. 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 /* This example shows how to block until the completion of a communication.
7  */
8
9  #include "simgrid/s4u.hpp"
10  #include "xbt/str.h"
11  #include <cstdlib>
12  #include <iostream>
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
15
16 /* Main function of the Sender process */
17 class sender {
18   long messages_count;             /* - number of tasks */
19   long receivers_count;            /* - number of receivers */
20   double sleep_start_time;         /* - start time */
21   double sleep_test_time;          /* - test time */
22   double msg_size;                 /* - computational cost */
23   double task_comm_size;           /* - communication cost */
24   simgrid::s4u::MailboxPtr mbox;
25   
26 public:
27   explicit sender(std::vector<std::string> args)
28 {
29   xbt_assert(args.size() == 7, "The sender function expects 6 arguments from the XML deployment file");
30   messages_count = std::stol(args[1]);
31   msg_size = std::stod(args[2]); 
32   task_comm_size = std::stod(args[3]); 
33   receivers_count = std::stol(args[4]);    
34   double sleep_start_time = std::stod(args[5]);
35   double sleep_test_time = std::stod(args[6]);
36   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
37 }
38 void operator()()
39 {
40   /* Start dispatching all messages to receivers, in a round robin fashion */
41   for (int i = 0; i < messages_count; i++) {
42     char mailbox[80];
43     char taskname[80];
44     
45     std::string mbox_name = std::string("receiver-") + std::to_string(i % receivers_count);
46     mbox = simgrid::s4u::Mailbox::byName(mbox_name);
47     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
48     snprintf(taskname,79, "Task_%d", i);
49     
50     /* Create a communication representing the ongoing communication */
51     simgrid::s4u::CommPtr comm = mbox->put_async((void*)mailbox, msg_size);
52     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
53     comm->wait(task_comm_size);
54   }
55   /* Start sending messages to let the workers know that they should stop */
56   for (int i = 0; i < receivers_count; i++) {
57     char mailbox[80];
58     char* payload   = xbt_strdup("finalize"); 
59     snprintf(mailbox, 79, "receiver-%d", i);
60     simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0);
61     comm->wait(task_comm_size);
62     XBT_INFO("Send to receiver-%d finalize", i);
63   }
64
65   XBT_INFO("Goodbye now!");
66
67 }
68 };
69
70 /* Receiver process expects 3 arguments: */
71 class receiver {
72   int id;                   /* - unique id */
73   double sleep_start_time;  /* - start time */
74   double sleep_test_time;   /* - test time */
75   simgrid::s4u::MailboxPtr mbox;
76   
77 public:
78   explicit receiver(std::vector<std::string> args)
79   {
80   xbt_assert(args.size() == 4, "The relay_runner function does not accept any parameter from the XML deployment file");
81   id = std::stoi(args[1]);
82   sleep_start_time = std::stod(args[2]); 
83   sleep_test_time = std::stod(args[3]);   
84   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
85   std::string mbox_name = std::string("receiver-") + std::to_string(id);
86   mbox = simgrid::s4u::Mailbox::byName(mbox_name);
87 }
88
89 void operator()()
90 {
91   char mailbox[80];
92   snprintf(mailbox,79, "receiver-%d", id);
93   while (1) {
94     char* received = static_cast<char*>(mbox->get());
95     XBT_INFO("Wait to receive a task");
96     XBT_INFO("I got a '%s'.", received);
97     if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
98       xbt_free(received);
99       break;
100     }
101   }
102 }
103 };
104
105 int main(int argc, char *argv[])
106 {
107   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
108   
109     xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
110   
111     e->registerFunction<sender>("sender");
112     e->registerFunction<receiver>("receiver");
113   
114     e->loadPlatform(argv[1]);
115     e->loadDeployment(argv[2]);
116     e->run();
117   
118     return 0;
119 }