From 9f0e5f8104a7c579806205d89904a9d560cae830 Mon Sep 17 00:00:00 2001 From: Takishipp Date: Mon, 28 Aug 2017 17:21:42 +0200 Subject: [PATCH] first attempt to convert async-waitany msg version to s4u (doesn't work) --- examples/s4u/CMakeLists.txt | 3 +- .../s4u/async-waitany/s4u_async-waitany.cpp | 112 ++++++++++++++++++ .../s4u/async-waitany/s4u_async-waitany.tesh | 34 ++++++ .../s4u/async-waitany/s4u_async-waitany_d.xml | 21 ++++ 4 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 examples/s4u/async-waitany/s4u_async-waitany.cpp create mode 100644 examples/s4u/async-waitany/s4u_async-waitany.tesh create mode 100644 examples/s4u/async-waitany/s4u_async-waitany_d.xml diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 6064c0caa5..4e39e8c232 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -1,5 +1,5 @@ foreach (example actions-comm actions-storage actor-create actor-daemon actor-kill actor-migration actor-suspend - app-masterworker app-pingpong app-token-ring plugin-hostload io mutex ) + app-masterworker app-pingpong app-token-ring plugin-hostload io mutex async-waitany) add_executable (s4u_${example} ${example}/s4u_${example}.cpp) target_link_libraries(s4u_${example} simgrid) set_target_properties(s4u_${example} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${example}) @@ -35,6 +35,7 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_a ${CMAKE_CURRENT_SOURCE_DIR}/actor-create/s4u_actor-create_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/s4u_app-bittorrent_d.xml ${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}/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 diff --git a/examples/s4u/async-waitany/s4u_async-waitany.cpp b/examples/s4u/async-waitany/s4u_async-waitany.cpp new file mode 100644 index 0000000000..e0fdd5b316 --- /dev/null +++ b/examples/s4u/async-waitany/s4u_async-waitany.cpp @@ -0,0 +1,112 @@ +/* Copyright (c) 2010-2016. 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. */ + +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example"); + +static int sender(std::vector args) +{ + xbt_assert(args.size() == 5, "This function expects 5 parameters from the XML deployment file"); + long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); + double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); + double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); + long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s"); + int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s"); + + std::vector comms; + /* First pack the communications in the dynar */ + for (int i = 0; i < number_of_tasks; i++) { + double coef = (diff_com == 0) ? 1 : (i + 1); + + char mailbox[80]; + char taskname[80]; + snprintf(mailbox,79, "receiver-%ld", (i % receivers_count)); + snprintf(taskname,79, "Task_%d", i); + + double* task = new double(); + simgrid::s4u::Mailbox::byName(mailbox)->put(task, 1); + comms.push_back(comm); + XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef); + } + + /* Here we are waiting for the completion of all communications */ + while (xbt_dynar_is_empty(comms) == 0) { + msg_comm_t comm; + comms.pop_back(&comm); + MSG_comm_destroy(comm); + } + comms.clear(); + + /* Here we are waiting for the completion of all tasks */ + for (int i = 0; i < receivers_count; i++) { + msg_task_t task = NULL; + msg_comm_t comm = MSG_task_irecv(&task, "finalize"); + msg_error_t res_wait = MSG_comm_wait(comm, -1); + xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed"); + MSG_comm_destroy(comm); + MSG_task_destroy(task); + } + + XBT_INFO("Goodbye now!"); + return 0; +} + +static int receiver(int argc, char *argv[]) +{ + xbt_assert(argc==3, "This function expects 2 parameters from the XML deployment file"); + int id = xbt_str_parse_int(argv[1], "ID should be numerical, not %s"); + int task_amount = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s"); + msg_task_t *tasks = new msg_task_t[task_amount]; + std::vector comms; + + char mailbox[80]; + snprintf(mailbox,79, "receiver-%d", id); + simgrid::s4u::Actor::sleep_for(10.0); + for (int i = 0; i < task_amount; i++) { + XBT_INFO("Wait to receive task %d", i); + tasks[i] = NULL; + msg_comm_t comm = MSG_task_irecv(&tasks[i], mailbox); + comms.push_back(comm); + } + + /* Here we are waiting for the receiving of all communications */ + while (!xbt_dynar_is_empty(comms)) { + msg_comm_t comm; + // MSG_comm_waitany returns the rank of the comm that just ended. Remove it. + comms.pop_back(&comm); + msg_task_t task = MSG_comm_get_task(comm); + MSG_comm_destroy(comm); + XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); + MSG_task_execute(task); + XBT_INFO("\"%s\" done", MSG_task_get_name(task)); + msg_error_t err = MSG_task_destroy(task); + xbt_assert(err == MSG_OK, "MSG_task_destroy failed"); + } + comms.clear(); + delete [] tasks; + + /* Here we tell to sender that all tasks are done */ + MSG_task_send(MSG_task_create(NULL, 0, 0, NULL), "finalize"); + XBT_INFO("I'm done. See you!"); + return 0; +} + +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" + "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]); + + e->registerFunction("sender"); + e->registerFunction("receiver"); + e->loadDeployment(argv[2]); // And then, you load the deployment file + + e->run(); + + delete e; + return 0; +} diff --git a/examples/s4u/async-waitany/s4u_async-waitany.tesh b/examples/s4u/async-waitany/s4u_async-waitany.tesh new file mode 100644 index 0000000000..6637734a1a --- /dev/null +++ b/examples/s4u/async-waitany/s4u_async-waitany.tesh @@ -0,0 +1,34 @@ +#! ./tesh + +p Testing the MSG_comm_waitany function + +! output sort 19 +$ $SG_TEST_EXENV ${bindir:=.}/async-waitany ${srcdir:=.}/small_platform.xml ${srcdir:=.}/../msg/async-waitany/async-waitany_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:sender@Tremblay) Send to receiver-0 Task_0 comm_size 1000000.000000 +> [ 0.000000] (1:sender@Tremblay) Send to receiver-1 Task_1 comm_size 1000000.000000 +> [ 0.000000] (1:sender@Tremblay) Send to receiver-0 Task_2 comm_size 1000000.000000 +> [ 0.000000] (1:sender@Tremblay) Send to receiver-1 Task_3 comm_size 1000000.000000 +> [ 0.000000] (1:sender@Tremblay) Send to receiver-0 Task_4 comm_size 1000000.000000 +> [ 0.000000] (1:sender@Tremblay) Send to receiver-1 Task_5 comm_size 1000000.000000 +> [ 10.000000] (2:receiver@Fafard) Wait to receive task 0 +> [ 10.000000] (2:receiver@Fafard) Wait to receive task 1 +> [ 10.000000] (2:receiver@Fafard) Wait to receive task 2 +> [ 10.000000] (3:receiver@Jupiter) Wait to receive task 0 +> [ 10.000000] (3:receiver@Jupiter) Wait to receive task 1 +> [ 10.000000] (3:receiver@Jupiter) Wait to receive task 2 +> [ 10.423774] (2:receiver@Fafard) Processing "Task_4" +> [ 10.469435] (3:receiver@Jupiter) Processing "Task_3" +> [ 11.079116] (2:receiver@Fafard) "Task_4" done +> [ 11.079116] (2:receiver@Fafard) Processing "Task_0" +> [ 11.124778] (3:receiver@Jupiter) "Task_3" done +> [ 11.124778] (3:receiver@Jupiter) Processing "Task_1" +> [ 11.734459] (2:receiver@Fafard) "Task_0" done +> [ 11.734459] (2:receiver@Fafard) Processing "Task_2" +> [ 11.780120] (3:receiver@Jupiter) "Task_1" done +> [ 11.780120] (3:receiver@Jupiter) Processing "Task_5" +> [ 12.389801] (2:receiver@Fafard) "Task_2" done +> [ 12.415509] (2:receiver@Fafard) I'm done. See you! +> [ 12.435462] (3:receiver@Jupiter) "Task_5" done +> [ 12.454477] (0:maestro@) Simulation time 12.4545 +> [ 12.454477] (1:sender@Tremblay) Goodbye now! +> [ 12.454477] (3:receiver@Jupiter) I'm done. See you! diff --git a/examples/s4u/async-waitany/s4u_async-waitany_d.xml b/examples/s4u/async-waitany/s4u_async-waitany_d.xml new file mode 100644 index 0000000000..2e680c4c3f --- /dev/null +++ b/examples/s4u/async-waitany/s4u_async-waitany_d.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + -- 2.20.1