1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
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 */
12 /* Create a log channel to have nice outputs. */
14 #include "xbt/asserts.h"
16 /** @addtogroup MSG_examples
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).
23 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
24 "Messages specific for this msg example");
26 int sender(int argc, char *argv[]);
27 int receiver(int argc, char *argv[]);
29 msg_error_t test_all(const char *platform_file,
30 const char *application_file);
32 double task_comm_size_lat = 1;
33 double task_comm_size_bw = 10e8;
35 /** Emitter function */
36 int sender(int argc, char *argv[])
38 msg_host_t host = NULL;
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];
47 /*host = xbt_new0(msg_host_t,1); */
49 XBT_INFO("host = %s", argv[1]);
51 host = MSG_host_by_name(argv[1]);
54 XBT_INFO("Unknown host %s. Stopping Now! ", argv[1]);
59 time = MSG_get_clock();
60 sprintf(sprintf_buffer_la, "latency task");
62 MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
63 task_la->data = xbt_new(double, 1);
64 *(double *) task_la->data = time;
65 XBT_INFO("task_la->data = %e", *((double *) task_la->data));
66 MSG_task_send(task_la, argv[1]);
69 time = MSG_get_clock();
70 sprintf(sprintf_buffer_bw, "bandwidth task");
72 MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
73 task_bw->data = xbt_new(double, 1);
74 *(double *) task_bw->data = time;
75 XBT_INFO("task_bw->data = %e", *((double *) task_bw->data));
76 MSG_task_send(task_bw, argv[1]);
81 /** Receiver function */
82 int receiver(int argc, char *argv[])
84 double time, time1, sender_time;
85 msg_task_t task_la = NULL;
86 msg_task_t task_bw = NULL;
88 double communication_time = 0;
93 a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
95 time1 = MSG_get_clock();
96 sender_time = *((double *) (task_la->data));
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 %e", communication_time);
103 XBT_INFO("--- la %f ----", communication_time);
105 xbt_die("Unexpected behavior");
109 a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
111 time1 = MSG_get_clock();
112 sender_time = *((double *) (task_bw->data));
114 communication_time = time1 - time;
115 XBT_INFO("Task received : %s", task_bw->name);
116 xbt_free(task_bw->data);
117 MSG_task_destroy(task_bw);
118 XBT_INFO("Communic. time %e", communication_time);
119 XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
121 xbt_die("Unexpected behavior");
126 } /* end_of_receiver */
130 msg_error_t test_all(const char *platform_file,
131 const char *application_file)
133 msg_error_t res = MSG_OK;
135 XBT_INFO("test_all");
137 /* Simulation setting */
138 MSG_create_environment(platform_file);
140 /* Application deployment */
141 MSG_function_register("sender", sender);
142 MSG_function_register("receiver", receiver);
144 MSG_launch_application(application_file);
149 } /* end_of_test_all */
153 int main(int argc, char *argv[])
155 msg_error_t res = MSG_OK;
158 unsigned int prev_exponent_format =
159 _set_output_format(_TWO_DIGIT_EXPONENT);
162 MSG_init(&argc, argv);
165 XBT_CRITICAL("Usage: %s platform_file deployment_file\n",
167 xbt_die("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
170 res = test_all(argv[1], argv[2]);
172 XBT_INFO("Total simulation time: %e", MSG_get_clock());
175 _set_output_format(prev_exponent_format);