Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4772376fa3301730e5db5b81d48a7f9c06efd9f0
[simgrid.git] / examples / cpp / io-dependent / s4u-io-dependent.cpp
1 /* Copyright (c) 2007-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/s4u.hpp"
7 #include <simgrid/plugins/file_system.h>
8 #include <vector>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11 namespace sg4 = simgrid::s4u;
12
13 static void test()
14 {
15   sg4::ExecPtr bob_compute = sg4::this_actor::exec_init(1e9);
16   sg4::IoPtr bob_write = sg4::Host::current()->get_disks().front()->io_init(4000000, sg4::Io::OpType::WRITE);
17   sg4::IoPtr carl_read = sg4::Host::by_name("carl")->get_disks().front()->io_init(4000000, sg4::Io::OpType::READ);
18   sg4::ExecPtr carl_compute = sg4::Host::by_name("carl")->exec_init(1e9);
19
20   sg4::ActivitySet pending_activities ({boost::dynamic_pointer_cast<sg4::Activity>(bob_compute),
21                                         boost::dynamic_pointer_cast<sg4::Activity>(bob_write),
22                                         boost::dynamic_pointer_cast<sg4::Activity>(carl_read),
23                                         boost::dynamic_pointer_cast<sg4::Activity>(carl_compute)});
24
25   // Name the activities (for logging purposes only)
26   bob_compute->set_name("bob compute");
27   bob_write->set_name("bob write");
28   carl_read->set_name("carl read");
29   carl_compute->set_name("carl compute");
30
31   // Create the dependencies:
32   // 'bob_write' is a successor of 'bob_compute'
33   // 'carl_read' is a successor of 'bob_write'
34   // 'carl_compute' is a successor of 'carl_read'
35   bob_compute->add_successor(bob_write);
36   bob_write->add_successor(carl_read);
37   carl_read->add_successor(carl_compute);
38
39   // Start the activities.
40   bob_compute->start();
41   bob_write->start();
42   carl_read->start();
43   carl_compute->start();
44
45   // wait for the completion of all activities
46   while (not pending_activities.empty()) {
47     auto completed_one = pending_activities.wait_any();
48     if (completed_one != nullptr)
49       XBT_INFO("Activity '%s' is complete", completed_one->get_cname());
50   }
51 }
52
53 int main(int argc, char* argv[])
54 {
55   sg4::Engine e(&argc, argv);
56   e.load_platform(argv[1]);
57
58   sg4::Actor::create("bob", e.host_by_name("bob"), test);
59
60   e.run();
61
62   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
63
64   return 0;
65 }