Logo AND Algorithmique Numérique Distribuée

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