Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / examples / s4u / exec-waitfor / s4u-exec-waitfor.cpp
1 /* Copyright (c) 2019-2020. 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_exec_waitfor, "Messages specific for this s4u example");
9
10 static void worker()
11 {
12   simgrid::s4u::ExecPtr exec;
13   double amount = 5 * simgrid::s4u::this_actor::get_host()->get_speed();
14   XBT_INFO("Create an activity that should run for 5 seconds");
15
16   exec = simgrid::s4u::this_actor::exec_async(amount);
17
18   /* Now that execution is started, wait for 3 seconds. */
19   XBT_INFO("But let it end after 3 seconds");
20   try {
21     exec->wait_for(3);
22     XBT_INFO("Execution complete");
23   } catch (const simgrid::TimeoutException&) {
24     XBT_INFO("Execution Timeout!");
25   }
26
27   /* do it again, but this time with a timeout greater than the duration of the execution */
28   XBT_INFO("Create another activity that should run for 5 seconds and wait for it for 6 seconds");
29   exec = simgrid::s4u::this_actor::exec_async(amount);
30   try {
31     exec->wait_for(6);
32     XBT_INFO("Execution complete");
33   } catch (const simgrid::TimeoutException&) {
34     XBT_INFO("Execution Timeout!");
35   }
36
37   XBT_INFO("Finally test with a parallel execution");
38   auto hosts         = simgrid::s4u::Engine::get_instance()->get_all_hosts();
39   size_t hosts_count = hosts.size();
40   std::vector<double> computation_amounts;
41   std::vector<double> communication_amounts;
42
43   computation_amounts.assign(hosts_count, 1e9 /*1Gflop*/);
44   communication_amounts.assign(hosts_count * hosts_count, 0);
45   for (size_t i = 0; i < hosts_count; i++)
46     for (size_t j = i + 1; j < hosts_count; j++)
47       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
48
49   exec = simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts);
50   try {
51     exec->wait_for(2);
52     XBT_INFO("Parallel Execution complete");
53   } catch (const simgrid::TimeoutException&) {
54     XBT_INFO("Parallel Execution Timeout!");
55   }
56 }
57
58 int main(int argc, char* argv[])
59 {
60   simgrid::s4u::Engine e(&argc, argv);
61   e.load_platform(argv[1]);
62   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Tremblay"), worker);
63   e.run();
64
65   return 0;
66 }