Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
just opened Pandora's doc ...
[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 "simgrid/msg.h"
8
9 /** @addtogroup MSG_examples
10  *
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).
14  */
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
17
18 double task_comm_size_lat = 1;
19 double task_comm_size_bw = 10e8;
20
21 static int sender(int argc, char *argv[])
22 {
23   msg_host_t host = NULL;
24   double time;
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];
29
30   XBT_INFO("sender");
31   XBT_INFO("host = %s", argv[1]);
32
33   host = MSG_host_by_name(argv[1]);
34
35   xbt_assert(host != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
36
37   /* Latency */
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]);
45
46   /* Bandwidth */
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]);
54
55   return 0;
56 }
57
58 static int receiver(int argc, char *argv[])
59 {
60   msg_task_t task_la = NULL;
61   msg_task_t task_bw = NULL;
62
63   XBT_INFO("receiver");
64
65   /* Get Latency */
66   int a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
67
68   xbt_assert(a == MSG_OK, "Unexpected behavior");
69
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);
79
80   /* Get Bandwidth */
81   a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
82   xbt_assert(a == MSG_OK, "Unexpected behavior");
83
84   time1 = MSG_get_clock();
85   sender_time = *((double *) (task_bw->data));
86   time = sender_time;
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);
93
94   return 0;
95 }
96
97 int main(int argc, char *argv[])
98 {
99   msg_error_t res = MSG_OK;
100
101   MSG_init(&argc, argv);
102
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]);
105
106   MSG_create_environment(argv[1]);
107
108   MSG_function_register("sender", sender);
109   MSG_function_register("receiver", receiver);
110
111   MSG_launch_application(argv[2]);
112
113   res = MSG_main();
114
115   XBT_INFO("Total simulation time: %e", MSG_get_clock());
116   return res!=MSG_OK;
117 }