Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
984db05cb3cb02e5ba1415322bfbd462a174f62c
[simgrid.git] / examples / c / energy-exec-ptask / energy-exec-ptask.c
1 /* Copyright (c) 2007-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/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/exec.h"
9 #include "simgrid/host.h"
10 #include "simgrid/plugins/energy.h"
11 #include "xbt/asserts.h"
12 #include "xbt/config.h"
13 #include "xbt/log.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(energy_exec_ptask, "Messages specific for this example");
16
17 static void runner(int argc, char* argv[])
18 {
19   /* Retrieve the list of all hosts as an array of hosts */
20   int host_count   = (int)sg_host_count();
21   sg_host_t* hosts = sg_host_list();
22
23   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
24            "and 10MB to exchange between each pair");
25   double* computation_amounts   = xbt_new0(double, host_count);
26   double* communication_amounts = xbt_new0(double, host_count * host_count);
27
28   for (int i = 0; i < host_count; i++)
29     computation_amounts[i] = 1e9; // 1 Gflop
30
31   for (int i = 0; i < host_count; i++)
32     for (int j = i + 1; j < host_count; j++)
33       communication_amounts[i * host_count + j] = 1e7; // 10 MB
34
35   sg_actor_parallel_execute(host_count, hosts, computation_amounts, communication_amounts);
36
37   xbt_free(communication_amounts);
38   xbt_free(computation_amounts);
39
40   XBT_INFO("We can do the same with a timeout of one second enabled.");
41   computation_amounts   = xbt_new0(double, host_count);
42   communication_amounts = xbt_new0(double, host_count * host_count);
43   for (int i = 0; i < host_count; i++)
44     computation_amounts[i] = 1e9; // 1 Gflop
45   for (int i = 0; i < host_count; i++)
46     for (int j = i + 1; j < host_count; j++)
47       communication_amounts[i * host_count + j] = 1e7; // 10 MB
48
49   sg_exec_t exec = sg_actor_parallel_exec_init(host_count, hosts, computation_amounts, communication_amounts);
50   sg_exec_wait_for(exec, 1 /* timeout (in seconds)*/);
51   xbt_free(communication_amounts);
52   xbt_free(computation_amounts);
53
54   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
55   computation_amounts = xbt_new0(double, host_count);
56   for (int i = 0; i < host_count; i++)
57     computation_amounts[i] = 1e9; // 1 Gflop
58   sg_actor_parallel_execute(host_count, hosts, computation_amounts, NULL);
59   xbt_free(computation_amounts);
60
61   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
62   computation_amounts   = xbt_new0(double, host_count);
63   communication_amounts = xbt_new0(double, host_count * host_count);
64   sg_actor_parallel_execute(host_count, hosts, computation_amounts, communication_amounts);
65   xbt_free(communication_amounts);
66   xbt_free(computation_amounts);
67
68   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", sg_host_get_name(hosts[1]));
69   computation_amounts    = xbt_new0(double, 1);
70   computation_amounts[0] = 1e9; // 1 Gflop
71   sg_host_t remote[1];
72   remote[0] = hosts[1];
73   sg_actor_parallel_execute(1, remote, computation_amounts, NULL /* no comm */);
74   xbt_free(computation_amounts);
75
76   XBT_INFO("Goodbye now!");
77   xbt_free(hosts);
78 }
79
80 int main(int argc, char* argv[])
81 {
82   simgrid_init(&argc, argv);
83   sg_cfg_set_string("host/model", "ptask_L07");
84
85   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
86   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
87
88   if (argc == 3 && argv[2][2] == 'e')
89     sg_host_energy_plugin_init();
90
91   simgrid_load_platform(argv[1]);
92
93   /* Pick the first host from the platform file */
94   sg_host_t* all_hosts = sg_host_list();
95   sg_host_t first_host = all_hosts[0];
96   xbt_free(all_hosts);
97
98   sg_actor_create("test", first_host, runner, 0, NULL);
99
100   simgrid_run();
101   XBT_INFO("Simulation done.");
102
103   return 0;
104 }