Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill tracing-gtnets examples
[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   double remaining = 0;
78   char id_alias[10];
79
80   xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
81
82   int id = atoi(argv[1]);
83   sprintf(id_alias, "%d", id);
84   int trace_id = id;
85
86   int a = MSG_task_receive(&(task), id_alias);
87
88   count_finished--;
89   if(count_finished == 0){
90       timer_start = 0;
91   }
92
93   xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
94
95   elapsed_time = MSG_get_clock() - start_time;
96
97   if (!bool_printed) {
98     bool_printed = 1;
99
100     for (id = 0; id < NTASKS; id++) {
101       if (gl_task_array[id] == NULL) continue;
102       if (gl_task_array[id] == task) {
103         XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
104              id, gl_data_size[id] / elapsed_time, masternames[id], slavenames[id], 0.0);
105         MSG_task_destroy(gl_task_array[id]);
106         gl_task_array[id]=NULL;
107       } else {
108         remaining = MSG_task_get_remaining_communication(gl_task_array[id]);
109         XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
110              id, (gl_data_size[id] - remaining) / elapsed_time, masternames[id], slavenames[id], remaining);
111         if(remaining==0) {
112           MSG_task_destroy(gl_task_array[id]);
113           gl_task_array[id]=NULL;
114         }
115       }
116     }
117     bool_printed = 2;
118   }
119   char mark[100];
120   snprintf(mark, 100, "flow_%d_finished", trace_id);
121   TRACE_mark("endmark", mark);
122
123   if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
124
125   return 0;
126 }
127
128 int main(int argc, char *argv[])
129 {
130   msg_error_t res = MSG_OK;
131   bool_printed = 0;
132
133   MSG_init(&argc, argv);
134   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
135              "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
136
137   MSG_create_environment(argv[1]);
138
139   TRACE_declare_mark("endmark");
140
141   MSG_function_register("master", master);
142   MSG_function_register("slave", slave);
143   MSG_launch_application(argv[2]);
144
145   res = MSG_main();
146
147   return res != MSG_OK;
148 }