Logo AND Algorithmique Numérique Distribuée

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