Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / teshsuite / s4u / comm-pt2pt / comm-pt2pt.cpp
1 /* Copyright (c) 2010-2021. 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 /* We want this test to be exhaustive in term of:
7  *   - communication involved (regular, asynchronous, detached, with a permanent receiver declared)
8  *   - whether the send or the receive was posted first
9  *
10  * FIXME: Missing elements: timeouts, host/actor failures, link failures
11  */
12
13 #include "simgrid/s4u.hpp"
14
15 #include <cstring>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
18
19 static void usage(const char* binaryName, const char* defaultSend, const char* defaultRecv)
20 {
21   std::fprintf(stderr, "Usage: %s examples/platforms/cluster_backbone.xml <send_spec> <recv_spec>\n"
22                        "where spec is a list of letters giving the kind of tests you want to see.\n"
23                        "Existing sender spec:\n"
24                        " r regular send\n"
25                        " R regular send (after a little delay)\n"
26                        " i asynchronous isend\n"
27                        " I asynchronous isend (after a little delay)\n"
28                        " d detached send\n"
29                        " D detached send (after a little delay)\n"
30                        "Existing receiver spec:\n"
31                        " r regular receive\n"
32                        " R regular receive (after a little delay)\n"
33                        " i asynchronous irecv\n"
34                        " I asynchronous irecv (after a little delay)\n"
35                        " p regular receive on permanent mailbox (after a little delay)\n"
36                        " P regular receive on permanent mailbox (after a little delay)\n"
37                        " j irecv on permanent mailbox (after a little delay)\n"
38                        " J irecv on permanent mailbox (after a little delay)\n"
39                        "\n"
40                        "Example 1: %s examples/platforms/cluster_backbone.xml rRiIdD rrrrrr # testing all send functions\n"
41                        "Default specs: %s %s (all possible pairs)\n",
42                binaryName, binaryName, defaultSend, defaultRecv);
43 }
44
45 static void sender(std::vector<std::string> args)
46 {
47   XBT_INFO("Sender spec: %s", args[0].c_str());
48   for (unsigned int test = 1; test <= args[0].size(); test++) {
49     simgrid::s4u::this_actor::sleep_until(test * 5 - 5);
50     auto* mboxName = new std::string("Test #" + std::to_string(test));
51     auto* mbox     = simgrid::s4u::Mailbox::by_name(*mboxName);
52
53     switch (args[0][test - 1]) {
54       case 'r':
55         XBT_INFO("Test %u: r (regular send)", test);
56         mbox->put(mboxName, 42.0);
57         break;
58       case 'R':
59         XBT_INFO("Test %u: R (sleep + regular send)", test);
60         simgrid::s4u::this_actor::sleep_for(0.5);
61         mbox->put(mboxName, 42.0);
62         break;
63
64       case 'i':
65         XBT_INFO("Test %u: i (asynchronous isend)", test);
66         mbox->put_async(mboxName, 42.0)->wait();
67         break;
68       case 'I':
69         XBT_INFO("Test %u: I (sleep + isend)", test);
70         simgrid::s4u::this_actor::sleep_for(0.5);
71         mbox->put_async(mboxName, 42.0)->wait();
72         break;
73
74       case 'd':
75         XBT_INFO("Test %u: d (detached send)", test);
76         mbox->put_init(mboxName, 42.0)->detach();
77         break;
78       case 'D':
79         XBT_INFO("Test %u: D (sleep + detached send)", test);
80         simgrid::s4u::this_actor::sleep_for(0.5);
81         mbox->put_init(mboxName, 42.0)->detach();
82         break;
83       default:
84         xbt_die("Unknown sender spec for test %u: '%c'", test, args[0][test - 1]);
85     }
86     XBT_INFO("Test %u OK", test);
87   }
88   simgrid::s4u::this_actor::sleep_for(0.5);
89   // FIXME: we should test what happens when the process ends before the end of remote comm instead of hiding it
90 }
91
92 static void receiver(std::vector<std::string> args)
93 {
94   XBT_INFO("Receiver spec: %s", args[0].c_str());
95   for (unsigned int test = 1; test <= args[0].size(); test++) {
96     simgrid::s4u::this_actor::sleep_until(test * 5 - 5);
97     std::string mboxName        = "Test #" + std::to_string(test);
98     simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(mboxName);
99     std::string* received       = nullptr;
100
101     switch (args[0][test - 1]) {
102       case 'r':
103         XBT_INFO("Test %u: r (regular receive)", test);
104         received = mbox->get<std::string>();
105         break;
106       case 'R':
107         XBT_INFO("Test %u: R (sleep + regular receive)", test);
108         simgrid::s4u::this_actor::sleep_for(0.5);
109         received = mbox->get<std::string>();
110         break;
111
112       case 'i':
113         XBT_INFO("Test %u: i (asynchronous irecv)", test);
114         mbox->get_async<std::string>(&received)->wait();
115         break;
116       case 'I':
117         XBT_INFO("Test %u: I (sleep + asynchronous irecv)", test);
118         simgrid::s4u::this_actor::sleep_for(0.5);
119         mbox->get_async<std::string>(&received)->wait();
120         break;
121       case 'p':
122         XBT_INFO("Test %u: p (regular receive on permanent mailbox)", test);
123         mbox->set_receiver(simgrid::s4u::Actor::self());
124         received = mbox->get<std::string>();
125         break;
126       case 'P':
127         XBT_INFO("Test %u: P (sleep + regular receive on permanent mailbox)", test);
128         simgrid::s4u::this_actor::sleep_for(0.5);
129         mbox->set_receiver(simgrid::s4u::Actor::self());
130         received = mbox->get<std::string>();
131         break;
132       case 'j':
133         XBT_INFO("Test %u: j (irecv on permanent mailbox)", test);
134         mbox->set_receiver(simgrid::s4u::Actor::self());
135         mbox->get_async<std::string>(&received)->wait();
136         break;
137       case 'J':
138         XBT_INFO("Test %u: J (sleep + irecv on permanent mailbox)", test);
139         simgrid::s4u::this_actor::sleep_for(0.5);
140         mbox->set_receiver(simgrid::s4u::Actor::self());
141         mbox->get_async<std::string>(&received)->wait();
142         break;
143       default:
144         xbt_die("Unknown receiver spec for test %u: '%c'", test, args[0][test - 1]);
145     }
146     xbt_assert(*received == mboxName);
147     delete received;
148     XBT_INFO("Test %u OK", test);
149   }
150   simgrid::s4u::this_actor::sleep_for(0.5);
151 }
152
153 int main(int argc, char* argv[])
154 {
155   std::string specSend;
156   std::string specRecv;
157   for (char s : {'r', 'R', 'i', 'I', 'd', 'D'})
158     for (char r : {'r', 'R', 'i', 'I', 'p', 'P', 'j', 'J'}) {
159       specSend += s;
160       specRecv += r;
161     }
162   std::vector<std::string> argSend{specSend.c_str()};
163   std::vector<std::string> argRecv{specRecv.c_str()};
164
165   simgrid::s4u::Engine e(&argc, argv);
166   if (argc < 2) {
167     usage(argv[0], specSend.c_str(), specRecv.c_str());
168     return 1;
169   }
170
171   e.load_platform(argv[1]);
172
173   if (argc >= 3) {
174     argSend.clear();
175     argSend.emplace_back(argv[2]);
176   }
177   if (argc >= 4) {
178     argRecv.clear();
179     argRecv.emplace_back(argv[3]);
180   }
181   xbt_assert(argSend.front().size() == argRecv.front().size(), "Sender and receiver spec must be of the same size");
182
183   std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
184
185   simgrid::s4u::Actor::create("sender", hosts[0], sender, argSend);
186   simgrid::s4u::Actor::create("recver", hosts[1], receiver, argRecv);
187
188   e.run();
189   XBT_INFO("Simulation time %g", e.get_clock());
190
191   return 0;
192 }