From 9914e1e7241086421588252189d28f051c9218bb Mon Sep 17 00:00:00 2001 From: Takishipp Date: Wed, 30 Aug 2017 17:54:09 +0200 Subject: [PATCH] Second phase to convert async-waitany msg vertion to s4u (bugs to solve) --- .../s4u/async-waitany/s4u_async-waitany.cpp | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/examples/s4u/async-waitany/s4u_async-waitany.cpp b/examples/s4u/async-waitany/s4u_async-waitany.cpp index e0fdd5b316..ae22c7e916 100644 --- a/examples/s4u/async-waitany/s4u_async-waitany.cpp +++ b/examples/s4u/async-waitany/s4u_async-waitany.cpp @@ -4,19 +4,21 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "simgrid/s4u.hpp" +#include "xbt/str.h" XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example"); -static int sender(std::vector args) +static int sender(int argc, char *argv[]) { - xbt_assert(args.size() == 5, "This function expects 5 parameters from the XML deployment file"); + xbt_assert(argc==6, "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; + std::vector comms; + simgrid::s4u::MailboxPtr mbox; /* First pack the communications in the dynar */ for (int i = 0; i < number_of_tasks; i++) { double coef = (diff_com == 0) ? 1 : (i + 1); @@ -26,28 +28,26 @@ static int sender(std::vector args) 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); + simgrid::s4u::CommPtr comm = mbox->put_async((void*)taskname, 42.0); 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); + while (!comms.empty()) { + simgrid::s4u::CommPtr comm; + comms.pop_back(comm); + comm->wait(); + delete 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); + std::sring received = nullptr; + simgrid::s4u::CommPtr comm = mbox->get_async(&received); + comm->wait(); + delete comm; } XBT_INFO("Goodbye now!"); @@ -59,37 +59,31 @@ 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); + void *received; + std::vector * comms; + simgrid::s4u::CommPtr comm; + simgrid::s4u::MailboxPtr mbox; + 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); + received[i] = NULL; + comm = mbox->get_async(&received[i]); comms.push_back(comm); } /* Here we are waiting for the receiving of all communications */ - while (!xbt_dynar_is_empty(comms)) { - msg_comm_t comm; + while (!comms.empty()) { + simgrid::s4u::CommPtr 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"); + simgrid::s4u::this_actor::execute(comm); + delete comm; } 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"); + simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1); XBT_INFO("I'm done. See you!"); return 0; } -- 2.20.1