Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / teshsuite / msg / energy-ptask / energy-ptask.c
1 /* Copyright (c) 2007-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 #include "simgrid/plugins/energy.h"
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  *
13  * - <b>energy-ptask/energy-ptask.c</b>: Demonstrates the use of @ref MSG_parallel_task_create, to create special
14  *   tasks that run on several hosts at the same time. The resulting simulations are very close to what can be
15  *   achieved in @ref SD_API, but still allows to use the other features of MSG (it'd be cool to be able to mix
16  *   interfaces, but it's not possible ATM).
17  */
18
19 static int runner(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
20 {
21   /* Retrieve the list of all hosts as an array of hosts */
22   int host_count    = MSG_get_host_number();
23   msg_host_t* hosts = xbt_dynar_to_array(MSG_hosts_as_dynar());
24
25   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
26            "and 10MB to exchange between each pair");
27   double* computation_amounts   = xbt_new0(double, host_count);
28   double* communication_amounts = xbt_new0(double, host_count* host_count);
29
30   for (int i = 0; i < host_count; i++)
31     computation_amounts[i] = 1e9; // 1 Gflop
32
33   for (int i = 0; i < host_count; i++)
34     for (int j = i + 1; j < host_count; j++)
35       communication_amounts[i * host_count + j] = 1e7; // 10 MB
36
37   msg_task_t ptask =
38       MSG_parallel_task_create("parallel task", host_count, hosts, computation_amounts, communication_amounts, NULL);
39   MSG_parallel_task_execute(ptask);
40   MSG_task_destroy(ptask);
41   xbt_free(communication_amounts);
42   xbt_free(computation_amounts);
43
44   XBT_INFO("We can do the same with a timeout of one second enabled.");
45   computation_amounts   = xbt_new0(double, host_count);
46   communication_amounts = xbt_new0(double, host_count* host_count);
47   for (int i = 0; i < host_count; i++)
48     computation_amounts[i] = 1e9; // 1 Gflop
49   for (int i = 0; i < host_count; i++)
50     for (int j = i + 1; j < host_count; j++)
51       communication_amounts[i * host_count + j] = 1e7; // 10 MB
52   ptask =
53       MSG_parallel_task_create("parallel task", host_count, hosts, computation_amounts, communication_amounts, NULL);
54   msg_error_t errcode = MSG_parallel_task_execute_with_timeout(ptask, 1 /* timeout (in seconds)*/);
55   xbt_assert(errcode == MSG_TIMEOUT, "Woops, this did not timeout as expected... Please report that bug.");
56   MSG_task_destroy(ptask);
57   xbt_free(communication_amounts);
58   xbt_free(computation_amounts);
59
60   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
61   computation_amounts = xbt_new0(double, host_count);
62   for (int i = 0; i < host_count; i++)
63     computation_amounts[i] = 1e9; // 1 Gflop
64   ptask = MSG_parallel_task_create("parallel exec", host_count, hosts, computation_amounts, NULL /* no comm */, NULL);
65   MSG_parallel_task_execute(ptask);
66   MSG_task_destroy(ptask);
67   xbt_free(computation_amounts);
68
69   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
70   computation_amounts   = xbt_new0(double, host_count);
71   communication_amounts = xbt_new0(double, host_count* host_count); /* memset to 0 by xbt_new0 */
72   ptask =
73       MSG_parallel_task_create("parallel sync", host_count, hosts, computation_amounts, communication_amounts, NULL);
74   MSG_parallel_task_execute(ptask);
75   MSG_task_destroy(ptask);
76   xbt_free(communication_amounts);
77   xbt_free(computation_amounts);
78
79   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", MSG_host_get_name(hosts[1]));
80   computation_amounts    = xbt_new0(double, 1);
81   computation_amounts[0] = 1e9; // 1 Gflop
82   msg_host_t* remote     = xbt_new(msg_host_t, 1);
83   remote[0]              = hosts[1];
84   ptask = MSG_parallel_task_create("remote exec", 1, remote, computation_amounts, NULL /* no comm */, NULL);
85   MSG_parallel_task_execute(ptask);
86   MSG_task_destroy(ptask);
87   xbt_free(remote);
88   xbt_free(computation_amounts);
89
90   XBT_INFO("Goodbye now!");
91   xbt_free(hosts);
92   return 0;
93 }
94
95 int main(int argc, char* argv[])
96 {
97   MSG_init(&argc, argv);
98   MSG_config("host/model", "ptask_L07");
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   MSG_create_environment(argv[1]);
107
108   /* Pick a process, no matter which, from the platform file */
109   xbt_dynar_t all_hosts = MSG_hosts_as_dynar();
110   msg_host_t first_host = xbt_dynar_getfirst_as(all_hosts, msg_host_t);
111   xbt_dynar_free(&all_hosts);
112
113   MSG_process_create("test", runner, NULL, first_host);
114   msg_error_t res = MSG_main();
115   XBT_INFO("Simulation done.");
116
117   return res != MSG_OK;
118 }