From: Martin Quinson Date: Mon, 2 Oct 2017 19:49:53 +0000 (+0200) Subject: Merge pull request #224 from Takishipp/async-wait X-Git-Tag: v3_17~48 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/3ce1a72143c8978e6e5764a3b250d930f3cd4a9b?hp=934e15f8ffc473c09214dd3dcbd4a926d2fa0846 Merge pull request #224 from Takishipp/async-wait Add s4u-async-wait to the examples of s4u --- diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 19857060f3..0ee6b1e500 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -1,7 +1,7 @@ foreach (example actions-comm actions-storage actor-create actor-daemon actor-kill actor-migration actor-suspend app-masterworker app-pingpong app-token-ring - async-waitany async-waitall + async-wait async-waitany async-waitall plugin-hostload io mutex) add_executable (s4u-${example} ${example}/s4u-${example}.cpp) target_link_libraries(s4u-${example} simgrid) @@ -32,6 +32,7 @@ endforeach() set(examples_src ${examples_src} PARENT_SCOPE) set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/s4u-app-bittorrent.tesh ${CMAKE_CURRENT_SOURCE_DIR}/dht-chord/s4u-dht-chord.tesh + ${CMAKE_CURRENT_SOURCE_DIR}/async-wait/s4u-async-wait.tesh ${CMAKE_CURRENT_SOURCE_DIR}/async-waitany/s4u-async-waitany.tesh ${CMAKE_CURRENT_SOURCE_DIR}/async-waitall/s4u-async-waitall.tesh PARENT_SCOPE) set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-actions-comm-split_d.xml @@ -42,6 +43,7 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-a ${CMAKE_CURRENT_SOURCE_DIR}/app-masterworker/s4u-app-masterworker_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/async-waitany/s4u-async-waitany_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/async-waitall/s4u-async-waitall_d.xml + ${CMAKE_CURRENT_SOURCE_DIR}/async-wait/s4u-async-wait_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/dht-chord/s4u-dht-chord_d.xml PARENT_SCOPE) set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-actions-comm-split-p0.txt ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-actions-comm-split-p1.txt @@ -52,7 +54,7 @@ set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-a foreach(example actions-comm actions-storage actor-create actor-daemon actor-kill actor-migration actor-suspend app-bittorrent app-masterworker app-pingpong app-token-ring - async-waitall async-waitany + async-wait async-waitall async-waitany dht-chord plugin-hostload io mutex) ADD_TESH_FACTORIES(s4u-${example} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_CURRENT_BINARY_DIR}/${example} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/${example} s4u-${example}.tesh) endforeach() diff --git a/examples/s4u/README.doc b/examples/s4u/README.doc index 78c993321d..c581f801a2 100644 --- a/examples/s4u/README.doc +++ b/examples/s4u/README.doc @@ -40,6 +40,13 @@ documentation, but it should remain readable directly. @section msg_ex_async Asynchronous communications + - Basic asynchronous communications. + @ref examples/s4u/async-wait/s4u-async-wait.cpp \n + Illustrates how to have non-blocking communications, that are + communications running in the background leaving the process free + to do something else during their completion. The main functions + involved are @ref put_async and @ref get. + - Waiting for all communications in a set. @ref examples/s4u/async-waitall/s4u-async-waitall.cpp\n The @ref simgrid::s4u::Comm::wait_all() function is useful when you want to block diff --git a/examples/s4u/async-wait/s4u-async-wait.cpp b/examples/s4u/async-wait/s4u-async-wait.cpp new file mode 100644 index 0000000000..73f3bff50a --- /dev/null +++ b/examples/s4u/async-wait/s4u-async-wait.cpp @@ -0,0 +1,119 @@ +/* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/* This example shows how to block until the completion of a communication. + */ + + #include "simgrid/s4u.hpp" + #include "xbt/str.h" + #include + #include + +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example"); + +/* Main function of the Sender process */ +class sender { + long messages_count; /* - number of tasks */ + long receivers_count; /* - number of receivers */ + double sleep_start_time; /* - start time */ + double sleep_test_time; /* - test time */ + double msg_size; /* - computational cost */ + double task_comm_size; /* - communication cost */ + simgrid::s4u::MailboxPtr mbox; + +public: + explicit sender(std::vector args) +{ + xbt_assert(args.size() == 7, "The sender function expects 6 arguments from the XML deployment file"); + messages_count = std::stol(args[1]); + msg_size = std::stod(args[2]); + task_comm_size = std::stod(args[3]); + receivers_count = std::stol(args[4]); + double sleep_start_time = std::stod(args[5]); + double sleep_test_time = std::stod(args[6]); + XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time); +} +void operator()() +{ + /* Start dispatching all messages to receivers, in a round robin fashion */ + for (int i = 0; i < messages_count; i++) { + char mailbox[80]; + char taskname[80]; + + std::string mbox_name = std::string("receiver-") + std::to_string(i % receivers_count); + mbox = simgrid::s4u::Mailbox::byName(mbox_name); + snprintf(mailbox,79, "receiver-%ld", i % receivers_count); + snprintf(taskname,79, "Task_%d", i); + + /* Create a communication representing the ongoing communication */ + simgrid::s4u::CommPtr comm = mbox->put_async((void*)mailbox, msg_size); + XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i); + comm->wait(task_comm_size); + } + /* Start sending messages to let the workers know that they should stop */ + for (int i = 0; i < receivers_count; i++) { + char mailbox[80]; + char* payload = xbt_strdup("finalize"); + snprintf(mailbox, 79, "receiver-%d", i); + simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0); + comm->wait(task_comm_size); + XBT_INFO("Send to receiver-%d finalize", i); + } + + XBT_INFO("Goodbye now!"); + +} +}; + +/* Receiver process expects 3 arguments: */ +class receiver { + int id; /* - unique id */ + double sleep_start_time; /* - start time */ + double sleep_test_time; /* - test time */ + simgrid::s4u::MailboxPtr mbox; + +public: + explicit receiver(std::vector args) + { + xbt_assert(args.size() == 4, "The relay_runner function does not accept any parameter from the XML deployment file"); + id = std::stoi(args[1]); + sleep_start_time = std::stod(args[2]); + sleep_test_time = std::stod(args[3]); + XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time); + std::string mbox_name = std::string("receiver-") + std::to_string(id); + mbox = simgrid::s4u::Mailbox::byName(mbox_name); +} + +void operator()() +{ + char mailbox[80]; + snprintf(mailbox,79, "receiver-%d", id); + while (1) { + char* received = static_cast(mbox->get()); + XBT_INFO("Wait to receive a task"); + XBT_INFO("I got a '%s'.", received); + if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */ + xbt_free(received); + break; + } + } +} +}; + +int main(int argc, char *argv[]) +{ + simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv); + + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); + + e->registerFunction("sender"); + e->registerFunction("receiver"); + + e->loadPlatform(argv[1]); + e->loadDeployment(argv[2]); + e->run(); + + return 0; +} diff --git a/examples/s4u/async-wait/s4u-async-wait.tesh b/examples/s4u/async-wait/s4u-async-wait.tesh new file mode 100644 index 0000000000..b46b2e849e --- /dev/null +++ b/examples/s4u/async-wait/s4u-async-wait.tesh @@ -0,0 +1,20 @@ +#! ./tesh + +p Test1 Sleep_sender > Sleep_receiver + +$ $SG_TEST_EXENV ${bindir:=.}/s4u-async-wait ${srcdir:=.}/small_platform_fatpipe.xml ${srcdir:=.}/../s4u/async-wait/s4u-async-wait_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:sender@Tremblay) sleep_start_time : 5.000000 , sleep_test_time : 0.100000 +> [ 0.000000] (2:receiver@Ruby) sleep_start_time : 1.000000 , sleep_test_time : 0.100000 +> [ 0.000000] (1:sender@Tremblay) Send to receiver-0 Task_0 +> [ 0.105458] (2:receiver@Ruby) Wait to receive a task +> [ 0.105458] (2:receiver@Ruby) I got a 'receiver-0'. +> [ 0.105458] (1:sender@Tremblay) Send to receiver-0 Task_1 +> [ 0.210917] (2:receiver@Ruby) Wait to receive a task +> [ 0.210917] (2:receiver@Ruby) I got a 'receiver-0'. +> [ 0.210917] (1:sender@Tremblay) Send to receiver-0 Task_2 +> [ 0.316375] (2:receiver@Ruby) Wait to receive a task +> [ 0.316375] (2:receiver@Ruby) I got a 'receiver-0'. +> [ 0.318326] (2:receiver@Ruby) Wait to receive a task +> [ 0.318326] (2:receiver@Ruby) I got a 'finalize'. +> [ 0.318326] (1:sender@Tremblay) Send to receiver-0 finalize +> [ 0.318326] (1:sender@Tremblay) Goodbye now! \ No newline at end of file diff --git a/examples/s4u/async-wait/s4u-async-wait_d.xml b/examples/s4u/async-wait/s4u-async-wait_d.xml new file mode 100644 index 0000000000..580374b8cd --- /dev/null +++ b/examples/s4u/async-wait/s4u-async-wait_d.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/s4u/async-waitall/s4u-async-waitall.cpp b/examples/s4u/async-waitall/s4u-async-waitall.cpp index 3437e4ad6f..55f645e4e1 100644 --- a/examples/s4u/async-waitall/s4u-async-waitall.cpp +++ b/examples/s4u/async-waitall/s4u-async-waitall.cpp @@ -17,7 +17,7 @@ #include #include -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example"); +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this s4u example"); class sender { long messages_count;