Missing s4u::Comm::wait_any_for example
While wrapping s4u::Comm::wait_any_for
in RSG, I could not find any s4u example that uses it.
I wrote this to test the function's behavior.
#include <cstdlib>
#include <iostream>
#include <string>
#include <simgrid/s4u.hpp>
XBT_LOG_NEW_DEFAULT_CATEGORY(meh, "meh");
static void worker()
{
auto mbox = simgrid::s4u::Mailbox::by_name("meh");
int input_data[2] = {42, 51};
XBT_INFO("Sending and receiving %d and %d asynchronously", input_data[0], input_data[1]);
auto put1 = mbox->put_async(input_data, 1000*1000*500);
auto put2 = mbox->put_async(input_data + 1, 1000*1000*1000);
int * out1;
auto get1 = mbox->get_async((void**)&out1);
int * out2;
auto get2 = mbox->get_async((void**)&out2);
XBT_INFO("All comms have started");
std::vector<simgrid::s4u::CommPtr> comms = {put1, put2, get1, get2};
while (!comms.empty()) {
int index = simgrid::s4u::Comm::wait_any_for(&comms, 0.5);
if (index < 0)
XBT_INFO("wait_any_for: Timeout reached");
else {
XBT_INFO("wait_any_for: A comm finished (index=%d, #comms=%zu)", index, comms.size());
comms.erase(comms.begin() + index);
}
}
XBT_INFO("All comms have finished");
XBT_INFO("Got %d and %d", *out1, *out2);
}
int main(int argc, char* argv[])
{
simgrid::s4u::Engine e(&argc, argv);
e.load_platform(argv[1]);
simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Tremblay"), worker);
e.run();
return 0;
}
Output
[Tremblay:worker:(1) 0.000000] [meh/INFO] Sending and receiving 42 and 51 asynchronously
[Tremblay:worker:(1) 0.000000] [meh/INFO] All comms have started
[Tremblay:worker:(1) 0.500000] [meh/INFO] wait_any_for: Timeout reached
[Tremblay:worker:(1) 1.000000] [meh/INFO] wait_any_for: Timeout reached
[Tremblay:worker:(1) 1.035263] [meh/INFO] wait_any_for: A comm finished (index=0, #comms=4)
[Tremblay:worker:(1) 1.035263] [meh/INFO] wait_any_for: A comm finished (index=1, #comms=3)
[Tremblay:worker:(1) 1.535263] [meh/INFO] wait_any_for: Timeout reached
[Tremblay:worker:(1) 2.035263] [meh/INFO] wait_any_for: Timeout reached
[Tremblay:worker:(1) 2.070331] [meh/INFO] wait_any_for: A comm finished (index=0, #comms=2)
[Tremblay:worker:(1) 2.070331] [meh/INFO] wait_any_for: A comm finished (index=0, #comms=1)
[Tremblay:worker:(1) 2.070331] [meh/INFO] All comms have finished
[Tremblay:worker:(1) 2.070331] [meh/INFO] Got 42 and 51```