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"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
14 long number_of_tasks = 0; /* - Number of tasks */
15 long receivers_count = 0; /* - Number of workers */
18 explicit sender(std::vector<std::string> args)
20 xbt_assert(args.size()== 4, "This function expects 5 parameters from the XML deployment file");
21 number_of_tasks = std::stol(args[0]);
22 double task_comp_size = std::stod(args[1]);
23 double task_comm_size = std::stod(args[2]);
24 receivers_count = std::stol(args[3]);
29 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
30 simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[number_of_tasks + receivers_count] ;
32 for (int i = 0; i < number_of_tasks; i++) {
35 snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
36 snprintf(taskname,79, "Task_%d", i);
37 comms[i] = mbox->put_async((void*)taskname, 42);
38 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
40 for (int i = 0; i < receivers_count; i++) {
42 snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
43 comms[i + number_of_tasks] = mbox->put_async((void*)"finalize", 42);
44 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
47 /* Here we are waiting for the completion of all communications */
48 for (int i = 0; i < number_of_tasks + receivers_count; i++)
52 XBT_INFO("Goodbye now!");
58 explicit receiver(std::vector<std::string> args)
60 xbt_assert(args.size() == 1,"This function expects 1 parameter from the XML deployment file");
61 int id = xbt_str_parse_int(args[0].c_str(), "Any process of this example must have a numerical name, not %s");
63 snprintf(mailbox,79, "receiver-%d", id);
65 simgrid::s4u::this_actor::sleep_for(10.0);
67 XBT_INFO("I'm done. See you!");
71 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
73 XBT_INFO("Wait to receive a task");
74 void *received = NULL;
75 simgrid::s4u::CommPtr comm = mbox->get_async(&received);
77 std::string* receivedStr = static_cast<std::string*>(received);
78 if (receivedStr->compare("finalize") == 0) {
82 double *comp_size = static_cast<double*>(received);
83 /* - Otherwise, process the task */
84 simgrid::s4u::this_actor::execute(*comp_size);
86 XBT_INFO("I'm done. See you!");
90 int main(int argc, char *argv[])
92 simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
94 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
95 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
97 e->registerFunction<sender>("sender");
98 e->registerFunction<receiver>("receiver");
100 e->loadDeployment(argv[2]);