Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
890f46046100472423685bb28c45421ac720a824
[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   simgrid::s4u::MailboxPtr theirmailbox = simgrid::s4u::Mailbox::byName("sender_mailbox");
19
20   std::vector<simgrid::s4u::CommPtr> pending_comms;
21
22   XBT_INFO("Placing %d asynchronous recv requests", NUM_COMMS);
23   void* data;
24   for (int i = 0; i < NUM_COMMS; i++) {
25     simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::recv_async(mymailbox, &data);
26     pending_comms.push_back(comm);
27   }
28
29   for (int i = 0; i < NUM_COMMS; i++) {
30     XBT_INFO("Sleeping for 3 seconds (for the %dth time)...", i + 1);
31     simgrid::s4u::this_actor::sleep_for(3.0);
32     XBT_INFO("Calling wait_any() for %zu pending comms", pending_comms.size());
33     std::vector<simgrid::s4u::CommPtr>::iterator ret_it =
34         simgrid::s4u::Comm::wait_any(pending_comms.begin(), pending_comms.end());
35     XBT_INFO("Counting the number of completed comms...");
36
37     int count = 0;
38     for (; ret_it != pending_comms.end(); count++, ret_it++)
39       ;
40
41     XBT_INFO("wait_any() replied that %d comms have completed", count);
42     // xbt_assert(count == 1, "wait_any() replied that %d comms have completed, which is broken!", count);
43   }
44 }
45
46 static void sender()
47 {
48   simgrid::s4u::MailboxPtr mymailbox    = simgrid::s4u::Mailbox::byName("sender_mailbox");
49   simgrid::s4u::MailboxPtr theirmailbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
50
51   void* data = (void*)"data";
52
53   for (int i = 0; i < NUM_COMMS; i++) {
54     XBT_INFO("Sending a message to the receiver");
55     simgrid::s4u::this_actor::send(theirmailbox, &data, 4);
56     XBT_INFO("Sleeping for 1000 seconds");
57     simgrid::s4u::this_actor::sleep_for(1000.0);
58   }
59 }
60
61 int main(int argc, char** argv)
62 {
63
64   simgrid::s4u::Engine* engine = new simgrid::s4u::Engine(&argc, argv);
65
66   if (argc < 2) {
67     std::cerr << "Usage: " << argv[0] << " <xml platform file>" << std::endl;
68     exit(1);
69   }
70
71   engine->loadPlatform(argv[1]);
72   simgrid::s4u::Host* host = simgrid::s4u::Host::by_name("Tremblay");
73
74   simgrid::s4u::Actor::createActor("Receiver", host, receiver);
75   simgrid::s4u::Actor::createActor("Sender", host, sender);
76
77   simgrid::s4u::Engine::instance()->run();
78
79   return 0;
80 }