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" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
20 /** Emitter function */
21 int master(int argc, char *argv[])
23 double task_comp_size = 5E7;
24 double task_comm_size = 1E6;
28 msg_task_t task = NULL;
29 msg_comm_t comm = NULL;
32 sprintf(mailbox, "jupi");
34 task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
35 XBT_INFO("Sending task: \"%s\"", task->name);
36 MSG_task_send_with_timeout(task, mailbox, timeout);
38 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
39 XBT_INFO("Canceling task \"%s\" directly", task->name);
40 MSG_task_cancel(task);
41 MSG_task_destroy(task);
43 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
44 XBT_INFO("Destroying task \"%s\" directly", task->name);
45 MSG_task_destroy(task);
47 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
48 comm = MSG_task_isend(task, mailbox);
49 XBT_INFO("Canceling task \"%s\" during comm", task->name);
50 MSG_task_cancel(task);
52 MSG_comm_wait(comm, -1);
56 MSG_comm_destroy(comm);
58 MSG_task_destroy(task);
60 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
61 comm = MSG_task_isend(task, mailbox);
62 XBT_INFO("Destroying task \"%s\" during comm", task->name);
63 MSG_task_destroy(task);
65 MSG_comm_wait(comm, -1);
69 MSG_comm_destroy(comm);
72 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
73 MSG_task_send_with_timeout(task, mailbox, timeout);
75 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
76 MSG_task_send_with_timeout(task, mailbox, timeout);
78 XBT_INFO("Goodbye now!");
82 static int worker_main(int argc, char *argv[])
84 msg_task_t task = MSG_process_get_data(MSG_process_self());
86 XBT_INFO("Start %s", task->name);
87 res = MSG_task_execute(task);
88 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
89 MSG_task_destroy(task);
93 /** Receiver function */
94 int slave(int argc, char *argv[])
97 _XBT_GNUC_UNUSED int res;
101 sprintf(mailbox, "jupi");
105 res = MSG_task_receive(&(task), mailbox);
106 xbt_assert(res == MSG_OK, "MSG_task_get failed");
107 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
109 if (!strcmp(MSG_task_get_name(task), "finalize")) {
110 XBT_INFO("Destroying task \"%s\"", task->name);
111 MSG_task_destroy(task);
115 if (!strcmp(MSG_task_get_name(task), "cancel")) {
116 MSG_process_create("worker1", worker_main, task, MSG_host_self());
117 MSG_process_sleep(0.1);
118 XBT_INFO("Canceling task \"%s\"", task->name);
119 MSG_task_cancel(task);
123 start = MSG_get_clock();
124 MSG_task_execute(task);
125 end = MSG_get_clock();
126 XBT_INFO("Task \"%s\" done in %f (amount %f)"
127 , MSG_task_get_name(task)
129 , MSG_task_get_flops_amount(task));
131 MSG_task_destroy(task);
135 XBT_INFO("I'm done. See you!");
140 int main(int argc, char *argv[])
143 const char *platform_file;
144 const char *application_file;
146 MSG_init(&argc, argv);
148 printf("Usage: %s platform_file deployment_file\n", argv[0]);
149 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
152 platform_file = argv[1];
153 application_file = argv[2];
155 { /* Simulation setting */
156 MSG_create_environment(platform_file);
158 { /* Application deployment */
159 MSG_function_register("master", master);
160 MSG_function_register("slave", slave);
162 MSG_launch_application(application_file);
166 XBT_INFO("Simulation time %g", MSG_get_clock());