Logo AND Algorithmique Numérique Distribuée

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