Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implementation of NS3.
[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 = 1;
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   if (argc != 4) {
53     XBT_INFO("Strange number of arguments expected 3 got %d", argc - 1);
54   }
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
85   /* time measurement */
86   sprintf(id_alias, "%d", id);
87   start_time = MSG_get_clock();
88   //MSG_task_execute(todo);
89   MSG_task_send(todo, id_alias);
90   end_time = MSG_get_clock();
91
92
93   return 0;
94 }                               /* end_of_master */
95
96
97 /** Timer function  */
98 int timer(int argc, char *argv[])
99 {
100   int sleep_time;
101   int first_sleep;
102
103   if (argc != 3) {
104     XBT_INFO("Strange number of arguments expected 2 got %d", argc - 1);
105   }
106
107   sscanf(argv[1], "%d", &first_sleep);
108   sscanf(argv[2], "%d", &sleep_time);
109
110   if(first_sleep){
111       MSG_process_sleep(first_sleep);
112   }
113
114   while(timer_start){
115       MSG_process_sleep(sleep_time);
116   }
117
118   return 0;
119 }
120
121 /** Receiver function  */
122 int slave(int argc, char *argv[])
123 {
124
125   m_task_t task = NULL;
126   int a = MSG_OK;
127   int id = 0;
128   char id_alias[10];
129
130   if (argc != 2) {
131     XBT_INFO("Strange number of arguments expected 1 got %d", argc - 1);
132   }
133
134   id = atoi(argv[1]);
135   sprintf(id_alias, "%d", id);
136
137   a = MSG_task_receive(&(task), id_alias);
138
139   count_finished--;
140   if(count_finished == 0){
141       timer_start = 0;
142   }
143
144
145
146   if (a != MSG_OK) {
147     XBT_INFO("Hey?! What's up?");
148     xbt_die("Unexpected behavior.");
149   }
150
151   elapsed_time = MSG_get_clock() - start_time;
152   
153   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
154                   id,
155                   MSG_task_get_data_size(task),
156        masternames[id],
157        slavenames[id]);
158
159   MSG_task_destroy(task);
160
161   return 0;
162 }                               /* end_of_slave */
163
164 /** Test function */
165 MSG_error_t test_all(const char *platform_file,
166                      const char *application_file)
167 {
168   MSG_error_t res = MSG_OK;
169
170   /* MSG_config("workstation/model", "GTNETS"); */
171   /* MSG_config("workstation/model","KCCFLN05"); */
172   {                             /*  Simulation setting */
173     MSG_set_channel_number(MAX_CHANNEL);
174     MSG_create_environment(platform_file);
175   }
176
177   TRACE_declare_mark("endmark");
178
179   {                             /*   Application deployment */
180     MSG_function_register("master", master);
181     MSG_function_register("slave", slave);
182     MSG_function_register("timer", timer);
183
184     MSG_launch_application(application_file);
185   }
186   res = MSG_main();
187   return res;
188 }                               /* end_of_test_all */
189
190 /** Main function */
191 int main(int argc, char *argv[])
192 {
193   MSG_error_t res = MSG_OK;
194   bool_printed = 0;
195
196   MSG_global_init(&argc, argv);
197   if (argc < 3) {
198     printf("Usage: %s platform_file deployment_file\n", argv[0]);
199     exit(1);
200   }
201
202   res = test_all(argv[1], argv[2]);
203
204   MSG_clean();
205
206   if (res == MSG_OK)
207     return 0;
208   else
209     return 1;
210 }                               /* end_of_main */