Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial implementation of kadeploy look-alike
[simgrid.git] / examples / msg / kadeploy / kadeploy.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * Copyright (c) 2012. Maximiliano Geier.
3  * All rights reserved.                                                     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include<stdio.h>
9
10 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
11 #include "xbt/sysdep.h"         /* calloc */
12
13 /* Create a log channel to have nice outputs. */
14 #include "xbt/log.h"
15 #include "xbt/asserts.h"
16
17 /** @addtogroup MSG_examples
18  * 
19  *  - <b>kadeploy/kadeploy.c: Kadeploy implementation</b>.
20  */
21
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kadeploy,
24                              "Messages specific for kadeploy");
25
26 /*
27  Data structures
28  */
29
30 /* Random iterator for xbt_dynar */
31 typedef struct xbt_dynar_iterator_struct {
32   xbt_dynar_t list;
33   xbt_dynar_t indices_list;
34   unsigned int current;
35   unsigned long length;
36   unsigned int (*criteria_fn)(void* it);
37 } *xbt_dynar_iterator_t;
38 typedef struct xbt_dynar_iterator_struct xbt_dynar_iterator_s;
39
40
41 xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, unsigned int (*criteria_fn)(void*));
42 void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it);
43 void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it);
44 unsigned int xbt_dynar_iterator_forward_criteria(void *p);
45
46 int broadcaster(int argc, char *argv[]);
47 int peer(int argc, char *argv[]);
48
49 void check_hosts(const int count, char **list);
50 xbt_dynar_t build_hostlist_from_argv(int argc, char *argv[]);
51 void build_chain(xbt_dynar_t host_list);
52
53 int peer_wait_for_init();
54
55 msg_error_t test_all(const char *platform_file,
56                      const char *application_file);
57
58 double task_comm_size_lat = 10e0;
59 double task_comm_size_bw = 10e8;
60
61 /* Allocates and initializes a new xbt_dynar iterator for list, using criteria_fn as iteration criteria
62    criteria_fn: given an iterator, it must update the iterator and give the next element's index, 
63    less than 0 otherwise*/
64 xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, unsigned int (*criteria_fn)(void*))
65 {
66   xbt_dynar_iterator_t it = xbt_new(xbt_dynar_iterator_s, 1);
67   
68   it->list = list;
69   it->length = xbt_dynar_length(list);
70   it->indices_list = xbt_dynar_new(sizeof(unsigned int), NULL);
71   it->criteria_fn = criteria_fn;
72   it->current = -1;
73 }
74
75 /* Returns the next element iterated by iterator it, NULL if there are no more elements */
76 void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it)
77 {
78   unsigned int next = it->criteria_fn((xbt_dynar_iterator_t)it);
79   XBT_INFO("%d current\n", next);
80   if (next < 0)
81     return NULL;
82   else {
83     xbt_dynar_push(it->indices_list, &next);
84     return xbt_dynar_get_ptr(it->list, next);
85   }
86 }
87
88 void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it)
89 {
90   xbt_dynar_free_container(&(it->indices_list));
91   xbt_free_ref(&it);
92 }
93
94 unsigned int xbt_dynar_iterator_forward_criteria(void *p)
95 {
96   xbt_dynar_iterator_t it = (xbt_dynar_iterator_t)p;
97   unsigned int r = -1;
98   if (it->current == -1) {
99     /* iterator initialization */
100     it->current = 0;
101   }
102   if (it->current < it->length) {
103     r = it->current;
104     it->current++;
105   }
106
107   return r;
108 }
109
110 xbt_dynar_t build_hostlist_from_argv(int argc, char *argv[])
111 {
112   xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
113   msg_host_t h = NULL;
114   int i = 1;
115   
116   for (; i < argc; i++) {
117     XBT_INFO("host%d = %s", i, argv[i]);
118     h = MSG_get_host_by_name(argv[i]);
119     if (h == NULL) {
120       XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
121       abort();
122     } else {
123       xbt_dynar_push(host_list, &(argv[i]));
124     }
125   }
126   return host_list;
127 }
128
129 void delete_hostlist(xbt_dynar_t h)
130 {
131   xbt_dynar_free_container(&h);
132 }
133
134 void build_chain(xbt_dynar_t host_list)
135 {
136   xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, xbt_dynar_iterator_forward_criteria);
137   char **cur = NULL;
138
139   for (cur = (char**)xbt_dynar_iterator_next(it); cur != NULL; cur = (char**)xbt_dynar_iterator_next(it)) {
140     XBT_INFO("iterating host = %s", *cur);
141   }
142 }
143
144 /*void setup_chain_criteria(chain_criteria_t c, char *(*fn)(void))
145 {
146   
147 }
148
149 void build_chain(const int hostcount, char **hostlist)
150 {
151   int i;
152   for (i = 0; i < hostcount; i++) {
153     
154   }
155 }*/
156
157 /** Emitter function  */
158 int broadcaster(int argc, char *argv[])
159 {
160   double time;
161   xbt_dynar_t host_list = NULL;
162   msg_task_t task_la = NULL;
163   msg_task_t task_bw = NULL;
164   char sprintf_buffer_la[64];
165   char sprintf_buffer_bw[64];
166
167   XBT_INFO("broadcaster");
168
169   /* Check that every host in the command line actually exists and add it to a dynamic array */
170   host_list = build_hostlist_from_argv(argc, argv);
171   
172   build_chain(host_list);
173
174   /* Latency */
175   /*time = MSG_get_clock();
176   sprintf(sprintf_buffer_la, "latency task");
177   task_la =
178       MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
179   task_la->data = xbt_new(double, 1);
180   *(double *) task_la->data = time;
181   XBT_INFO("task_la->data = %le", *((double *) task_la->data));
182   MSG_task_send(task_la, argv[1]);*/
183
184   /* Bandwidth */
185   /*time = MSG_get_clock();
186   sprintf(sprintf_buffer_bw, "bandwidth task");
187   task_bw =
188       MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
189   task_bw->data = xbt_new(double, 1);
190   *(double *) task_bw->data = time;
191   XBT_INFO("task_bw->data = %le", *((double *) task_bw->data));
192   MSG_task_send(task_bw, argv[1]);
193   */
194   return 0;
195 }                               /* end_of_client */
196
197 int peer_wait_for_init()
198 {
199   return MSG_OK;
200 }
201
202 /** Peer function  */
203 int peer(int argc, char *argv[])
204 {
205   double time, time1, sender_time;
206   msg_task_t task_la = NULL;
207   msg_task_t task_bw = NULL;
208   int a;
209   double communication_time = 0;
210
211   XBT_INFO("peer");
212
213   time = MSG_get_clock();
214
215   a = peer_wait_for_init();
216   /* Get Latency */
217   /*a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
218   if (a == MSG_OK) {
219     time1 = MSG_get_clock();
220     sender_time = *((double *) (task_la->data));
221     time = sender_time;
222     communication_time = time1 - time;
223     XBT_INFO("Task received : %s", task_la->name);
224     xbt_free(task_la->data);
225     MSG_task_destroy(task_la);
226     XBT_INFO("Communic. time %le", communication_time);
227     XBT_INFO("--- la %f ----", communication_time);
228   } else {
229     xbt_die("Unexpected behavior");
230   }*/
231
232
233   /* Get Bandwidth */
234   /*a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
235   if (a == MSG_OK) {
236     time1 = MSG_get_clock();
237     sender_time = *((double *) (task_bw->data));
238     time = sender_time;
239     communication_time = time1 - time;
240     XBT_INFO("Task received : %s", task_bw->name);
241     xbt_free(task_bw->data);
242     MSG_task_destroy(task_bw);
243     XBT_INFO("Communic. time %le", communication_time);
244     XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
245   } else {
246     xbt_die("Unexpected behavior");
247   }*/
248
249
250   return 0;
251 }                               /* end_of_receiver */
252
253
254 /** Test function */
255 msg_error_t test_all(const char *platform_file,
256                      const char *application_file)
257 {
258
259   msg_error_t res = MSG_OK;
260
261
262
263   XBT_INFO("test_all");
264
265   /*  Simulation setting */
266   MSG_create_environment(platform_file);
267
268   /*   Application deployment */
269   MSG_function_register("broadcaster", broadcaster);
270   MSG_function_register("peer", peer);
271
272   MSG_launch_application(application_file);
273
274   res = MSG_main();
275
276   return res;
277 }                               /* end_of_test_all */
278
279
280 /** Main function */
281 int main(int argc, char *argv[])
282 {
283   msg_error_t res = MSG_OK;
284
285 #ifdef _MSC_VER
286   unsigned int prev_exponent_format =
287       _set_output_format(_TWO_DIGIT_EXPONENT);
288 #endif
289
290   MSG_init(&argc, argv);
291
292
293   if (argc != 3) {
294     XBT_CRITICAL("Usage: %s platform_file deployment_file <model>\n",
295               argv[0]);
296     XBT_CRITICAL
297         ("example: %s msg_platform.xml msg_deployment.xml KCCFLN05_Vegas\n",
298          argv[0]);
299     exit(1);
300   }
301
302   /* Options for the workstation/model:
303
304      KCCFLN05              => for maxmin
305      KCCFLN05_proportional => for proportional (Vegas)
306      KCCFLN05_Vegas        => for TCP Vegas
307      KCCFLN05_Reno         => for TCP Reno
308    */
309   //MSG_config("workstation/model", argv[3]);
310
311   res = test_all(argv[1], argv[2]);
312
313   XBT_INFO("Total simulation time: %le", MSG_get_clock());
314
315   MSG_clean();
316
317 #ifdef _MSC_VER
318   _set_output_format(prev_exponent_format);
319 #endif
320
321   if (res == MSG_OK)
322     return 0;
323   else
324     return 1;
325 }                               /* end_of_main */