Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rework the OO design of S4U comms
[simgrid.git] / teshsuite / s4u / comm-waitany / comm-waitany.cpp
1 /* Copyright (c) 2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <iostream>
7 #include <simgrid/s4u.hpp>
8 #include <stdlib.h>
9 #include <vector>
10
11 #define NUM_COMMS 1
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(mwe, "Minimum Working Example");
14
15 static void receiver()
16 {
17   simgrid::s4u::MailboxPtr mymailbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
18
19   std::vector<simgrid::s4u::CommPtr> pending_comms;
20
21   XBT_INFO("Placing %d asynchronous recv requests", NUM_COMMS);
22   void* data;
23   for (int i = 0; i < NUM_COMMS; i++) {
24     simgrid::s4u::CommPtr comm = mymailbox->recv_async(&data);
25     pending_comms.push_back(comm);
26   }
27
28   for (int i = 0; i < NUM_COMMS; i++) {
29     XBT_INFO("Sleeping for 3 seconds (for the %dth time)...", i + 1);
30     simgrid::s4u::this_actor::sleep_for(3.0);
31     XBT_INFO("Calling wait_any() for %zu pending comms", pending_comms.size());
32     int changed_pos = simgrid::s4u::Comm::wait_any(&pending_comms);
33     XBT_INFO("Counting the number of completed comms...");
34
35     pending_comms.erase(pending_comms.begin() + changed_pos);
36   }
37 }
38
39 static void sender()
40 {
41   simgrid::s4u::MailboxPtr theirmailbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
42
43   void* data = (void*)"data";
44
45   for (int i = 0; i < NUM_COMMS; i++) {
46     XBT_INFO("Sending a message to the receiver");
47     theirmailbox->send(&data, 4);
48     XBT_INFO("Sleeping for 1000 seconds");
49     simgrid::s4u::this_actor::sleep_for(1000.0);
50   }
51 }
52
53 int main(int argc, char** argv)
54 {
55
56   simgrid::s4u::Engine* engine = new simgrid::s4u::Engine(&argc, argv);
57
58   xbt_assert(argc >= 2, "Usage: %s <xml platform file>", argv[0]);
59
60   engine->loadPlatform(argv[1]);
61   simgrid::s4u::Host** hosts = sg_host_list();
62   simgrid::s4u::Actor::createActor("Receiver", hosts[0], receiver);
63   simgrid::s4u::Actor::createActor("Sender", hosts[1], sender);
64   xbt_free(hosts);
65
66   engine->run();
67
68   delete engine;
69   return 0;
70 }