Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rework the portability layer around our getline definition. Damn thing. A proper...
[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, printf */
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 {INFO0("sender");
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   nbr = argc - 1;
43  hosts = calloc(nbr, sizeof(m_host_t));
44     
45   for (i = 1; i < argc; i++) 
46     {
47  INFO2("host [%d]= %s",i-1, argv[i]);
48       hosts[i-1] = MSG_get_host_by_name(argv[i]);
49       if(hosts[i-1]==NULL) 
50         {
51           INFO1("Unknown host %s. Stopping Now! ", argv[i]);
52           abort();
53         }
54     }
55
56   for (i = 0; i < nbr; i++) 
57     { 
58 /* Latency */
59  time=MSG_get_clock(); 
60        task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size_lat, NULL); 
61  INFO1("task = %p",task); 
62        task->data=xbt_new0(double,1); 
63        *(double*)(task->data)=time; 
64  INFO1("task->data = %le", *(double*)(task->data)); 
65  INFO2("host [%d]=%s ",i, argv[i+1]); 
66        MSG_task_put(task, hosts[i],PORT_22);    
67
68       /* Bandwidth */
69       time=MSG_get_clock();
70       task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size_bw, NULL);
71       task->data=xbt_new0(double,1);
72       *(double*)(task->data)=time;
73       MSG_task_put(task, hosts[i % nbr],PORT_22);  
74     }
75
76
77
78   return 0;
79 } /* end_of_client */
80
81 /** Receiver function  */
82 int receiver(int argc, char *argv[])
83 {
84  INFO0("receiver");
85   double communication_time=0;
86   while(1) 
87     {
88       double time, time1, sender_time;
89       m_task_t task = NULL;
90       int a;
91
92       time=MSG_get_clock();
93       a=MSG_task_get(&task,PORT_22);
94       if (a == MSG_OK) 
95         {
96      
97       time1=MSG_get_clock();
98       sender_time=(*(double*)(task->data));
99      
100 /*      if (sender_time > time) */
101         time=sender_time;
102       communication_time=time1-time;
103       INFO1("Communic. time %.35f",communication_time);
104       MSG_task_destroy(task);
105       INFO1("Communic. time %le",communication_time);
106
107       INFO1("--- bw %f ----",task_comm_size_bw/communication_time);
108         }
109       else 
110         {
111           xbt_assert0(0,"Unexpected behavior");
112         }
113     } 
114  return 0;
115 }/* end_of_receiver */
116
117
118 /** Test function */
119 MSG_error_t test_all(const char *platform_file,
120                             const char *application_file)
121 {
122   INFO0("test_all");
123   MSG_error_t res = MSG_OK; 
124                                 /*  Simulation setting */
125     MSG_set_channel_number(MAX_CHANNEL);
126     MSG_paje_output("msg_test.trace");
127     MSG_create_environment(platform_file);
128  
129                              /*   Application deployment */
130     MSG_function_register("sender", sender);
131     MSG_function_register("receiver", receiver);
132    
133     MSG_launch_application(application_file);
134   
135   res = MSG_main();
136   return res;
137 } /* end_of_test_all */
138
139
140 /** Main function */
141 int main(int argc, char *argv[])
142 {
143   MSG_error_t res = MSG_OK;
144
145   MSG_global_init(&argc,argv);
146   if (argc < 3) 
147 {
148      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
149      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
150      exit(1);
151   }
152   res = test_all(argv[1],argv[2]);
153   MSG_clean();
154
155   if(res==MSG_OK) return 0; 
156   else return 1;
157 } /* end_of_main */