Logo AND Algorithmique Numérique Distribuée

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