1 /* Copyright (c) 2010-2017. 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 using namespace simgrid::s4u;
21 static void usage(const char* binaryName, const char* defaultSend, const char* defaultRecv)
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"
27 " R regular send (after a little delay)\n"
28 " i asynchronous isend\n"
29 " I asynchronous isend (after a little delay)\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"
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);
48 static void sender(std::vector<std::string> args)
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());
56 switch (args[0][test - 1]) {
58 XBT_INFO("Test %u: r (regular send)", test);
59 mbox->put((void*)mboxName, 42.0);
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);
68 XBT_INFO("Test %u: i (asynchronous isend)", test);
69 mbox->put_async((void*)mboxName, 42.0)->wait();
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();
78 XBT_INFO("Test %u: d (detached send)", test);
79 mbox->put_init((void*)mboxName, 42.0)->detach();
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();
87 xbt_die("Unknown sender spec for test %u: '%c'", test, args[0][test - 1]);
89 XBT_INFO("Test %u OK", test);
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
95 static void receiver(std::vector<std::string> args)
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;
104 switch (args[0][test - 1]) {
106 XBT_INFO("Test %u: r (regular receive)", test);
107 received = mbox->get();
110 XBT_INFO("Test %u: R (sleep + regular receive)", test);
111 simgrid::s4u::this_actor::sleep_for(0.5);
112 received = mbox->get();
116 XBT_INFO("Test %u: i (asynchronous irecv)", test);
117 mbox->get_async(&received)->wait();
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();
125 XBT_INFO("Test %u: p (regular receive on permanent mailbox)", test);
126 mbox->setReceiver(Actor::self());
127 received = mbox->get();
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();
136 XBT_INFO("Test %u: j (irecv on permanent mailbox)", test);
137 mbox->setReceiver(Actor::self());
138 mbox->get_async(&received)->wait();
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();
147 xbt_die("Unknown receiver spec for test %u: '%c'", test, args[0][test - 1]);
149 std::string* receivedStr = static_cast<std::string*>(received);
150 xbt_assert(*receivedStr == mboxName);
152 XBT_INFO("Test %u OK", test);
154 simgrid::s4u::this_actor::sleep_for(0.5);
157 int main(int argc, char* argv[])
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'}) {
166 std::vector<std::string> argSend{specSend.c_str()};
167 std::vector<std::string> argRecv{specRecv.c_str()};
169 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
171 usage(argv[0], specSend.c_str(), specRecv.c_str());
173 e->loadPlatform(argv[1]);
177 argSend.push_back(argv[2]);
181 argRecv.push_back(argv[3]);
183 xbt_assert(argSend.front().size() == argRecv.front().size(), "Sender and receiver spec must be of the same size");
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);
191 XBT_INFO("Simulation time %g", e->getClock());