Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further improve the MSG doc by documenting the examples
[simgrid.git] / examples / msg / parallel_task / parallel_task.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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 int test(int argc, char *argv[]);
28 MSG_error_t test_all(const char *platform_file);
29
30 /** Emitter function  */
31 int test(int argc, char *argv[])
32 {
33   xbt_dynar_t slaves_dynar;
34   int slaves_count = 0;
35   m_host_t *slaves = NULL;
36   double task_comp_size = 100000;
37   double task_comm_size = 10000;
38   double *computation_amount = NULL;
39   double *communication_amount = NULL;
40   m_task_t ptask = NULL;
41   int i, j;
42
43   slaves_dynar = MSG_hosts_as_dynar();
44   slaves_count = xbt_dynar_length(slaves_dynar);
45   slaves = xbt_dynar_to_array(slaves_dynar);
46
47   computation_amount = xbt_new0(double, slaves_count);
48   communication_amount = xbt_new0(double, slaves_count * slaves_count);
49
50   for (i = 0; i < slaves_count; i++)
51     computation_amount[i] = task_comp_size;
52
53   for (i = 0; i < slaves_count; i++)
54     for (j = i + 1; j < slaves_count; j++)
55       communication_amount[i * slaves_count + j] = task_comm_size;
56
57   ptask = MSG_parallel_task_create("parallel task",
58                                    slaves_count, slaves,
59                                    computation_amount,
60                                    communication_amount, NULL);
61   MSG_parallel_task_execute(ptask);
62
63   MSG_task_destroy(ptask);
64   /* There is no need to free that! */
65 /*   free(communication_amount); */
66 /*   free(computation_amount); */
67
68   XBT_INFO("Goodbye now!");
69   free(slaves);
70   return 0;
71 }
72
73 /** Test function */
74 MSG_error_t test_all(const char *platform_file)
75 {
76   MSG_error_t res = MSG_OK;
77   xbt_dynar_t all_hosts;
78   m_host_t first_host;
79
80   MSG_config("workstation/model", "ptask_L07");
81   MSG_create_environment(platform_file);
82
83   all_hosts = MSG_hosts_as_dynar();
84   first_host = xbt_dynar_getfirst_as(all_hosts,m_host_t);
85   MSG_process_create("test", test, NULL, first_host);
86   res = MSG_main();
87   xbt_dynar_free(&all_hosts);
88
89   XBT_INFO("Simulation time %g", MSG_get_clock());
90   return res;
91 }
92
93 int main(int argc, char *argv[])
94 {
95   MSG_error_t res = MSG_OK;
96
97   MSG_global_init(&argc, argv);
98   if (argc < 2) {
99     printf("Usage: %s platform_file\n", argv[0]);
100     printf("example: %s msg_platform.xml\n", argv[0]);
101     exit(1);
102   }
103   res = test_all(argv[1]);
104   MSG_clean();
105
106   if (res == MSG_OK)
107     return 0;
108   else
109     return 1;
110 }