Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
improve dag-comm example
[simgrid.git] / examples / cpp / dag-comm / s4u-dag-comm.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/plugins/file_system.h>
7 #include <simgrid/s4u.hpp>
8 #include <vector>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11
12 int main(int argc, char* argv[])
13 {
14   simgrid::s4u::Engine e(&argc, argv);
15   sg_storage_file_system_init();
16   e.load_platform(argv[1]);
17
18   auto tremblay = e.host_by_name("Tremblay");
19   auto jupiter  = e.host_by_name("Jupiter");
20
21   // Display the details on vetoed activities
22   simgrid::s4u::Activity::on_veto.connect([](simgrid::s4u::Activity& a) {
23     XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", a.get_cname(),
24              (a.dependencies_solved() ? "solved" : "NOT solved"), (a.is_assigned() ? "assigned" : "NOT assigned"));
25   });
26
27   simgrid::s4u::Activity::on_completion.connect([](simgrid::s4u::Activity& activity) {
28     auto* exec = dynamic_cast<simgrid::s4u::Exec*>(&activity);
29     if (exec != nullptr)
30       XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", exec->get_cname(), exec->get_start_time(),
31                exec->get_finish_time());
32     auto* comm = dynamic_cast<simgrid::s4u::Comm*>(&activity);
33     if (comm != nullptr)
34       XBT_INFO("Activity '%s' is complete", comm->get_cname());
35   });
36
37   // Create a small DAG: parent->transfert->child
38   simgrid::s4u::ExecPtr parent    = simgrid::s4u::Exec::init();
39   simgrid::s4u::CommPtr transfert = simgrid::s4u::Comm::sendto_init();
40   simgrid::s4u::ExecPtr child     = simgrid::s4u::Exec::init();
41   parent->add_successor(transfert);
42   transfert->add_successor(child);
43
44   // Set the parameters (the name is for logging purposes only)
45   // + parent and child end after 1 second
46   parent->set_name("parent")->set_flops_amount(tremblay->get_speed())->vetoable_start();
47   transfert->set_name("transfert")->set_payload_size(125e6)->vetoable_start();
48   child->set_name("child")->set_flops_amount(jupiter->get_speed())->vetoable_start();
49
50   // Schedule the different activities
51   parent->set_host(tremblay);
52   transfert->set_from(tremblay);
53   child->set_host(jupiter);
54   transfert->set_to(jupiter);
55
56   e.run();
57
58   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
59
60   return 0;
61 }