Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
03c5b949bb1b7cfddee34598e0e18773514fa657
[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 = 0;
39   double computation_amount = 0;
40   double execution_time;
41
42
43   host_list_size = argc - 3;
44   XBT_DEBUG("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_assert(m_host_list[i - 1] != NULL,
49                 "Unknown host %s. Stopping Now! ", argv[i]);
50   }
51
52   _XBT_GNUC_UNUSED int read;
53   read = sscanf(argv[argc - 2], "%lg", &computation_amount);
54   xbt_assert(read, "Invalid argument %s\n", argv[argc - 2]);
55   read = sscanf(argv[argc - 1], "%lg", &communication_amount);
56   xbt_assert(read, "Invalid argument %s\n", argv[argc - 1]);
57   computation_duration = (double *) calloc(host_list_size, sizeof(double));
58   communication_table =
59       (double *) calloc(host_list_size * host_list_size, sizeof(double));
60   for (i = 0; i < host_list_size; i++) {
61     computation_duration[i] = computation_amount / host_list_size;
62     for (j = 0; j < host_list_size; j++)
63       communication_table[i * host_list_size + j] =
64           communication_amount / (host_list_size * host_list_size);
65   }
66
67   sprintf(buffer, "redist#0\n");
68   task = MSG_parallel_task_create(buffer,
69                                   host_list_size,
70                                   m_host_list,
71                                   computation_duration,
72                                   communication_table, NULL);
73
74   execution_time = MSG_get_clock();
75   MSG_parallel_task_execute(task);
76   MSG_task_destroy(task);
77   xbt_free(m_host_list);
78   execution_time = MSG_get_clock() - execution_time;
79
80   XBT_INFO("execution_time=%g ", execution_time);
81
82   return 0;
83 }
84
85
86 int redistribute(int argc, char *argv[])
87 {
88   char buffer[32];
89   int i, j;
90   m_host_t *m_host_list = NULL;
91   m_task_t task = NULL;
92   int host_list_size;
93   double *computation_duration = NULL;
94   double *communication_table = NULL;
95   double communication_amount = 0;
96   double redistribution_time;
97
98
99   host_list_size = argc - 2;
100   XBT_DEBUG("host_list_size=%d", host_list_size);
101   m_host_list = calloc(host_list_size, sizeof(m_host_t));
102   for (i = 1; i <= host_list_size; i++) {
103     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
104     xbt_assert(m_host_list[i - 1] != NULL,
105                 "Unknown host %s. Stopping Now! ", argv[i]);
106   }
107
108   _XBT_GNUC_UNUSED int read;
109   read = sscanf(argv[argc - 1], "%lg", &communication_amount);
110   xbt_assert(read, "Invalid argument %s\n", argv[argc - 1]);
111   computation_duration = (double *) calloc(host_list_size, sizeof(double));
112   communication_table =
113       (double *) calloc(host_list_size * host_list_size, sizeof(double));
114   for (i = 0; i < host_list_size; i++) {
115     for (j = 0; j < host_list_size; j++)
116       communication_table[i * host_list_size + j] =
117           communication_amount / (host_list_size * host_list_size);
118   }
119
120   sprintf(buffer, "redist#0\n");
121   task = MSG_parallel_task_create(buffer,
122                                   host_list_size,
123                                   m_host_list,
124                                   computation_duration,
125                                   communication_table, NULL);
126
127   redistribution_time = MSG_get_clock();
128   MSG_parallel_task_execute(task);
129   MSG_task_destroy(task);
130   xbt_free(m_host_list);
131   redistribution_time = MSG_get_clock() - redistribution_time;
132
133   XBT_INFO("redistribution_time=%g ", redistribution_time);
134
135   return 0;
136 }
137
138
139 MSG_error_t test_all(const char *platform_file,
140                      const char *application_file)
141 {
142   MSG_error_t res = MSG_OK;
143
144
145   MSG_config("workstation/model", "ptask_L07");
146
147   /*  Simulation setting */
148   MSG_set_channel_number(MAX_CHANNEL);
149   MSG_create_environment(platform_file);
150
151   /*   Application deployment */
152   MSG_function_register("execute", execute);
153   MSG_function_register("redistribute", redistribute);
154   MSG_launch_application(application_file);
155
156   res = MSG_main();
157
158   XBT_INFO("Simulation time %g", MSG_get_clock());
159   return res;
160 }
161
162
163 int main(int argc, char *argv[])
164 {
165   MSG_error_t res = MSG_OK;
166
167   MSG_global_init(&argc, argv);
168   if (argc < 3) {
169     printf("Usage: %s platform_file deployment_file\n", argv[0]);
170     exit(1);
171   }
172   res = test_all(argv[1], argv[2]);
173   MSG_clean();
174
175   if (res == MSG_OK)
176     return 0;
177   else
178     return 1;
179 }