Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / sendrecv / sendrecv_main.c
1 /* Copyright (c) 2007-2015. 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 "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/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 double task_comm_size_lat = 1;
30 double task_comm_size_bw = 10e8;
31
32 /** Emitter function  */
33 int sender(int argc, char *argv[])
34 {
35   msg_host_t host = NULL;
36   double time;
37   msg_task_t task_la = NULL;
38   msg_task_t task_bw = NULL;
39   char sprintf_buffer_la[64];
40   char sprintf_buffer_bw[64];
41
42   XBT_INFO("sender");
43
44   /*host = xbt_new0(msg_host_t,1); */
45
46   XBT_INFO("host = %s", argv[1]);
47
48   host = MSG_host_by_name(argv[1]);
49
50   if (host == NULL) {
51     XBT_INFO("Unknown host %s. Stopping Now! ", argv[1]);
52     abort();
53   }
54
55   /* Latency */
56   time = MSG_get_clock();
57   sprintf(sprintf_buffer_la, "latency task");
58   task_la =
59       MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
60   task_la->data = xbt_new(double, 1);
61   *(double *) task_la->data = time;
62   XBT_INFO("task_la->data = %e", *((double *) task_la->data));
63   MSG_task_send(task_la, argv[1]);
64
65   /* Bandwidth */
66   time = MSG_get_clock();
67   sprintf(sprintf_buffer_bw, "bandwidth task");
68   task_bw =
69       MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
70   task_bw->data = xbt_new(double, 1);
71   *(double *) task_bw->data = time;
72   XBT_INFO("task_bw->data = %e", *((double *) task_bw->data));
73   MSG_task_send(task_bw, argv[1]);
74
75   return 0;
76 }                               /* end_of_client */
77
78 /** Receiver function  */
79 int receiver(int argc, char *argv[])
80 {
81   double time, time1, sender_time;
82   msg_task_t task_la = NULL;
83   msg_task_t task_bw = NULL;
84   int a;
85   double communication_time = 0;
86
87   XBT_INFO("receiver");
88
89   /* Get Latency */
90   a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
91   if (a == MSG_OK) {
92     time1 = MSG_get_clock();
93     sender_time = *((double *) (task_la->data));
94     time = sender_time;
95     communication_time = time1 - time;
96     XBT_INFO("Task received : %s", task_la->name);
97     xbt_free(task_la->data);
98     MSG_task_destroy(task_la);
99     XBT_INFO("Communic. time %e", communication_time);
100     XBT_INFO("--- la %f ----", communication_time);
101   } else {
102     xbt_die("Unexpected behavior");
103   }
104
105   /* Get Bandwidth */
106   a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
107   if (a == MSG_OK) {
108     time1 = MSG_get_clock();
109     sender_time = *((double *) (task_bw->data));
110     time = sender_time;
111     communication_time = time1 - time;
112     XBT_INFO("Task received : %s", task_bw->name);
113     xbt_free(task_bw->data);
114     MSG_task_destroy(task_bw);
115     XBT_INFO("Communic. time %e", communication_time);
116     XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
117   } else {
118     xbt_die("Unexpected behavior");
119   }
120
121
122   return 0;
123 }                               /* end_of_receiver */
124
125 struct application {
126   const char* platform_file;
127   const char* application_file;
128 };
129
130 /** Test function */
131 static msg_error_t test_all(struct application* app)
132 {
133   msg_error_t res = MSG_OK;
134
135   XBT_INFO("test_all");
136
137   /*  Simulation setting */
138   MSG_create_environment(app->platform_file);
139
140   /* Become one of the simulated process.
141    *
142    * This must be done after the creation of the platform
143    * because we are depending attaching to a host.*/
144   MSG_process_attach("sender", NULL, MSG_host_by_name("Tremblay"), NULL);
145
146   /*   Application deployment */
147   MSG_function_register("receiver", receiver);
148
149   MSG_launch_application(app->application_file);
150
151   // Execute the sender code:
152   const char* argv[3] = { "sender", "Jupiter", NULL };
153   sender(2, (char**) argv);
154
155   MSG_process_detach();
156   return res;
157 }                               /* end_of_test_all */
158
159 static
160 void maestro(void* data)
161 {
162   // struct application* app = (struct application*) data;
163   MSG_main();
164 }
165
166 /** Main function */
167 int main(int argc, char *argv[])
168 {
169   msg_error_t res = MSG_OK;
170
171 #ifdef _MSC_VER
172   unsigned int prev_exponent_format =
173       _set_output_format(_TWO_DIGIT_EXPONENT);
174 #endif
175
176   struct application app;
177   app.platform_file = argv[1];
178   app.application_file = argv[2];
179
180   SIMIX_set_maestro(maestro, &app);
181   MSG_init(&argc, argv);
182
183   if (argc != 3) {
184     XBT_CRITICAL("Usage: %s platform_file deployment_file\n",
185               argv[0]);
186     xbt_die("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
187   }
188
189   res = test_all(&app);
190
191   XBT_INFO("Total simulation time: %e", MSG_get_clock());
192
193 #ifdef _MSC_VER
194   _set_output_format(prev_exponent_format);
195 #endif
196
197   if (res == MSG_OK)
198     return 0;
199   else
200     return 1;
201 }                               /* end_of_main */