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 == 1.0, "Progress should be 1.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.0, "Progress should be equal to 0.0 not %f", progress);
34 MSG_task_destroy(task);
35 XBT_INFO("Goodbye now!");
39 static int par_task(int /*argc*/, char* /*argv*/ [])
41 double * computation_amount = new double[2] {10E7, 10E7};
42 double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6};
45 std::vector<msg_host_t> hosts_to_use = std::vector<msg_host_t>();
46 hosts_to_use.push_back(MSG_get_host_by_name("Tremblay"));
47 hosts_to_use.push_back(MSG_get_host_by_name("Jupiter"));
49 msg_task_t task = MSG_parallel_task_create("ptask", 2, hosts_to_use.data(), computation_amount, communication_amount, NULL);
50 tasks.push_back(task);
52 XBT_INFO("get the progress of %s before the task starts", task->name);
53 progress = MSG_task_get_remaining_work_ratio(task);
54 xbt_assert(progress == 1.0, "Progress should be 1.0 not %f", progress);
56 XBT_INFO("Executing task: \"%s\"", task->name);
57 MSG_parallel_task_execute(task);
59 XBT_INFO("get the progress of %s after the task finishes", task->name);
60 progress = MSG_task_get_remaining_work_ratio(task);
61 xbt_assert(progress == 0.0, "Progress should be equal to 0.0 not %f", progress);
63 MSG_task_destroy(task);
64 delete[] computation_amount;
65 delete[] communication_amount;
67 XBT_INFO("Goodbye now!");
71 static int get_progress(int /*argc*/, char* /*argv*/ [])
73 while (tasks.empty()) {
74 MSG_process_sleep(0.5);
77 for(auto const& task: tasks) {
78 double progress_prev = 1;
79 for (int i = 0; i < 3; i++) {
80 MSG_process_sleep(0.2);
81 progress = MSG_task_get_remaining_work_ratio(task);
82 xbt_assert(progress >= 0 and progress < 1, "Progress should be in [0, 1[, and not %f", progress);
83 xbt_assert(progress < progress_prev, "Progress should decrease, not increase");
84 XBT_INFO("Progress of \"%s\": %f", task->name, progress);
85 progress_prev = progress;
91 int main(int argc, char *argv[])
93 MSG_init(&argc, argv);
94 MSG_config("host/model", "ptask_L07");
95 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../examples/platforms/two_hosts.xml\n", argv[0], argv[0]);
97 MSG_create_environment(argv[1]);
99 MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
101 MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
103 // Create a process to test in progress task
104 MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
106 msg_error_t res = MSG_main();
108 XBT_INFO("Simulation time %g", MSG_get_clock());
110 return res != MSG_OK;