Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
05968509705fd9113444985720f35903f926cb8d
[simgrid.git] / teshsuite / msg / task_progress / task_progress.cpp
1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/exception.hpp"
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static std::vector<msg_task_t> tasks = std::vector<msg_task_t>();
12
13 static int seq_task(int /*argc*/, char* /*argv*/ [])
14 {
15   double task_comp_size = 5E7;
16   double task_comm_size = 1E6;
17   double progress;
18
19   msg_task_t task = MSG_task_create("simple", task_comp_size, task_comm_size, NULL);
20   tasks.push_back(task);
21
22   XBT_INFO("get the progress of %s before the task starts", task->name);
23   progress = MSG_task_get_remaining_work_ratio(task);
24   xbt_assert(progress == 1.0, "Progress should be 1.0 not %f", progress);
25
26   XBT_INFO("Executing task: \"%s\"", task->name);
27   MSG_task_execute(task);
28
29   XBT_INFO("get the progress of %s after the task finishes", task->name);
30   progress = MSG_task_get_remaining_work_ratio(task);
31   xbt_assert(progress == 0.0, "Progress should be equal to 0.0 not %f", progress);
32
33   MSG_task_destroy(task);
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;
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 == 1.0, "Progress should be 1.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.0, "Progress should be equal to 0.0 not %f", progress);
61
62   MSG_task_destroy(task);
63   delete[] computation_amount;
64   delete[] communication_amount;
65
66   XBT_INFO("Goodbye now!");
67   return 0;
68 }
69
70 static int get_progress(int /*argc*/, char* /*argv*/ [])
71 {
72   while (tasks.empty()) {
73     MSG_process_sleep(0.5);
74   }
75   double progress;
76   for(auto const& task: tasks) {
77     double progress_prev = 1;
78     for (int i = 0; i < 3; i++) {
79       MSG_process_sleep(0.2);
80       progress = MSG_task_get_remaining_work_ratio(task);
81       xbt_assert(progress >= 0 and progress < 1, "Progress should be in [0, 1[, and not %f", progress);
82       xbt_assert(progress < progress_prev, "Progress should decrease, not increase");
83       XBT_INFO("Progress of \"%s\": %f", task->name, progress);
84       progress_prev = progress;
85     }
86   }
87   return 0;
88 }
89
90 int main(int argc, char *argv[])
91 {
92   MSG_init(&argc, argv);
93   MSG_config("host/model", "ptask_L07");
94   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../examples/platforms/two_hosts.xml\n", argv[0], argv[0]);
95
96   MSG_create_environment(argv[1]);
97
98   MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
99
100   MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
101
102   // Create a process to test in progress task
103   MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
104
105   msg_error_t res = MSG_main();
106
107   XBT_INFO("Simulation time %g", MSG_get_clock());
108
109   return res != MSG_OK;
110 }