Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reorganize examples/msg/energy
[simgrid.git] / examples / msg / energy-ptask / energy-ptask.c
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8 #include "simgrid/plugins/energy.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 /** @addtogroup MSG_examples
13  * 
14  * - <b>parallel_task/parallel_task.c</b>: Demonstrates the use of @ref MSG_parallel_task_create, to create special
15  *   tasks that run on several hosts at the same time. The resulting simulations are very close to what can be
16  *   achieved in @ref SD_API, but still allows to use the other features of MSG (it'd be cool to be able to mix
17  *   interfaces, but it's not possible ATM).
18  */
19
20 static int runner(int argc, char *argv[])
21 {
22   /* Retrieve the list of all hosts as an array of hosts */
23   xbt_dynar_t slaves_dynar = MSG_hosts_as_dynar();
24   int slaves_count = xbt_dynar_length(slaves_dynar);
25   msg_host_t *slaves = xbt_dynar_to_array(slaves_dynar);
26
27   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
28            "and 10MB to exchange between each pair");
29   double *computation_amounts = xbt_new0(double, slaves_count);
30   double *communication_amounts = xbt_new0(double, slaves_count * slaves_count);
31
32   for (int i = 0; i < slaves_count; i++)
33     computation_amounts[i] = 1e9; // 1 Gflop
34
35   for (int i = 0; i < slaves_count; i++)
36     for (int j = i + 1; j < slaves_count; j++)
37       communication_amounts[i * slaves_count + j] = 1e7; // 10 MB
38
39   msg_task_t ptask =
40     MSG_parallel_task_create("parallel task", slaves_count, slaves, computation_amounts, communication_amounts, NULL);
41   MSG_parallel_task_execute(ptask);
42   MSG_task_destroy(ptask);
43   /* The arrays communication_amounts and computation_amounts are not to be freed manually */
44
45   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
46   computation_amounts = xbt_new0(double, slaves_count);
47   for (int i = 0; i < slaves_count; i++)
48     computation_amounts[i] = 1e9; // 1 Gflop
49   ptask =
50     MSG_parallel_task_create("parallel exec", slaves_count, slaves, computation_amounts, NULL/* no comm */, NULL);
51   MSG_parallel_task_execute(ptask);
52   MSG_task_destroy(ptask);
53
54   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", MSG_host_get_name(slaves[1]));
55   computation_amounts = xbt_new0(double, 1);
56   computation_amounts[0] = 1e9; // 1 Gflop
57   msg_host_t *remote = xbt_new(msg_host_t,1);
58   remote[0] = slaves[1];
59   ptask = MSG_parallel_task_create("remote exec", 1, remote, computation_amounts, NULL/* no comm */, NULL);
60   MSG_parallel_task_execute(ptask);
61   MSG_task_destroy(ptask);
62   free(remote);
63
64   XBT_INFO("Goodbye now!");
65   free(slaves);
66   return 0;
67 }
68
69 int main(int argc, char *argv[])
70 {
71   MSG_init(&argc, argv);
72   MSG_config("host/model", "ptask_L07");
73
74   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
75   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
76
77   if(argc == 3 && argv[2][2] == 'e')
78     sg_energy_plugin_init();
79
80   MSG_create_environment(argv[1]);
81
82   /* Pick a process, no matter which, from the platform file */
83   xbt_dynar_t all_hosts = MSG_hosts_as_dynar();
84   msg_host_t first_host = xbt_dynar_getfirst_as(all_hosts,msg_host_t);
85   xbt_dynar_free(&all_hosts);
86
87   MSG_process_create("test", runner, NULL, first_host);
88   msg_error_t res = MSG_main();
89   XBT_INFO("Simulation done.");
90
91   return res != MSG_OK;
92 }