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 int master(int /*argc*/, char* /*argv*/ [])
13 double task_comp_size = 5E7;
14 double task_comm_size = 1E6;
17 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
18 XBT_INFO("Sending task: \"%s\"", MSG_task_get_name(task));
19 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
21 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
22 XBT_INFO("Canceling task \"%s\" directly", MSG_task_get_name(task));
23 MSG_task_cancel(task);
24 MSG_task_destroy(task);
26 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
27 XBT_INFO("Destroying task \"%s\" directly", MSG_task_get_name(task));
28 MSG_task_destroy(task);
30 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
31 msg_comm_t comm = MSG_task_isend(task, "worker_mailbox");
32 XBT_INFO("Canceling task \"%s\" during comm", MSG_task_get_name(task));
33 MSG_task_cancel(task);
34 if (MSG_comm_wait(comm, -1) != MSG_OK)
35 MSG_comm_destroy(comm);
36 MSG_task_destroy(task);
38 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
39 comm = MSG_task_isend(task, "worker_mailbox");
40 XBT_INFO("Destroying task \"%s\" during comm", MSG_task_get_name(task));
41 MSG_task_destroy(task);
42 if (MSG_comm_wait(comm, -1) != MSG_OK)
43 MSG_comm_destroy(comm);
45 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
46 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
48 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
49 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
51 XBT_INFO("Goodbye now!");
55 static int worker_main(int /*argc*/, char* /*argv*/ [])
57 msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
59 XBT_INFO("Start %s", MSG_task_get_name(task));
60 res = MSG_task_execute(task);
61 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
62 MSG_task_destroy(task);
66 static int worker(int /*argc*/, char* /*argv*/ [])
69 msg_task_t task = NULL;
70 XBT_ATTRIB_UNUSED int res = MSG_task_receive(&(task), "worker_mailbox");
71 xbt_assert(res == MSG_OK, "MSG_task_get failed");
72 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
74 if (not strcmp(MSG_task_get_name(task), "finalize")) {
75 XBT_INFO("Destroying task \"%s\"", MSG_task_get_name(task));
76 MSG_task_destroy(task);
80 if (not strcmp(MSG_task_get_name(task), "cancel")) {
81 MSG_process_create("worker1", worker_main, task, MSG_host_self());
82 MSG_process_sleep(0.1);
83 XBT_INFO("Canceling task \"%s\"", MSG_task_get_name(task));
84 MSG_task_cancel(task);
88 double start = MSG_get_clock();
89 MSG_task_execute(task);
90 double end = MSG_get_clock();
91 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
92 MSG_task_get_flops_amount(task));
94 MSG_task_destroy(task);
96 XBT_INFO("I'm done. See you!");
100 int main(int argc, char *argv[])
102 MSG_init(&argc, argv);
103 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
105 MSG_create_environment(argv[1]);
107 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
108 MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
110 msg_error_t res = MSG_main();
112 XBT_INFO("Simulation time %g", MSG_get_clock());
114 return res != MSG_OK;