Logo AND Algorithmique Numérique Distribuée

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