Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
17                              "Messages specific for this msg example");
18
19 int sender(int argc, char *argv[]);
20 int receiver(int argc, char *argv[]);
21
22 MSG_error_t test_all(const char *platform_file,
23                      const char *application_file);
24
25 double task_comm_size_lat = 10e0;
26 double task_comm_size_bw = 10e8;
27
28 /** Emitter function  */
29 int sender(int argc, char *argv[])
30 {
31   m_host_t host = NULL;
32   double time;
33   m_task_t task_la = NULL;
34   m_task_t task_bw = NULL;
35   char sprintf_buffer_la[64];
36   char sprintf_buffer_bw[64];
37
38   XBT_INFO("sender");
39
40   /*host = xbt_new0(m_host_t,1); */
41
42   XBT_INFO("host = %s", argv[1]);
43
44   host = MSG_get_host_by_name(argv[1]);
45
46   if (host == NULL) {
47     XBT_INFO("Unknown host %s. Stopping Now! ", argv[1]);
48     abort();
49   }
50
51   /* Latency */
52   time = MSG_get_clock();
53   sprintf(sprintf_buffer_la, "latency task");
54   task_la =
55       MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
56   task_la->data = xbt_new(double, 1);
57   *(double *) task_la->data = time;
58   XBT_INFO("task_la->data = %le", *((double *) task_la->data));
59   MSG_task_send(task_la, argv[1]);
60
61   /* Bandwidth */
62   time = MSG_get_clock();
63   sprintf(sprintf_buffer_bw, "bandwidth task");
64   task_bw =
65       MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
66   task_bw->data = xbt_new(double, 1);
67   *(double *) task_bw->data = time;
68   XBT_INFO("task_bw->data = %le", *((double *) task_bw->data));
69   MSG_task_send(task_bw, argv[1]);
70
71   return 0;
72 }                               /* end_of_client */
73
74 /** Receiver function  */
75 int receiver(int argc, char *argv[])
76 {
77   double time, time1, sender_time;
78   m_task_t task_la = NULL;
79   m_task_t task_bw = NULL;
80   int a;
81   double communication_time = 0;
82
83   XBT_INFO("receiver");
84
85   time = MSG_get_clock();
86
87   /* Get Latency */
88   a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
89   if (a == MSG_OK) {
90     time1 = MSG_get_clock();
91     sender_time = *((double *) (task_la->data));
92     time = sender_time;
93     communication_time = time1 - time;
94     XBT_INFO("Task received : %s", task_la->name);
95     xbt_free(task_la->data);
96     MSG_task_destroy(task_la);
97     XBT_INFO("Communic. time %le", communication_time);
98     XBT_INFO("--- la %f ----", communication_time);
99   } else {
100     xbt_die("Unexpected behavior");
101   }
102
103
104   /* Get Bandwidth */
105   a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
106   if (a == MSG_OK) {
107     time1 = MSG_get_clock();
108     sender_time = *((double *) (task_bw->data));
109     time = sender_time;
110     communication_time = time1 - time;
111     XBT_INFO("Task received : %s", task_bw->name);
112     xbt_free(task_bw->data);
113     MSG_task_destroy(task_bw);
114     XBT_INFO("Communic. time %le", communication_time);
115     XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
116   } else {
117     xbt_die("Unexpected behavior");
118   }
119
120
121   return 0;
122 }                               /* end_of_receiver */
123
124
125 /** Test function */
126 MSG_error_t test_all(const char *platform_file,
127                      const char *application_file)
128 {
129
130   MSG_error_t res = MSG_OK;
131
132
133
134   XBT_INFO("test_all");
135
136   /*  Simulation setting */
137   MSG_create_environment(platform_file);
138
139   /*   Application deployment */
140   MSG_function_register("sender", sender);
141   MSG_function_register("receiver", receiver);
142
143   MSG_launch_application(application_file);
144
145   res = MSG_main();
146
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 #ifdef _MSC_VER
157   unsigned int prev_exponent_format =
158       _set_output_format(_TWO_DIGIT_EXPONENT);
159 #endif
160
161   MSG_global_init(&argc, argv);
162
163
164   if (argc != 3) {
165     XBT_CRITICAL("Usage: %s platform_file deployment_file <model>\n",
166               argv[0]);
167     XBT_CRITICAL
168         ("example: %s msg_platform.xml msg_deployment.xml KCCFLN05_Vegas\n",
169          argv[0]);
170     exit(1);
171   }
172
173   /* Options for the workstation/model:
174
175      KCCFLN05              => for maxmin
176      KCCFLN05_proportional => for proportional (Vegas)
177      KCCFLN05_Vegas        => for TCP Vegas
178      KCCFLN05_Reno         => for TCP Reno
179    */
180   //MSG_config("workstation/model", argv[3]);
181
182   res = test_all(argv[1], argv[2]);
183
184   XBT_INFO("Total simulation time: %le", MSG_get_clock());
185
186   MSG_clean();
187
188 #ifdef _MSC_VER
189   _set_output_format(prev_exponent_format);
190 #endif
191
192   if (res == MSG_OK)
193     return 0;
194   else
195     return 1;
196 }                               /* end_of_main */