Logo AND Algorithmique Numérique Distribuée

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