Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
14a4b741e28a7f3730d80d6ed5c76d47b85a7730
[simgrid.git] / examples / cpp / trace-masterworkers / s4u-trace-masterworkers.cpp
1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/instr.h>
7 #include <simgrid/s4u.hpp>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_trace_masterworker, "Messages specific for this example");
10 namespace sg4 = simgrid::s4u;
11
12 struct Task {
13   std::string name;
14   std::string category;
15   double flops;
16 };
17
18 static void master(std::vector<std::string> args)
19 {
20   xbt_assert(args.size() > 4, "The master function expects at least 3 arguments");
21
22   long tasks_count        = std::stol(args[1]);
23   double compute_cost     = std::stod(args[2]);
24   long communication_cost = std::stol(args[3]);
25   size_t workers_count    = args.size() - 4;
26   const auto& my_host     = sg4::this_actor::get_host()->get_name();
27   auto mailbox            = sg4::Mailbox::by_name("master_mailbox");
28
29   XBT_DEBUG("Got %zu workers and %ld tasks to process", workers_count, tasks_count);
30
31   // setting the variable "is_master" (previously declared) to value 1
32   simgrid::instr::set_host_variable(my_host, "is_master", 1);
33
34   simgrid::instr::mark("msmark", "start_send_tasks");
35   for (int i = 0; i < tasks_count; i++) {
36     // setting the variable "task_creation" to value i
37     simgrid::instr::set_host_variable(my_host, "task_creation", i);
38
39     // setting the category of task to "compute"
40     Task task = {"task", "compute", compute_cost};
41     mailbox->put(new Task(task), communication_cost);
42   }
43   simgrid::instr::mark("msmark", "finish_send_tasks");
44
45   XBT_DEBUG("All tasks have been dispatched. Request all workers to stop.");
46   for (unsigned int i = 0; i < workers_count; i++) {
47     Task finalize = {"finalize", "finalize", 0};
48     mailbox->put(new Task(finalize), 0);
49   }
50 }
51
52 static void worker(std::vector<std::string> args)
53 {
54   xbt_assert(args.size() == 1, "The worker expects no argument");
55
56   const auto& my_host = sg4::this_actor::get_host()->get_name();
57   auto mailbox        = sg4::Mailbox::by_name("master_mailbox");
58
59   simgrid::instr::set_host_variable(my_host, "is_worker", 1);
60   simgrid::instr::set_host_variable(my_host, "task_computation", 0);
61
62   while (true) {
63     auto task = mailbox->get_unique<Task>();
64     if (task->name == "finalize") {
65       break;
66     }
67     // adding the task's cost to the variable "task_computation"
68     simgrid::instr::add_host_variable(my_host, "task_computation", task->flops);
69     sg4::this_actor::exec_init(task->flops)->set_name(task->name)->set_tracing_category(task->category)->wait();
70   }
71
72   XBT_DEBUG("Exiting now.");
73 }
74
75 int main(int argc, char* argv[])
76 {
77   sg4::Engine e(&argc, argv);
78   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
79
80   e.load_platform(argv[1]);
81
82   // declaring user variables
83   simgrid::instr::declare_host_variable("is_worker");
84   simgrid::instr::declare_host_variable("is_master");
85   simgrid::instr::declare_host_variable("task_creation");
86   simgrid::instr::declare_host_variable("task_computation");
87
88   // declaring user markers and values
89   simgrid::instr::declare_mark("msmark");
90   simgrid::instr::declare_mark_value("msmark", "start_send_tasks");
91   simgrid::instr::declare_mark_value("msmark", "finish_send_tasks");
92
93   // declaring user categories with RGB colors (values from 0 to 1)
94   simgrid::instr::declare_tracing_category("compute", "1 0 0");  // compute is red
95   simgrid::instr::declare_tracing_category("finalize", "0 1 0"); // finalize is green
96   // categories without user-defined colors receive random colors generated by the tracing system
97   simgrid::instr::declare_tracing_category("request");
98   simgrid::instr::declare_tracing_category("report");
99
100   e.register_function("master", &master);
101   e.register_function("worker", &worker);
102   e.load_deployment(argv[2]);
103
104   e.run();
105
106   XBT_DEBUG("Simulation is over");
107
108   if (const auto& categories = simgrid::instr::get_tracing_categories(); not categories.empty()) {
109     XBT_INFO("Declared tracing categories:");
110     for (const auto& category : categories)
111       XBT_INFO("%s", category.c_str());
112   }
113
114   if (const auto& marks = simgrid::instr::get_marks(); not marks.empty()) {
115     XBT_INFO("Declared marks:");
116     for (const auto& mark : marks)
117       XBT_INFO("%s", mark.c_str());
118   }
119
120   return 0;
121 }