1 /* Copyright (c) 2010-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. */
8 #include "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 /** @addtogroup MSG_examples
19 * @section MSG_ex_icomms Asynchronous communications
21 * There is several examples of asynchronous communications coming in
22 * the archive. In addition to the fully documented example \ref
23 * MSG_ex_asynchronous_communications, there is several other
24 * examples in the archive:
26 * - <b>msg/icomms/peer.c</b>: basic example of async functions (@ref MSG_task_isend, @ref MSG_task_irecv, @ref MSG_comm_wait)
28 int sender(int argc, char *argv[]);
29 int receiver(int argc, char *argv[]);
31 msg_error_t test_all(const char *platform_file,
32 const char *application_file);
34 /** Sender function */
35 int sender(int argc, char *argv[])
37 long number_of_tasks = atol(argv[1]);
38 double task_comp_size = atof(argv[2]);
39 double task_comm_size = atof(argv[3]);
40 long receivers_count = atol(argv[4]);
41 double sleep_start_time = atof(argv[5]);
42 double sleep_test_time = atof(argv[6]);
44 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time,
47 msg_comm_t comm = NULL;
49 msg_task_t task = NULL;
50 MSG_process_sleep(sleep_start_time);
51 for (i = 0; i < number_of_tasks; i++) {
53 char sprintf_buffer[256];
55 sprintf(mailbox, "receiver-%ld", i % receivers_count);
56 sprintf(sprintf_buffer, "Task_%d", i);
59 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
61 comm = MSG_task_isend(task, mailbox);
62 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
64 if (sleep_test_time == 0) {
65 MSG_comm_wait(comm, -1);
67 while (MSG_comm_test(comm) == 0) {
68 MSG_process_sleep(sleep_test_time);
71 MSG_comm_destroy(comm);
75 for (i = 0; i < receivers_count; i++) {
77 sprintf(mailbox, "receiver-%ld", i % receivers_count);
78 task = MSG_task_create("finalize", 0, 0, 0);
79 comm = MSG_task_isend(task, mailbox);
80 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
81 if (sleep_test_time == 0) {
82 MSG_comm_wait(comm, -1);
84 while (MSG_comm_test(comm) == 0) {
85 MSG_process_sleep(sleep_test_time);
88 MSG_comm_destroy(comm);
92 XBT_INFO("Goodbye now!");
96 /** Receiver function */
97 int receiver(int argc, char *argv[])
99 msg_task_t task = NULL;
100 XBT_ATTRIB_UNUSED msg_error_t res;
103 msg_comm_t res_irecv;
104 double sleep_start_time = atof(argv[2]);
105 double sleep_test_time = atof(argv[3]);
106 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time,
109 XBT_ATTRIB_UNUSED int read;
110 read = sscanf(argv[1], "%d", &id);
112 "Invalid argument %s\n", argv[1]);
114 MSG_process_sleep(sleep_start_time);
116 sprintf(mailbox, "receiver-%d", id);
118 res_irecv = MSG_task_irecv(&(task), mailbox);
119 XBT_INFO("Wait to receive a task");
121 if (sleep_test_time == 0) {
122 res = MSG_comm_wait(res_irecv, -1);
123 xbt_assert(res == MSG_OK, "MSG_task_get failed");
125 while (MSG_comm_test(res_irecv) == 0) {
126 MSG_process_sleep(sleep_test_time);
129 MSG_comm_destroy(res_irecv);
131 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
132 if (!strcmp(MSG_task_get_name(task), "finalize")) {
133 MSG_task_destroy(task);
137 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
138 MSG_task_execute(task);
139 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
140 MSG_task_destroy(task);
143 XBT_INFO("I'm done. See you!");
145 } /* end_of_receiver */
148 msg_error_t test_all(const char *platform_file,
149 const char *application_file)
151 msg_error_t res = MSG_OK;
153 { /* Simulation setting */
154 MSG_create_environment(platform_file);
156 { /* Application deployment */
157 MSG_function_register("sender", sender);
158 MSG_function_register("receiver", receiver);
159 MSG_launch_application(application_file);
163 XBT_INFO("Simulation time %g", MSG_get_clock());
165 } /* end_of_test_all */
169 int main(int argc, char *argv[])
171 msg_error_t res = MSG_OK;
173 MSG_init(&argc, argv);
174 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
175 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
178 res = test_all(argv[1], argv[2]);
180 return res != MSG_OK;