Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #280 from mpoquet/replay-steroid-example
[simgrid.git] / examples / s4u / exec-ptask / s4u-exec-ptask.cpp
1 /* Copyright (c) 2017-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 /* Parallel tasks are convenient abstractions of parallel computational kernels that span over several machines.
7  * To create a new one, you have to provide several things:
8  *   - a vector of hosts on which the task will execute
9  *   - a vector of values, the amount of computation for each of the hosts (in flops)
10  *   - a matrix of values, the amount of communication between each pair of hosts (in bytes)
11  *
12  * Each of these operation will be processed at the same relative speed.
13  * This means that at some point in time, all sub-executions and all sub-communications will be at 20% of completion.
14  * Also, they will all complete at the exact same time.
15  *
16  * This is obviously a simplistic abstraction, but this is very handful in a large amount of situations.
17  *
18  * Please note that you must have the LV07 platform model enabled to use such constructs.
19  */
20
21 #include "simgrid/plugins/energy.h"
22 #include <simgrid/s4u.hpp>
23 #include <xbt/ex.hpp>
24 #include <xbt/log.h>
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_energyptask, "Messages specific for this s4u example");
27
28 static void runner()
29 {
30   /* Retrieve the list of all hosts as an array of hosts */
31   std::vector<simgrid::s4u::Host*> hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts();
32   int hosts_count = hosts.size();
33
34   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
35            "and 10MB to exchange between each pair");
36   double* computation_amounts   = new double[hosts_count]();
37   double* communication_amounts = new double[hosts_count * hosts_count]();
38
39   for (int i               = 0; i < hosts_count; i++)
40     computation_amounts[i] = 1e9; // 1 Gflop
41
42   for (int i = 0; i < hosts_count; i++)
43     for (int j = i + 1; j < hosts_count; j++)
44       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
45
46   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts.data(), computation_amounts, communication_amounts);
47
48   XBT_INFO("We can do the same with a timeout of one second enabled.");
49   computation_amounts   = new double[hosts_count]();
50   communication_amounts = new double[hosts_count * hosts_count]();
51
52   for (int i               = 0; i < hosts_count; i++)
53     computation_amounts[i] = 1e9; // 1 Gflop
54
55   for (int i = 0; i < hosts_count; i++)
56     for (int j = i + 1; j < hosts_count; j++)
57       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
58
59   try {
60     simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts.data(), computation_amounts, communication_amounts,
61                                                1.0 /* timeout (in seconds)*/);
62     XBT_WARN("Woops, this did not timeout as expected... Please report that bug.");
63   } catch (xbt_ex& e) {
64     /* Do nothing this exception on timeout was expected */
65     XBT_DEBUG("Caught expected exception: %s", e.what());
66   }
67
68   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
69   computation_amounts = new double[hosts_count]();
70   for (int i               = 0; i < hosts_count; i++)
71     computation_amounts[i] = 1e9; // 1 Gflop
72   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts.data(), computation_amounts, nullptr /* no comm */);
73
74   XBT_INFO("Then, build a parallel task involving only heterogeneous computations and no communication");
75   computation_amounts = new double[hosts_count]();
76   for (int i               = 0; i < hosts_count; i++)
77     computation_amounts[i] = 5 * (i + 1) * 1e8; // 500Mflop, 1Gflop, 1.5Gflop
78   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts.data(), computation_amounts, nullptr /* no comm */);
79
80   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
81   computation_amounts   = new double[hosts_count]();
82   communication_amounts = new double[hosts_count * hosts_count]();
83   simgrid::s4u::this_actor::parallel_execute(hosts_count, hosts.data(), computation_amounts, communication_amounts);
84
85   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", hosts[1]->get_cname());
86   computation_amounts = new double[1]{1e9};
87
88   simgrid::s4u::Host* remote[] = {hosts[1]};
89   simgrid::s4u::this_actor::parallel_execute(1, remote, computation_amounts, nullptr);
90
91   XBT_INFO("Goodbye now!");
92 }
93
94 int main(int argc, char* argv[])
95 {
96   simgrid::s4u::Engine e(&argc, argv);
97
98   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
99   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
100
101   if (argc == 3 && argv[2][2] == 'e')
102     sg_host_energy_plugin_init();
103
104   e.load_platform(argv[1]);
105
106   /* Pick a process, no matter which, from the platform file */
107   simgrid::s4u::Actor::create("test", simgrid::s4u::Host::by_name("MyHost1"), runner);
108
109   e.run();
110   XBT_INFO("Simulation done.");
111   return 0;
112 }