Logo AND Algorithmique Numérique Distribuée

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