Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no need to cast with ActivitySet
[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 ({bob_compute, bob_write, carl_read, carl_compute});
21
22   // Name the activities (for logging purposes only)
23   bob_compute->set_name("bob compute");
24   bob_write->set_name("bob write");
25   carl_read->set_name("carl read");
26   carl_compute->set_name("carl compute");
27
28   // Create the dependencies:
29   // 'bob_write' is a successor of 'bob_compute'
30   // 'carl_read' is a successor of 'bob_write'
31   // 'carl_compute' is a successor of 'carl_read'
32   bob_compute->add_successor(bob_write);
33   bob_write->add_successor(carl_read);
34   carl_read->add_successor(carl_compute);
35
36   // Start the activities.
37   bob_compute->start();
38   bob_write->start();
39   carl_read->start();
40   carl_compute->start();
41
42   // wait for the completion of all activities
43   while (not pending_activities.empty()) {
44     auto completed_one = pending_activities.wait_any();
45     if (completed_one != nullptr)
46       XBT_INFO("Activity '%s' is complete", completed_one->get_cname());
47   }
48 }
49
50 int main(int argc, char* argv[])
51 {
52   sg4::Engine e(&argc, argv);
53   e.load_platform(argv[1]);
54
55   sg4::Actor::create("bob", e.host_by_name("bob"), test);
56
57   e.run();
58
59   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
60
61   return 0;
62 }