1 /* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/msg.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
10 static int sender(int argc, char* argv[])
12 xbt_assert(argc == 6, "This function expects 5 parameters from the XML deployment file");
13 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
14 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
15 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
16 long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
17 int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");
19 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
20 /* First pack the communications in the dynar */
21 for (int i = 0; i < number_of_tasks; i++) {
22 double coef = (diff_com == 0) ? 1 : (i + 1);
26 snprintf(mailbox, 79, "receiver-%ld", (i % receivers_count));
27 snprintf(taskname, 79, "Task_%d", i);
28 msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size / coef, NULL);
29 msg_comm_t comm = MSG_task_isend(task, mailbox);
30 xbt_dynar_push_as(comms, msg_comm_t, comm);
31 XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef);
34 /* Here we are waiting for the completion of all communications */
35 while (xbt_dynar_is_empty(comms) == 0) {
37 xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
38 MSG_comm_destroy(comm);
40 xbt_dynar_free(&comms);
42 /* Here we are waiting for the completion of all tasks */
43 for (int i = 0; i < receivers_count; i++) {
44 msg_task_t task = NULL;
45 msg_comm_t comm = MSG_task_irecv(&task, "finalize");
46 msg_error_t res_wait = MSG_comm_wait(comm, -1);
47 xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
48 MSG_comm_destroy(comm);
49 MSG_task_destroy(task);
52 XBT_INFO("Goodbye now!");
56 static int receiver(int argc, char* argv[])
58 xbt_assert(argc == 3, "This function expects 2 parameters from the XML deployment file");
59 int id = xbt_str_parse_int(argv[1], "ID should be numerical, not %s");
60 int task_amount = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
61 msg_task_t* tasks = xbt_new(msg_task_t, task_amount);
62 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
65 snprintf(mailbox, 79, "receiver-%d", id);
67 MSG_process_sleep(10);
68 for (int i = 0; i < task_amount; i++) {
69 XBT_INFO("Wait to receive task %d", i);
71 msg_comm_t comm = MSG_task_irecv(&tasks[i], mailbox);
72 xbt_dynar_push_as(comms, msg_comm_t, comm);
75 /* Here we are waiting for the receiving of all communications */
76 while (!xbt_dynar_is_empty(comms)) {
78 // MSG_comm_waitany returns the rank of the comm that just ended. Remove it.
79 xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
80 msg_task_t task = MSG_comm_get_task(comm);
81 MSG_comm_destroy(comm);
82 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
83 MSG_task_execute(task);
84 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
85 msg_error_t err = MSG_task_destroy(task);
86 xbt_assert(err == MSG_OK, "MSG_task_destroy failed");
88 xbt_dynar_free(&comms);
91 /* Here we tell to sender that all tasks are done */
92 MSG_task_send(MSG_task_create(NULL, 0, 0, NULL), "finalize");
93 XBT_INFO("I'm done. See you!");
97 int main(int argc, char* argv[])
99 MSG_init(&argc, argv);
100 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
101 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
104 MSG_create_environment(argv[1]);
106 MSG_function_register("sender", sender);
107 MSG_function_register("receiver", receiver);
108 MSG_launch_application(argv[2]);
110 msg_error_t res = MSG_main();
112 XBT_INFO("Simulation time %g", MSG_get_clock());
114 return res != MSG_OK;