1 /* Copyright (c) 2010-2018. 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. */
7 #include "simgrid/msg.h"
9 #include <stdio.h> /* snprintf */
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
13 static std::vector<msg_task_t> tasks = std::vector<msg_task_t>();
15 static int seq_task(int /*argc*/, char* /*argv*/ [])
17 double task_comp_size = 5E7;
18 double task_comm_size = 1E6;
21 msg_task_t task = MSG_task_create("simple", task_comp_size, task_comm_size, NULL);
22 tasks.push_back(task);
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);
28 XBT_INFO("Executing task: \"%s\"", task->name);
29 MSG_task_execute(task);
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);
35 MSG_task_destroy(task);
36 XBT_INFO("Goodbye now!");
40 static int par_task(int /*argc*/, char* /*argv*/ [])
42 double * computation_amount = new double[2] {10E7, 10E7};
43 double * communication_amount = new double[4] {1E6, 1E6, 1E6, 1E6};
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"));
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);
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);
57 XBT_INFO("Executing task: \"%s\"", task->name);
58 MSG_parallel_task_execute(task);
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);
64 MSG_task_destroy(task);
65 delete[] computation_amount;
66 delete[] communication_amount;
68 XBT_INFO("Goodbye now!");
72 static int get_progress(int /*argc*/, char* /*argv*/ [])
74 while (tasks.empty()) {
75 MSG_process_sleep(0.5);
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;
92 int main(int argc, char *argv[])
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]);
98 MSG_create_environment(argv[1]);
100 MSG_process_create("sequential", seq_task, NULL, MSG_get_host_by_name("Tremblay"));
102 MSG_process_create("parallel", par_task, NULL, MSG_get_host_by_name("Tremblay"));
104 // Create a process to test in progress task
105 MSG_process_create("get_progress", get_progress, NULL, MSG_get_host_by_name("Tremblay"));
107 msg_error_t res = MSG_main();
109 XBT_INFO("Simulation time %g", MSG_get_clock());
111 return res != MSG_OK;