Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added a reliable print mechanism, now flow labels depend on id. Hence, the order...
[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,"Messages specific for this msg example");
8
9 int master(int argc, char *argv[]);
10 int slave(int argc, char *argv[]);
11 MSG_error_t test_all(const char *platform_file, const char *application_file);
12
13 typedef enum {
14   PORT_22 = 0,
15   MAX_CHANNEL
16 } channel_t;
17
18 //keep a pointer to all surf running tasks.
19 #define NTASKS 2
20 int bool_printed=0;
21 double start_time, end_time, elapsed_time;
22 double   gl_data_size[NTASKS];
23 m_task_t gl_task_array[NTASKS];
24 int gl_task_array_id=0;
25
26 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
27
28 /** master */
29 int master(int argc, char *argv[])
30 {
31   char *slavename = NULL;
32   double task_comm_size = 0;
33   m_task_t todo;
34   m_host_t slave;
35   //unique id to control statistics
36   int id=gl_task_array_id++;
37
38   /* data size */
39   xbt_assert1(sscanf(argv[1],"%lg", &task_comm_size),
40               "Invalid argument %s\n", argv[1]);
41
42   /* slave name */
43   slavename = argv[2];
44   
45   { /*  Task creation.  */
46     char sprintf_buffer[64] = "Task_0";
47     todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
48     //keep track of running tasks
49     gl_task_array[id] = todo;
50     gl_data_size[id]=task_comm_size;
51   }
52
53   { /* Process organisation */
54     slave = MSG_get_host_by_name(slavename);
55   }
56
57   /* time measurement */
58   start_time = MSG_get_clock();
59   MSG_task_put(todo, slave, PORT_22);
60   end_time = MSG_get_clock();
61   INFO3("Send completed (to %s). Transfer time: %f\t Agregate bandwidth: %f",
62         slave->name, (end_time - start_time), task_comm_size/(end_time-start_time));
63   INFO2("Completed peer: %s time: %f", slave->name, (end_time-start_time));
64
65   return 0;
66 } /* end_of_master */
67
68 /** Receiver function  */
69 int slave(int argc, char *argv[])
70 {
71   char flow='A';
72   m_task_t task = NULL;
73   int a;
74   int id=0;
75   double remaining=0;
76   a = MSG_task_get(&(task), PORT_22);
77   if (a != MSG_OK) {
78     INFO0("Hey?! What's up?");
79     xbt_assert0(0,"Unexpected behavior.");
80   }
81   
82   elapsed_time = MSG_get_clock() - start_time;
83
84   if(!bool_printed){
85     bool_printed=1;
86     for(id=0; id<NTASKS; id++){
87       if(gl_task_array[id] == NULL){
88         INFO0("===> Task already done, skipping print statistics");
89       }else if(gl_task_array[id] == task){
90         INFO2("===> Estimated Bw of FLOW%c : %f", flow+id, gl_data_size[id]/elapsed_time);
91       }else{
92         remaining = MSG_task_get_remaining_communication(gl_task_array[id]);
93         INFO3("===> Estimated Bw of FLOW%c : %f , with remaining %f", flow+id, (gl_data_size[id]-remaining)/elapsed_time, remaining);
94       }
95     }
96   }
97
98   MSG_task_destroy(task);
99
100   return 0;
101 } /* end_of_slave */
102
103 /** Test function */
104 MSG_error_t test_all(const char *platform_file,
105                      const char *application_file)
106 {
107   MSG_error_t res = MSG_OK;
108
109   /* MSG_config("workstation_model", "GTNETS"); */
110   /* MSG_config("workstation_model","KCCFLN05"); */
111   {                             /*  Simulation setting */
112     MSG_set_channel_number(MAX_CHANNEL);
113     MSG_paje_output("msg_test.trace");
114     MSG_create_environment(platform_file);
115   }
116   {                   /*   Application deployment */
117     MSG_function_register("master", master);
118     MSG_function_register("slave", slave);
119     MSG_launch_application(application_file);
120   }
121   res = MSG_main();
122   return res;
123 } /* end_of_test_all */
124
125 /** Main function */
126 int main(int argc, char *argv[])
127 {
128   MSG_error_t res = MSG_OK;
129   bool_printed = 0;
130
131   MSG_global_init(&argc,argv);
132   if (argc < 3) {
133      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
134      exit(1);
135   }
136   res = test_all(argv[1],argv[2]);
137
138   MSG_clean();
139
140   if(res==MSG_OK) return 0; 
141   else return 1;
142 } /* end_of_main */