Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
smooth differences between tests before merge
[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   msg_task_t todo;
32
33   xbt_assert(argc == 4, "Strange number of arguments expected 3 got %d", argc - 1);
34
35   /* data size */
36   double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
37
38   /* slave name */
39   char *slavename = argv[2];
40   int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s");   //unique id to control statistics
41   char *id_alias = bprintf("flow_%d", id);
42   slavenames[id] = slavename;
43   TRACE_category(id_alias);
44
45   masternames[id] = MSG_host_get_name(MSG_host_self());
46
47   {                             /*  Task creation.  */
48     todo = MSG_task_create("Task_0", 0, task_comm_size, NULL);
49     MSG_task_set_category(todo, id_alias);
50     //keep track of running tasks
51     gl_task_array[id] = todo;
52     gl_data_size[id] = task_comm_size;
53   }
54
55   count_finished++;
56
57   /* time measurement */
58   sprintf(id_alias, "%d", id);
59   start_time = MSG_get_clock();
60   MSG_task_send(todo, id_alias);
61   end_time = MSG_get_clock();
62
63   xbt_free(id_alias);
64   return 0;
65 }
66
67 static int slave(int argc, char *argv[])
68 {
69   msg_task_t task = NULL;
70   double remaining = 0;
71   char id_alias[10];
72
73   xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
74
75   int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
76   sprintf(id_alias, "%d", id);
77   int trace_id = id;
78
79   msg_error_t a = MSG_task_receive(&(task), id_alias);
80
81   count_finished--;
82   if(count_finished == 0){
83       timer_start = 0;
84   }
85
86   xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
87
88   elapsed_time = MSG_get_clock() - start_time;
89
90   if (!bool_printed) {
91     bool_printed = 1;
92
93     for (id = 0; id < NTASKS; id++) {
94       if (gl_task_array[id] == NULL) continue;
95       if (gl_task_array[id] == task) {
96         XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
97              id, gl_data_size[id] / elapsed_time, masternames[id], slavenames[id], 0.0);
98         MSG_task_destroy(gl_task_array[id]);
99         gl_task_array[id]=NULL;
100       } else {
101         remaining = MSG_task_get_remaining_communication(gl_task_array[id]);
102         XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
103              id, (gl_data_size[id] - remaining) / elapsed_time, masternames[id], slavenames[id], remaining);
104         if(remaining==0) {
105           MSG_task_destroy(gl_task_array[id]);
106           gl_task_array[id]=NULL;
107         }
108       }
109     }
110     bool_printed = 2;
111   }
112   char mark[100];
113   snprintf(mark, 100, "flow_%d_finished", trace_id);
114   TRACE_mark("endmark", mark);
115
116   if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
117
118   return 0;
119 }
120
121 int main(int argc, char *argv[])
122 {
123   msg_error_t res = MSG_OK;
124   bool_printed = 0;
125
126   MSG_init(&argc, argv);
127   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
128              "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
129
130   MSG_create_environment(argv[1]);
131   TRACE_declare_mark("endmark");
132
133   MSG_function_register("master", master);
134   MSG_function_register("slave", slave);
135   MSG_launch_application(argv[2]);
136
137   res = MSG_main();
138
139   return res != MSG_OK;
140 }