1 /* Copyright (c) 2010-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 #include "simgrid/Exception.hpp"
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 static std::vector<msg_task_t> tasks = std::vector<msg_task_t>();
13 static int seq_task(int /*argc*/, char* /*argv*/ [])
15 double task_comp_size = 5E7;
16 double task_comm_size = 1E6;
19 msg_task_t task = MSG_task_create("simple", task_comp_size, task_comm_size, NULL);
20 tasks.push_back(task);
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);
26 XBT_INFO("Executing task: \"%s\"", task->name);
27 MSG_task_execute(task);
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);
33 MSG_task_destroy(task);
34 XBT_INFO("Goodbye now!");
38 static int par_task(int /*argc*/, char* /*argv*/ [])
40 double * computation_amount = new double[2] {10E7, 10E7};
41 double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6};
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"));
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);
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);
55 XBT_INFO("Executing task: \"%s\"", task->name);
56 MSG_parallel_task_execute(task);
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);
62 MSG_task_destroy(task);
63 delete[] computation_amount;
64 delete[] communication_amount;
66 XBT_INFO("Goodbye now!");
70 static int get_progress(int /*argc*/, char* /*argv*/ [])
72 while (tasks.empty()) {
73 MSG_process_sleep(0.5);
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;
90 int main(int argc, char *argv[])
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]);
96 MSG_create_environment(argv[1]);
98 MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
100 MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
102 // Create a process to test in progress task
103 MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
105 msg_error_t res = MSG_main();
107 XBT_INFO("Simulation time %g", MSG_get_clock());
109 return res != MSG_OK;