Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add Io operations and an example
[simgrid.git] / examples / cpp / operation-io / s4u-operation-io.cpp
1 /* Copyright (c) 2017-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 /* This example demonstrate basic use of the operation plugin.
7  *
8  * We model the following graph:
9  *
10  * exec1 -> comm -> exec2
11  *
12  * exec1 and exec2 are execution operations.
13  * comm is a communication operation.
14  */
15
16 #include <simgrid/plugins/file_system.h>
17 #include "simgrid/plugins/operation.hpp"
18 #include "simgrid/s4u.hpp"
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(operation_simple, "Messages specific for this operation example");
21
22 int main(int argc, char* argv[])
23 {
24   simgrid::s4u::Engine e(&argc, argv);
25   sg_storage_file_system_init();
26   e.load_platform(argv[1]);
27   simgrid::plugins::Operation::init();
28
29   // Retrieve hosts
30   auto bob  = e.host_by_name("bob");
31   auto carl = e.host_by_name("carl");
32
33   // Create operations
34   auto exec1 = simgrid::plugins::ExecOp::init("exec1", 1e9, bob);
35   auto exec2 = simgrid::plugins::ExecOp::init("exec2", 1e9, carl);
36   auto write = simgrid::plugins::IoOp::init("write", 1e7, bob->get_disks().front(), simgrid::s4u::Io::OpType::WRITE);
37   auto read  = simgrid::plugins::IoOp::init("read", 1e7, carl->get_disks().front(), simgrid::s4u::Io::OpType::READ);
38
39   // Create the graph by defining dependencies between operations
40   exec1->add_successor(write);
41   write->add_successor(read);
42   read->add_successor(exec2);
43
44   // Add a function to be called when operations end for log purpose
45   simgrid::plugins::Operation::on_end_cb([](const simgrid::plugins::Operation* op) {
46     XBT_INFO("Operation %s finished (%d)", op->get_name().c_str(), op->get_count());
47   });
48
49   // Enqueue two executions for operation exec1
50   exec1->enqueue_execs(2);
51
52   // Start the simulation
53   e.run();
54   return 0;
55 }