Logo AND Algorithmique Numérique Distribuée

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