1 /* Copyright (c) 2010-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "simgrid/msg.h"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
12 static std::vector<msg_task_t> tasks = std::vector<msg_task_t>();
14 static int seq_task(int /*argc*/, char* /*argv*/ [])
16 double task_comp_size = 5E7;
17 double task_comm_size = 1E6;
20 msg_task_t task = MSG_task_create("simple", task_comp_size, task_comm_size, NULL);
21 tasks.push_back(task);
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);
27 XBT_INFO("Executing task: \"%s\"", task->name);
28 MSG_task_execute(task);
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);
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 == 0, "Progress should be 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, "Progress should be equal to 1 not %f", progress);
62 XBT_INFO("Goodbye now!");
66 static int get_progress(int /*argc*/, char* /*argv*/ [])
68 while (tasks.empty()) {
69 MSG_process_sleep(0.5);
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;
86 int main(int argc, char *argv[])
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]);
92 MSG_create_environment(argv[1]);
94 MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
96 MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
98 // Create a process to test in progress task
99 MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
101 msg_error_t res = MSG_main();
103 XBT_INFO("Simulation time %g", MSG_get_clock());
105 return res != MSG_OK;