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";
19 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
20 XBT_INFO("Sending task: \"%s\"", task->name);
21 MSG_task_send_with_timeout(task, mailbox, timeout);
23 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
24 XBT_INFO("Canceling task \"%s\" directly", task->name);
25 MSG_task_cancel(task);
26 MSG_task_destroy(task);
28 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
29 XBT_INFO("Destroying task \"%s\" directly", task->name);
30 MSG_task_destroy(task);
32 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
33 msg_comm_t comm = MSG_task_isend(task, mailbox);
34 XBT_INFO("Canceling task \"%s\" during comm", task->name);
35 MSG_task_cancel(task);
37 MSG_comm_wait(comm, -1);
40 MSG_comm_destroy(comm);
42 MSG_task_destroy(task);
44 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
45 comm = MSG_task_isend(task, mailbox);
46 XBT_INFO("Destroying task \"%s\" during comm", task->name);
47 MSG_task_destroy(task);
49 MSG_comm_wait(comm, -1);
52 MSG_comm_destroy(comm);
55 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
56 MSG_task_send_with_timeout(task, mailbox, timeout);
58 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
59 MSG_task_send_with_timeout(task, mailbox, timeout);
61 XBT_INFO("Goodbye now!");
65 static int worker_main(int argc, char *argv[])
67 msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
69 XBT_INFO("Start %s", task->name);
70 res = MSG_task_execute(task);
71 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
72 MSG_task_destroy(task);
76 static int slave(int argc, char *argv[])
79 XBT_ATTRIB_UNUSED int res;
81 const char * mailbox = "jupi";
86 res = MSG_task_receive(&(task), mailbox);
87 xbt_assert(res == MSG_OK, "MSG_task_get failed");
88 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
90 if (!strcmp(MSG_task_get_name(task), "finalize")) {
91 XBT_INFO("Destroying task \"%s\"", task->name);
92 MSG_task_destroy(task);
96 if (!strcmp(MSG_task_get_name(task), "cancel")) {
97 MSG_process_create("worker1", worker_main, task, MSG_host_self());
98 MSG_process_sleep(0.1);
99 XBT_INFO("Canceling task \"%s\"", task->name);
100 MSG_task_cancel(task);
104 start = MSG_get_clock();
105 MSG_task_execute(task);
106 end = MSG_get_clock();
107 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
108 MSG_task_get_flops_amount(task));
110 MSG_task_destroy(task);
114 XBT_INFO("I'm done. See you!");
118 int main(int argc, char *argv[])
122 MSG_init(&argc, argv);
123 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
125 MSG_create_environment(argv[1]);
127 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
128 MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
132 XBT_INFO("Simulation time %g", MSG_get_clock());
134 return res != MSG_OK;