1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
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. */
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"
13 #include "xbt/sysdep.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(energy_exec_ptask, "Messages specific for this example");
17 static void runner(int argc, char* argv[])
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();
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);
28 for (int i = 0; i < host_count; i++)
29 computation_amounts[i] = 1e9; // 1 Gflop
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
35 sg_actor_parallel_execute(host_count, hosts, computation_amounts, communication_amounts);
37 xbt_free(communication_amounts);
38 xbt_free(computation_amounts);
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
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)
52 xbt_free(communication_amounts);
53 xbt_free(computation_amounts);
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);
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);
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
74 sg_actor_parallel_execute(1, remote, computation_amounts, NULL /* no comm */);
75 xbt_free(computation_amounts);
77 XBT_INFO("Goodbye now!");
81 int main(int argc, char* argv[])
83 simgrid_init(&argc, argv);
84 sg_cfg_set_string("host/model", "ptask_L07");
86 xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
87 xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
89 if (argc == 3 && argv[2][2] == 'e')
90 sg_host_energy_plugin_init();
92 simgrid_load_platform(argv[1]);
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];
99 sg_actor_create("test", first_host, runner, 0, NULL);
102 XBT_INFO("Simulation done.");