Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c243494d00425b2ee27e2cfdf802f62f0c160623
[simgrid.git] / examples / s4u / exec-ptask / s4u-exec-ptask.cpp
1 /* Copyright (c) 2017-2019. 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
24 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_energyptask, "Messages specific for this s4u example");
25
26 static void runner()
27 {
28   /* Retrieve the list of all hosts as an array of hosts */
29   auto hosts         = simgrid::s4u::Engine::get_instance()->get_all_hosts();
30   size_t hosts_count = hosts.size();
31
32   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
33            "and 10MB to exchange between each pair");
34
35   std::vector<double> computation_amounts;
36   std::vector<double> communication_amounts;
37
38   /* ------[ test 1 ]----------------- */
39   computation_amounts.assign(hosts.size(), 1e9 /*1Gflop*/);
40   communication_amounts.assign(hosts.size() * hosts.size(), 0);
41   for (size_t i = 0; i < hosts_count; i++)
42     for (size_t j = i + 1; j < hosts_count; j++)
43       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
44
45   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
46
47   /* ------[ test 2 ]----------------- */
48   XBT_INFO("We can do the same with a timeout of one second enabled.");
49   computation_amounts.assign(hosts.size(), 1e9 /*1Gflop*/);
50   communication_amounts.assign(hosts.size() * hosts.size(), 0);
51   for (size_t i = 0; i < hosts_count; i++)
52     for (size_t j = i + 1; j < hosts_count; j++)
53       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
54
55   try {
56     simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts,
57                                                1.0 /* timeout (in seconds)*/);
58     XBT_WARN("Woops, this did not timeout as expected... Please report that bug.");
59   } catch (xbt_ex& e) {
60     /* Do nothing this exception on timeout was expected */
61     XBT_DEBUG("Caught expected exception: %s", e.what());
62   }
63
64   /* ------[ test 3 ]----------------- */
65   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
66   computation_amounts.assign(hosts.size(), 1e9 /*1Gflop*/);
67   communication_amounts.clear(); /* no comm */
68   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
69
70   /* ------[ test 4 ]----------------- */
71   XBT_INFO("Then, build a parallel task involving only heterogeneous computations and no communication");
72   computation_amounts.resize(hosts.size());
73   for (size_t i = 0; i < hosts_count; i++)
74     computation_amounts[i] = 5 * (i + 1) * 1e8; // 500Mflop, 1Gflop, 1.5Gflop
75   communication_amounts.clear();                /* no comm */
76   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
77
78   /* ------[ test 5 ]----------------- */
79   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
80   computation_amounts.clear();
81   communication_amounts.clear();
82   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
83
84   /* ------[ test 6 ]----------------- */
85   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", hosts[1]->get_cname());
86   std::vector<simgrid::s4u::Host*> remote;
87   remote.push_back(hosts[1]);
88   computation_amounts.assign(1, 1e9);
89   communication_amounts.clear();
90
91   simgrid::s4u::this_actor::parallel_execute(remote, computation_amounts, communication_amounts);
92
93   XBT_INFO("Goodbye now!");
94 }
95
96 int main(int argc, char* argv[])
97 {
98   simgrid::s4u::Engine e(&argc, argv);
99
100   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
101   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
102
103   if (argc == 3 && argv[2][2] == 'e')
104     sg_host_energy_plugin_init();
105
106   e.load_platform(argv[1]);
107
108   /* Pick a process, no matter which, from the platform file */
109   simgrid::s4u::Actor::create("test", simgrid::s4u::Host::by_name("MyHost1"), runner);
110
111   e.run();
112   XBT_INFO("Simulation done.");
113   return 0;
114 }