Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e131c30cccfd39e59a9e682a6e5d10926909cc5a
[simgrid.git] / examples / s4u / synchro-barrier / s4u-synchro-barrier.cpp
1 /* Copyright (c) 2006-2019. 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
10 /// Wait on the barrier then leave
11 static void worker(simgrid::s4u::BarrierPtr barrier)
12 {
13     XBT_INFO("Waiting on the barrier");
14     barrier->wait();
15
16     XBT_INFO("Bye");
17 }
18
19 /// Spawn process_count-1 workers and do a barrier with them
20 static void master(int process_count)
21 {
22     simgrid::s4u::BarrierPtr barrier = simgrid::s4u::Barrier::create(process_count);
23
24     XBT_INFO("Spawning %d workers", process_count-1);
25     for (int i = 0; i < process_count-1; i++)
26     {
27         simgrid::s4u::Actor::create("worker", simgrid::s4u::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   // Parameter: Number of processes in the barrier
39   xbt_assert(argc >= 2, "Usage: %s <process-count>\n", argv[0]);
40   int process_count = std::stoi(argv[1]);
41   xbt_assert(process_count > 0, "<process-count> must be greater than 0");
42
43   simgrid::s4u::Engine e(&argc, argv);
44   e.load_platform("../../platforms/two_hosts.xml");
45   simgrid::s4u::Actor::create("master", simgrid::s4u::Host::by_name("Tremblay"), master, process_count);
46   e.run();
47
48   return 0;
49 }