Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'coverity_scan' of github.com:mquinson/simgrid
[simgrid.git] / examples / msg / gtnets / gtnets.c
1 /* Copyright (c) 2007-2015. 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 "simgrid/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 /** @addtogroup MSG_examples
17  * 
18  * - <b>gtnets</b> Simple ping-pong using GTNeTs instead of the SimGrid network models.
19  */
20
21 int master(int argc, char *argv[]);
22 int slave(int argc, char *argv[]);
23 msg_error_t test_all(const char *platform_file,
24                      const char *application_file);
25
26 int timer_start = 1;
27
28 //keep a pointer to all surf running tasks.
29 #define NTASKS 1500
30 int bool_printed = 0;
31 double start_time, end_time, elapsed_time;
32 double gl_data_size[NTASKS];
33 msg_task_t gl_task_array[NTASKS];
34 const char *slavenames[NTASKS];
35 const char *masternames[NTASKS];
36 int gl_task_array_id = 0;
37 int count_finished = 0;
38
39 /** master */
40 int master(int argc, char *argv[])
41 {
42   char *slavename = NULL;
43   double task_comm_size = 0;
44   msg_task_t todo;
45   char id_alias[10];
46   //unique id to control statistics
47   int id = -1;
48
49   xbt_assert(argc == 4, "Strange number of arguments expected 3 got %d",
50              argc - 1);
51
52   /* data size */
53   int read;
54   read = sscanf(argv[1], "%lg", &task_comm_size);
55   xbt_assert(read, "Invalid argument %s\n", argv[1]);
56
57   /* slave name */
58   slavename = argv[2];
59   id = atoi(argv[3]);
60   sprintf(id_alias, "flow_%d", id);
61   slavenames[id] = slavename;
62   TRACE_category(id_alias);
63
64   masternames[id] = MSG_host_get_name(MSG_host_self());
65
66   {                             /*  Task creation.  */
67     char sprintf_buffer[64] = "Task_0";
68     todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
69     MSG_task_set_category(todo, id_alias);
70     //keep track of running tasks
71     gl_task_array[id] = todo;
72     gl_data_size[id] = task_comm_size;
73   }
74
75   count_finished++;
76
77   /* time measurement */
78   sprintf(id_alias, "%d", id);
79   start_time = MSG_get_clock();
80   MSG_task_send(todo, id_alias);
81   end_time = MSG_get_clock();
82
83
84   return 0;
85 }                               /* end_of_master */
86
87 /** Receiver function  */
88 int slave(int argc, char *argv[])
89 {
90
91   msg_task_t task = NULL;
92   int a;
93   int id = 0;
94 #ifdef HAVE_LATENCY_BOUND_TRACKING
95   int limited_latency = 0;
96 #endif
97   double remaining = 0;
98   char id_alias[10];
99
100   xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d",
101              argc - 1);
102
103   id = atoi(argv[1]);
104   sprintf(id_alias, "%d", id);
105   int trace_id = id;
106
107   a = MSG_task_receive(&(task), id_alias);
108
109   count_finished--;
110   if(count_finished == 0){
111       timer_start = 0;
112   }
113
114  xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
115
116   elapsed_time = MSG_get_clock() - start_time;
117
118   if (!bool_printed) {
119     bool_printed = 1;
120
121     for (id = 0; id < NTASKS; id++) {
122       if (gl_task_array[id] == NULL) continue;
123       if (gl_task_array[id] == task) {
124 #ifdef HAVE_LATENCY_BOUND_TRACKING
125         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
126         if (limited_latency) {
127           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
128         }
129 #endif
130         XBT_INFO
131             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
132              id, gl_data_size[id] / elapsed_time, masternames[id],
133              slavenames[id], 0.0);
134         MSG_task_destroy(gl_task_array[id]);
135         gl_task_array[id]=NULL;
136       } else {
137         remaining =
138             MSG_task_get_remaining_communication(gl_task_array[id]);
139 #ifdef HAVE_LATENCY_BOUND_TRACKING
140         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
141
142         if (limited_latency) {
143           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
144         }
145 #endif
146         XBT_INFO
147             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
148              id, (gl_data_size[id] - remaining) / elapsed_time,
149              masternames[id], slavenames[id], remaining);
150         if(remaining==0) {
151           MSG_task_destroy(gl_task_array[id]);
152           gl_task_array[id]=NULL;
153         }
154       }
155     }
156     bool_printed = 2;
157   }
158   char mark[100];
159   snprintf(mark, 100, "flow_%d_finished", trace_id);
160   TRACE_mark("endmark", mark);
161
162   if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
163
164   return 0;
165 }                               /* end_of_slave */
166
167 /** Test function */
168 msg_error_t test_all(const char *platform_file,
169                      const char *application_file)
170 {
171   msg_error_t res = MSG_OK;
172
173   {                             /*  Simulation setting */
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
183     MSG_launch_application(application_file);
184   }
185   res = MSG_main();
186   return res;
187 }                               /* end_of_test_all */
188
189 /** Main function */
190 int main(int argc, char *argv[])
191 {
192   msg_error_t res = MSG_OK;
193   bool_printed = 0;
194
195   MSG_init(&argc, argv);
196   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
197             "\tExample: %s platform.xml deployment.xml\n", 
198             argv[0], argv[0]);
199    
200   res = test_all(argv[1], argv[2]);
201
202   return res != MSG_OK;
203 }
204