1 /* Copyright (c) 2010-2019. 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 #include <stdio.h> /* snprintf */
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
12 static int sender(int argc, char* argv[])
14 xbt_assert(argc == 6, "This function expects 5 parameters from the XML deployment file");
15 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
16 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
17 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
18 long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
19 int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");
21 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
22 /* First pack the communications in the dynar */
23 for (int i = 0; i < number_of_tasks; i++) {
24 double coef = (diff_com == 0) ? 1 : (i + 1);
28 snprintf(mailbox, 79, "receiver-%ld", (i % receivers_count));
29 snprintf(taskname, 79, "Task_%d", i);
30 msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size / coef, NULL);
31 msg_comm_t comm = MSG_task_isend(task, mailbox);
32 xbt_dynar_push_as(comms, msg_comm_t, comm);
33 XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef);
36 /* Here we are waiting for the completion of all communications */
37 while (xbt_dynar_is_empty(comms) == 0) {
39 xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
40 MSG_comm_destroy(comm);
42 xbt_dynar_free(&comms);
44 /* Here we are waiting for the completion of all tasks */
45 for (int i = 0; i < receivers_count; i++) {
46 msg_task_t task = NULL;
47 msg_comm_t comm = MSG_task_irecv(&task, "finalize");
48 msg_error_t res_wait = MSG_comm_wait(comm, -1);
49 xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
50 MSG_comm_destroy(comm);
51 MSG_task_destroy(task);
54 XBT_INFO("Goodbye now!");
58 static int receiver(int argc, char* argv[])
60 xbt_assert(argc == 3, "This function expects 2 parameters from the XML deployment file");
61 int id = xbt_str_parse_int(argv[1], "ID should be numerical, not %s");
62 int task_amount = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
63 msg_task_t* tasks = xbt_new(msg_task_t, task_amount);
64 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
67 snprintf(mailbox, 79, "receiver-%d", id);
69 MSG_process_sleep(10);
70 for (int i = 0; i < task_amount; i++) {
71 XBT_INFO("Wait to receive task %d", i);
73 msg_comm_t comm = MSG_task_irecv(&tasks[i], mailbox);
74 xbt_dynar_push_as(comms, msg_comm_t, comm);
77 /* Here we are waiting for the receiving of all communications */
78 while (!xbt_dynar_is_empty(comms)) {
80 // MSG_comm_waitany returns the rank of the comm that just ended. Remove it.
81 xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
82 msg_task_t task = MSG_comm_get_task(comm);
83 MSG_comm_destroy(comm);
84 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
85 MSG_task_execute(task);
86 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
87 msg_error_t err = MSG_task_destroy(task);
88 xbt_assert(err == MSG_OK, "MSG_task_destroy failed");
90 xbt_dynar_free(&comms);
93 /* Here we tell to sender that all tasks are done */
94 MSG_task_send(MSG_task_create(NULL, 0, 0, NULL), "finalize");
95 XBT_INFO("I'm done. See you!");
99 int main(int argc, char* argv[])
101 MSG_init(&argc, argv);
102 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
103 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
106 MSG_create_environment(argv[1]);
108 MSG_function_register("sender", sender);
109 MSG_function_register("receiver", receiver);
110 MSG_launch_application(argv[2]);
112 msg_error_t res = MSG_main();
114 XBT_INFO("Simulation time %g", MSG_get_clock());
116 return res != MSG_OK;