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"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
16 explicit sender(std::vector<std::string> args)
18 xbt_assert(args.size()== 5, "This function expects 5 parameters from the XML deployment file");
19 long number_of_tasks = xbt_str_parse_int(args[0].c_str(), "Invalid amount of tasks: %s");
20 double task_comp_size = xbt_str_parse_double(args[1].c_str(), "Invalid computational size: %s");
21 double task_comm_size = xbt_str_parse_double(args[2].c_str(), "Invalid communication size: %s");
22 long receivers_count = xbt_str_parse_int(args[3].c_str(), "Invalid amount of receivers: %s");
23 int diff_com = xbt_str_parse_int(args[4].c_str(), "Invalid value for diff_comm: %s");
25 std::vector<simgrid::s4u::CommPtr> comms;
26 simgrid::s4u::CommPtr comm;
27 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
28 /* First pack the communications in the dynar */
29 for (int i = 0; i < number_of_tasks; i++) {
30 double coef = (diff_com == 0) ? 1 : (i + 1);
34 snprintf(mailbox,79, "receiver-%ld", (i % receivers_count));
35 snprintf(taskname,79, "Task_%d", i);
37 comm = mbox->put_async((void*)taskname, 42.0);
38 comms.push_back(comm);
39 XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef);
42 /* Here we are waiting for the completion of all communications */
43 while (!comms.empty()) {
51 /* Here we are waiting for the completion of all tasks */
52 for (int i = 0; i < receivers_count; i++) {
53 void* received = nullptr;
54 simgrid::s4u::CommPtr comm = mbox->get_async(&received);
62 XBT_INFO("Goodbye now!");
68 explicit receiver(std::vector<std::string> args)
70 xbt_assert(args.size() == 2, "This function expects 2 parameters from the XML deployment file");
71 int id = xbt_str_parse_int(args[0].c_str(), "ID should be numerical, not %s");
72 int task_amount = xbt_str_parse_int(args[1].c_str(), "Invalid amount of tasks: %s");
74 std::vector<simgrid::s4u::CommPtr> comms;
75 simgrid::s4u::CommPtr comm;
76 simgrid::s4u::MailboxPtr mbox;
78 simgrid::s4u::this_actor::sleep_for(10.0);
79 for (int i = 0; i < task_amount; i++) {
80 XBT_INFO("Wait to receive task %d", i);
82 comm = mbox->get_async(&received);
83 comms.push_back(comm);
86 /* Here we are waiting for the receiving of all communications */
87 while (!comms.empty()) {
88 // returns the rank of the comm that just ended. Remove it.
98 /* Here we tell to sender that all tasks are done */
99 simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
100 XBT_INFO("I'm done. See you!");
104 int main(int argc, char *argv[])
106 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
108 e->registerFunction<sender>("sender");
109 e->registerFunction<receiver>("receiver");
111 e->loadDeployment(argv[2]);