Logo AND Algorithmique Numérique Distribuée

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