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 double task_comm_size; /* - communication cost */
24 simgrid::s4u::MailboxPtr mbox;
27 explicit sender(std::vector<std::string> args)
29 xbt_assert(args.size() == 7, "The sender function expects 6 arguments from the XML deployment file");
30 messages_count = std::stol(args[1]);
31 msg_size = std::stod(args[2]);
32 task_comm_size = std::stod(args[3]);
33 receivers_count = std::stol(args[4]);
34 double sleep_start_time = std::stod(args[5]);
35 double sleep_test_time = std::stod(args[6]);
36 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
40 /* Start dispatching all messages to receivers, in a round robin fashion */
41 for (int i = 0; i < messages_count; i++) {
45 std::string mbox_name = std::string("receiver-") + std::to_string(i % receivers_count);
46 mbox = simgrid::s4u::Mailbox::byName(mbox_name);
47 snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
48 snprintf(taskname,79, "Task_%d", i);
50 /* Create a communication representing the ongoing communication */
51 simgrid::s4u::CommPtr comm = mbox->put_async((void*)mailbox, msg_size);
52 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
53 comm->wait(task_comm_size);
55 /* Start sending messages to let the workers know that they should stop */
56 for (int i = 0; i < receivers_count; i++) {
58 char* payload = xbt_strdup("finalize");
59 snprintf(mailbox, 79, "receiver-%d", i);
60 simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0);
61 comm->wait(task_comm_size);
62 XBT_INFO("Send to receiver-%d finalize", i);
65 XBT_INFO("Goodbye now!");
70 /* Receiver process expects 3 arguments: */
72 int id; /* - unique id */
73 double sleep_start_time; /* - start time */
74 double sleep_test_time; /* - test time */
75 simgrid::s4u::MailboxPtr mbox;
78 explicit receiver(std::vector<std::string> args)
80 xbt_assert(args.size() == 4, "The relay_runner function does not accept any parameter from the XML deployment file");
81 id = std::stoi(args[1]);
82 sleep_start_time = std::stod(args[2]);
83 sleep_test_time = std::stod(args[3]);
84 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
85 std::string mbox_name = std::string("receiver-") + std::to_string(id);
86 mbox = simgrid::s4u::Mailbox::byName(mbox_name);
92 snprintf(mailbox,79, "receiver-%d", id);
94 char* received = static_cast<char*>(mbox->get());
95 XBT_INFO("Wait to receive a task");
96 XBT_INFO("I got a '%s'.", received);
97 if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
105 int main(int argc, char *argv[])
107 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
109 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
111 e->registerFunction<sender>("sender");
112 e->registerFunction<receiver>("receiver");
114 e->loadPlatform(argv[1]);
115 e->loadDeployment(argv[2]);