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. */
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;
20 sprintf(mailbox, "jupi");
22 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
23 XBT_INFO("Sending task: \"%s\"", task->name);
24 MSG_task_send_with_timeout(task, mailbox, timeout);
26 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
27 XBT_INFO("Canceling task \"%s\" directly", task->name);
28 MSG_task_cancel(task);
29 MSG_task_destroy(task);
31 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
32 XBT_INFO("Destroying task \"%s\" directly", task->name);
33 MSG_task_destroy(task);
35 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
36 msg_comm_t comm = MSG_task_isend(task, mailbox);
37 XBT_INFO("Canceling task \"%s\" during comm", task->name);
38 MSG_task_cancel(task);
40 MSG_comm_wait(comm, -1);
44 MSG_comm_destroy(comm);
46 MSG_task_destroy(task);
48 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
49 comm = MSG_task_isend(task, mailbox);
50 XBT_INFO("Destroying task \"%s\" during comm", task->name);
51 MSG_task_destroy(task);
53 MSG_comm_wait(comm, -1);
57 MSG_comm_destroy(comm);
60 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
61 MSG_task_send_with_timeout(task, mailbox, timeout);
63 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
64 MSG_task_send_with_timeout(task, mailbox, timeout);
66 XBT_INFO("Goodbye now!");
70 static int worker_main(int argc, char *argv[])
72 msg_task_t task = MSG_process_get_data(MSG_process_self());
74 XBT_INFO("Start %s", task->name);
75 res = MSG_task_execute(task);
76 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
77 MSG_task_destroy(task);
81 static int slave(int argc, char *argv[])
84 XBT_ATTRIB_UNUSED int res;
88 sprintf(mailbox, "jupi");
92 res = MSG_task_receive(&(task), mailbox);
93 xbt_assert(res == MSG_OK, "MSG_task_get failed");
94 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
96 if (!strcmp(MSG_task_get_name(task), "finalize")) {
97 XBT_INFO("Destroying task \"%s\"", task->name);
98 MSG_task_destroy(task);
102 if (!strcmp(MSG_task_get_name(task), "cancel")) {
103 MSG_process_create("worker1", worker_main, task, MSG_host_self());
104 MSG_process_sleep(0.1);
105 XBT_INFO("Canceling task \"%s\"", task->name);
106 MSG_task_cancel(task);
110 start = MSG_get_clock();
111 MSG_task_execute(task);
112 end = MSG_get_clock();
113 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
114 MSG_task_get_flops_amount(task));
116 MSG_task_destroy(task);
120 XBT_INFO("I'm done. See you!");
124 int main(int argc, char *argv[])
128 MSG_init(&argc, argv);
129 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
131 MSG_create_environment(argv[1]);
133 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
134 MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
138 XBT_INFO("Simulation time %g", MSG_get_clock());
140 return res != MSG_OK;