1 /* Copyright (c) 2010-2022. 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/msg.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10 static int master(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
12 double task_comp_size = 5E7;
13 double task_comm_size = 1E6;
16 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
17 XBT_INFO("Sending task: \"%s\"", MSG_task_get_name(task));
18 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
20 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
21 XBT_INFO("Canceling task \"%s\" directly", MSG_task_get_name(task));
22 MSG_task_cancel(task);
23 MSG_task_destroy(task);
25 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
26 XBT_INFO("Destroying task \"%s\" directly", MSG_task_get_name(task));
27 MSG_task_destroy(task);
29 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
30 msg_comm_t comm = MSG_task_isend(task, "worker_mailbox");
31 XBT_INFO("Canceling task \"%s\" during comm", MSG_task_get_name(task));
32 MSG_task_cancel(task);
33 if (MSG_comm_wait(comm, -1) != MSG_OK)
34 MSG_comm_destroy(comm);
35 MSG_task_destroy(task);
37 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
38 comm = MSG_task_isend(task, "worker_mailbox");
39 XBT_INFO("Destroying task \"%s\" during comm", MSG_task_get_name(task));
40 MSG_task_destroy(task);
41 if (MSG_comm_wait(comm, -1) != MSG_OK)
42 MSG_comm_destroy(comm);
44 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
45 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
47 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
48 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
50 XBT_INFO("Goodbye now!");
54 static int worker_main(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
56 msg_task_t task = (msg_task_t)MSG_process_get_data(MSG_process_self());
58 XBT_INFO("Start %s", MSG_task_get_name(task));
59 res = MSG_task_execute(task);
60 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
61 MSG_task_destroy(task);
65 static int worker(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
68 msg_task_t task = NULL;
69 xbt_assert(MSG_task_receive(&(task), "worker_mailbox") == MSG_OK, "MSG_task_receive failed");
70 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
72 if (!strcmp(MSG_task_get_name(task), "finalize")) {
73 XBT_INFO("Destroying task \"%s\"", MSG_task_get_name(task));
74 MSG_task_destroy(task);
78 if (!strcmp(MSG_task_get_name(task), "cancel")) {
79 MSG_process_create("worker1", worker_main, task, MSG_host_self());
80 MSG_process_sleep(0.1);
81 XBT_INFO("Canceling task \"%s\"", MSG_task_get_name(task));
82 MSG_task_cancel(task);
86 double start = MSG_get_clock();
87 MSG_task_execute(task);
88 double end = MSG_get_clock();
89 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
90 MSG_task_get_flops_amount(task));
92 MSG_task_destroy(task);
94 XBT_INFO("I'm done. See you!");
98 int main(int argc, char* argv[])
100 MSG_init(&argc, argv);
101 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
103 MSG_create_environment(argv[1]);
105 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
106 MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
108 msg_error_t res = MSG_main();
110 XBT_INFO("Simulation time %g", MSG_get_clock());
112 return res != MSG_OK;