1 /* Copyright (c) 2010-2017. 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 /* This example shows how to block until the completion of a communication.
9 #include "simgrid/s4u.hpp"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
16 /* Main function of the Sender process */
18 long messages_count; /* - number of tasks */
19 long receivers_count; /* - number of receivers */
20 double sleep_start_time; /* - start time */
21 double sleep_test_time; /* - test time */
22 double msg_size; /* - computational cost */
23 simgrid::s4u::MailboxPtr mbox;
26 explicit sender(std::vector<std::string> args)
28 xbt_assert(args.size() == 7, "The sender function expects 6 arguments from the XML deployment file");
29 messages_count = std::stol(args[1]);
30 msg_size = std::stod(args[2]);
31 double task_comm_size = std::stod(args[3]); /* - communication cost */
32 receivers_count = std::stol(args[4]);
33 double sleep_start_time = std::stod(args[5]);
34 double sleep_test_time = std::stod(args[6]);
35 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
39 /* Start dispatching all messages to receivers, in a round robin fashion */
40 for (int i = 0; i < messages_count; i++) {
44 std::string mbox_name = std::string("receiver-") + std::to_string(i % receivers_count);
45 mbox = simgrid::s4u::Mailbox::byName(mbox_name);
46 snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
47 snprintf(taskname,79, "Task_%d", i);
49 /* Create a communication representing the ongoing communication */
50 simgrid::s4u::CommPtr comm = mbox->put_async((void*)mailbox, msg_size);
51 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
53 /* Start sending messages to let the workers know that they should stop */
54 for (int i = 0; i < receivers_count; i++) {
56 char* payload = xbt_strdup("finalize");
57 snprintf(mailbox, 79, "receiver-%d", i);
58 simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0);
59 XBT_INFO("Send to receiver-%d finalize", i);
62 XBT_INFO("Goodbye now!");
67 /* Receiver process expects 3 arguments: */
69 int id; /* - unique id */
70 double sleep_start_time; /* - start time */
71 double sleep_test_time; /* - test time */
72 simgrid::s4u::MailboxPtr mbox;
75 explicit receiver(std::vector<std::string> args)
77 xbt_assert(args.size() == 4, "The relay_runner function does not accept any parameter from the XML deployment file");
78 id = std::stoi(args[1]);
79 sleep_start_time = std::stod(args[2]);
80 sleep_test_time = std::stod(args[3]);
81 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
82 std::string mbox_name = std::string("receiver-") + std::to_string(id);
83 mbox = simgrid::s4u::Mailbox::byName(mbox_name);
89 snprintf(mailbox,79, "receiver-%d", id);
91 char* received = static_cast<char*>(mbox->get());
92 XBT_INFO("Wait to receive a task");
93 XBT_INFO("I got a '%s'.", received);
94 if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
98 /* Otherwise receiving the message was all we were supposed to do */
104 int main(int argc, char *argv[])
106 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
108 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
110 e->registerFunction<sender>("sender");
111 e->registerFunction<receiver>("receiver");
113 e->loadPlatform(argv[1]);
114 e->loadDeployment(argv[2]);