1 /* Copyright (c) 2017-2019. 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 /* Parallel tasks are convenient abstractions of parallel computational kernels that span over several machines.
7 * To create a new one, you have to provide several things:
8 * - a vector of hosts on which the task will execute
9 * - a vector of values, the amount of computation for each of the hosts (in flops)
10 * - a matrix of values, the amount of communication between each pair of hosts (in bytes)
12 * Each of these operation will be processed at the same relative speed.
13 * This means that at some point in time, all sub-executions and all sub-communications will be at 20% of completion.
14 * Also, they will all complete at the exact same time.
16 * This is obviously a simplistic abstraction, but this is very handful in a large amount of situations.
18 * Please note that you must have the LV07 platform model enabled to use such constructs.
21 #include <simgrid/s4u.hpp>
23 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_ptask, "Messages specific for this s4u example");
27 /* Retrieve the list of all hosts as an array of hosts */
28 auto hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts();
29 size_t hosts_count = hosts.size();
31 XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
32 "and 10MB to exchange between each pair");
34 std::vector<double> computation_amounts;
35 std::vector<double> communication_amounts;
37 /* ------[ test 1 ]----------------- */
38 computation_amounts.assign(hosts_count, 1e9 /*1Gflop*/);
39 communication_amounts.assign(hosts_count * hosts_count, 0);
40 for (size_t i = 0; i < hosts_count; i++)
41 for (size_t j = i + 1; j < hosts_count; j++)
42 communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
44 simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
46 /* ------[ test 2 ]----------------- */
47 XBT_INFO("We can do the same with a timeout of 10 seconds enabled.");
48 computation_amounts.assign(hosts_count, 1e9 /*1Gflop*/);
49 communication_amounts.assign(hosts_count * hosts_count, 0);
50 for (size_t i = 0; i < hosts_count; i++)
51 for (size_t j = i + 1; j < hosts_count; j++)
52 communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
55 simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts)
56 ->wait_for(10.0 /* timeout (in seconds)*/);
57 xbt_die("Woops, this did not timeout as expected... Please report that bug.");
58 } catch (const simgrid::TimeoutException&) {
59 XBT_INFO("Caught the expected timeout exception.");
62 /* ------[ test 3 ]----------------- */
63 XBT_INFO("Then, build a parallel task involving only computations (of different amounts) and no communication");
64 computation_amounts = {3e8, 6e8, 1e9}; // 300Mflop, 600Mflop, 1Gflop
65 communication_amounts.clear(); // no comm
66 simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
68 /* ------[ test 4 ]----------------- */
69 XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
70 computation_amounts.clear();
71 communication_amounts.clear();
72 simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
74 /* ------[ test 5 ]----------------- */
75 XBT_INFO("Then, Monitor the execution of a parallel task");
76 computation_amounts.assign(hosts_count, 1e6 /*1Mflop*/);
77 communication_amounts = {0, 1e6, 0, 0, 0, 1e6, 1e6, 0, 0};
78 simgrid::s4u::ExecPtr activity =
79 simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts);
82 while (not activity->test()) {
83 XBT_INFO("Remaining flop ratio: %.0f%%", 100 * activity->get_remaining_ratio());
84 simgrid::s4u::this_actor::sleep_for(5);
88 XBT_INFO("Goodbye now!");
91 int main(int argc, char* argv[])
93 simgrid::s4u::Engine e(&argc, argv);
95 xbt_assert(argc == 2, "Usage: %s <platform file>", argv[0]);
97 e.load_platform(argv[1]);
98 simgrid::s4u::Actor::create("test", simgrid::s4u::Host::by_name("MyHost1"), runner);
101 XBT_INFO("Simulation done.");