From 772fd99baf3583d3a8c93250df69ba3b35ed7e6e Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Thu, 16 Aug 2018 12:40:29 +0200 Subject: [PATCH 1/1] make examples/s4u/app-masterworkers more complex to make simplification exercises more interesting in tuto --- .../s4u-app-masterworkers-class.cpp | 33 ++++---- .../s4u-app-masterworkers-fun.cpp | 33 ++++---- .../s4u-app-masterworkers.tesh | 80 +++++++++---------- .../s4u-app-masterworkers_d.xml | 29 +++---- 4 files changed, 86 insertions(+), 89 deletions(-) diff --git a/examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp b/examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp index 65bf93f11f..81c84cb4c1 100644 --- a/examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp +++ b/examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp @@ -3,6 +3,9 @@ /* 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 is part of the following tutorial: */ +/* https://simgrid.frama.io/simgrid/usecase_algorithms.html */ + #include XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this s4u example"); @@ -11,27 +14,27 @@ class Master { long tasks_count = 0; double compute_cost = 0; double communicate_cost = 0; - long workers_count = 0; - simgrid::s4u::MailboxPtr mailbox = nullptr; + std::vector workers; public: explicit Master(std::vector args) { - xbt_assert(args.size() == 5, "The master actor expects 4 arguments from the XML deployment file"); + xbt_assert(args.size() > 4, "The master function expects 3 arguments plus the workers' names"); - workers_count = std::stol(args[1]); - tasks_count = std::stol(args[2]); - compute_cost = std::stod(args[3]); - communicate_cost = std::stod(args[4]); + tasks_count = std::stol(args[1]); + compute_cost = std::stod(args[2]); + communicate_cost = std::stod(args[3]); + for (unsigned int i = 4; i < args.size(); i++) + workers.push_back(simgrid::s4u::Mailbox::by_name(args[i])); - XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count); + XBT_INFO("Got %zu workers and %ld tasks to process", workers.size(), tasks_count); } void operator()() { for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */ /* - Select a worker in a round-robin way */ - mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count)); + simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()]; /* - Send the computation amount to the worker */ if (tasks_count < 10000 || (tasks_count < 100000 && i % 10000 == 0) || i % 100000 == 0) @@ -40,25 +43,23 @@ public: } XBT_INFO("All tasks have been dispatched. Request all workers to stop."); - for (int i = 0; i < workers_count; i++) { + for (unsigned int i = 0; i < workers.size(); i++) { /* The workers stop when receiving a negative compute_cost */ - mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i)); + simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()]; mailbox->put(new double(-1.0), 0); } } }; class Worker { - long id = -1; simgrid::s4u::MailboxPtr mailbox = nullptr; public: explicit Worker(std::vector args) { - xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: " - "its worker ID (its numerical rank)"); - id = std::stol(args[1]); - mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id)); + xbt_assert(args.size() == 1, "The worker expects to not get any argument"); + + mailbox = simgrid::s4u::Mailbox::by_name(simgrid::s4u::this_actor::get_host()->get_name()); } void operator()() diff --git a/examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp b/examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp index 0cfae30d86..e63565fb87 100644 --- a/examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp +++ b/examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp @@ -3,6 +3,9 @@ /* 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 is part of the following tutorial: */ +/* https://simgrid.frama.io/simgrid/usecase_algorithms.html */ + #include XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example"); @@ -10,20 +13,20 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this e // master-begin static void master(std::vector args) { - xbt_assert(args.size() == 5, "The master function expects 4 arguments"); + xbt_assert(args.size() > 4, "The master function expects at least 3 arguments"); - long workers_count = std::stol(args[1]); - long tasks_count = std::stol(args[2]); - double compute_cost = std::stod(args[3]); - double communication_cost = std::stod(args[4]); + long tasks_count = std::stol(args[1]); + double compute_cost = std::stod(args[2]); + double communication_cost = std::stod(args[3]); + std::vector workers; + for (unsigned int i = 4; i < args.size(); i++) + workers.push_back(simgrid::s4u::Mailbox::by_name(args[i])); - XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count); + XBT_INFO("Got %zu workers and %ld tasks to process", workers.size(), tasks_count); for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */ /* - Select a worker in a round-robin way */ - std::string worker_rank = std::to_string(i % workers_count); - std::string mailbox_name = std::string("worker-") + worker_rank; - simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()]; /* - Send the computation cost to that worker */ XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname()); @@ -31,10 +34,9 @@ static void master(std::vector args) } XBT_INFO("All tasks have been dispatched. Request all workers to stop."); - for (int i = 0; i < workers_count; i++) { + for (unsigned int i = 0; i < workers.size(); i++) { /* The workers stop when receiving a negative compute_cost */ - std::string mailbox_name = std::string("worker-") + std::to_string(i); - simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()]; mailbox->put(new double(-1.0), 0); } @@ -44,11 +46,10 @@ static void master(std::vector args) // worker-begin static void worker(std::vector args) { - xbt_assert(args.size() == 2, "The worker expects a single argument"); - long id = std::stol(args[1]); + xbt_assert(args.size() == 1, "The worker expects no argument"); - const std::string mailbox_name = std::string("worker-") + std::to_string(id); - simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + auto my_host = simgrid::s4u::this_actor::get_host(); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(my_host->get_name()); double compute_cost; do { diff --git a/examples/s4u/app-masterworkers/s4u-app-masterworkers.tesh b/examples/s4u/app-masterworkers/s4u-app-masterworkers.tesh index 206dd72563..4abff1312c 100644 --- a/examples/s4u/app-masterworkers/s4u-app-masterworkers.tesh +++ b/examples/s4u/app-masterworkers/s4u-app-masterworkers.tesh @@ -5,26 +5,26 @@ p Testing a simple master/workers example application ! output sort 19 $ $SG_TEST_EXENV ${bindir:=.}/s4u-app-masterworkers-class$EXEEXT ${platfdir}/small_platform.xml s4u-app-masterworkers_d.xml --trace "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" > [ 0.000000] (master@Tremblay) Got 5 workers and 20 tasks to process -> [ 0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'worker-0' -> [ 0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'worker-1' -> [ 0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'worker-2' -> [ 0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'worker-3' -> [ 0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'worker-4' -> [ 0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'worker-0' -> [ 0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'worker-1' -> [ 0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'worker-2' -> [ 1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'worker-3' -> [ 1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'worker-4' -> [ 1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'worker-0' -> [ 1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'worker-1' -> [ 1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'worker-2' -> [ 2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'worker-3' -> [ 2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'worker-4' -> [ 2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'worker-0' -> [ 2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'worker-1' -> [ 3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'worker-2' -> [ 3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'worker-3' -> [ 3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'worker-4' +> [ 0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'Tremblay' +> [ 0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'Jupiter' +> [ 0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'Fafard' +> [ 0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'Ginette' +> [ 0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'Bourassa' +> [ 0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'Tremblay' +> [ 0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'Jupiter' +> [ 0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'Fafard' +> [ 1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'Ginette' +> [ 1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'Bourassa' +> [ 1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'Tremblay' +> [ 1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'Jupiter' +> [ 1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'Fafard' +> [ 2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'Ginette' +> [ 2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'Bourassa' +> [ 2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'Tremblay' +> [ 2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'Jupiter' +> [ 3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'Fafard' +> [ 3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'Ginette' +> [ 3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'Bourassa' > [ 4.077318] (master@Tremblay) All tasks have been dispatched. Request all workers to stop. > [ 4.077513] (worker@Tremblay) Exiting now. > [ 4.096528] (worker@Jupiter) Exiting now. @@ -36,26 +36,26 @@ $ $SG_TEST_EXENV ${bindir:=.}/s4u-app-masterworkers-class$EXEEXT ${platfdir}/sma ! output sort 19 $ $SG_TEST_EXENV ${bindir:=.}/s4u-app-masterworkers-fun$EXEEXT ${platfdir}/small_platform.xml s4u-app-masterworkers_d.xml --trace "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" > [ 0.000000] (master@Tremblay) Got 5 workers and 20 tasks to process -> [ 0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'worker-0' -> [ 0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'worker-1' -> [ 0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'worker-2' -> [ 0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'worker-3' -> [ 0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'worker-4' -> [ 0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'worker-0' -> [ 0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'worker-1' -> [ 0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'worker-2' -> [ 1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'worker-3' -> [ 1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'worker-4' -> [ 1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'worker-0' -> [ 1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'worker-1' -> [ 1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'worker-2' -> [ 2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'worker-3' -> [ 2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'worker-4' -> [ 2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'worker-0' -> [ 2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'worker-1' -> [ 3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'worker-2' -> [ 3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'worker-3' -> [ 3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'worker-4' +> [ 0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'Tremblay' +> [ 0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'Jupiter' +> [ 0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'Fafard' +> [ 0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'Ginette' +> [ 0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'Bourassa' +> [ 0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'Tremblay' +> [ 0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'Jupiter' +> [ 0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'Fafard' +> [ 1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'Ginette' +> [ 1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'Bourassa' +> [ 1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'Tremblay' +> [ 1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'Jupiter' +> [ 1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'Fafard' +> [ 2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'Ginette' +> [ 2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'Bourassa' +> [ 2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'Tremblay' +> [ 2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'Jupiter' +> [ 3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'Fafard' +> [ 3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'Ginette' +> [ 3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'Bourassa' > [ 4.077318] (master@Tremblay) All tasks have been dispatched. Request all workers to stop. > [ 4.077513] (worker@Tremblay) Exiting now. > [ 4.096528] (worker@Jupiter) Exiting now. diff --git a/examples/s4u/app-masterworkers/s4u-app-masterworkers_d.xml b/examples/s4u/app-masterworkers/s4u-app-masterworkers_d.xml index 29fddcaef6..292e3bb6f6 100644 --- a/examples/s4u/app-masterworkers/s4u-app-masterworkers_d.xml +++ b/examples/s4u/app-masterworkers/s4u-app-masterworkers_d.xml @@ -3,25 +3,20 @@ - + + + + + + - - - - - - - - - - - - - - - - + + + + + + -- 2.20.1