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 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);
41 MSG_comm_destroy(comm);
43 MSG_task_destroy(task);
45 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
46 comm = MSG_task_isend(task, mailbox);
47 XBT_INFO("Destroying task \"%s\" during comm", task->name);
48 MSG_task_destroy(task);
50 MSG_comm_wait(comm, -1);
53 MSG_comm_destroy(comm);
56 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
57 MSG_task_send_with_timeout(task, mailbox, timeout);
59 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
60 MSG_task_send_with_timeout(task, mailbox, timeout);
62 XBT_INFO("Goodbye now!");
66 static int worker_main(int argc, char *argv[])
68 msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
70 XBT_INFO("Start %s", task->name);
71 res = MSG_task_execute(task);
72 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
73 MSG_task_destroy(task);
77 static int slave(int argc, char *argv[])
80 XBT_ATTRIB_UNUSED int res;
82 const char * mailbox = "jupi";
87 res = MSG_task_receive(&(task), mailbox);
88 xbt_assert(res == MSG_OK, "MSG_task_get failed");
89 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
91 if (!strcmp(MSG_task_get_name(task), "finalize")) {
92 XBT_INFO("Destroying task \"%s\"", task->name);
93 MSG_task_destroy(task);
97 if (!strcmp(MSG_task_get_name(task), "cancel")) {
98 MSG_process_create("worker1", worker_main, task, MSG_host_self());
99 MSG_process_sleep(0.1);
100 XBT_INFO("Canceling task \"%s\"", task->name);
101 MSG_task_cancel(task);
105 start = MSG_get_clock();
106 MSG_task_execute(task);
107 end = MSG_get_clock();
108 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
109 MSG_task_get_flops_amount(task));
111 MSG_task_destroy(task);
115 XBT_INFO("I'm done. See you!");
119 int main(int argc, char *argv[])
123 MSG_init(&argc, argv);
124 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
126 MSG_create_environment(argv[1]);
128 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
129 MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
133 XBT_INFO("Simulation time %g", MSG_get_clock());
135 return res != MSG_OK;