Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4def160b81cf5a49cf89f3b1acf538188fcf7c3a
[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 1500
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 const char * slavenames[NTASKS];
25 const char * masternames[NTASKS];
26 int gl_task_array_id=0;
27
28 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
29
30 /** master */
31 int master(int argc, char *argv[])
32 {
33   char *slavename = NULL;
34   double task_comm_size = 0;
35   m_task_t todo;
36   m_host_t slave;
37   char id_alias[10];
38   //unique id to control statistics
39   int id = -1;
40
41   if(argc != 4){
42     INFO1("Strange number of arguments expected 3 got %d", argc-1 );
43   }
44
45   /* data size */
46   xbt_assert1(sscanf(argv[1],"%lg", &task_comm_size),
47               "Invalid argument %s\n", argv[1]);
48
49   /* slave name */
50   slavename = argv[2];
51   id = atoi(argv[3]);
52   sprintf(id_alias, "%d",id);
53   slavenames[id] = slavename;
54
55   masternames[id] = MSG_host_get_name(MSG_host_self());
56   
57   { /*  Task creation.  */
58     char sprintf_buffer[64] = "Task_0";
59     todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
60     //keep track of running tasks
61     gl_task_array[id] = todo;
62     gl_data_size[id]=task_comm_size;
63   }
64
65   { /* Process organisation */
66     slave = MSG_get_host_by_name(slavename);
67   }
68
69   /* time measurement */
70   start_time = MSG_get_clock();
71   //MSG_task_put(todo, slave, PORT_22);
72   INFO1("Sending to %s", id_alias);
73   MSG_task_send(todo, id_alias);  
74
75   end_time = MSG_get_clock();
76   INFO3("Send completed (to %s). Transfer time: %f\t Agregate bandwidth: %f",
77         slave->name, (end_time - start_time), task_comm_size/(end_time-start_time));
78   INFO2("Completed peer: %s time: %f", slave->name, (end_time-start_time));
79
80   return 0;
81 } /* end_of_master */
82
83 /** Receiver function  */
84 int slave(int argc, char *argv[])
85 {
86
87   m_task_t task = NULL;
88   int a;
89   int id=0;
90   double remaining=0;
91   char id_alias[10];
92
93   if(argc != 2){
94     INFO1("Strange number of arguments expected 1 got %d", argc-1 );
95   }
96
97   id = atoi(argv[1]);
98   sprintf(id_alias, "%d",id);
99
100
101   //a = MSG_task_get(&(task), PORT_22);
102   INFO1("Receiving on %s", id_alias);
103   a = MSG_task_receive(&(task), id_alias );
104   
105
106   if (a != MSG_OK) {
107     INFO0("Hey?! What's up?");
108     xbt_assert0(0,"Unexpected behavior.");
109   }
110   
111   elapsed_time = MSG_get_clock() - start_time;
112
113   if(!bool_printed){
114     bool_printed=1;
115     for(id=0; id<NTASKS; id++){
116       if(gl_task_array[id] == NULL){
117       }else if(gl_task_array[id] == task){
118         INFO5("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f", id+1,  gl_data_size[id]/elapsed_time, masternames[id], slavenames[id],  0.0);
119       }else{
120         remaining = MSG_task_get_remaining_communication(gl_task_array[id]);
121         INFO5("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f", id+1,  (gl_data_size[id]-remaining)/elapsed_time, masternames[id], slavenames[id],  remaining);
122       }
123     }
124     exit(0);
125   }
126
127   for(id=0; id<NTASKS; id++){
128     if(gl_task_array[id] == task){
129       MSG_task_destroy(task);
130       gl_task_array[id] = NULL;
131       return 0;
132     }
133   }
134
135   return 0;
136 } /* end_of_slave */
137
138 /** Test function */
139 MSG_error_t test_all(const char *platform_file,
140                      const char *application_file)
141 {
142   MSG_error_t res = MSG_OK;
143
144   /* MSG_config("workstation_model", "GTNETS"); */
145   /* MSG_config("workstation_model","KCCFLN05"); */
146   {                             /*  Simulation setting */
147     MSG_set_channel_number(MAX_CHANNEL);
148     //MSG_paje_output("msg_test.trace");
149     MSG_create_environment(platform_file);
150   }
151   {                   /*   Application deployment */
152     MSG_function_register("master", master);
153     MSG_function_register("slave", slave);
154     MSG_launch_application(application_file);
155   }
156   res = MSG_main();
157   return res;
158 } /* end_of_test_all */
159
160 /** Main function */
161 int main(int argc, char *argv[])
162 {
163   MSG_error_t res = MSG_OK;
164   bool_printed = 0;
165
166   MSG_global_init(&argc,argv);
167   if (argc < 3) {
168      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
169      exit(1);
170   }
171   res = test_all(argv[1],argv[2]);
172
173   MSG_clean();
174
175   if(res==MSG_OK) return 0; 
176   else return 1;
177 } /* end_of_main */