Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use reasonable values in deployment files.
[simgrid.git] / examples / msg / ping_pong.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
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
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
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 = 1;
29 double task_comm_size_bw = 100000000;
30
31 /** Emitter function  */
32 int sender(int argc,char *argv[] )
33 {
34   int nbr = 0;
35   m_host_t *hosts = NULL; 
36   int i;
37   double time;
38   double task_comp_size = 0;
39   m_task_t task=NULL;
40   char sprintf_buffer[64];
41
42   INFO0("sender");
43  
44   nbr = argc - 1;
45  hosts = calloc(nbr, sizeof(m_host_t));
46     
47   for (i = 1; i < argc; i++) 
48     {
49  INFO2("host [%d]= %s",i-1, argv[i]);
50       hosts[i-1] = MSG_get_host_by_name(argv[i]);
51       if(hosts[i-1]==NULL) 
52         {
53           INFO1("Unknown host %s. Stopping Now! ", argv[i]);
54           abort();
55         }
56     }
57
58   for (i = 0; i < nbr; i++) 
59     { 
60 /* Latency */
61  time=MSG_get_clock(); 
62        task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size_lat, NULL); 
63  INFO1("task = %p",task); 
64        task->data=xbt_new0(double,1); 
65        *(double*)(task->data)=time; 
66  INFO1("task->data = %le", *(double*)(task->data)); 
67  INFO2("host [%d]=%s ",i, argv[i+1]); 
68        MSG_task_put(task, hosts[i],PORT_22);    
69
70       /* Bandwidth */
71       time=MSG_get_clock();
72       task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size_bw, NULL);
73       task->data=xbt_new0(double,1);
74       *(double*)(task->data)=time;
75       MSG_task_put(task, hosts[i % nbr],PORT_22);  
76     }
77
78
79
80   return 0;
81 } /* end_of_client */
82
83 /** Receiver function  */
84 int receiver(int argc, char *argv[])
85 {
86
87   double communication_time=0;
88
89   INFO0("receiver");
90   while(1) 
91     {
92       double time, time1, sender_time;
93       m_task_t task = NULL;
94       int a;
95
96       time=MSG_get_clock();
97       a=MSG_task_get(&task,PORT_22);
98       if (a == MSG_OK) 
99         {
100      
101       time1=MSG_get_clock();
102       sender_time=(*(double*)(task->data));
103      
104 /*      if (sender_time > time) */
105         time=sender_time;
106       communication_time=time1-time;
107       INFO1("Communic. time %.35f",communication_time);
108       MSG_task_destroy(task);
109       INFO1("Communic. time %le",communication_time);
110
111       INFO1("--- bw %f ----",task_comm_size_bw/communication_time);
112         }
113       else 
114         {
115           xbt_assert0(0,"Unexpected behavior");
116         }
117     } 
118  return 0;
119 }/* end_of_receiver */
120
121
122 /** Test function */
123 MSG_error_t test_all(const char *platform_file,
124                             const char *application_file)
125 {
126
127   MSG_error_t res = MSG_OK;
128
129   INFO0("test_all"); 
130                                 /*  Simulation setting */
131     MSG_set_channel_number(MAX_CHANNEL);
132     MSG_paje_output("msg_test.trace");
133     MSG_create_environment(platform_file);
134  
135                              /*   Application deployment */
136     MSG_function_register("sender", sender);
137     MSG_function_register("receiver", receiver);
138    
139     MSG_launch_application(application_file);
140   
141   res = MSG_main();
142   return res;
143 } /* end_of_test_all */
144
145
146 /** Main function */
147 int main(int argc, char *argv[])
148 {
149   MSG_error_t res = MSG_OK;
150
151   MSG_global_init(&argc,argv);
152   if (argc < 3) 
153 {
154      CRITICAL1 ("Usage: %s platform_file deployment_file\n",argv[0]);
155      CRITICAL1 ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
156      exit(1);
157   }
158   res = test_all(argv[1],argv[2]);
159   MSG_clean();
160
161   if(res==MSG_OK) return 0; 
162   else return 1;
163 } /* end_of_main */