Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d0c7456e4be6da05b451e36e5eafb1341595035d
[simgrid.git] / examples / msg / ns3 / ns3.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 int timer(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file,
20                      const char *application_file);
21
22 int timer_start; //set as 1 in the master process
23
24 typedef enum {
25   PORT_22 = 0,
26   MAX_CHANNEL
27 } channel_t;
28
29 //keep a pointer to all surf running tasks.
30 #define NTASKS 1500
31 int bool_printed = 0;
32 double start_time, end_time, elapsed_time;
33 double gl_data_size[NTASKS];
34 m_task_t gl_task_array[NTASKS];
35 const char *slavenames[NTASKS];
36 const char *masternames[NTASKS];
37 int gl_task_array_id = 0;
38 int count_finished = 0;
39
40 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
41
42 /** master */
43 int master(int argc, char *argv[])
44 {
45   char *slavename = NULL;
46   double task_comm_size = 0;
47   m_task_t todo;
48   char id_alias[10];
49   //unique id to control statistics
50   int id = -1;
51
52   xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
53
54   XBT_DEBUG ("Master started");
55
56   /* data size */
57   int read;
58   read = sscanf(argv[1], "%lg", &task_comm_size);
59   xbt_assert(read, "Invalid argument %s\n", argv[1]);
60
61   /* slave name */
62   slavename = argv[2];
63   id = atoi(argv[3]);
64   sprintf(id_alias, "flow_%d", id);
65   slavenames[id] = slavename;
66   TRACE_category(id_alias);
67
68   masternames[id] = MSG_host_get_name(MSG_host_self());
69
70   {                             /*  Task creation.  */
71     char sprintf_buffer[64] = "Task_0";
72     todo = MSG_task_create(sprintf_buffer, 100*task_comm_size, task_comm_size, NULL);
73     TRACE_msg_set_task_category(todo, id_alias);
74     //keep track of running tasks
75     gl_task_array[id] = todo;
76     gl_data_size[id] = task_comm_size;
77   }
78
79   {                             /* Process organisation */
80     MSG_get_host_by_name(slavename);
81   }
82
83   count_finished++;
84   timer_start = 1 ;
85
86   /* time measurement */
87   sprintf(id_alias, "%d", id);
88   start_time = MSG_get_clock();
89   //MSG_task_execute(todo);
90   MSG_task_send(todo, id_alias);
91   end_time = MSG_get_clock();
92
93   XBT_DEBUG ("Finished");
94   return 0;
95 }                               /* end_of_master */
96
97
98 /** Timer function  */
99 int timer(int argc, char *argv[])
100 {
101   double sleep_time;
102   double first_sleep;
103
104   xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
105
106   sscanf(argv[1], "%lf", &first_sleep);
107   sscanf(argv[2], "%lf", &sleep_time);
108
109   XBT_DEBUG ("Timer started");
110
111   if(first_sleep){
112       MSG_process_sleep(first_sleep);
113   }
114
115   do {
116     XBT_DEBUG ("Get sleep");
117       MSG_process_sleep(sleep_time);
118   } while(timer_start);
119
120   XBT_DEBUG ("Finished");
121   return 0;
122 }
123
124 /** Receiver function  */
125 int slave(int argc, char *argv[])
126 {
127
128   m_task_t task = NULL;
129   int a = MSG_OK;
130   int id = 0;
131   char id_alias[10];
132
133   xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
134
135   XBT_DEBUG ("Slave started");
136
137   id = atoi(argv[1]);
138   sprintf(id_alias, "%d", id);
139
140   a = MSG_task_receive(&(task), id_alias);
141
142   count_finished--;
143   if(count_finished == 0){
144       timer_start = 0;
145   }
146
147
148
149   if (a != MSG_OK) {
150     XBT_INFO("Hey?! What's up?");
151     xbt_die("Unexpected behavior.");
152   }
153
154   elapsed_time = MSG_get_clock() - start_time;
155   
156   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
157                   id,
158                   MSG_task_get_data_size(task),
159        masternames[id],
160        slavenames[id]);
161 //  MSG_task_execute(task);
162   MSG_task_destroy(task);
163
164   XBT_DEBUG ("Finished");
165   return 0;
166 }                               /* end_of_slave */
167
168 /** Test function */
169 MSG_error_t test_all(const char *platform_file,
170                      const char *application_file)
171 {
172   MSG_error_t res = MSG_OK;
173
174   /* MSG_config("workstation/model", "GTNETS"); */
175   /* MSG_config("workstation/model","KCCFLN05"); */
176   {                             /*  Simulation setting */
177     MSG_set_channel_number(MAX_CHANNEL);
178     MSG_create_environment(platform_file);
179   }
180
181   TRACE_declare_mark("endmark");
182
183   {                             /*   Application deployment */
184     MSG_function_register("master", master);
185     MSG_function_register("slave", slave);
186     MSG_function_register("timer", timer);
187
188     MSG_launch_application(application_file);
189   }
190   res = MSG_main();
191   return res;
192 }                               /* end_of_test_all */
193
194 /** Main function */
195 int main(int argc, char *argv[])
196 {
197   MSG_error_t res = MSG_OK;
198   bool_printed = 0;
199
200   MSG_global_init(&argc, argv);
201   if (argc < 3) {
202     printf("Usage: %s platform_file deployment_file\n", argv[0]);
203     exit(1);
204   }
205
206   res = test_all(argv[1], argv[2]);
207
208   MSG_clean();
209
210   if (res == MSG_OK)
211     return 0;
212   else
213     return 1;
214 }                               /* end_of_main */