Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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   std::vector<sg4::ActivityPtr> pending_activities;
16
17   sg4::ExecPtr bob_compute = sg4::this_actor::exec_init(1e9);
18   pending_activities.push_back(boost::dynamic_pointer_cast<sg4::Activity>(bob_compute));
19   sg4::IoPtr bob_write = sg4::Host::current()->get_disks().front()->io_init(4000000, sg4::Io::OpType::WRITE);
20   pending_activities.push_back(boost::dynamic_pointer_cast<sg4::Activity>(bob_write));
21   sg4::IoPtr carl_read = sg4::Host::by_name("carl")->get_disks().front()->io_init(4000000, sg4::Io::OpType::READ);
22   pending_activities.push_back(boost::dynamic_pointer_cast<sg4::Activity>(carl_read));
23   sg4::ExecPtr carl_compute = sg4::Host::by_name("carl")->exec_init(1e9);
24   pending_activities.push_back(boost::dynamic_pointer_cast<sg4::Activity>(carl_compute));
25
26   // Name the activities (for logging purposes only)
27   bob_compute->set_name("bob compute");
28   bob_write->set_name("bob write");
29   carl_read->set_name("carl read");
30   carl_compute->set_name("carl compute");
31
32   // Create the dependencies:
33   // 'bob_write' is a successor of 'bob_compute'
34   // 'carl_read' is a successor of 'bob_write'
35   // 'carl_compute' is a successor of 'carl_read'
36   bob_compute->add_successor(bob_write);
37   bob_write->add_successor(carl_read);
38   carl_read->add_successor(carl_compute);
39
40   // Start the activities.
41   bob_compute->start();
42   bob_write->start();
43   carl_read->start();
44   carl_compute->start();
45
46   // wait for the completion of all activities
47   while (not pending_activities.empty()) {
48     ssize_t changed_pos = sg4::Activity::wait_any(pending_activities);
49     XBT_INFO("Activity '%s' is complete", pending_activities[changed_pos]->get_cname());
50     pending_activities.erase(pending_activities.begin() + changed_pos);
51   }
52 }
53
54 int main(int argc, char* argv[])
55 {
56   sg4::Engine e(&argc, argv);
57   e.load_platform(argv[1]);
58
59   sg4::Actor::create("bob", e.host_by_name("bob"), test);
60
61   e.run();
62
63   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
64
65   return 0;
66 }