Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #237 from oar-team/upstream
[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   XBT_INFO("Goodbye now!");
35   return 0;
36 }
37
38 static int par_task(int /*argc*/, char* /*argv*/ [])
39 {
40   double * computation_amount = new double[2] {10E7, 10E7};
41   double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6};
42   double progress = 0;
43
44   std::vector<msg_host_t> hosts_to_use = std::vector<msg_host_t>();
45   hosts_to_use.push_back(MSG_get_host_by_name("Tremblay"));
46   hosts_to_use.push_back(MSG_get_host_by_name("Jupiter"));
47
48   msg_task_t task = MSG_parallel_task_create("ptask", 2, hosts_to_use.data(), computation_amount, communication_amount, NULL);
49   tasks.push_back(task);
50
51   XBT_INFO("get the progress of %s before the task starts", task->name);
52   progress = MSG_task_get_remaining_work_ratio(task);
53   xbt_assert(progress == 0, "Progress should be 0 not %f", progress);
54
55   XBT_INFO("Executing task: \"%s\"", task->name);
56   MSG_parallel_task_execute(task);
57
58   XBT_INFO("get the progress of %s after the task finishes", task->name);
59   progress = MSG_task_get_remaining_work_ratio(task);
60   xbt_assert(progress == 0, "Progress should be equal to 1 not %f", progress);
61
62   XBT_INFO("Goodbye now!");
63   return 0;
64 }
65
66 static int get_progress(int /*argc*/, char* /*argv*/ [])
67 {
68   while (tasks.empty()) {
69     MSG_process_sleep(0.5);
70   }
71   double progress;
72   for(auto const& task: tasks) {
73     double progress_prev = 1;
74     for (int i = 0; i < 3; i++) {
75       MSG_process_sleep(0.2);
76       progress = MSG_task_get_remaining_work_ratio(task);
77       xbt_assert(progress >= 0 and progress < 1, "Progress should be in [0, 1[, and not %f", progress);
78       xbt_assert(progress < progress_prev, "Progress should decrease, not increase");
79       XBT_INFO("Progress of \"%s\": %f", task->name, progress);
80       progress_prev = progress;
81     }
82   }
83   return 0;
84 }
85
86 int main(int argc, char *argv[])
87 {
88   MSG_init(&argc, argv);
89   MSG_config("host/model", "ptask_L07");
90   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../examples/platforms/two_hosts.xml\n", argv[0], argv[0]);
91
92   MSG_create_environment(argv[1]);
93
94   MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
95
96   MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
97
98   // Create a process to test in progress task
99   MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
100
101   msg_error_t res = MSG_main();
102
103   XBT_INFO("Simulation time %g", MSG_get_clock());
104
105   return res != MSG_OK;
106 }