Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent the rest of the code (examples, buildtools, doc...) except for examples/SMPI...
[simgrid.git] / examples / msg / parallel_task / test_ptask.c
1 /* Copyright (c) 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
9                                    need to include msg/msg.h */
10 #include "xbt/sysdep.h"         /* calloc, printf */
11
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16                              "Messages specific for this msg example");
17
18 int execute(int argc, char *argv[]);
19 int redistribute(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file,
21                      const char *application_file);
22
23 typedef enum {
24   PORT_22 = 0,
25   MAX_CHANNEL
26 } channel_t;
27
28
29 int execute(int argc, char *argv[])
30 {
31   char buffer[32];
32   int i, j;
33   m_host_t *m_host_list = NULL;
34   m_task_t task = NULL;
35   int host_list_size;
36   double *computation_duration = NULL;
37   double *communication_table = NULL;
38   double communication_amount;
39   double computation_amount;
40   double execution_time;
41
42
43   host_list_size = argc - 3;
44   DEBUG1("host_list_size=%d", host_list_size);
45   m_host_list = calloc(host_list_size, sizeof(m_host_t));
46   for (i = 1; i <= host_list_size; i++) {
47     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
48     xbt_assert1(m_host_list[i - 1] != NULL,
49                 "Unknown host %s. Stopping Now! ", argv[i]);
50   }
51
52   xbt_assert1(sscanf(argv[argc - 2], "%lg", &computation_amount),
53               "Invalid argument %s\n", argv[argc - 2]);
54   xbt_assert1(sscanf(argv[argc - 1], "%lg", &communication_amount),
55               "Invalid argument %s\n", argv[argc - 1]);
56   computation_duration = (double *) calloc(host_list_size, sizeof(double));
57   communication_table =
58       (double *) calloc(host_list_size * host_list_size, sizeof(double));
59   for (i = 0; i < host_list_size; i++) {
60     computation_duration[i] = computation_amount / host_list_size;
61     for (j = 0; j < host_list_size; j++)
62       communication_table[i * host_list_size + j] =
63           communication_amount / (host_list_size * host_list_size);
64   }
65
66   sprintf(buffer, "redist#0\n");
67   task = MSG_parallel_task_create(buffer,
68                                   host_list_size,
69                                   m_host_list,
70                                   computation_duration,
71                                   communication_table, NULL);
72
73   execution_time = MSG_get_clock();
74   MSG_parallel_task_execute(task);
75   execution_time = MSG_get_clock() - execution_time;
76
77   INFO1("execution_time=%g ", execution_time);
78
79   return 0;
80 }
81
82
83 int redistribute(int argc, char *argv[])
84 {
85   char buffer[32];
86   int i, j;
87   m_host_t *m_host_list = NULL;
88   m_task_t task = NULL;
89   int host_list_size;
90   double *computation_duration = NULL;
91   double *communication_table = NULL;
92   double communication_amount;
93   double redistribution_time;
94
95
96   host_list_size = argc - 2;
97   DEBUG1("host_list_size=%d", host_list_size);
98   m_host_list = calloc(host_list_size, sizeof(m_host_t));
99   for (i = 1; i <= host_list_size; i++) {
100     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
101     xbt_assert1(m_host_list[i - 1] != NULL,
102                 "Unknown host %s. Stopping Now! ", argv[i]);
103   }
104
105   xbt_assert1(sscanf(argv[argc - 1], "%lg", &communication_amount),
106               "Invalid argument %s\n", argv[argc - 1]);
107   computation_duration = (double *) calloc(host_list_size, sizeof(double));
108   communication_table =
109       (double *) calloc(host_list_size * host_list_size, sizeof(double));
110   for (i = 0; i < host_list_size; i++) {
111     for (j = 0; j < host_list_size; j++)
112       communication_table[i * host_list_size + j] =
113           communication_amount / (host_list_size * host_list_size);
114   }
115
116   sprintf(buffer, "redist#0\n");
117   task = MSG_parallel_task_create(buffer,
118                                   host_list_size,
119                                   m_host_list,
120                                   computation_duration,
121                                   communication_table, NULL);
122
123   redistribution_time = MSG_get_clock();
124   MSG_parallel_task_execute(task);
125   redistribution_time = MSG_get_clock() - redistribution_time;
126
127   INFO1("redistribution_time=%g ", redistribution_time);
128
129   return 0;
130 }
131
132
133 MSG_error_t test_all(const char *platform_file,
134                      const char *application_file)
135 {
136   MSG_error_t res = MSG_OK;
137
138
139   MSG_config("workstation/model", "ptask_L07");
140
141   /*  Simulation setting */
142   MSG_set_channel_number(MAX_CHANNEL);
143   MSG_create_environment(platform_file);
144
145   /*   Application deployment */
146   MSG_function_register("execute", execute);
147   MSG_function_register("redistribute", redistribute);
148   MSG_launch_application(application_file);
149
150   res = MSG_main();
151
152   INFO1("Simulation time %g", MSG_get_clock());
153   return res;
154 }
155
156
157 int main(int argc, char *argv[])
158 {
159   MSG_error_t res = MSG_OK;
160
161   MSG_global_init(&argc, argv);
162   if (argc < 3) {
163     printf("Usage: %s platform_file deployment_file\n", argv[0]);
164     exit(1);
165   }
166   res = test_all(argv[1], argv[2]);
167   MSG_clean();
168
169   if (res == MSG_OK)
170     return 0;
171   else
172     return 1;
173 }