From 95c5924b2fb29b1ca09d2be2b35c9622b8bfe6c3 Mon Sep 17 00:00:00 2001 From: Augustin Degomme Date: Fri, 23 Aug 2019 01:44:10 +0200 Subject: [PATCH] add test proposed in #39 For now in teshsuite, might be moved to examples if someone asks --- teshsuite/s4u/CMakeLists.txt | 4 +- teshsuite/s4u/wait-any-for/wait-any-for.cpp | 49 ++++++++++++++++++++ teshsuite/s4u/wait-any-for/wait-any-for.tesh | 18 +++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 teshsuite/s4u/wait-any-for/wait-any-for.cpp create mode 100644 teshsuite/s4u/wait-any-for/wait-any-for.tesh diff --git a/teshsuite/s4u/CMakeLists.txt b/teshsuite/s4u/CMakeLists.txt index 029dafd8ab..049d6ba295 100644 --- a/teshsuite/s4u/CMakeLists.txt +++ b/teshsuite/s4u/CMakeLists.txt @@ -1,6 +1,6 @@ foreach(x actor actor-autorestart actor-migration activity-lifecycle - comm-pt2pt + comm-pt2pt wait-any-for cloud-interrupt-migration cloud-sharing concurrent_rw storage_client_server listen_async pid ) add_executable (${x} EXCLUDE_FROM_ALL ${x}/${x}.cpp) @@ -14,7 +14,7 @@ endforeach() ## Add the tests. ## Some need to be run with all factories, some need not tesh to run foreach(x actor actor-autorestart actor-migration - activity-lifecycle + activity-lifecycle wait-any-for cloud-interrupt-migration concurrent_rw) # TODO: actor-autorestart is disabled for now set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh) ADD_TESH_FACTORIES(tesh-s4u-${x} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/s4u/${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x} --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_BINARY_DIR}/teshsuite/s4u/${x} ${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x}/${x}.tesh) diff --git a/teshsuite/s4u/wait-any-for/wait-any-for.cpp b/teshsuite/s4u/wait-any-for/wait-any-for.cpp new file mode 100644 index 0000000000..6558a473a4 --- /dev/null +++ b/teshsuite/s4u/wait-any-for/wait-any-for.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +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 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; +} diff --git a/teshsuite/s4u/wait-any-for/wait-any-for.tesh b/teshsuite/s4u/wait-any-for/wait-any-for.tesh new file mode 100644 index 0000000000..a28b79002b --- /dev/null +++ b/teshsuite/s4u/wait-any-for/wait-any-for.tesh @@ -0,0 +1,18 @@ +#!/usr/bin/env tesh + +p Testing the wait_any_for feature of S4U + +! output sort 19 +$ ${bindir:=.}/wait-any-for ${platfdir:=.}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:worker@Tremblay) Sending and receiving 42 and 51 asynchronously +> [ 0.000000] (1:worker@Tremblay) All comms have started +> [ 0.500000] (1:worker@Tremblay) wait_any_for: Timeout reached +> [ 1.000000] (1:worker@Tremblay) wait_any_for: Timeout reached +> [ 1.035263] (1:worker@Tremblay) wait_any_for: A comm finished (index=0, #comms=4) +> [ 1.035263] (1:worker@Tremblay) wait_any_for: A comm finished (index=1, #comms=3) +> [ 1.535263] (1:worker@Tremblay) wait_any_for: Timeout reached +> [ 2.035263] (1:worker@Tremblay) wait_any_for: Timeout reached +> [ 2.070331] (1:worker@Tremblay) wait_any_for: A comm finished (index=0, #comms=2) +> [ 2.070331] (1:worker@Tremblay) wait_any_for: A comm finished (index=0, #comms=1) +> [ 2.070331] (1:worker@Tremblay) All comms have finished +> [ 2.070331] (1:worker@Tremblay) Got 42 and 51 -- 2.20.1