Logo AND Algorithmique Numérique Distribuée

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