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 #include "simgrid/s4u.hpp"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
12 using namespace simgrid::s4u;
14 static void usage(const char* binaryName, const char* defaultSend, const char* defaultRecv)
16 std::fprintf(stderr, "Usage: %s examples/platforms/cluster.xml <send_spec> <recv_spec>\n"
17 "where spec is a list of letters giving the kind of tests you want to see.\n"
18 "Existing sender spec:\n"
20 " R regular send (after a little delay)\n"
21 " i asynchronous isend\n"
22 " I asynchronous isend (after a little delay)\n"
24 " D detached send (after a little delay)\n"
25 "Existing receiver spec:\n"
26 " r regular receive\n"
27 " R regular receive (after a little delay)\n"
28 " i asynchronous irecv\n"
29 " I asynchronous irecv (after a little delay)\n"
30 " p regular receive on permanent mailbox (after a little delay)\n"
31 " P regular receive on permanent mailbox (after a little delay)\n"
32 " j irecv on permanent mailbox (after a little delay)\n"
33 " J irecv on permanent mailbox (after a little delay)\n"
35 "Example 1: %s examples/platforms/cluster.xml ripd rrrr # testing fancy functions\n"
36 "Default specs: %s %s (all possible pair)",
37 binaryName, binaryName, defaultSend, defaultRecv);
41 static void receiver(std::vector<std::string> args)
43 XBT_INFO("Receiver spec: %s", args[0].c_str());
44 for (unsigned int test = 1; test <= args[0].size(); test++) {
45 this_actor::sleep_until(test * 5 - 5);
46 char* mboxName = bprintf("Test #%u", test);
47 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
49 switch (args[0][test - 1]) {
51 XBT_INFO("Test %d: r (regular send)", test);
52 simgrid::s4u::this_actor::send(mbox, (void*)mboxName, 42.0);
55 XBT_INFO("Test %d: R (sleep + regular send)", test);
56 simgrid::s4u::this_actor::sleep_for(0.5);
57 simgrid::s4u::this_actor::send(mbox, (void*)mboxName, 42.0);
61 XBT_INFO("Test %d: i (asynchronous isend)", test);
62 simgrid::s4u::this_actor::isend(mbox, (void*)mboxName, 42.0)->wait();
65 XBT_INFO("Test %d: I (sleep + isend)", test);
66 simgrid::s4u::this_actor::sleep_for(0.5);
67 simgrid::s4u::this_actor::isend(mbox, (void*)mboxName, 42.0)->wait();
71 XBT_INFO("Test %d: d (detached send)", test);
72 simgrid::s4u::this_actor::dsend(mbox, (void*)mboxName, 42.0);
75 XBT_INFO("Test %d: D (sleep + detached send)", test);
76 simgrid::s4u::this_actor::sleep_for(0.5);
77 simgrid::s4u::this_actor::dsend(mbox, (void*)mboxName, 42.0);
80 xbt_die("Unknown sender spec for test %d: '%c'", test, args[0][test - 1]);
82 XBT_INFO("Test %d OK", test);
84 simgrid::s4u::this_actor::sleep_for(0.5);
85 // FIXME: we should test what happens when the process ends before the end of remote comm instead of hiding it
88 static void sender(std::vector<std::string> args)
90 XBT_INFO("Sender spec: %s", args[0].c_str());
91 for (unsigned int test = 1; test <= args[0].size(); test++) {
92 this_actor::sleep_until(test * 5 - 5);
93 char* mboxName = bprintf("Test #%u", test);
94 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
95 void* received = nullptr;
97 switch (args[0][test - 1]) {
99 XBT_INFO("Test %d: r (regular receive)", test);
100 received = simgrid::s4u::this_actor::recv(mbox);
103 XBT_INFO("Test %d: R (sleep + regular receive)", test);
104 simgrid::s4u::this_actor::sleep_for(0.5);
105 received = simgrid::s4u::this_actor::recv(mbox);
109 XBT_INFO("Test %d: i (asynchronous irecv)", test);
110 simgrid::s4u::this_actor::irecv(mbox, &received)->wait();
113 XBT_INFO("Test %d: I (sleep + asynchronous irecv)", test);
114 simgrid::s4u::this_actor::sleep_for(0.5);
115 simgrid::s4u::this_actor::irecv(mbox, &received)->wait();
118 XBT_INFO("Test %d: p (regular receive on permanent mailbox)", test);
119 mbox->setReceiver(Actor::self());
120 received = simgrid::s4u::this_actor::recv(mbox);
123 XBT_INFO("Test %d: P (sleep + regular receive on permanent mailbox)", test);
124 simgrid::s4u::this_actor::sleep_for(0.5);
125 mbox->setReceiver(Actor::self());
126 received = simgrid::s4u::this_actor::recv(mbox);
129 XBT_INFO("Test %d: j (irecv on permanent mailbox)", test);
130 mbox->setReceiver(Actor::self());
131 simgrid::s4u::this_actor::irecv(mbox, &received)->wait();
134 XBT_INFO("Test %d: J (sleep + irecv on permanent mailbox)", test);
135 simgrid::s4u::this_actor::sleep_for(0.5);
136 mbox->setReceiver(Actor::self());
137 simgrid::s4u::this_actor::irecv(mbox, &received)->wait();
140 xbt_die("Unknown receiver spec for test %d: '%c'", test, args[0][test - 1]);
143 xbt_assert(strcmp(static_cast<char*>(received), mboxName) == 0);
146 XBT_INFO("Test %d OK", test);
148 simgrid::s4u::this_actor::sleep_for(0.5);
151 int main(int argc, char* argv[])
153 std::string specSend;
154 std::string specRecv;
155 for (char s : {'r', 'R', 'i', 'I', 'p', 'P', 'j', 'J'})
156 for (char r : {'r', 'R', 'i', 'I', 'd', 'D'}) {
160 std::vector<std::string> argSend{specSend.c_str()};
161 std::vector<std::string> argRecv{specRecv.c_str()};
163 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
165 usage(argv[0], specSend.c_str(), specRecv.c_str());
167 e->loadPlatform(argv[1]);
171 argSend.push_back(argv[2]);
175 argRecv.push_back(argv[3]);
177 xbt_assert(argSend.front().size() == argRecv.front().size(), "Sender and receiver spec must be of the same size");
179 simgrid::s4u::Host** hosts = sg_host_list();
180 simgrid::s4u::Actor::createActor("sender", hosts[0], sender, argSend);
181 simgrid::s4u::Actor::createActor("recver", hosts[1], receiver, argRecv);
185 XBT_INFO("Simulation time %g", e->getClock());