1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <simgrid/instr.h>
7 #include <simgrid/s4u.hpp>
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_trace_masterworker, "Messages specific for this example");
17 static void master(std::vector<std::string> args)
19 xbt_assert(args.size() > 4, "The master function expects at least 3 arguments");
21 long tasks_count = std::stol(args[1]);
22 double compute_cost = std::stod(args[2]);
23 long communication_cost = std::stol(args[3]);
24 size_t workers_count = args.size() - 4;
25 const auto& my_host = simgrid::s4u::this_actor::get_host()->get_name();
26 auto mailbox = simgrid::s4u::Mailbox::by_name("master_mailbox");
28 XBT_DEBUG("Got %zu workers and %ld tasks to process", workers_count, tasks_count);
30 // setting the variable "is_master" (previously declared) to value 1
31 simgrid::instr::set_host_variable(my_host, "is_master", 1);
33 simgrid::instr::mark("msmark", "start_send_tasks");
34 for (int i = 0; i < tasks_count; i++) {
35 // setting the variable "task_creation" to value i
36 simgrid::instr::set_host_variable(my_host, "task_creation", i);
38 // setting the category of task to "compute"
39 Task task = {"task", "compute", compute_cost};
40 mailbox->put(new Task(task), communication_cost);
42 simgrid::instr::mark("msmark", "finish_send_tasks");
44 XBT_DEBUG("All tasks have been dispatched. Request all workers to stop.");
45 for (unsigned int i = 0; i < workers_count; i++) {
46 Task finalize = {"finalize", "finalize", 0};
47 mailbox->put(new Task(finalize), 0);
51 static void worker(std::vector<std::string> args)
53 xbt_assert(args.size() == 1, "The worker expects no argument");
55 const auto& my_host = simgrid::s4u::this_actor::get_host()->get_name();
56 auto mailbox = simgrid::s4u::Mailbox::by_name("master_mailbox");
58 simgrid::instr::set_host_variable(my_host, "is_worker", 1);
59 simgrid::instr::set_host_variable(my_host, "task_computation", 0);
62 auto task = mailbox->get_unique<Task>();
63 if (task->name == "finalize") {
66 // adding the task's cost to the variable "task_computation"
67 simgrid::instr::add_host_variable(my_host, "task_computation", task->flops);
68 simgrid::s4u::this_actor::exec_init(task->flops)
69 ->set_name(task->name)
70 ->set_tracing_category(task->category)
74 XBT_DEBUG("Exiting now.");
77 int main(int argc, char* argv[])
79 simgrid::s4u::Engine e(&argc, argv);
80 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
82 e.load_platform(argv[1]);
84 // declaring user variables
85 simgrid::instr::declare_host_variable("is_worker");
86 simgrid::instr::declare_host_variable("is_master");
87 simgrid::instr::declare_host_variable("task_creation");
88 simgrid::instr::declare_host_variable("task_computation");
90 // declaring user markers and values
91 simgrid::instr::declare_mark("msmark");
92 simgrid::instr::declare_mark_value("msmark", "start_send_tasks");
93 simgrid::instr::declare_mark_value("msmark", "finish_send_tasks");
95 // declaring user categories with RGB colors (values from 0 to 1)
96 simgrid::instr::declare_tracing_category("compute", "1 0 0"); // compute is red
97 simgrid::instr::declare_tracing_category("finalize", "0 1 0"); // finalize is green
98 // categories without user-defined colors receive random colors generated by the tracing system
99 simgrid::instr::declare_tracing_category("request");
100 simgrid::instr::declare_tracing_category("report");
102 e.register_function("master", &master);
103 e.register_function("worker", &worker);
104 e.load_deployment(argv[2]);
108 XBT_DEBUG("Simulation is over");
110 const auto& categories = simgrid::instr::get_tracing_categories();
111 if (not categories.empty()) {
112 XBT_INFO("Declared tracing categories:");
113 for (const auto& category : categories)
114 XBT_INFO("%s", category.c_str());
117 const auto& marks = simgrid::instr::get_marks();
118 if (not marks.empty()) {
119 XBT_INFO("Declared marks:");
120 for (const auto& mark : marks)
121 XBT_INFO("%s", mark.c_str());