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()) {
48 /* Here we are waiting for the completion of all tasks */
49 for (int i = 0; i < receivers_count; i++) {
50 void* received = nullptr;
51 simgrid::s4u::CommPtr comm = mbox->get_async(&received);
56 XBT_INFO("Goodbye now!");
62 explicit receiver(std::vector<std::string> args)
64 xbt_assert(args.size() == 2, "This function expects 2 parameters from the XML deployment file");
65 int id = xbt_str_parse_int(args[0].c_str(), "ID should be numerical, not %s");
66 int task_amount = xbt_str_parse_int(args[1].c_str(), "Invalid amount of tasks: %s");
68 std::vector<simgrid::s4u::CommPtr> comms;
69 simgrid::s4u::CommPtr comm;
70 simgrid::s4u::MailboxPtr mbox;
72 simgrid::s4u::this_actor::sleep_for(10.0);
73 for (int i = 0; i < task_amount; i++) {
74 XBT_INFO("Wait to receive task %d", i);
76 comm = mbox->get_async(&received);
77 comms.push_back(comm);
80 /* Here we are waiting for the receiving of all communications */
81 while (!comms.empty()) {
82 // returns the rank of the comm that just ended. Remove it.
85 // simgrid::s4u::this_actor::execute(comm);
90 /* Here we tell to sender that all tasks are done */
91 simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
92 XBT_INFO("I'm done. See you!");
95 int main(int argc, char *argv[])
97 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
99 e->loadDeployment(argv[2]);
101 e->registerFunction<sender>("sender");
102 // e->registerFunction<receiver>("receiver");