Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / synchro-barrier / s4u-synchro-barrier.cpp
1 /* Copyright (c) 2006-2023. 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 #include "simgrid/s4u.hpp"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
9 namespace sg4 = simgrid::s4u;
10
11 /// Wait on the barrier then leave
12 static void worker(sg4::BarrierPtr barrier)
13 {
14     XBT_INFO("Waiting on the barrier");
15     barrier->wait();
16
17     XBT_INFO("Bye");
18 }
19
20 /// Spawn actor_count-1 workers and do a barrier with them
21 static void master(int actor_count)
22 {
23   sg4::BarrierPtr barrier = sg4::Barrier::create(actor_count);
24
25   XBT_INFO("Spawning %d workers", actor_count - 1);
26   for (int i = 0; i < actor_count - 1; i++) {
27     sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), worker, barrier);
28   }
29
30     XBT_INFO("Waiting on the barrier");
31     barrier->wait();
32
33     XBT_INFO("Bye");
34 }
35
36 int main(int argc, char **argv)
37 {
38   sg4::Engine e(&argc, argv);
39
40   // Parameter: Number of actores in the barrier
41   xbt_assert(argc >= 2, "Usage: %s <actor-count>\n", argv[0]);
42   int actor_count = std::stoi(argv[1]);
43   xbt_assert(actor_count > 0, "<actor-count> must be greater than 0");
44
45   e.load_platform(argc > 2 ? argv[2] : "../../platforms/two_hosts.xml");
46   sg4::Actor::create("master", e.host_by_name("Tremblay"), master, actor_count);
47   e.run();
48
49   return 0;
50 }