Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve the doc of MSG_task_*_bounded
[simgrid.git] / teshsuite / msg / task_progress / task_progress.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <xbt/ex.hpp>
8 #include "simgrid/msg.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 static std::vector<msg_task_t> tasks = std::vector<msg_task_t>();
13
14 static int seq_task(int /*argc*/, char* /*argv*/ [])
15 {
16   double task_comp_size = 5E7;
17   double task_comm_size = 1E6;
18   double progress = 0;
19
20   msg_task_t task = MSG_task_create("simple", task_comp_size, task_comm_size, NULL);
21   tasks.push_back(task);
22
23   XBT_INFO("get the progress of %s before the task starts", task->name);
24   progress = MSG_task_get_remaining_work_ratio(task);
25   xbt_assert(progress == 0, "Progress should be 0 not %f", progress);
26
27   XBT_INFO("Executing task: \"%s\"", task->name);
28   MSG_task_execute(task);
29
30   XBT_INFO("get the progress of %s after the task finishes", task->name);
31   progress = MSG_task_get_remaining_work_ratio(task);
32   xbt_assert(progress == 0, "Progress should be equal to 1 not %f", progress);
33
34   MSG_task_destroy(task);
35   XBT_INFO("Goodbye now!");
36   return 0;
37 }
38
39 static int par_task(int /*argc*/, char* /*argv*/ [])
40 {
41   double * computation_amount = new double[2] {10E7, 10E7};
42   double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6};
43   double progress = 0;
44
45   std::vector<msg_host_t> hosts_to_use = std::vector<msg_host_t>();
46   hosts_to_use.push_back(MSG_get_host_by_name("Tremblay"));
47   hosts_to_use.push_back(MSG_get_host_by_name("Jupiter"));
48
49   msg_task_t task = MSG_parallel_task_create("ptask", 2, hosts_to_use.data(), computation_amount, communication_amount, NULL);
50   tasks.push_back(task);
51
52   XBT_INFO("get the progress of %s before the task starts", task->name);
53   progress = MSG_task_get_remaining_work_ratio(task);
54   xbt_assert(progress == 0, "Progress should be 0 not %f", progress);
55
56   XBT_INFO("Executing task: \"%s\"", task->name);
57   MSG_parallel_task_execute(task);
58
59   XBT_INFO("get the progress of %s after the task finishes", task->name);
60   progress = MSG_task_get_remaining_work_ratio(task);
61   xbt_assert(progress == 0, "Progress should be equal to 1 not %f", progress);
62
63   MSG_task_destroy(task);
64   delete[] computation_amount;
65   delete[] communication_amount;
66
67   XBT_INFO("Goodbye now!");
68   return 0;
69 }
70
71 static int get_progress(int /*argc*/, char* /*argv*/ [])
72 {
73   while (tasks.empty()) {
74     MSG_process_sleep(0.5);
75   }
76   double progress;
77   for(auto const& task: tasks) {
78     double progress_prev = 1;
79     for (int i = 0; i < 3; i++) {
80       MSG_process_sleep(0.2);
81       progress = MSG_task_get_remaining_work_ratio(task);
82       xbt_assert(progress >= 0 and progress < 1, "Progress should be in [0, 1[, and not %f", progress);
83       xbt_assert(progress < progress_prev, "Progress should decrease, not increase");
84       XBT_INFO("Progress of \"%s\": %f", task->name, progress);
85       progress_prev = progress;
86     }
87   }
88   return 0;
89 }
90
91 int main(int argc, char *argv[])
92 {
93   MSG_init(&argc, argv);
94   MSG_config("host/model", "ptask_L07");
95   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../examples/platforms/two_hosts.xml\n", argv[0], argv[0]);
96
97   MSG_create_environment(argv[1]);
98
99   MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
100
101   MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
102
103   // Create a process to test in progress task
104   MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
105
106   msg_error_t res = MSG_main();
107
108   XBT_INFO("Simulation time %g", MSG_get_clock());
109
110   return res != MSG_OK;
111 }