1 /* Copyright (c) 2017-2023. 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 /* This example takes the main concepts of Apache Storm presented here https://storm.apache.org/releases/2.4.0/Concepts.html
7 and use them to build a simulation of a stream processing application
9 Spout SA produces data every 100ms. The volume produced is alternatively 1e3, 1e6 and 1e9 bytes.
10 Spout SB produces 1e6 bytes every 200ms.
12 Bolt B1 and B2 processes data from Spout SA alternatively. The quantity of work to process this data is 10 flops per bytes
13 Bolt B3 processes data from Spout SB.
14 Bolt B4 processes data from Bolt B3.
30 │ SB ├─────┤ B3 ──► B4│
35 #include "simgrid/plugins/task.hpp"
36 #include "simgrid/s4u.hpp"
38 XBT_LOG_NEW_DEFAULT_CATEGORY(task_storm, "Messages specific for this s4u example");
40 int main(int argc, char* argv[])
42 simgrid::s4u::Engine e(&argc, argv);
43 e.load_platform(argv[1]);
44 simgrid::plugins::Task::init();
47 auto tremblay = e.host_by_name("Tremblay");
48 auto jupiter = e.host_by_name("Jupiter");
49 auto fafard = e.host_by_name("Fafard");
50 auto ginette = e.host_by_name("Ginette");
51 auto bourassa = e.host_by_name("Bourassa");
53 // Create execution tasks
54 auto SA = simgrid::plugins::ExecTask::init("SA", tremblay->get_speed() * 0.1, tremblay);
55 auto SB = simgrid::plugins::ExecTask::init("SB", jupiter->get_speed() * 0.2, jupiter);
56 auto B1 = simgrid::plugins::ExecTask::init("B1", 1e8, fafard);
57 auto B2 = simgrid::plugins::ExecTask::init("B2", 1e8, ginette);
58 auto B3 = simgrid::plugins::ExecTask::init("B3", 1e8, bourassa);
59 auto B4 = simgrid::plugins::ExecTask::init("B4", 2e8, bourassa);
61 // Create communication tasks
62 auto SA_to_B1 = simgrid::plugins::CommTask::init("SA_to_B1", 0, tremblay, fafard);
63 auto SA_to_B2 = simgrid::plugins::CommTask::init("SA_to_B2", 0, tremblay, ginette);
64 auto SB_to_B3 = simgrid::plugins::CommTask::init("SB_to_B3", 1e6, jupiter, bourassa);
66 // Create the graph by defining dependencies between tasks
67 // Some dependencies are defined dynamically
68 SA_to_B1->add_successor(B1);
69 SA_to_B2->add_successor(B2);
70 SB->add_successor(SB_to_B3);
71 SB_to_B3->add_successor(B3);
72 B3->add_successor(B4);
74 /* Dynamic modification of the graph and bytes sent
75 Alternatively we: remove/add the link between SA and SA_to_B2
76 add/remove the link between SA and SA_to_B1
78 SA->on_this_start_cb([&](simgrid::plugins::Task* t) {
79 int count = t->get_count();
80 simgrid::plugins::CommTaskPtr comm;
82 t->remove_successor(SA_to_B2);
83 t->add_successor(SA_to_B1);
87 t->remove_successor(SA_to_B1);
88 t->add_successor(SA_to_B2);
91 std::vector<double> amount = {1e3,1e6,1e9};
92 comm->set_amount(amount[count % 3]);
93 auto token = std::make_shared<simgrid::plugins::Token>();
94 token->set_data(new double(amount[count % 3]));
98 // The token sent by SA is forwarded by both communication tasks
99 SA_to_B1->on_this_start_cb([&](simgrid::plugins::Task* t) {
100 t->set_token(t->get_next_token_from(SA));
102 SA_to_B2->on_this_start_cb([&](simgrid::plugins::Task* t) {
103 t->set_token(t->get_next_token_from(SA));
106 /* B1 and B2 read the value of the token received by their predecessors
107 and use it to adapt their amount of work to do.
109 B1->on_this_start_cb([&](simgrid::plugins::Task* t) {
110 auto data = t->get_next_token_from(SA_to_B1)->get_data<double>();
111 t->set_amount(*data * 10);
114 B2->on_this_start_cb([&](simgrid::plugins::Task* t) {
115 auto data = t->get_next_token_from(SA_to_B2)->get_data<double>();
116 t->set_amount(*data * 10);
120 // Enqueue executions for tasks without predecessors
121 SA->enqueue_execs(5);
122 SB->enqueue_execs(5);
124 // Add a function to be called when tasks end for log purpose
125 simgrid::plugins::Task::on_end_cb([]
126 (const simgrid::plugins::Task* t) {
127 XBT_INFO("Task %s finished (%d)", t->get_name().c_str(), t->get_count());
130 // Start the simulation