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 think of a simpler example. The tesh files laying in
12 * the directory are instructive concerning the way to pass options to the simulators (as described in \ref options).
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
17 double task_comm_size_lat = 1;
18 double task_comm_size_bw = 10e8;
20 static int sender(int argc, char *argv[])
22 msg_host_t host = NULL;
24 msg_task_t task_la = NULL;
25 msg_task_t task_bw = NULL;
26 char sprintf_buffer_la[64];
27 char sprintf_buffer_bw[64];
30 XBT_INFO("host = %s", argv[1]);
32 host = MSG_host_by_name(argv[1]);
35 XBT_INFO("Unknown host %s. Stopping Now! ", argv[1]);
40 time = MSG_get_clock();
41 snprintf(sprintf_buffer_la,64, "latency task");
42 task_la = MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
43 task_la->data = xbt_new(double, 1);
44 *(double *) task_la->data = time;
45 XBT_INFO("task_la->data = %e", *((double *) task_la->data));
46 MSG_task_send(task_la, argv[1]);
49 time = MSG_get_clock();
50 snprintf(sprintf_buffer_bw,64, "bandwidth task");
51 task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
52 task_bw->data = xbt_new(double, 1);
53 *(double *) task_bw->data = time;
54 XBT_INFO("task_bw->data = %e", *((double *) task_bw->data));
55 MSG_task_send(task_bw, argv[1]);
60 static int receiver(int argc, char *argv[])
62 msg_task_t task_la = NULL;
63 msg_task_t task_bw = NULL;
68 int a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
69 xbt_assert(a == MSG_OK, "Unexpected behavior");
71 double time1 = MSG_get_clock();
72 double sender_time = *((double *) (task_la->data));
73 double time = sender_time;
74 double communication_time = time1 - time;
75 XBT_INFO("Task received : %s", task_la->name);
76 xbt_free(task_la->data);
77 MSG_task_destroy(task_la);
78 XBT_INFO("Communic. time %e", communication_time);
79 XBT_INFO("--- la %f ----", communication_time);
82 a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
83 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);
98 const char* platform_file;
99 const char* application_file;
103 static msg_error_t test_all(struct application* app)
105 msg_error_t res = MSG_OK;
107 MSG_create_environment(app->platform_file);
109 /* Become one of the simulated process.
111 * This must be done after the creation of the platform because we are depending attaching to a host.*/
112 MSG_process_attach("sender", NULL, MSG_host_by_name("Tremblay"), NULL);
114 MSG_function_register("receiver", receiver);
116 MSG_launch_application(app->application_file);
118 // Execute the sender code:
119 const char* argv[3] = { "sender", "Jupiter", NULL };
120 sender(2, (char**) argv);
122 MSG_process_detach();
124 } /* end_of_test_all */
126 static void maestro(void* data)
132 int main(int argc, char *argv[])
134 msg_error_t res = MSG_OK;
136 struct application app;
137 app.platform_file = argv[1];
138 app.application_file = argv[2];
140 SIMIX_set_maestro(maestro, &app);
141 MSG_init(&argc, argv);
144 XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
145 xbt_die("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
148 res = test_all(&app);
150 XBT_INFO("Total simulation time: %e", MSG_get_clock());
151 return res != MSG_OK;