Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'energy-pstate' of https://github.com/Takishipp/simgrid into Takishipp...
[simgrid.git] / examples / s4u / energy-ptask / s4u-energy-ptask.cpp
1 /* Copyright (c) 2017. 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/plugins/energy.h"
7 #include <simgrid/s4u.hpp>
8 #include <xbt/ex.hpp>
9 #include <xbt/log.h>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_energyptask, "Messages specific for this s4u example");
12
13 static void runner()
14 {
15   /* Retrieve the list of all hosts as an array of hosts */
16   int hosts_count            = sg_host_count();
17   simgrid::s4u::Host** hosts = sg_host_list();
18
19   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
20            "and 10MB to exchange between each pair");
21   double* computation_amounts   = new double[hosts_count]();
22   double* communication_amounts = new double[hosts_count * hosts_count]();
23
24   for (int i               = 0; i < hosts_count; i++)
25     computation_amounts[i] = 1e9; // 1 Gflop
26
27   for (int i = 0; i < hosts_count; i++)
28     for (int j = i + 1; j < hosts_count; j++)
29       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
30
31   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts, computation_amounts, communication_amounts);
32
33   XBT_INFO("We can do the same with a timeout of one second enabled.");
34   computation_amounts   = new double[hosts_count]();
35   communication_amounts = new double[hosts_count * hosts_count]();
36
37   for (int i               = 0; i < hosts_count; i++)
38     computation_amounts[i] = 1e9; // 1 Gflop
39
40   for (int i = 0; i < hosts_count; i++)
41     for (int j = i + 1; j < hosts_count; j++)
42       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
43
44   try {
45     simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts, computation_amounts, communication_amounts,
46                                                1.0 /* timeout (in seconds)*/);
47     XBT_WARN("Woops, this did not timeout as expected... Please report that bug.");
48   } catch (xbt_ex& e) {
49     /* Do nothing this exception on timeout was expected */
50   }
51
52   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
53   computation_amounts = new double[hosts_count]();
54   for (int i               = 0; i < hosts_count; i++)
55     computation_amounts[i] = 1e9; // 1 Gflop
56   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts, computation_amounts, nullptr /* no comm */);
57
58   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
59   computation_amounts   = new double[hosts_count]();
60   communication_amounts = new double[hosts_count * hosts_count]();
61   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts, computation_amounts, communication_amounts);
62
63   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", hosts[1]->getCname());
64   computation_amounts = new double[1]{1e9};
65
66   simgrid::s4u::Host* remote[] = {hosts[1]};
67   simgrid::s4u::this_actor::parallel_execute(1, remote, computation_amounts, nullptr);
68
69   XBT_INFO("Goodbye now!");
70   std::free(hosts);
71 }
72
73 int main(int argc, char* argv[])
74 {
75   simgrid::s4u::Engine e(&argc, argv);
76
77   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
78   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
79
80   if (argc == 3 && argv[2][2] == 'e')
81     sg_host_energy_plugin_init();
82
83   e.loadPlatform(argv[1]);
84
85   /* Pick a process, no matter which, from the platform file */
86   simgrid::s4u::Actor::createActor("test", simgrid::s4u::Host::by_name("MyHost1"), runner);
87
88   e.run();
89   XBT_INFO("Simulation done.");
90   return 0;
91 }