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/s4u.hpp"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
10 static int sender(std::vector<std::string> args)
12 xbt_assert(args.size() == 5, "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 std::vector<msg_comm_t> comms;
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);
29 double* task = new double();
30 simgrid::s4u::Mailbox::byName(mailbox)->put(task, 1);
31 comms.push_back(comm);
32 XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef);
35 /* Here we are waiting for the completion of all communications */
36 while (xbt_dynar_is_empty(comms) == 0) {
38 comms.pop_back(&comm);
39 MSG_comm_destroy(comm);
43 /* Here we are waiting for the completion of all tasks */
44 for (int i = 0; i < receivers_count; i++) {
45 msg_task_t task = NULL;
46 msg_comm_t comm = MSG_task_irecv(&task, "finalize");
47 msg_error_t res_wait = MSG_comm_wait(comm, -1);
48 xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
49 MSG_comm_destroy(comm);
50 MSG_task_destroy(task);
53 XBT_INFO("Goodbye now!");
57 static int receiver(int argc, char *argv[])
59 xbt_assert(argc==3, "This function expects 2 parameters from the XML deployment file");
60 int id = xbt_str_parse_int(argv[1], "ID should be numerical, not %s");
61 int task_amount = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
62 msg_task_t *tasks = new msg_task_t[task_amount];
63 std::vector<msg_comm_t> comms;
66 snprintf(mailbox,79, "receiver-%d", id);
67 simgrid::s4u::Actor::sleep_for(10.0);
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 comms.push_back(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 comms.pop_back(&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");
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 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
101 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
102 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
104 e->registerFunction<Sender>("sender");
105 e->registerFunction<Receiver>("receiver");
106 e->loadDeployment(argv[2]); // And then, you load the deployment file