Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Corrected some bugs, added the simix support to the other workstation
[simgrid.git] / examples / msg / ping_pong.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 = calloc(1, sizeof(m_host_t));
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       = %p", task_la); 
61   INFO1("task_la->data = %le", *((double *)task_la->data)); 
62   MSG_task_put(task_la, host,PORT_22);    
63   
64   /* Bandwidth */
65   time=MSG_get_clock();
66   sprintf(sprintf_buffer_bw, "bandwidth task");
67   task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
68   task_bw->data = xbt_new(double, 1);
69   *(double *)task_bw->data = time;
70   INFO1("task_bw       = %p", task_bw); 
71   INFO1("task_bw->data = %le", *((double *)task_bw->data) ); 
72   MSG_task_put(task_bw, host,PORT_22);  
73
74   return 0;
75 } /* end_of_client */
76
77 /** Receiver function  */
78 int receiver(int argc, char *argv[])
79 {
80   double time, time1, sender_time;
81   m_task_t task_la = NULL;
82   m_task_t task_bw = NULL;
83   int a;
84   double communication_time=0;
85
86   INFO0("receiver");
87
88   time = MSG_get_clock();
89   
90   /* Get Latency */
91   a = MSG_task_get(&task_la,PORT_22);
92   if (a == MSG_OK) {
93     time1=MSG_get_clock();
94     sender_time= *((double*)(task_la->data));
95     time=sender_time;
96     communication_time=time1-time;
97     INFO1("Task received : %s", task_la->name);
98     MSG_task_destroy(task_la);
99     INFO1("Communic. time %le",communication_time);
100     INFO1("--- la %f ----",task_comm_size_bw/communication_time);
101   }else{
102     xbt_assert0(0,"Unexpected behavior");
103   }
104
105
106   /* Get Bandwidth */
107   a=MSG_task_get(&task_bw,PORT_22);
108   if (a == MSG_OK) {
109     time1=MSG_get_clock();
110     sender_time= *((double*)(task_bw->data));
111     time=sender_time;
112     communication_time=time1-time;
113     INFO1("Task received : %s", task_bw->name);
114     MSG_task_destroy(task_bw);
115     INFO1("Communic. time %le",communication_time);
116     INFO1("--- bw %f ----",task_comm_size_bw/communication_time);
117   }else{
118     xbt_assert0(0,"Unexpected behavior");
119   }
120
121   
122   return 0;
123 }/* end_of_receiver */
124
125
126 /** Test function */
127 MSG_error_t test_all(const char *platform_file,
128                             const char *application_file)
129 {
130
131   MSG_error_t res = MSG_OK;
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   return res;
148 } /* end_of_test_all */
149
150
151 /** Main function */
152 int main(int argc, char *argv[])
153 {
154   MSG_error_t res = MSG_OK;
155
156   
157   MSG_global_init(&argc,argv);
158
159
160   if (argc != 4){
161      CRITICAL1 ("Usage: %s platform_file deployment_file <model>\n",argv[0]);
162      CRITICAL1 ("example: %s msg_platform.xml msg_deployment.xml KCCFLN05_Vegas\n",argv[0]);
163      exit(1);
164   }
165
166   /* Options for the workstation_model:
167
168      KCCFLN05              => for maxmin
169      KCCFLN05_proportional => for proportional (Vegas)
170      KCCFLN05_Vegas        => for TCP Vegas
171      KCCFLN05_Reno         => for TCP Reno
172   */
173   MSG_config("surf_workstation_model", argv[3]);
174
175   res = test_all(argv[1],argv[2]);
176
177   INFO1("Total simulation time: %le", MSG_get_clock());
178
179   MSG_clean();
180
181   if(res==MSG_OK) return 0; 
182   else return 1;
183 } /* end_of_main */