1 /* Copyright (c) 2007-2023. 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");
10 namespace sg4 = simgrid::s4u;
14 // Define an amount of work that should take 1 second to execute.
15 double computation_amount = sg4::this_actor::get_host()->get_speed();
17 std::vector<sg4::ExecPtr> pending_execs;
19 // + Two parents and a child
20 // + First parent ends after 1 second and the Second parent after 2 seconds.
21 sg4::ExecPtr first_parent = sg4::this_actor::exec_init(computation_amount);
22 pending_execs.push_back(first_parent);
23 sg4::ExecPtr second_parent = sg4::this_actor::exec_init(2 * computation_amount);
24 pending_execs.push_back(second_parent);
25 sg4::ExecPtr child = sg4::Exec::init()->set_flops_amount(computation_amount);
26 pending_execs.push_back(child);
28 // Name the activities (for logging purposes only)
29 first_parent->set_name("parent 1");
30 second_parent->set_name("parent 2");
31 child->set_name("child");
33 // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent
34 first_parent->add_successor(child);
35 second_parent->add_successor(child);
37 // Start the activities.
38 first_parent->start();
39 second_parent->start();
42 // wait for the completion of all activities
43 while (not pending_execs.empty()) {
44 ssize_t changed_pos = sg4::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 sg4::Engine e(&argc, argv);
53 e.load_platform(argv[1]);
55 sg4::Actor::create("worker", e.host_by_name("Fafard"), worker);
57 sg4::Exec::on_veto_cb([&e](sg4::Exec& exec) {
58 // First display the situation
59 XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
60 (exec.dependencies_solved() ? "solved" : "NOT solved"),
61 (exec.is_assigned() ? "assigned" : "NOT assigned"));
63 // In this simple case, we just assign the child task to a resource when its dependencies are solved
64 if (exec.dependencies_solved() && not exec.is_assigned()) {
65 XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec.get_cname());
66 exec.set_host(e.host_by_name("Fafard"));
72 XBT_INFO("Simulation time %g", sg4::Engine::get_clock());