Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use ssize_t for Exec::wait_any and Io::wait_any too.
[simgrid.git] / examples / cpp / io-dependent / s4u-io-dependent.cpp
1 /* Copyright (c) 2007-2021. 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
12 static void test()
13 {
14   std::vector<simgrid::s4u::IoPtr> pending_ios;
15
16   simgrid::s4u::ExecPtr bob_compute = simgrid::s4u::this_actor::exec_init(1e9);
17   simgrid::s4u::IoPtr bob_write =
18       simgrid::s4u::Host::current()->get_disks().front()->io_init(4000000, simgrid::s4u::Io::OpType::WRITE);
19   pending_ios.push_back(bob_write);
20   simgrid::s4u::IoPtr carl_read =
21       simgrid::s4u::Host::by_name("carl")->get_disks().front()->io_init(4000000, simgrid::s4u::Io::OpType::READ);
22   pending_ios.push_back(carl_read);
23   simgrid::s4u::ExecPtr carl_compute = simgrid::s4u::Host::by_name("carl")->exec_init(1e9);
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->vetoable_start();
42   carl_read->vetoable_start();
43   carl_compute->vetoable_start();
44
45   // wait for the completion of all activities
46   bob_compute->wait();
47   while (not pending_ios.empty()) {
48     ssize_t changed_pos = simgrid::s4u::Io::wait_any(pending_ios);
49     XBT_INFO("Io '%s' is complete", pending_ios[changed_pos]->get_cname());
50     pending_ios.erase(pending_ios.begin() + changed_pos);
51   }
52   carl_compute->wait();
53 }
54
55 int main(int argc, char* argv[])
56 {
57   simgrid::s4u::Engine e(&argc, argv);
58   sg_storage_file_system_init();
59   e.load_platform(argv[1]);
60
61   simgrid::s4u::Actor::create("bob", simgrid::s4u::Host::by_name("bob"), test);
62
63   e.run();
64
65   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
66
67   return 0;
68 }