Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pull from simgrid/master and a subsequent merge with origin/master
[simgrid.git] / examples / s4u / trace-masterworkers / s4u-trace-masterworkers.cpp
1 /* Copyright (c) 2010-2020. 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
11 struct Task {
12   std::string name;
13   std::string category;
14   double flops;
15 };
16
17 static void master(std::vector<std::string> args)
18 {
19   xbt_assert(args.size() > 4, "The master function expects at least 3 arguments");
20
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 char* my_hostname = simgrid::s4u::this_actor::get_host()->get_cname();
26   auto mailbox            = simgrid::s4u::Mailbox::by_name("master_mailbox");
27
28   XBT_DEBUG("Got %zu workers and %ld tasks to process", workers_count, tasks_count);
29
30   // setting the variable "is_master" (previously declared) to value 1
31   TRACE_host_variable_set(my_hostname, "is_master", 1);
32
33   TRACE_mark("msmark", "start_send_tasks");
34   for (int i = 0; i < tasks_count; i++) {
35     // setting the variable "task_creation" to value i
36     TRACE_host_variable_set(my_hostname, "task_creation", i);
37
38     // setting the category of task to "compute"
39     Task task = {"task", "compute", compute_cost};
40     mailbox->put(new Task(task), communication_cost);
41   }
42   TRACE_mark("msmark", "finish_send_tasks");
43
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);
48   }
49 }
50
51 static void worker(std::vector<std::string> args)
52 {
53   xbt_assert(args.size() == 1, "The worker expects no argument");
54
55   const char* my_hostname = simgrid::s4u::this_actor::get_host()->get_cname();
56   auto mailbox            = simgrid::s4u::Mailbox::by_name("master_mailbox");
57
58   TRACE_host_variable_set(my_hostname, "is_worker", 1);
59   TRACE_host_variable_set(my_hostname, "task_computation", 0);
60
61   while (true) {
62     const auto* task = static_cast<Task*>(mailbox->get());
63     if (task->name == "finalize") {
64       delete task;
65       break;
66     }
67     // adding the task's cost to the variable "task_computation"
68     TRACE_host_variable_add(my_hostname, "task_computation", task->flops);
69     simgrid::s4u::this_actor::exec_init(task->flops)
70         ->set_name(task->name)
71         ->set_tracing_category(task->category)
72         ->wait();
73     delete task;
74   }
75
76   XBT_DEBUG("Exiting now.");
77 }
78
79 int main(int argc, char* argv[])
80 {
81   simgrid::s4u::Engine e(&argc, argv);
82   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
83
84   e.load_platform(argv[1]);
85
86   // declaring user variables
87   TRACE_host_variable_declare("is_worker");
88   TRACE_host_variable_declare("is_master");
89   TRACE_host_variable_declare("task_creation");
90   TRACE_host_variable_declare("task_computation");
91
92   // declaring user markers and values
93   TRACE_declare_mark("msmark");
94   TRACE_declare_mark_value("msmark", "start_send_tasks");
95   TRACE_declare_mark_value("msmark", "finish_send_tasks");
96
97   // declaring user categories with RGB colors (values from 0 to 1)
98   TRACE_category_with_color("compute", "1 0 0");  // compute is red
99   TRACE_category_with_color("finalize", "0 1 0"); // finalize is green
100   // categories without user-defined colors receive random colors generated by the tracing system
101   TRACE_category("request");
102   TRACE_category_with_color("report", NULL);
103
104   e.register_function("master", &master);
105   e.register_function("worker", &worker);
106   e.load_deployment(argv[2]);
107
108   e.run();
109
110   XBT_DEBUG("Simulation is over");
111
112   unsigned int cursor;
113   xbt_dynar_t categories = TRACE_get_categories();
114   if (categories) {
115     XBT_INFO("Declared tracing categories:");
116     char* category;
117     xbt_dynar_foreach (categories, cursor, category) {
118       XBT_INFO("%s", category);
119     }
120     xbt_dynar_free(&categories);
121   }
122
123   xbt_dynar_t marks = TRACE_get_marks();
124   if (marks) {
125     XBT_INFO("Declared marks:");
126     char* mark;
127     xbt_dynar_foreach (marks, cursor, mark) {
128       XBT_INFO("%s", mark);
129     }
130     xbt_dynar_free(&marks);
131   }
132
133   return 0;
134 }