Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / c / energy-exec-ptask / energy-exec-ptask.c
1 /* Copyright (c) 2007-2022. 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/config.h"
12 #include "xbt/log.h"
13 #include "xbt/sysdep.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   if (sg_exec_wait_for(exec, 1 /* timeout (in seconds)*/) == SG_ERROR_TIMEOUT)
51     sg_exec_cancel(exec);
52   xbt_free(communication_amounts);
53   xbt_free(computation_amounts);
54
55   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
56   computation_amounts = xbt_new0(double, host_count);
57   for (int i = 0; i < host_count; i++)
58     computation_amounts[i] = 1e9; // 1 Gflop
59   sg_actor_parallel_execute(host_count, hosts, computation_amounts, NULL);
60   xbt_free(computation_amounts);
61
62   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
63   computation_amounts   = xbt_new0(double, host_count);
64   communication_amounts = xbt_new0(double, host_count * host_count);
65   sg_actor_parallel_execute(host_count, hosts, computation_amounts, communication_amounts);
66   xbt_free(communication_amounts);
67   xbt_free(computation_amounts);
68
69   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", sg_host_get_name(hosts[1]));
70   computation_amounts    = xbt_new0(double, 1);
71   computation_amounts[0] = 1e9; // 1 Gflop
72   sg_host_t remote[1];
73   remote[0] = hosts[1];
74   sg_actor_parallel_execute(1, remote, computation_amounts, NULL /* no comm */);
75   xbt_free(computation_amounts);
76
77   XBT_INFO("Goodbye now!");
78   xbt_free(hosts);
79 }
80
81 int main(int argc, char* argv[])
82 {
83   simgrid_init(&argc, argv);
84   sg_cfg_set_string("host/model", "ptask_L07");
85
86   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
87   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
88
89   if (argc == 3 && argv[2][2] == 'e')
90     sg_host_energy_plugin_init();
91
92   simgrid_load_platform(argv[1]);
93
94   /* Pick the first host from the platform file */
95   sg_host_t* all_hosts = sg_host_list();
96   sg_host_t first_host = all_hosts[0];
97   xbt_free(all_hosts);
98
99   sg_actor_create("test", first_host, runner, 0, NULL);
100
101   simgrid_run();
102   XBT_INFO("Simulation done.");
103
104   return 0;
105 }