Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add an example for waitany fct.
[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, const char *application_file);
23
24 typedef enum {
25   PORT_22 = 0,
26   MAX_CHANNEL
27 } channel_t;
28
29 double task_comm_size_lat = 10e0;
30 double task_comm_size_bw = 10e8;
31
32 /** Emitter function  */
33 int sender(int argc, char *argv[])
34 {
35   m_host_t host = NULL;
36   double time;
37   m_task_t task_la = NULL;
38   m_task_t task_bw = NULL;
39   char sprintf_buffer_la[64];
40   char sprintf_buffer_bw[64];
41
42   INFO0("sender");
43
44   /*host = xbt_new0(m_host_t,1); */
45
46   INFO1("host = %s", argv[1]);
47
48   host = MSG_get_host_by_name(argv[1]);
49
50   if (host == NULL) {
51     INFO1("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 = MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
59   task_la->data = xbt_new(double, 1);
60   *(double *) task_la->data = time;
61   INFO1("task_la->data = %le", *((double *) task_la->data));
62   MSG_task_put(task_la, host, PORT_22);
63
64   /* Bandwidth */
65   time = MSG_get_clock();
66   sprintf(sprintf_buffer_bw, "bandwidth task");
67   task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
68   task_bw->data = xbt_new(double, 1);
69   *(double *) task_bw->data = time;
70   INFO1("task_bw->data = %le", *((double *) task_bw->data));
71   MSG_task_put(task_bw, host, PORT_22);
72
73   return 0;
74 }                               /* end_of_client */
75
76 /** Receiver function  */
77 int receiver(int argc, char *argv[])
78 {
79   double time, time1, sender_time;
80   m_task_t task_la = NULL;
81   m_task_t task_bw = NULL;
82   int a;
83   double communication_time = 0;
84
85   INFO0("receiver");
86
87   time = MSG_get_clock();
88
89   /* Get Latency */
90   a = MSG_task_get(&task_la, PORT_22);
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     INFO1("Task received : %s", task_la->name);
97     MSG_task_destroy(task_la);
98     INFO1("Communic. time %le", communication_time);
99     INFO1("--- la %f ----", communication_time);
100   } else {
101     xbt_assert0(0, "Unexpected behavior");
102   }
103
104
105   /* Get Bandwidth */
106   a = MSG_task_get(&task_bw, PORT_22);
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     INFO1("Task received : %s", task_bw->name);
113     MSG_task_destroy(task_bw);
114     INFO1("Communic. time %le", communication_time);
115     INFO1("--- bw %f ----", task_comm_size_bw / communication_time);
116   } else {
117     xbt_assert0(0, "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, const char *application_file)
127 {
128
129   MSG_error_t res = MSG_OK;
130
131
132
133   INFO0("test_all");
134
135   /*  Simulation setting */
136   MSG_set_channel_number(MAX_CHANNEL);
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 = _set_output_format(_TWO_DIGIT_EXPONENT);
158 #endif
159
160   MSG_global_init(&argc, argv);
161
162
163   if (argc != 3) {
164     CRITICAL1("Usage: %s platform_file deployment_file <model>\n", argv[0]);
165     CRITICAL1
166       ("example: %s msg_platform.xml msg_deployment.xml KCCFLN05_Vegas\n",
167        argv[0]);
168     exit(1);
169   }
170
171   /* Options for the workstation/model:
172
173      KCCFLN05              => for maxmin
174      KCCFLN05_proportional => for proportional (Vegas)
175      KCCFLN05_Vegas        => for TCP Vegas
176      KCCFLN05_Reno         => for TCP Reno
177    */
178   //MSG_config("workstation/model", argv[3]);
179
180   res = test_all(argv[1], argv[2]);
181
182   INFO1("Total simulation time: %le", MSG_get_clock());
183
184   MSG_clean();
185
186 #ifdef _MSC_VER
187   _set_output_format(prev_exponent_format);
188 #endif
189
190   if (res == MSG_OK)
191     return 0;
192   else
193     return 1;
194 }                               /* end_of_main */