Logo AND Algorithmique Numérique Distribuée

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