1 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */
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. */
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
10 * FIXME: Missing elements: timeouts, host/actor failures, link failures
13 #include "simgrid/s4u.hpp"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
19 static void usage(const char* binaryName, const char* defaultSend, const char* defaultRecv)
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"
25 " R regular send (after a little delay)\n"
26 " i asynchronous isend\n"
27 " I asynchronous isend (after a little delay)\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"
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);
45 static void sender(std::vector<std::string> args)
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 std::string* mboxName = new std::string("Test #" + std::to_string(test));
51 simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(*mboxName);
53 switch (args[0][test - 1]) {
55 XBT_INFO("Test %u: r (regular send)", test);
56 mbox->put((void*)mboxName, 42.0);
59 XBT_INFO("Test %u: R (sleep + regular send)", test);
60 simgrid::s4u::this_actor::sleep_for(0.5);
61 mbox->put((void*)mboxName, 42.0);
65 XBT_INFO("Test %u: i (asynchronous isend)", test);
66 mbox->put_async((void*)mboxName, 42.0)->wait();
69 XBT_INFO("Test %u: I (sleep + isend)", test);
70 simgrid::s4u::this_actor::sleep_for(0.5);
71 mbox->put_async((void*)mboxName, 42.0)->wait();
75 XBT_INFO("Test %u: d (detached send)", test);
76 mbox->put_init((void*)mboxName, 42.0)->detach();
79 XBT_INFO("Test %u: D (sleep + detached send)", test);
80 simgrid::s4u::this_actor::sleep_for(0.5);
81 mbox->put_init((void*)mboxName, 42.0)->detach();
84 xbt_die("Unknown sender spec for test %u: '%c'", test, args[0][test - 1]);
86 XBT_INFO("Test %u OK", test);
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
92 static void receiver(std::vector<std::string> args)
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 void* received = nullptr;
101 switch (args[0][test - 1]) {
103 XBT_INFO("Test %u: r (regular receive)", test);
104 received = mbox->get();
107 XBT_INFO("Test %u: R (sleep + regular receive)", test);
108 simgrid::s4u::this_actor::sleep_for(0.5);
109 received = mbox->get();
113 XBT_INFO("Test %u: i (asynchronous irecv)", test);
114 mbox->get_async(&received)->wait();
117 XBT_INFO("Test %u: I (sleep + asynchronous irecv)", test);
118 simgrid::s4u::this_actor::sleep_for(0.5);
119 mbox->get_async(&received)->wait();
122 XBT_INFO("Test %u: p (regular receive on permanent mailbox)", test);
123 mbox->set_receiver(simgrid::s4u::Actor::self());
124 received = mbox->get();
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();
133 XBT_INFO("Test %u: j (irecv on permanent mailbox)", test);
134 mbox->set_receiver(simgrid::s4u::Actor::self());
135 mbox->get_async(&received)->wait();
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(&received)->wait();
144 xbt_die("Unknown receiver spec for test %u: '%c'", test, args[0][test - 1]);
146 const std::string* receivedStr = static_cast<std::string*>(received);
147 xbt_assert(*receivedStr == mboxName);
149 XBT_INFO("Test %u OK", test);
151 simgrid::s4u::this_actor::sleep_for(0.5);
154 int main(int argc, char* argv[])
156 std::string specSend;
157 std::string specRecv;
158 for (char s : {'r', 'R', 'i', 'I', 'd', 'D'})
159 for (char r : {'r', 'R', 'i', 'I', 'p', 'P', 'j', 'J'}) {
163 std::vector<std::string> argSend{specSend.c_str()};
164 std::vector<std::string> argRecv{specRecv.c_str()};
166 simgrid::s4u::Engine e(&argc, argv);
168 usage(argv[0], specSend.c_str(), specRecv.c_str());
172 e.load_platform(argv[1]);
176 argSend.push_back(argv[2]);
180 argRecv.push_back(argv[3]);
182 xbt_assert(argSend.front().size() == argRecv.front().size(), "Sender and receiver spec must be of the same size");
184 std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
186 simgrid::s4u::Actor::create("sender", hosts[0], sender, argSend);
187 simgrid::s4u::Actor::create("recver", hosts[1], receiver, argRecv);
190 XBT_INFO("Simulation time %g", e.get_clock());