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;
17 const char * mailbox = "jupi";
20 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
21 XBT_INFO("Sending task: \"%s\"", task->name);
22 MSG_task_send_with_timeout(task, mailbox, timeout);
24 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
25 XBT_INFO("Canceling task \"%s\" directly", task->name);
26 MSG_task_cancel(task);
27 MSG_task_destroy(task);
29 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
30 XBT_INFO("Destroying task \"%s\" directly", task->name);
31 MSG_task_destroy(task);
33 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
34 msg_comm_t comm = MSG_task_isend(task, mailbox);
35 XBT_INFO("Canceling task \"%s\" during comm", task->name);
36 MSG_task_cancel(task);
38 MSG_comm_wait(comm, -1);
42 MSG_comm_destroy(comm);
44 MSG_task_destroy(task);
46 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
47 comm = MSG_task_isend(task, mailbox);
48 XBT_INFO("Destroying task \"%s\" during comm", task->name);
49 MSG_task_destroy(task);
51 MSG_comm_wait(comm, -1);
55 MSG_comm_destroy(comm);
58 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
59 MSG_task_send_with_timeout(task, mailbox, timeout);
61 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
62 MSG_task_send_with_timeout(task, mailbox, timeout);
64 XBT_INFO("Goodbye now!");
68 static int worker_main(int argc, char *argv[])
70 msg_task_t task = MSG_process_get_data(MSG_process_self());
72 XBT_INFO("Start %s", task->name);
73 res = MSG_task_execute(task);
74 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
75 MSG_task_destroy(task);
79 static int slave(int argc, char *argv[])
82 XBT_ATTRIB_UNUSED int res;
84 const char * mailbox = "jupi";
89 res = MSG_task_receive(&(task), mailbox);
90 xbt_assert(res == MSG_OK, "MSG_task_get failed");
91 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
93 if (!strcmp(MSG_task_get_name(task), "finalize")) {
94 XBT_INFO("Destroying task \"%s\"", task->name);
95 MSG_task_destroy(task);
99 if (!strcmp(MSG_task_get_name(task), "cancel")) {
100 MSG_process_create("worker1", worker_main, task, MSG_host_self());
101 MSG_process_sleep(0.1);
102 XBT_INFO("Canceling task \"%s\"", task->name);
103 MSG_task_cancel(task);
107 start = MSG_get_clock();
108 MSG_task_execute(task);
109 end = MSG_get_clock();
110 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
111 MSG_task_get_flops_amount(task));
113 MSG_task_destroy(task);
117 XBT_INFO("I'm done. See you!");
121 int main(int argc, char *argv[])
125 MSG_init(&argc, argv);
126 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
128 MSG_create_environment(argv[1]);
130 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
131 MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
135 XBT_INFO("Simulation time %g", MSG_get_clock());
137 return res != MSG_OK;