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();
18 // + Two parents and a child
19 // + First parent ends after 1 second and the Second parent after 2 seconds.
20 sg4::ExecPtr first_parent = sg4::this_actor::exec_init(computation_amount);
21 sg4::ExecPtr second_parent = sg4::this_actor::exec_init(2 * computation_amount);
22 sg4::ExecPtr child = sg4::Exec::init()->set_flops_amount(computation_amount);
24 sg4::ActivitySet pending_execs ({first_parent, second_parent, child});
26 // Name the activities (for logging purposes only)
27 first_parent->set_name("parent 1");
28 second_parent->set_name("parent 2");
29 child->set_name("child");
31 // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent
32 first_parent->add_successor(child);
33 second_parent->add_successor(child);
35 // Start the activities.
36 first_parent->start();
37 second_parent->start();
40 // wait for the completion of all activities
41 while (not pending_execs.empty()) {
42 auto completed_one = pending_execs.wait_any();
43 if (completed_one != nullptr)
44 XBT_INFO("Exec '%s' is complete", completed_one->get_cname());
48 int main(int argc, char* argv[])
50 sg4::Engine e(&argc, argv);
51 e.load_platform(argv[1]);
53 sg4::Actor::create("worker", e.host_by_name("Fafard"), worker);
55 sg4::Exec::on_veto_cb([&e](sg4::Exec& exec) {
56 // First display the situation
57 XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
58 (exec.dependencies_solved() ? "solved" : "NOT solved"),
59 (exec.is_assigned() ? "assigned" : "NOT assigned"));
61 // In this simple case, we just assign the child task to a resource when its dependencies are solved
62 if (exec.dependencies_solved() && not exec.is_assigned()) {
63 XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec.get_cname());
64 exec.set_host(e.host_by_name("Fafard"));
70 XBT_INFO("Simulation time %g", sg4::Engine::get_clock());