Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #65 from fabienchaix/master
[simgrid.git] / examples / msg / sendrecv / sendrecv.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 msg_error_t test_all(const char *platform_file,
30                      const char *application_file);
31
32 double task_comm_size_lat = 1;
33 double task_comm_size_bw = 10e8;
34
35 /** Emitter function  */
36 int sender(int argc, char *argv[])
37 {
38   msg_host_t host = NULL;
39   double time;
40   msg_task_t task_la = NULL;
41   msg_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(msg_host_t,1); */
48
49   XBT_INFO("host = %s", argv[1]);
50
51   host = MSG_host_by_name(argv[1]);
52
53   xbt_assert(host != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
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
92   xbt_assert(a == MSG_OK, "Unexpected behavior");
93
94   time1 = MSG_get_clock();
95   sender_time = *((double *) (task_la->data));
96   time = sender_time;
97   communication_time = time1 - time;
98   XBT_INFO("Task received : %s", task_la->name);
99   xbt_free(task_la->data);
100   MSG_task_destroy(task_la);
101   XBT_INFO("Communic. time %e", communication_time);
102   XBT_INFO("--- la %f ----", communication_time);
103
104   /* Get Bandwidth */
105   a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
106
107   xbt_assert(a == MSG_OK, "Unexpected behavior");
108
109   time1 = MSG_get_clock();
110   sender_time = *((double *) (task_bw->data));
111   time = sender_time;
112   communication_time = time1 - time;
113   XBT_INFO("Task received : %s", task_bw->name);
114   xbt_free(task_bw->data);
115   MSG_task_destroy(task_bw);
116   XBT_INFO("Communic. time %e", communication_time);
117   XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
118
119   return 0;
120 }                               /* end_of_receiver */
121
122
123 /** Test function */
124 msg_error_t test_all(const char *platform_file,
125                      const char *application_file)
126 {
127   msg_error_t res = MSG_OK;
128
129   XBT_INFO("test_all");
130
131   /*  Simulation setting */
132   MSG_create_environment(platform_file);
133
134   /*   Application deployment */
135   MSG_function_register("sender", sender);
136   MSG_function_register("receiver", receiver);
137
138   MSG_launch_application(application_file);
139
140   res = MSG_main();
141
142   return res;
143 }                               /* end_of_test_all */
144
145
146 /** Main function */
147 int main(int argc, char *argv[])
148 {
149   msg_error_t res = MSG_OK;
150
151 #ifdef _MSC_VER
152   unsigned int prev_exponent_format =
153       _set_output_format(_TWO_DIGIT_EXPONENT);
154 #endif
155
156   MSG_init(&argc, argv);
157
158   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
159        "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
160
161   res = test_all(argv[1], argv[2]);
162
163   XBT_INFO("Total simulation time: %e", MSG_get_clock());
164
165 #ifdef _MSC_VER
166   _set_output_format(prev_exponent_format);
167 #endif
168
169   return res != MSG_OK;
170 }                               /* end_of_main */