Logo AND Algorithmique Numérique Distribuée

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