Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Dirty hacks again: shot more bullets, it may be a zombie
[simgrid.git] / examples / msg / gtnets / gtnets.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 <stdlib.h>
9 #include "msg/msg.h"
10 #include "xbt/log.h"
11 #include "xbt/asserts.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 MSG_error_t test_all(const char *platform_file,
19                      const char *application_file);
20
21 typedef enum {
22   PORT_22 = 0,
23   MAX_CHANNEL
24 } channel_t;
25
26 //keep a pointer to all surf running tasks.
27 #define NTASKS 1500
28 int bool_printed = 0;
29 double start_time, end_time, elapsed_time;
30 double gl_data_size[NTASKS];
31 m_task_t gl_task_array[NTASKS];
32 const char *slavenames[NTASKS];
33 const char *masternames[NTASKS];
34 int gl_task_array_id = 0;
35
36 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
37
38 /** master */
39 int master(int argc, char *argv[])
40 {
41   char *slavename = NULL;
42   double task_comm_size = 0;
43   m_task_t todo;
44   m_host_t slave;
45   char id_alias[10];
46   //unique id to control statistics
47   int id = -1;
48
49   if (argc != 4) {
50     XBT_INFO("Strange number of arguments expected 3 got %d", argc - 1);
51   }
52
53   /* data size */
54   int read;
55   read = sscanf(argv[1], "%lg", &task_comm_size);
56   xbt_assert1(read, "Invalid argument %s\n", argv[1]);
57
58   /* slave name */
59   slavename = argv[2];
60   id = atoi(argv[3]);
61   sprintf(id_alias, "flow_%d", id);
62   slavenames[id] = slavename;
63   TRACE_category(id_alias);
64
65   masternames[id] = MSG_host_get_name(MSG_host_self());
66
67   {                             /*  Task creation.  */
68     char sprintf_buffer[64] = "Task_0";
69     todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
70     TRACE_msg_set_task_category(todo, id_alias);
71     //keep track of running tasks
72     gl_task_array[id] = todo;
73     gl_data_size[id] = task_comm_size;
74   }
75
76   {                             /* Process organisation */
77     slave = MSG_get_host_by_name(slavename);
78   }
79
80   /* time measurement */
81   sprintf(id_alias, "%d", id);
82   start_time = MSG_get_clock();
83   MSG_task_send(todo, id_alias);
84   end_time = MSG_get_clock();
85
86
87   return 0;
88 }                               /* end_of_master */
89
90 /** Receiver function  */
91 int slave(int argc, char *argv[])
92 {
93
94   m_task_t task = NULL;
95   int a;
96   int id = 0;
97 #ifdef HAVE_LATENCY_BOUND_TRACKING
98   int limited_latency = 0;
99 #endif
100   double remaining = 0;
101   char id_alias[10];
102
103   if (argc != 2) {
104     XBT_INFO("Strange number of arguments expected 1 got %d", argc - 1);
105   }
106
107   id = atoi(argv[1]);
108   sprintf(id_alias, "%d", id);
109   int trace_id = id;
110
111   a = MSG_task_receive(&(task), id_alias);
112
113   if (a != MSG_OK) {
114     XBT_INFO("Hey?! What's up?");
115     xbt_die("Unexpected behavior.");
116   }
117
118   elapsed_time = MSG_get_clock() - start_time;
119   
120   
121   if (!bool_printed) {
122     bool_printed = 1;
123     
124     for (id = 0; id < NTASKS; id++) {
125       if (gl_task_array[id] == NULL) {
126       } else if (gl_task_array[id] == task) {
127 #ifdef HAVE_LATENCY_BOUND_TRACKING
128         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
129         if (limited_latency) {
130           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
131         }
132 #endif
133         XBT_INFO
134             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
135              id, gl_data_size[id] / elapsed_time, masternames[id],
136              slavenames[id], 0.0);
137       } else {
138         remaining =
139             MSG_task_get_remaining_communication(gl_task_array[id]);
140 #ifdef HAVE_LATENCY_BOUND_TRACKING
141         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
142
143         if (limited_latency) {
144           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
145         }
146 #endif
147         XBT_INFO
148             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
149              id, (gl_data_size[id] - remaining) / elapsed_time,
150              masternames[id], slavenames[id], remaining);
151       }
152
153     }
154   }
155   char mark[100];
156   snprintf(mark, 100, "flow_%d_finished", trace_id);
157   TRACE_mark("endmark", mark);
158
159   MSG_task_destroy(task);
160   return 0;
161 }                               /* end_of_slave */
162
163 /** Test function */
164 MSG_error_t test_all(const char *platform_file,
165                      const char *application_file)
166 {
167   MSG_error_t res = MSG_OK;
168
169   /* MSG_config("workstation/model", "GTNETS"); */
170   /* MSG_config("workstation/model","KCCFLN05"); */
171   {                             /*  Simulation setting */
172     MSG_set_channel_number(MAX_CHANNEL);
173     MSG_create_environment(platform_file);
174   }
175   {                             /*   Application deployment */
176     MSG_function_register("master", master);
177     MSG_function_register("slave", slave);
178     MSG_launch_application(application_file);
179   }
180   res = MSG_main();
181   return res;
182 }                               /* end_of_test_all */
183
184 /** Main function */
185 int main(int argc, char *argv[])
186 {
187   MSG_error_t res = MSG_OK;
188   bool_printed = 0;
189
190   MSG_global_init(&argc, argv);
191   if (argc < 3) {
192     printf("Usage: %s platform_file deployment_file\n", argv[0]);
193     exit(1);
194   }
195
196   TRACE_declare_mark("endmark");
197
198   res = test_all(argv[1], argv[2]);
199
200   MSG_clean();
201
202   if (res == MSG_OK)
203     return 0;
204   else
205     return 1;
206 }                               /* end_of_main */