Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Test that ptasks with 0 exec and 0 comm do work
[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   int hosts_count = MSG_get_host_number();
24   msg_host_t *hosts = xbt_dynar_to_array(MSG_hosts_as_dynar());
25
26   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
27            "and 10MB to exchange between each pair");
28   double *computation_amounts = xbt_new0(double, hosts_count);
29   double *communication_amounts = xbt_new0(double, hosts_count * hosts_count);
30
31   for (int i = 0; i < hosts_count; i++)
32     computation_amounts[i] = 1e9; // 1 Gflop
33
34   for (int i = 0; i < hosts_count; i++)
35     for (int j = i + 1; j < hosts_count; j++)
36       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
37
38   msg_task_t ptask =
39     MSG_parallel_task_create("parallel task", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
40   MSG_parallel_task_execute(ptask);
41   MSG_task_destroy(ptask);
42   /* The arrays communication_amounts and computation_amounts are not to be freed manually */
43
44   XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
45   computation_amounts = xbt_new0(double, hosts_count);
46   for (int i = 0; i < hosts_count; i++)
47     computation_amounts[i] = 1e9; // 1 Gflop
48   ptask = MSG_parallel_task_create("parallel exec", hosts_count, hosts, computation_amounts, NULL/* no comm */, NULL);
49   MSG_parallel_task_execute(ptask);
50   MSG_task_destroy(ptask);
51
52   XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
53   computation_amounts = xbt_new0(double, hosts_count);
54   communication_amounts = xbt_new0(double, hosts_count * hosts_count); /* memset to 0 by xbt_new0 */
55   ptask = MSG_parallel_task_create("parallel sync", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
56   MSG_parallel_task_execute(ptask);
57   MSG_task_destroy(ptask);
58
59    XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", MSG_host_get_name(hosts[1]));
60   computation_amounts = xbt_new0(double, 1);
61   computation_amounts[0] = 1e9; // 1 Gflop
62   msg_host_t *remote = xbt_new(msg_host_t,1);
63   remote[0] = hosts[1];
64   ptask = MSG_parallel_task_create("remote exec", 1, remote, computation_amounts, NULL/* no comm */, NULL);
65   MSG_parallel_task_execute(ptask);
66   MSG_task_destroy(ptask);
67   free(remote);
68
69   XBT_INFO("Goodbye now!");
70   free(hosts);
71   return 0;
72 }
73
74 int main(int argc, char *argv[])
75 {
76   MSG_init(&argc, argv);
77   MSG_config("host/model", "ptask_L07");
78
79   xbt_assert(argc <= 3, "1Usage: %s <platform file> [--energy]", argv[0]);
80   xbt_assert(argc >= 2, "2Usage: %s <platform file> [--energy]", argv[0]);
81
82   if(argc == 3 && argv[2][2] == 'e')
83     sg_energy_plugin_init();
84
85   MSG_create_environment(argv[1]);
86
87   /* Pick a process, no matter which, from the platform file */
88   xbt_dynar_t all_hosts = MSG_hosts_as_dynar();
89   msg_host_t first_host = xbt_dynar_getfirst_as(all_hosts,msg_host_t);
90   xbt_dynar_free(&all_hosts);
91
92   MSG_process_create("test", runner, NULL, first_host);
93   msg_error_t res = MSG_main();
94   XBT_INFO("Simulation done.");
95
96   return res != MSG_OK;
97 }