Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cope with model best gtnets fit, that is latency_factor = 10.4, bandwidth_factor...
[simgrid.git] / examples / msg / sendrecv / sendrecv.c
1 /*      $Id$        */
2 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include<stdio.h>
7
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
16
17 int sender(int argc, char *argv[]);
18 int receiver(int argc, char *argv[]);
19
20 MSG_error_t test_all(const char *platform_file, const char *application_file);
21
22 typedef enum 
23   {
24     PORT_22 = 0,
25     MAX_CHANNEL
26   } channel_t;
27
28 double task_comm_size_lat = 10e0;
29 double task_comm_size_bw  = 10e8;
30
31 /** Emitter function  */
32 int sender(int argc,char *argv[] )
33 {
34   m_host_t host = NULL; 
35   double time;
36   m_task_t task_la=NULL;
37   m_task_t task_bw=NULL;
38   char sprintf_buffer_la[64];
39   char sprintf_buffer_bw[64];
40
41   INFO0("sender");
42  
43   /*host = xbt_new0(m_host_t,1);*/
44     
45   INFO1("host = %s", argv[1]);
46   
47   host = MSG_get_host_by_name(argv[1]);
48   
49   if(host==NULL){
50     INFO1("Unknown host %s. Stopping Now! ", argv[1]);
51     abort();
52   }
53
54   /* Latency */
55   time= MSG_get_clock();
56   sprintf(sprintf_buffer_la, "latency task");
57   task_la = MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
58   task_la->data = xbt_new(double, 1);
59   *(double *)task_la->data = time;
60   INFO1("task_la->data = %le", *((double *)task_la->data));
61   MSG_task_put(task_la, host,PORT_22);
62   
63   /* Bandwidth */
64   time=MSG_get_clock();
65   sprintf(sprintf_buffer_bw, "bandwidth task");
66   task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
67   task_bw->data = xbt_new(double, 1);
68   *(double *)task_bw->data = time;
69   INFO1("task_bw->data = %le", *((double *)task_bw->data) ); 
70   MSG_task_put(task_bw, host,PORT_22);  
71
72   return 0;
73 } /* end_of_client */
74
75 /** Receiver function  */
76 int receiver(int argc, char *argv[])
77 {
78   double time, time1, sender_time;
79   m_task_t task_la = NULL;
80   m_task_t task_bw = NULL;
81   int a;
82   double communication_time=0;
83
84   INFO0("receiver");
85
86   time = MSG_get_clock();
87   
88   /* Get Latency */
89   a = MSG_task_get(&task_la,PORT_22);
90   if (a == MSG_OK) {
91     time1=MSG_get_clock();
92     sender_time= *((double*)(task_la->data));
93     time=sender_time;
94     communication_time=time1-time;
95     INFO1("Task received : %s", task_la->name);
96     MSG_task_destroy(task_la);
97     INFO1("Communic. time %le",communication_time);
98     INFO1("--- la %f ----",communication_time);
99   }else{
100     xbt_assert0(0,"Unexpected behavior");
101   }
102
103
104   /* Get Bandwidth */
105   a=MSG_task_get(&task_bw,PORT_22);
106   if (a == MSG_OK) {
107     time1=MSG_get_clock();
108     sender_time= *((double*)(task_bw->data));
109     time=sender_time;
110     communication_time=time1-time;
111     INFO1("Task received : %s", task_bw->name);
112     MSG_task_destroy(task_bw);
113     INFO1("Communic. time %le",communication_time);
114     INFO1("--- bw %f ----",task_comm_size_bw/communication_time);
115   }else{
116     xbt_assert0(0,"Unexpected behavior");
117   }
118
119   
120   return 0;
121 }/* end_of_receiver */
122
123
124 /** Test function */
125 MSG_error_t test_all(const char *platform_file,
126                             const char *application_file)
127 {
128
129   MSG_error_t res = MSG_OK;
130
131   
132
133   INFO0("test_all"); 
134                         
135   /*  Simulation setting */
136   MSG_set_channel_number(MAX_CHANNEL);
137   MSG_paje_output("msg_test.trace");
138   MSG_create_environment(platform_file);
139  
140   /*   Application deployment */
141   MSG_function_register("sender", sender);
142   MSG_function_register("receiver", receiver);
143    
144   MSG_launch_application(application_file);
145   
146   res = MSG_main();
147
148   return res;
149 } /* end_of_test_all */
150
151
152 /** Main function */
153 int main(int argc, char *argv[])
154 {
155   MSG_error_t res = MSG_OK;
156         
157   #ifdef _MSC_VER
158   unsigned int prev_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
159   #endif
160   
161   MSG_global_init(&argc,argv);
162
163
164   if (argc != 3){
165      CRITICAL1 ("Usage: %s platform_file deployment_file <model>\n",argv[0]);
166      CRITICAL1 ("example: %s msg_platform.xml msg_deployment.xml KCCFLN05_Vegas\n",argv[0]);
167      exit(1);
168   }
169
170   /* Options for the workstation_model:
171
172      KCCFLN05              => for maxmin
173      KCCFLN05_proportional => for proportional (Vegas)
174      KCCFLN05_Vegas        => for TCP Vegas
175      KCCFLN05_Reno         => for TCP Reno
176   */
177   //MSG_config("workstation_model", argv[3]);
178
179   res = test_all(argv[1],argv[2]);
180
181   INFO1("Total simulation time: %le", MSG_get_clock());
182
183   MSG_clean();
184
185   #ifdef _MSC_VER
186   _set_output_format(prev_exponent_format);
187   #endif
188
189   if(res==MSG_OK) return 0; 
190   else return 1;
191 } /* end_of_main */