1 /* Copyright (c) 2010-2014. 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 "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/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;
30 sprintf(mailbox, "jupi");
32 task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
33 XBT_INFO("Sending task: \"%s\"", task->name);
34 MSG_task_send_with_timeout(task, mailbox, timeout);
36 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
37 XBT_INFO("Canceling task \"%s\" directly", task->name);
38 MSG_task_cancel(task);
40 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
41 XBT_INFO("Destroying task \"%s\" directly", task->name);
42 MSG_task_destroy(task);
44 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
45 comm = MSG_task_isend(task, mailbox);
46 XBT_INFO("Canceling task \"%s\" during comm", task->name);
47 MSG_task_cancel(task);
49 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
50 comm = MSG_task_isend(task, mailbox);
51 XBT_INFO("Destroying task \"%s\" during comm", task->name);
52 MSG_task_destroy(task);
54 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
55 MSG_task_send_with_timeout(task, mailbox, timeout);
57 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
58 MSG_task_send_with_timeout(task, mailbox, timeout);
60 XBT_INFO("Goodbye now!");
64 static int worker_main(int argc, char *argv[])
66 msg_task_t task = MSG_process_get_data(MSG_process_self());
67 XBT_INFO("Start %s", task->name);
68 MSG_task_execute(task);
69 XBT_INFO("Task done");
73 /** Receiver function */
74 int slave(int argc, char *argv[])
77 _XBT_GNUC_UNUSED int res;
80 _XBT_GNUC_UNUSED int read;
81 double duration, start, end;
82 sprintf(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 XBT_INFO("Canceling task \"%s\"", task->name);
99 MSG_task_cancel(task);
103 duration = MSG_task_get_compute_duration(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)"
108 , MSG_task_get_name(task)
110 , MSG_task_get_remaining_computation(task));
112 MSG_task_destroy(task);
116 XBT_INFO("I'm done. See you!");
121 int main(int argc, char *argv[])
124 const char *platform_file;
125 const char *application_file;
127 MSG_init(&argc, argv);
129 printf("Usage: %s platform_file deployment_file\n", argv[0]);
130 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
133 platform_file = argv[1];
134 application_file = argv[2];
136 /* MSG_config("workstation/model","KCCFLN05"); */
137 { /* Simulation setting */
138 MSG_create_environment(platform_file);
140 { /* Application deployment */
141 MSG_function_register("master", master);
142 MSG_function_register("slave", slave);
144 MSG_launch_application(application_file);
148 XBT_INFO("Simulation time %g", MSG_get_clock());