Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar a bit
[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 = simgrid::s4u::Comm::recv_async(mymailbox, &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     std::vector<simgrid::s4u::CommPtr>::iterator ret_it =
33         simgrid::s4u::Comm::wait_any(pending_comms.begin(), pending_comms.end());
34     XBT_INFO("Counting the number of completed comms...");
35
36     int count = 0;
37     for (; ret_it != pending_comms.end(); count++, ret_it++)
38       ;
39
40     XBT_INFO("wait_any() replied that %d comms have completed", count);
41     // xbt_assert(count == 1, "wait_any() replied that %d comms have completed, which is broken!", count);
42   }
43 }
44
45 static void sender()
46 {
47   simgrid::s4u::MailboxPtr theirmailbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
48
49   void* data = (void*)"data";
50
51   for (int i = 0; i < NUM_COMMS; i++) {
52     XBT_INFO("Sending a message to the receiver");
53     simgrid::s4u::this_actor::send(theirmailbox, &data, 4);
54     XBT_INFO("Sleeping for 1000 seconds");
55     simgrid::s4u::this_actor::sleep_for(1000.0);
56   }
57 }
58
59 int main(int argc, char** argv)
60 {
61
62   simgrid::s4u::Engine* engine = new simgrid::s4u::Engine(&argc, argv);
63
64   xbt_assert(argc >= 2, "Usage: %s <xml platform file>", argv[0]);
65
66   engine->loadPlatform(argv[1]);
67   simgrid::s4u::Host* host = simgrid::s4u::Host::by_name("Tremblay");
68
69   simgrid::s4u::Actor::createActor("Receiver", host, receiver);
70   simgrid::s4u::Actor::createActor("Sender", host, sender);
71
72   simgrid::s4u::Engine::instance()->run();
73
74   return 0;
75 }