Logo AND Algorithmique Numérique Distribuée

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