Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display proper execution status.
[simgrid.git] / teshsuite / msg / task_destroy_cancel.c
1 /* Copyright (c) 2010-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
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 */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   double task_comp_size = 5E7;
24   double task_comm_size = 1E6;
25   double timeout = 1;
26
27   char mailbox[256];
28   msg_task_t task = NULL;
29   msg_comm_t comm = NULL;
30   xbt_ex_t ex;
31
32   sprintf(mailbox, "jupi");
33
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);
37
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
42   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
43   XBT_INFO("Destroying task \"%s\" directly", task->name);
44   MSG_task_destroy(task);
45
46   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
47   comm = MSG_task_isend(task, mailbox);
48   XBT_INFO("Canceling task \"%s\" during comm", task->name);
49   MSG_task_cancel(task);
50   TRY {
51     MSG_comm_wait(comm, -1);
52   }
53   CATCH (ex) {
54     xbt_ex_free(ex);
55     MSG_comm_destroy(comm);
56   }
57
58   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
59   comm = MSG_task_isend(task, mailbox);
60   XBT_INFO("Destroying task \"%s\" during comm", task->name);
61   MSG_task_destroy(task);
62   TRY {
63     MSG_comm_wait(comm, -1);
64   }
65   CATCH (ex) {
66     xbt_ex_free(ex);
67     MSG_comm_destroy(comm);
68   }
69
70   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
71   MSG_task_send_with_timeout(task, mailbox, timeout);
72
73   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
74   MSG_task_send_with_timeout(task, mailbox, timeout);
75
76   XBT_INFO("Goodbye now!");
77   return 0;
78 }                               /* end_of_master */
79
80 static int worker_main(int argc, char *argv[])
81 {
82   msg_task_t task = MSG_process_get_data(MSG_process_self());
83   msg_error_t res;
84   XBT_INFO("Start %s", task->name);
85   res = MSG_task_execute(task);
86   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
87   return 0;
88 }
89
90 /** Receiver function  */
91 int slave(int argc, char *argv[])
92 {
93   msg_task_t task;
94   _XBT_GNUC_UNUSED int res;
95   int id = -1;
96   char mailbox[80];
97   double start, end;
98   sprintf(mailbox, "jupi");
99
100   while (1) {
101     task = NULL;
102     res = MSG_task_receive(&(task), mailbox);
103     xbt_assert(res == MSG_OK, "MSG_task_get failed");
104     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
105
106     if (!strcmp(MSG_task_get_name(task), "finalize")) {
107       XBT_INFO("Destroying task \"%s\"", task->name);
108       MSG_task_destroy(task);
109       break;
110     }
111
112     if (!strcmp(MSG_task_get_name(task), "cancel")) {
113       MSG_process_create("worker1", worker_main, task, MSG_host_self());
114       XBT_INFO("Canceling task \"%s\"", task->name);
115       MSG_task_cancel(task);
116       continue;
117     }
118
119     start = MSG_get_clock();
120     MSG_task_execute(task);
121     end = MSG_get_clock();
122     XBT_INFO("Task \"%s\" done in %f (amount %f)"
123                 , MSG_task_get_name(task)
124                 , end - start
125                 , MSG_task_get_remaining_computation(task));
126
127     MSG_task_destroy(task);
128     task = NULL;
129     id--;
130   }
131   XBT_INFO("I'm done. See you!");
132   return 0;
133 }                               /* end_of_slave */
134
135 /** Main function */
136 int main(int argc, char *argv[])
137 {
138   msg_error_t res;
139   const char *platform_file;
140   const char *application_file;
141
142   MSG_init(&argc, argv);
143   if (argc != 3) {
144     printf("Usage: %s platform_file deployment_file\n", argv[0]);
145     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
146     exit(1);
147   }
148   platform_file = argv[1];
149   application_file = argv[2];
150
151   /* MSG_config("workstation/model","KCCFLN05"); */
152   {                             /*  Simulation setting */
153     MSG_create_environment(platform_file);
154   }
155   {                             /*   Application deployment */
156     MSG_function_register("master", master);
157     MSG_function_register("slave", slave);
158
159     MSG_launch_application(application_file);
160   }
161   res = MSG_main();
162
163   XBT_INFO("Simulation time %g", MSG_get_clock());
164
165   if (res == MSG_OK)
166     return 0;
167   else
168     return 1;
169 }                               /* end_of_main */