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. */
7 #include "simgrid/msg.h"
9 /** @addtogroup MSG_examples
11 * - <b>sendrecv/sendrecv.c: Ping-pong example</b>. It's hard to
12 * think of a simpler example. The tesh files laying in the
13 * directory are instructive concerning the way to pass options to the simulators (as described in \ref options).
16 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
18 double task_comm_size_lat = 1;
19 double task_comm_size_bw = 10e8;
21 static int sender(int argc, char *argv[])
23 msg_host_t host = NULL;
25 msg_task_t task_la = NULL;
26 msg_task_t task_bw = NULL;
27 char sprintf_buffer_la[64];
28 char sprintf_buffer_bw[64];
31 XBT_INFO("host = %s", argv[1]);
33 host = MSG_host_by_name(argv[1]);
35 xbt_assert(host != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
38 time = MSG_get_clock();
39 sprintf(sprintf_buffer_la, "latency task");
40 task_la = MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
41 task_la->data = xbt_new(double, 1);
42 *(double *) task_la->data = time;
43 XBT_INFO("task_la->data = %e", *((double *) task_la->data));
44 MSG_task_send(task_la, argv[1]);
47 time = MSG_get_clock();
48 sprintf(sprintf_buffer_bw, "bandwidth task");
49 task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
50 task_bw->data = xbt_new(double, 1);
51 *(double *) task_bw->data = time;
52 XBT_INFO("task_bw->data = %e", *((double *) task_bw->data));
53 MSG_task_send(task_bw, argv[1]);
58 static int receiver(int argc, char *argv[])
60 msg_task_t task_la = NULL;
61 msg_task_t task_bw = NULL;
66 int a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
68 xbt_assert(a == MSG_OK, "Unexpected behavior");
70 double time1 = MSG_get_clock();
71 double sender_time = *((double *) (task_la->data));
72 double time = sender_time;
73 double communication_time = time1 - time;
74 XBT_INFO("Task received : %s", task_la->name);
75 xbt_free(task_la->data);
76 MSG_task_destroy(task_la);
77 XBT_INFO("Communic. time %e", communication_time);
78 XBT_INFO("--- la %f ----", communication_time);
81 a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
82 xbt_assert(a == MSG_OK, "Unexpected behavior");
84 time1 = MSG_get_clock();
85 sender_time = *((double *) (task_bw->data));
87 communication_time = time1 - time;
88 XBT_INFO("Task received : %s", task_bw->name);
89 xbt_free(task_bw->data);
90 MSG_task_destroy(task_bw);
91 XBT_INFO("Communic. time %e", communication_time);
92 XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
97 int main(int argc, char *argv[])
99 msg_error_t res = MSG_OK;
101 MSG_init(&argc, argv);
103 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
104 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
106 MSG_create_environment(argv[1]);
108 MSG_function_register("sender", sender);
109 MSG_function_register("receiver", receiver);
111 MSG_launch_application(argv[2]);
115 XBT_INFO("Total simulation time: %e", MSG_get_clock());