From: Martin Quinson Date: Tue, 21 Aug 2018 06:20:10 +0000 (+0200) Subject: take the tuto myself, and add how to categorize tasks in S4U X-Git-Tag: v3_21~233 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/ed0bfa2c1c2fe26abe8615f1e174c7dfedd4359f take the tuto myself, and add how to categorize tasks in S4U --- diff --git a/docs/source/tuto_s4u.rst b/docs/source/tuto_s4u.rst index a6b54d319a..433a69f76a 100644 --- a/docs/source/tuto_s4u.rst +++ b/docs/source/tuto_s4u.rst @@ -377,7 +377,7 @@ information is only written once. It thus follows the `DRY :language: xml -Copy your ``master-workers.cpp`` into ``master-workers-exo1.cpp`` and +Copy your ``master-workers.cpp`` into ``master-workers-lab1.cpp`` and add a new executable into ``CMakeLists.txt``. Then modify your worker function so that it gets its mailbox name not from the name of its host, but from the string passed as ``args[1]``. The master will send @@ -429,7 +429,7 @@ Creating the workers from the master .................................... For that, the master needs to retrieve the list of hosts declared in -the platform with :cpp:func:`simgrid::s4u::Engine::get_all_host()`. +the platform with :cpp:func:`simgrid::s4u::Engine::get_all_hosts`. Then, the master should start the worker processes with :cpp:func:`simgrid::s4u::Actor::create`. @@ -478,7 +478,8 @@ the workers of a given master would pull their work from the same mailbox, which should be passed as parameter to the workers. This reduces the amount of mailboxes, but prevents the master from taking any scheduling decision. It really depends on how you want to organize -your application and what you want to study with your simulator. +your application and what you want to study with your simulator. In +this tutorial, that's probably not a good idea. Wrap up ....... @@ -529,13 +530,15 @@ Controlling the message verbosity ................................. Not all messages are equally informative, so you probably want to -change most of the ``XBT_INFO`` into ``XBT_DEBUG`` so that they are hidden -by default. You could for example show only the total number of tasks -processed by default. You can still see the debug messages as follows: +change some of the ``XBT_INFO`` into ``XBT_DEBUG`` so that they are +hidden by default. For example, you may want to use ``XBT_INFO`` once +every 100 tasks and ``XBT_DEBUG`` when sending all the other tasks. Or +you could show only the total number of tasks processed by +default. You can still see the debug messages as follows: .. code-block:: shell - ./masterworker examples/platforms/small_platform.xml deployment3.xml --log=msg_test.thres:debug + ./master-workers-lab3 small_platform.xml deployment3.xml --log=msg_test.thres:debug Lab 4: Understanding competing applications @@ -552,15 +555,29 @@ contain too much information to be useful: it is impossible to understand which task belong to which application. To fix this, we will categorize the tasks. -For that, first let each master create its own category of tasks with -@ref TRACE_category(), and then assign this category to each task using -@ref MSG_task_set_category(). +Instead of starting the execution in one function call only with +``simgrid::s4u::this_actor::execute(compute_cost)``, you need to +create the execution activity, set its tracing category and then start +it and wait for its completion, as follows: + +.. code-block:: cpp + + simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(compute_cost); + exec->set_tracing_category(category); + // exec->start() is optional here as wait() starts the activity on need + exec->wait(); + +You can make the same code shorter as follows: + +.. code-block:: cpp + + simgrid::s4u::this_actor::exec_init(compute_cost)->set_tracing_category(category)->wait(); The outcome can then be visualized as a Gantt-chart as follows: .. code-block:: shell - ./masterworker examples/platforms/small_platform.xml deployment4.xml --cfg=tracing:yes --cfg=tracing/msg/process:yes + ./master-workers-lab4 small_platform.xml deployment4.xml --cfg=tracing:yes --cfg=tracing/msg/process:yes vite simgrid.trace .. todo:: diff --git a/docs/source/tuto_s4u/.gitignore b/docs/source/tuto_s4u/.gitignore new file mode 100644 index 0000000000..f3328e3d3a --- /dev/null +++ b/docs/source/tuto_s4u/.gitignore @@ -0,0 +1,6 @@ +master-workers-lab1 +master-workers-lab2 +master-workers-lab3 +master-workers-lab4 +Rplots.pdf +gantt.csv diff --git a/docs/source/tuto_s4u/deployment1.xml b/docs/source/tuto_s4u/deployment1.xml index 3812cf4ebc..5fc2809114 100644 --- a/docs/source/tuto_s4u/deployment1.xml +++ b/docs/source/tuto_s4u/deployment1.xml @@ -1,27 +1,28 @@ - + - + + - + - + - + - + - + diff --git a/docs/source/tuto_s4u/master-workers-lab1.cpp b/docs/source/tuto_s4u/master-workers-lab1.cpp new file mode 100644 index 0000000000..5caa2d4e83 --- /dev/null +++ b/docs/source/tuto_s4u/master-workers-lab1.cpp @@ -0,0 +1,92 @@ +/* Copyright (c) 2010-2018. 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 is our solution to the first lab of the S4U tutorial + * (available online at https://simgrid.frama.io/simgrid/tuto_s4u.html) + * + * Reading this further before taking the tutorial will SPOIL YOU!!! + * + ****************************************************************************/ + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example"); + +static void master(std::vector args) +{ + xbt_assert(args.size() == 5, "The master function expects 4 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]); + + XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, 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); + + /* - Send the computation cost to that worker */ + XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname()); + mailbox->put(new double(compute_cost), communication_cost); + } + + XBT_INFO("All tasks have been dispatched. Request all workers to stop."); + for (int i = 0; i < workers_count; 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); + + mailbox->put(new double(-1.0), 0); + } +} + +static void worker(std::vector args) +{ + xbt_assert(args.size() == 2, "The worker expects a single argument"); + long id = std::stol(args[1]); + + const std::string mailbox_name = std::string("worker-") + std::to_string(id); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + double compute_cost; + do { + double* msg = static_cast(mailbox->get()); + compute_cost = *msg; + delete msg; + + if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */ + simgrid::s4u::this_actor::execute(compute_cost); + + } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */ + + XBT_INFO("Exiting now."); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); + + /* Register the functions representing the actors */ + e.register_function("master", &master); + e.register_function("worker", &worker); + + /* Load the platform description and then deploy the application */ + e.load_platform(argv[1]); + e.load_deployment(argv[2]); + + /* Run the simulation */ + e.run(); + + XBT_INFO("Simulation is over"); + + return 0; +} diff --git a/docs/source/tuto_s4u/master-workers-lab2.cpp b/docs/source/tuto_s4u/master-workers-lab2.cpp new file mode 100644 index 0000000000..e47d33aee6 --- /dev/null +++ b/docs/source/tuto_s4u/master-workers-lab2.cpp @@ -0,0 +1,94 @@ +/* Copyright (c) 2010-2018. 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 is our solution to the second lab of the S4U tutorial + * (available online at https://simgrid.frama.io/simgrid/tuto_s4u.html) + * + * Reading this further before taking the tutorial will SPOIL YOU!!! + * + ****************************************************************************/ + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example"); + +static void worker() +{ + const std::string mailbox_name = std::string("worker-") + std::to_string(simgrid::s4u::this_actor::get_pid()); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + double compute_cost; + do { + double* msg = static_cast(mailbox->get()); + compute_cost = *msg; + delete msg; + + if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */ + simgrid::s4u::this_actor::execute(compute_cost); + + } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */ + + XBT_INFO("Exiting now."); +} + +static void master(std::vector args) +{ + xbt_assert(args.size() == 4, "The master function expects 3 arguments"); + + long tasks_count = std::stol(args[1]); + double compute_cost = std::stod(args[2]); + double communication_cost = std::stod(args[3]); + + std::vector actors; + + for (auto* host : simgrid::s4u::Engine::get_instance()->get_all_hosts()) { + simgrid::s4u::ActorPtr act = simgrid::s4u::Actor::create(std::string("Worker-") + host->get_name(), host, worker); + actors.push_back(act); + } + + XBT_INFO("Got %ld tasks to process", tasks_count); + + for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */ + /* - Select a worker in a round-robin way */ + aid_t worker_pid = actors.at(i % actors.size())->get_pid(); + std::string mailbox_name = std::string("worker-") + std::to_string(worker_pid); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + /* - Send the computation cost to that worker */ + XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname()); + mailbox->put(new double(compute_cost), communication_cost); + } + + XBT_INFO("All tasks have been dispatched. Request all workers to stop."); + for (unsigned long i = 0; i < actors.size(); i++) { + /* The workers stop when receiving a negative compute_cost */ + std::string mailbox_name = std::string("worker-") + std::to_string(actors.at(i)->get_pid()); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + mailbox->put(new double(-1.0), 0); + } +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); + + /* Register the functions representing the actors */ + e.register_function("master", &master); + + /* Load the platform description and then deploy the application */ + e.load_platform(argv[1]); + e.load_deployment(argv[2]); + + /* Run the simulation */ + e.run(); + + XBT_INFO("Simulation is over"); + + return 0; +} diff --git a/docs/source/tuto_s4u/master-workers-lab3.cpp b/docs/source/tuto_s4u/master-workers-lab3.cpp new file mode 100644 index 0000000000..8f964ce0b8 --- /dev/null +++ b/docs/source/tuto_s4u/master-workers-lab3.cpp @@ -0,0 +1,94 @@ +/* Copyright (c) 2010-2018. 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 is our solution to the third lab of the S4U tutorial + * (available online at https://simgrid.frama.io/simgrid/tuto_s4u.html) + * + * Reading this further before taking the tutorial will SPOIL YOU!!! + * + ****************************************************************************/ + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example"); + +static void worker() +{ + const std::string mailbox_name = std::string("worker-") + std::to_string(simgrid::s4u::this_actor::get_pid()); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + double compute_cost; + while (true) { // Master forcefully kills the workers by the end of the simulation + double* msg = static_cast(mailbox->get()); + compute_cost = *msg; + delete msg; + + simgrid::s4u::this_actor::execute(compute_cost); + } +} + +static void master(std::vector args) +{ + xbt_assert(args.size() == 4, "The master function expects 3 arguments"); + + double simulation_duration = std::stod(args[1]); + double compute_cost = std::stod(args[2]); + double communication_cost = std::stod(args[3]); + + std::vector actors; + + simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance(); + + XBT_INFO("Asked to run for %.1f seconds", simulation_duration); + + for (auto* host : e->get_all_hosts()) { + simgrid::s4u::ActorPtr act = simgrid::s4u::Actor::create(std::string("Worker-") + host->get_name(), host, worker); + actors.push_back(act); + } + + int task_id = 0; + while (e->get_clock() < simulation_duration) { /* For each task: */ + /* - Select a worker in a round-robin way */ + aid_t worker_pid = actors.at(task_id % actors.size())->get_pid(); + std::string mailbox_name = std::string("worker-") + std::to_string(worker_pid); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + /* - Send the computation cost to that worker */ + if (task_id % 100 == 0) + XBT_INFO("Sending task %d to mailbox '%s'", task_id, mailbox->get_cname()); + else + XBT_DEBUG("Sending task %d to mailbox '%s'", task_id, mailbox->get_cname()); + mailbox->put(new double(compute_cost), communication_cost); + + task_id++; + } + + XBT_INFO("Time is up. Forcefully kill all workers."); + for (unsigned long i = 0; i < actors.size(); i++) { + actors.at(i)->kill(); + } +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); + + /* Register the functions representing the actors */ + e.register_function("master", &master); + + /* Load the platform description and then deploy the application */ + e.load_platform(argv[1]); + e.load_deployment(argv[2]); + + /* Run the simulation */ + e.run(); + + XBT_INFO("Simulation is over"); + + return 0; +} diff --git a/docs/source/tuto_s4u/master-workers-lab4.cpp b/docs/source/tuto_s4u/master-workers-lab4.cpp new file mode 100644 index 0000000000..ad2fd7fb4c --- /dev/null +++ b/docs/source/tuto_s4u/master-workers-lab4.cpp @@ -0,0 +1,96 @@ +/* Copyright (c) 2010-2018. 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 is our solution to the fourth lab of the S4U tutorial + * (available online at https://simgrid.frama.io/simgrid/tuto_s4u.html) + * + * Reading this further before taking the tutorial will SPOIL YOU!!! + * + ****************************************************************************/ + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example"); + +static void worker(std::string category) +{ + const std::string mailbox_name = std::string("worker-") + std::to_string(simgrid::s4u::this_actor::get_pid()); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + while (true) { // Master forcefully kills the workers by the end of the simulation + double* msg = static_cast(mailbox->get()); + double compute_cost = *msg; + delete msg; + + // simgrid::s4u::this_actor::exec_init(compute_cost)->set_tracing_category(category)->wait(); + /* Long form:*/ + simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(compute_cost); + exec->set_tracing_category(category); + exec->wait(); + } +} + +static void master(std::vector args) +{ + xbt_assert(args.size() == 4, "The master function expects 3 arguments"); + + double simulation_duration = std::stod(args[1]); + double compute_cost = std::stod(args[2]); + double communication_cost = std::stod(args[3]); + + std::vector actors; + + simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance(); + std::string my_name = std::string("master-") + std::to_string(simgrid::s4u::this_actor::get_pid()); + + XBT_INFO("Asked to run for %.1f seconds", simulation_duration); + + for (auto* host : e->get_all_hosts()) { + simgrid::s4u::ActorPtr act = + simgrid::s4u::Actor::create(std::string("Worker-") + host->get_name(), host, worker, my_name); + actors.push_back(act); + } + + int task_id = 0; + while (e->get_clock() < simulation_duration) { /* For each task: */ + /* - Select a worker in a round-robin way */ + aid_t worker_pid = actors.at(task_id % actors.size())->get_pid(); + std::string mailbox_name = std::string("worker-") + std::to_string(worker_pid); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name); + + /* - Send the computation cost to that worker */ + XBT_DEBUG("Sending task %d to mailbox '%s'", task_id, mailbox->get_cname()); + mailbox->put(new double(compute_cost), communication_cost); + + task_id++; + } + + XBT_INFO("Time is up. Forcefully kill all workers."); + for (unsigned long i = 0; i < actors.size(); i++) { + actors.at(i)->kill(); + } +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]); + + /* Register the functions representing the actors */ + e.register_function("master", &master); + + /* Load the platform description and then deploy the application */ + e.load_platform(argv[1]); + e.load_deployment(argv[2]); + + /* Run the simulation */ + e.run(); + + XBT_INFO("Simulation is over"); + + return 0; +}