Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2115622106a6e1d15e301ff0400b58324b2be071
[simgrid.git] / teshsuite / msg / task_destroy_cancel / task_destroy_cancel.c
1 /* Copyright (c) 2010-2015. 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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static int master(int argc, char *argv[])
12 {
13   double task_comp_size = 5E7;
14   double task_comm_size = 1E6;
15   double timeout = 1;
16
17   char mailbox[256];
18   xbt_ex_t ex;
19
20   sprintf(mailbox, "jupi");
21
22   msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
23   XBT_INFO("Sending task: \"%s\"", task->name);
24   MSG_task_send_with_timeout(task, mailbox, timeout);
25
26   task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
27   XBT_INFO("Canceling task \"%s\" directly", task->name);
28   MSG_task_cancel(task);
29   MSG_task_destroy(task);
30
31   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
32   XBT_INFO("Destroying task \"%s\" directly", task->name);
33   MSG_task_destroy(task);
34
35   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
36   msg_comm_t comm = MSG_task_isend(task, mailbox);
37   XBT_INFO("Canceling task \"%s\" during comm", task->name);
38   MSG_task_cancel(task);
39   TRY {
40     MSG_comm_wait(comm, -1);
41   }
42   CATCH (ex) {
43     xbt_ex_free(ex);
44     MSG_comm_destroy(comm);
45   }
46   MSG_task_destroy(task);
47
48   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
49   comm = MSG_task_isend(task, mailbox);
50   XBT_INFO("Destroying task \"%s\" during comm", task->name);
51   MSG_task_destroy(task);
52   TRY {
53     MSG_comm_wait(comm, -1);
54   }
55   CATCH (ex) {
56     xbt_ex_free(ex);
57     MSG_comm_destroy(comm);
58   }
59
60   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
61   MSG_task_send_with_timeout(task, mailbox, timeout);
62
63   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
64   MSG_task_send_with_timeout(task, mailbox, timeout);
65
66   XBT_INFO("Goodbye now!");
67   return 0;
68 }
69
70 static int worker_main(int argc, char *argv[])
71 {
72   msg_task_t task = MSG_process_get_data(MSG_process_self());
73   msg_error_t res;
74   XBT_INFO("Start %s", task->name);
75   res = MSG_task_execute(task);
76   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
77   MSG_task_destroy(task);
78   return 0;
79 }
80
81 static int slave(int argc, char *argv[])
82 {
83   msg_task_t task;
84   XBT_ATTRIB_UNUSED int res;
85   int id = -1;
86   char mailbox[80];
87   double start, end;
88   sprintf(mailbox, "jupi");
89
90   while (1) {
91     task = NULL;
92     res = MSG_task_receive(&(task), mailbox);
93     xbt_assert(res == MSG_OK, "MSG_task_get failed");
94     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
95
96     if (!strcmp(MSG_task_get_name(task), "finalize")) {
97       XBT_INFO("Destroying task \"%s\"", task->name);
98       MSG_task_destroy(task);
99       break;
100     }
101
102     if (!strcmp(MSG_task_get_name(task), "cancel")) {
103       MSG_process_create("worker1", worker_main, task, MSG_host_self());
104       MSG_process_sleep(0.1);
105       XBT_INFO("Canceling task \"%s\"", task->name);
106       MSG_task_cancel(task);
107       continue;
108     }
109
110     start = MSG_get_clock();
111     MSG_task_execute(task);
112     end = MSG_get_clock();
113     XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
114              MSG_task_get_flops_amount(task));
115
116     MSG_task_destroy(task);
117     task = NULL;
118     id--;
119   }
120   XBT_INFO("I'm done. See you!");
121   return 0;
122 }
123
124 int main(int argc, char *argv[])
125 {
126   msg_error_t res;
127
128   MSG_init(&argc, argv);
129   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
130              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
131
132   MSG_create_environment(argv[1]);
133
134   MSG_function_register("master", master);
135   MSG_function_register("slave", slave);
136
137   MSG_launch_application(argv[2]);
138
139   res = MSG_main();
140
141   XBT_INFO("Simulation time %g", MSG_get_clock());
142
143   return res != MSG_OK;
144 }