Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f540dd3bc6644a209a66e92d307ec8f36fb38e85
[simgrid.git] / examples / cpp / operation-simple / s4u-operation-simple.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/s4u.hpp"
17 #include "simgrid/plugins/operation.hpp"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(operation_simple, "Messages specific for this operation example");
20
21 int main(int argc, char* argv[])
22 {
23     simgrid::s4u::Engine e(&argc, argv);
24     e.load_platform(argv[1]);
25     simgrid::plugins::Operation::init();
26
27     // Retrieve hosts
28     auto tremblay = e.host_by_name("Tremblay");
29     auto jupiter = e.host_by_name("Jupiter");
30
31     // Create operations
32     auto exec1 = simgrid::plugins::ExecOp::create("exec1",1e9,tremblay);
33     auto exec2 = simgrid::plugins::ExecOp::create("exec2",1e9,jupiter);
34     auto comm = simgrid::plugins::CommOp::create("comm",1e7,tremblay,jupiter);
35
36     // Create the graph by defining dependencies between operations
37     exec1->add_successor(comm);
38     comm->add_successor(exec2);
39
40     // Add a function to be called when operations end for log purpose
41     std::vector<simgrid::plugins::OperationPtr> ops{exec1,exec2,comm};
42     for (auto op: ops)
43         op->on_end([](simgrid::plugins::Operation* op) {
44             XBT_INFO("Operation %s finished (%d)",op->get_name().c_str(), op->get_count());
45         });
46
47     // Enqueue two executions for operation exec1
48     exec1->enqueue_execs(2);
49
50     // Start the simulation
51     e.run();
52     return 0;
53 }