Logo AND Algorithmique Numérique Distribuée

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