Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'coverity_scan' of github.com:mquinson/simgrid
[simgrid.git] / examples / msg / parallel_task / parallel_task.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 <stdio.h>
8 #include "simgrid/msg.h"        /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 /** @addtogroup MSG_examples
18  * 
19  * - <b>parallel_task/parallel_task.c</b>: Demonstrates the use of
20  *   @ref MSG_parallel_task_create, to create special tasks that run
21  *   on several hosts at the same time. The resulting simulations are
22  *   very close to what can be achieved in @ref SD_API, but still
23  *   allows to use the other features of MSG (it'd be cool to be able
24  *   to mix interfaces, but it's not possible ATM).
25  */
26
27
28 /** Function in charge of running the example (that's a simgrid process) */
29 static int runner(int argc, char *argv[])
30 {
31   int i, j;
32
33   /* Retrieve the list of all hosts as an array of hosts */
34   xbt_dynar_t slaves_dynar = MSG_hosts_as_dynar();
35   int slaves_count = xbt_dynar_length(slaves_dynar);
36   msg_host_t *slaves = xbt_dynar_to_array(slaves_dynar);
37
38   XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, and 10MB to exchange between each pair");
39   double *computation_amounts = xbt_new0(double, slaves_count);
40   double *communication_amounts = xbt_new0(double, slaves_count * slaves_count);
41
42   for (i = 0; i < slaves_count; i++)
43     computation_amounts[i] = 1e9; // 1 Gflop
44
45   for (i = 0; i < slaves_count; i++)
46     for (j = i + 1; j < slaves_count; j++)
47       communication_amounts[i * slaves_count + j] = 1e7; // 10 MB
48
49   msg_task_t ptask = MSG_parallel_task_create("parallel task",
50       slaves_count, slaves, computation_amounts, communication_amounts, NULL /* no specific data to attach */);
51   MSG_parallel_task_execute(ptask);
52   MSG_task_destroy(ptask);
53   /* The arrays communication_amounts and computation_amounts are not to be freed manually */
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, slaves_count);
57   for (i = 0; i < slaves_count; i++)
58     computation_amounts[i] = 1e9; // 1 Gflop
59   ptask = MSG_parallel_task_create("parallel exec", slaves_count, slaves, computation_amounts, NULL/* no comm */, NULL /* no data */);
60   MSG_parallel_task_execute(ptask);
61   MSG_task_destroy(ptask);
62
63   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", MSG_host_get_name(slaves[1]));
64   computation_amounts = xbt_new0(double, 1);
65   computation_amounts[0] = 1e9; // 1 Gflop
66   msg_host_t *remote = xbt_new(msg_host_t,1);
67   remote[0] = slaves[1];
68   ptask = MSG_parallel_task_create("remote exec", 1, remote, computation_amounts, NULL/* no comm */, NULL /* no data */);
69   MSG_parallel_task_execute(ptask);
70   MSG_task_destroy(ptask);
71   free(remote);
72
73   XBT_INFO("Goodbye now!");
74   free(slaves);
75   return 0;
76 }
77
78 int main(int argc, char *argv[])
79 {
80   MSG_init(&argc, argv);
81   MSG_config("host/model", "ptask_L07");
82
83   xbt_assert(argc > 1, "Usage: %s <platform file>", argv[0]);
84   MSG_create_environment(argv[1]);
85
86   /* Pick a process, no matter which, from the platform file */
87   xbt_dynar_t all_hosts = MSG_hosts_as_dynar();
88   msg_host_t first_host = xbt_dynar_getfirst_as(all_hosts,msg_host_t);
89   xbt_dynar_free(&all_hosts);
90
91   MSG_process_create("test", runner, NULL, first_host);
92   msg_error_t res = MSG_main();
93   XBT_INFO("Simulation done.");
94
95   return res != MSG_OK;
96 }