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*/ [])
41 //double * computation_amount = xbt_new(double, nb_res);
42 double * computation_amount = new double[2] {10E7, 10E7};
43 //double * communication_amount = xbt_new(double, nb_res);
44 double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6};
47 std::vector<msg_host_t> hosts_to_use = std::vector<msg_host_t>();
48 hosts_to_use.push_back(MSG_get_host_by_name("Tremblay"));
49 hosts_to_use.push_back(MSG_get_host_by_name("Jupiter"));
51 msg_task_t task = MSG_parallel_task_create("ptask", 2, hosts_to_use.data(), computation_amount, communication_amount, NULL);
52 tasks.push_back(task);
54 XBT_INFO("get the progress of %s before the task starts", task->name);
55 progress = MSG_task_get_remaining_work_ratio(task);
56 xbt_assert(progress == 0, "Progress should be 0 not %f", progress);
58 XBT_INFO("Executing task: \"%s\"", task->name);
59 MSG_parallel_task_execute(task);
61 XBT_INFO("get the progress of %s after the task finishes", task->name);
62 progress = MSG_task_get_remaining_work_ratio(task);
63 xbt_assert(progress == 0, "Progress should be equal to 1 not %f", progress);
65 XBT_INFO("Goodbye now!");
69 static int get_progress(int /*argc*/, char* /*argv*/ [])
71 while (tasks.empty()) {
72 MSG_process_sleep(0.5);
74 for(auto const& task: tasks) {
76 double progress_prev = 1;
77 for (int i = 0; i < 3; i++) {
78 MSG_process_sleep(0.2);
79 progress = MSG_task_get_remaining_work_ratio(task);
80 xbt_assert(progress >= 0 and progress < 1, "Progress should be in [0, 1[, and not %f", progress);
81 xbt_assert(progress < progress_prev, "Progress should decrease, not increase");
82 XBT_INFO("Progress of \"%s\": %f", task->name, progress);
83 progress_prev = progress;
88 int main(int argc, char *argv[])
90 MSG_init(&argc, argv);
91 MSG_config("host/model", "ptask_L07");
92 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../examples/platforms/two_hosts.xml\n", argv[0], argv[0]);
94 MSG_create_environment(argv[1]);
96 MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
98 MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
100 // Create a process to test in progress task
101 MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
103 msg_error_t res = MSG_main();
105 XBT_INFO("Simulation time %g", MSG_get_clock());
107 return res != MSG_OK;