Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / mc-failing-assert / s4u-mc-failing-assert.cpp
1 /* Copyright (c) 2010-2022. 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 /******************** Non-deterministic message ordering  *********************/
7 /* Server assumes a fixed order in the reception of messages from its clients */
8 /* which is incorrect because the message ordering is non-deterministic       */
9 /******************************************************************************/
10
11 #include <simgrid/modelchecker.h>
12 #include <simgrid/s4u.hpp>
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(mc_assert_example, "Logging channel used in this example");
15
16 static int server(int worker_amount)
17 {
18   int value_got             = -1;
19   simgrid::s4u::Mailbox* mb = simgrid::s4u::Mailbox::by_name("server");
20   for (int count = 0; count < worker_amount; count++) {
21     auto msg  = mb->get_unique<int>();
22     value_got = *msg;
23   }
24   /*
25    * We assert here that the last message we got (which overwrite any previously received message) is the one from the
26    * last worker This will obviously fail when the messages are received out of order.
27    */
28   MC_assert(value_got == 2);
29
30   XBT_INFO("OK");
31   return 0;
32 }
33
34 static int client(int rank)
35 {
36   /* I just send my rank onto the mailbox. It must be passed as a stable memory block (thus the new) so that that
37    * memory survives even after the end of the client */
38
39   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("server");
40   mailbox->put(new int(rank), 1 /* communication cost is not really relevant in MC mode */);
41
42   XBT_INFO("Sent!");
43   return 0;
44 }
45
46 int main(int argc, char* argv[])
47 {
48   simgrid::s4u::Engine e(&argc, argv);
49   xbt_assert(argc > 1, "Usage: %s platform_file\n", argv[0]);
50
51   e.load_platform(argv[1]);
52   auto hosts = e.get_all_hosts();
53   xbt_assert(hosts.size() >= 3, "This example requires at least 3 hosts");
54
55   simgrid::s4u::Actor::create("server", hosts[0], &server, 2);
56   simgrid::s4u::Actor::create("client1", hosts[1], &client, 1);
57   simgrid::s4u::Actor::create("client2", hosts[2], &client, 2);
58
59   e.run();
60   return 0;
61 }