1 /* Copyright (c) 2010-2015. 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 int master(int argc, char *argv[])
14 double task_comp_size = 5E7;
15 double task_comm_size = 1E6;
18 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
19 XBT_INFO("Sending task: \"%s\"", task->name);
20 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
22 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
23 XBT_INFO("Canceling task \"%s\" directly", task->name);
24 MSG_task_cancel(task);
25 MSG_task_destroy(task);
27 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
28 XBT_INFO("Destroying task \"%s\" directly", task->name);
29 MSG_task_destroy(task);
31 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
32 msg_comm_t comm = MSG_task_isend(task, "worker_mailbox");
33 XBT_INFO("Canceling task \"%s\" during comm", task->name);
34 MSG_task_cancel(task);
36 MSG_comm_wait(comm, -1);
39 MSG_comm_destroy(comm);
41 MSG_task_destroy(task);
43 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
44 comm = MSG_task_isend(task, "worker_mailbox");
45 XBT_INFO("Destroying task \"%s\" during comm", task->name);
46 MSG_task_destroy(task);
48 MSG_comm_wait(comm, -1);
51 MSG_comm_destroy(comm);
54 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
55 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
57 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
58 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
60 XBT_INFO("Goodbye now!");
64 static int worker_main(int argc, char *argv[])
66 msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
68 XBT_INFO("Start %s", task->name);
69 res = MSG_task_execute(task);
70 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
71 MSG_task_destroy(task);
75 static int worker(int argc, char *argv[])
78 msg_task_t task = NULL;
79 XBT_ATTRIB_UNUSED int res = MSG_task_receive(&(task), "worker_mailbox");
80 xbt_assert(res == MSG_OK, "MSG_task_get failed");
81 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
83 if (!strcmp(MSG_task_get_name(task), "finalize")) {
84 XBT_INFO("Destroying task \"%s\"", task->name);
85 MSG_task_destroy(task);
89 if (!strcmp(MSG_task_get_name(task), "cancel")) {
90 MSG_process_create("worker1", worker_main, task, MSG_host_self());
91 MSG_process_sleep(0.1);
92 XBT_INFO("Canceling task \"%s\"", task->name);
93 MSG_task_cancel(task);
97 double start = MSG_get_clock();
98 MSG_task_execute(task);
99 double end = MSG_get_clock();
100 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
101 MSG_task_get_flops_amount(task));
103 MSG_task_destroy(task);
105 XBT_INFO("I'm done. See you!");
109 int main(int argc, char *argv[])
111 MSG_init(&argc, argv);
112 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
114 MSG_create_environment(argv[1]);
116 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
117 MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
119 msg_error_t res = MSG_main();
121 XBT_INFO("Simulation time %g", MSG_get_clock());
123 return res != MSG_OK;