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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * @section MSG_ex_icomms Asynchronous communications
15 * There is several examples of asynchronous communications coming in the archive. In addition to the fully documented
16 * example \ref MSG_ex_asynchronous_communications, there is several other examples in the archive:
18 * - <b>msg/icomms/peer.c</b>: basic example of async functions (@ref MSG_task_isend, @ref MSG_task_irecv, @ref MSG_comm_wait)
21 static int sender(int argc, char *argv[])
23 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
24 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
25 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
26 long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
27 double sleep_start_time = xbt_str_parse_double(argv[5], "Invalid sleep start time: %s");
28 double sleep_test_time = xbt_str_parse_double(argv[6], "Invalid test time: %s");
30 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
33 msg_task_t task = NULL;
34 msg_comm_t comm = NULL;
35 MSG_process_sleep(sleep_start_time);
36 for (i = 0; i < number_of_tasks; i++) {
38 char sprintf_buffer[256];
40 sprintf(mailbox, "receiver-%ld", i % receivers_count);
41 sprintf(sprintf_buffer, "Task_%d", i);
43 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
44 comm = MSG_task_isend(task, mailbox);
45 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
47 if (sleep_test_time == 0) {
48 MSG_comm_wait(comm, -1);
50 while (MSG_comm_test(comm) == 0) {
51 MSG_process_sleep(sleep_test_time);
54 MSG_comm_destroy(comm);
57 for (i = 0; i < receivers_count; i++) {
59 sprintf(mailbox, "receiver-%ld", i % receivers_count);
60 task = MSG_task_create("finalize", 0, 0, 0);
61 comm = MSG_task_isend(task, mailbox);
62 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
63 if (sleep_test_time == 0) {
64 MSG_comm_wait(comm, -1);
66 while (MSG_comm_test(comm) == 0) {
67 MSG_process_sleep(sleep_test_time);
70 MSG_comm_destroy(comm);
73 XBT_INFO("Goodbye now!");
77 static int receiver(int argc, char *argv[])
79 msg_task_t task = NULL;
80 XBT_ATTRIB_UNUSED msg_error_t res;
84 double sleep_start_time = xbt_str_parse_double(argv[2], "Invalid sleep start parameter: %s");
85 double sleep_test_time = xbt_str_parse_double(argv[3], "Invalid sleep test parameter: %s");
86 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
88 XBT_ATTRIB_UNUSED int read;
89 read = sscanf(argv[1], "%d", &id);
90 xbt_assert(read, "Invalid argument %s\n", argv[1]);
92 MSG_process_sleep(sleep_start_time);
94 sprintf(mailbox, "receiver-%d", id);
96 res_irecv = MSG_task_irecv(&(task), mailbox);
97 XBT_INFO("Wait to receive a task");
99 if (sleep_test_time == 0) {
100 res = MSG_comm_wait(res_irecv, -1);
101 xbt_assert(res == MSG_OK, "MSG_task_get failed");
103 while (MSG_comm_test(res_irecv) == 0) {
104 MSG_process_sleep(sleep_test_time);
107 MSG_comm_destroy(res_irecv);
109 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
110 if (!strcmp(MSG_task_get_name(task), "finalize")) {
111 MSG_task_destroy(task);
115 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
116 MSG_task_execute(task);
117 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
118 MSG_task_destroy(task);
121 XBT_INFO("I'm done. See you!");
125 int main(int argc, char *argv[])
127 msg_error_t res = MSG_OK;
129 MSG_init(&argc, argv);
130 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
131 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
133 MSG_create_environment(argv[1]);
135 MSG_function_register("sender", sender);
136 MSG_function_register("receiver", receiver);
137 MSG_launch_application(argv[2]);
141 XBT_INFO("Simulation time %g", MSG_get_clock());
143 return res != MSG_OK;