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"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
11 static int sender(int argc, char *argv[])
13 xbt_assert(argc==6, "This function expects 5 parameters from the XML deployment file");
14 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
15 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
16 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
17 long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
18 int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");
20 std::vector<simgrid::s4u::CommPtr> comms;
21 simgrid::s4u::MailboxPtr mbox;
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);
31 simgrid::s4u::CommPtr comm = mbox->put_async((void*)taskname, 42.0);
32 comms.push_back(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 (!comms.empty()) {
38 simgrid::s4u::CommPtr comm;
45 /* Here we are waiting for the completion of all tasks */
46 for (int i = 0; i < receivers_count; i++) {
47 std::sring received = nullptr;
48 simgrid::s4u::CommPtr comm = mbox->get_async(&received);
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");
63 std::vector<simgrid::s4u::CommPtr> * comms;
64 simgrid::s4u::CommPtr comm;
65 simgrid::s4u::MailboxPtr mbox;
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 comm = mbox->get_async(&received[i]);
72 comms.push_back(comm);
75 /* Here we are waiting for the receiving of all communications */
76 while (!comms.empty()) {
77 simgrid::s4u::CommPtr comm;
78 // MSG_comm_waitany returns the rank of the comm that just ended. Remove it.
79 comms.pop_back(&comm);
80 simgrid::s4u::this_actor::execute(comm);
85 /* Here we tell to sender that all tasks are done */
86 simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
87 XBT_INFO("I'm done. See you!");
91 int main(int argc, char *argv[])
93 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
95 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
96 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
98 e->registerFunction<Sender>("sender");
99 e->registerFunction<Receiver>("receiver");
100 e->loadDeployment(argv[2]); // And then, you load the deployment file