Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / examples / cpp / task-storm / s4u-task-storm.cpp
1 /* Copyright (c) 2017-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 /* This example takes the main concepts of Apache Storm presented here
7    https://storm.apache.org/releases/2.4.0/Concepts.html and use them to build a simulation of a stream processing
8    application
9
10    Spout SA produces data every 100ms. The volume produced is alternatively 1e3, 1e6 and 1e9 bytes.
11    Spout SB produces 1e6 bytes every 200ms.
12
13    Bolt B1 and B2 processes data from Spout SA alternatively. The quantity of work to process this data is 10 flops per
14    bytes Bolt B3 processes data from Spout SB. Bolt B4 processes data from Bolt B3.
15
16                         Fafard
17                         ┌────┐
18                     ┌──►│ B1 │
19          Tremblay   │   └────┘
20           ┌────┐    │
21           │ SA ├────┤  Ginette
22           └────┘    │   ┌────┐
23                     └──►│ B2 │
24                         └────┘
25
26
27                        Bourassa
28          Jupiter     ┌──────────┐
29           ┌────┐     │          │
30           │ SB ├─────┤ B3 ──► B4│
31           └────┘     │          │
32                      └──────────┘
33  */
34
35 #include "simgrid/s4u.hpp"
36
37 XBT_LOG_NEW_DEFAULT_CATEGORY(task_storm, "Messages specific for this s4u example");
38 namespace sg4 = simgrid::s4u;
39
40 int main(int argc, char* argv[])
41 {
42   sg4::Engine e(&argc, argv);
43   e.load_platform(argv[1]);
44
45   // Retrieve hosts
46   auto tremblay = e.host_by_name("Tremblay");
47   auto jupiter  = e.host_by_name("Jupiter");
48   auto fafard   = e.host_by_name("Fafard");
49   auto ginette  = e.host_by_name("Ginette");
50   auto bourassa = e.host_by_name("Bourassa");
51
52   // Create execution tasks
53   auto SA = sg4::ExecTask::init("SA", tremblay->get_speed() * 0.1, tremblay);
54   auto SB = sg4::ExecTask::init("SB", jupiter->get_speed() * 0.2, jupiter);
55   auto B1 = sg4::ExecTask::init("B1", 1e8, fafard);
56   auto B2 = sg4::ExecTask::init("B2", 1e8, ginette);
57   auto B3 = sg4::ExecTask::init("B3", 1e8, bourassa);
58   auto B4 = sg4::ExecTask::init("B4", 2e8, bourassa);
59
60   // Create communication tasks
61   auto SA_to_B1 = sg4::CommTask::init("SA_to_B1", 0, tremblay, fafard);
62   auto SA_to_B2 = sg4::CommTask::init("SA_to_B2", 0, tremblay, ginette);
63   auto SB_to_B3 = sg4::CommTask::init("SB_to_B3", 1e6, jupiter, bourassa);
64
65   // Create the graph by defining dependencies between tasks
66   // Some dependencies are defined dynamically
67   SA_to_B1->add_successor(B1);
68   SA_to_B2->add_successor(B2);
69   SB->add_successor(SB_to_B3);
70   SB_to_B3->add_successor(B3);
71   B3->add_successor(B4);
72
73   /* Dynamic modification of the graph and bytes sent
74      Alternatively we: remove/add the link between SA and SA_to_B2
75                        add/remove the link between SA and SA_to_B1
76   */
77   SA->on_this_start_cb([SA_to_B1, SA_to_B2](sg4::Task* t) {
78     int count = t->get_count();
79     sg4::CommTaskPtr comm;
80     if (count % 2 == 0) {
81       t->remove_successor(SA_to_B2);
82       t->add_successor(SA_to_B1);
83       comm = SA_to_B1;
84     } else {
85       t->remove_successor(SA_to_B1);
86       t->add_successor(SA_to_B2);
87       comm = SA_to_B2;
88     }
89     std::vector<double> amount = {1e3, 1e6, 1e9};
90     comm->set_amount(amount[count % 3]);
91     auto token = std::make_shared<sg4::Token>();
92     token->set_data(new double(amount[count % 3]));
93     t->set_token(token);
94   });
95
96   // The token sent by SA is forwarded by both communication tasks
97   SA_to_B1->on_this_start_cb([&SA](sg4::Task* t) { t->set_token(t->get_next_token_from(SA)); });
98   SA_to_B2->on_this_start_cb([&SA](sg4::Task* t) { t->set_token(t->get_next_token_from(SA)); });
99
100   /* B1 and B2 read the value of the token received by their predecessors
101      and use it to adapt their amount of work to do.
102   */
103   B1->on_this_start_cb([SA_to_B1](sg4::Task* t) {
104     auto data = t->get_next_token_from(SA_to_B1)->get_unique_data<double>();
105     t->set_amount(*data * 10);
106   });
107   B2->on_this_start_cb([SA_to_B2](sg4::Task* t) {
108     auto data = t->get_next_token_from(SA_to_B2)->get_unique_data<double>();
109     t->set_amount(*data * 10);
110   });
111
112   // Enqueue firings for tasks without predecessors
113   SA->enqueue_firings(5);
114   SB->enqueue_firings(5);
115
116   // Add a function to be called when tasks end for log purpose
117   sg4::Task::on_completion_cb(
118       [](const sg4::Task* t) { XBT_INFO("Task %s finished (%d)", t->get_name().c_str(), t->get_count()); });
119
120   // Start the simulation
121   e.run();
122   return 0;
123 }