1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/s4u.hpp"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
13 // Define an amount of work that should take 1 second to execute.
14 double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
16 std::vector<simgrid::s4u::ExecPtr> pending_execs;
18 // + Two parents and a child
19 // + First parent ends after 1 second and the Second parent after 2 seconds.
20 simgrid::s4u::ExecPtr first_parent = simgrid::s4u::this_actor::exec_init(computation_amount);
21 pending_execs.push_back(first_parent);
22 simgrid::s4u::ExecPtr second_parent = simgrid::s4u::this_actor::exec_init(2 * computation_amount);
23 pending_execs.push_back(second_parent);
24 simgrid::s4u::ExecPtr child = simgrid::s4u::Exec::init()->set_flops_amount(computation_amount);
25 pending_execs.push_back(child);
27 // Name the activities (for logging purposes only)
28 first_parent->set_name("parent 1");
29 second_parent->set_name("parent 2");
30 child->set_name("child");
32 // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent
33 first_parent->add_successor(child);
34 second_parent->add_successor(child);
36 // Start the activities.
37 first_parent->start();
38 second_parent->start();
39 // child uses a vetoable start to force it to wait for the completion of its predecessors
40 child->vetoable_start();
42 // wait for the completion of all activities
43 while (not pending_execs.empty()) {
44 ssize_t changed_pos = simgrid::s4u::Exec::wait_any_for(pending_execs, -1);
45 XBT_INFO("Exec '%s' is complete", pending_execs[changed_pos]->get_cname());
46 pending_execs.erase(pending_execs.begin() + changed_pos);
50 int main(int argc, char* argv[])
52 simgrid::s4u::Engine e(&argc, argv);
53 e.load_platform(argv[1]);
55 simgrid::s4u::Actor::create("worker", e.host_by_name("Fafard"), worker);
57 simgrid::s4u::Activity::on_veto.connect([&e](simgrid::s4u::Activity& a) {
58 auto& exec = static_cast<simgrid::s4u::Exec&>(a);
60 // First display the situation
61 XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
62 (exec.dependencies_solved() ? "solved" : "NOT solved"),
63 (exec.is_assigned() ? "assigned" : "NOT assigned"));
65 // In this simple case, we just assign the child task to a resource when its dependencies are solved
66 if (exec.dependencies_solved() && not exec.is_assigned()) {
67 XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec.get_cname());
68 exec.set_host(e.host_by_name("Fafard"));
74 XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());