Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename simgrid::exception into simgrid::Exception
[simgrid.git] / teshsuite / msg / task_destroy_cancel / task_destroy_cancel.cpp
1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/Exception.hpp"
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   msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
18   XBT_INFO("Sending task: \"%s\"", task->name);
19   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
20
21   task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
22   XBT_INFO("Canceling task \"%s\" directly", task->name);
23   MSG_task_cancel(task);
24   MSG_task_destroy(task);
25
26   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
27   XBT_INFO("Destroying task \"%s\" directly", task->name);
28   MSG_task_destroy(task);
29
30   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
31   msg_comm_t comm = MSG_task_isend(task, "worker_mailbox");
32   XBT_INFO("Canceling task \"%s\" during comm", task->name);
33   MSG_task_cancel(task);
34   try {
35     MSG_comm_wait(comm, -1);
36   }
37   catch (xbt_ex& ex) {
38     MSG_comm_destroy(comm);
39   }
40   MSG_task_destroy(task);
41
42   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
43   comm = MSG_task_isend(task, "worker_mailbox");
44   XBT_INFO("Destroying task \"%s\" during comm", task->name);
45   MSG_task_destroy(task);
46   try {
47     MSG_comm_wait(comm, -1);
48   }
49   catch (xbt_ex& ex) {
50     MSG_comm_destroy(comm);
51   }
52
53   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
54   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
55
56   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
57   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
58
59   XBT_INFO("Goodbye now!");
60   return 0;
61 }
62
63 static int worker_main(int /*argc*/, char* /*argv*/ [])
64 {
65   msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
66   msg_error_t res;
67   XBT_INFO("Start %s", task->name);
68   res = MSG_task_execute(task);
69   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
70   MSG_task_destroy(task);
71   return 0;
72 }
73
74 static int worker(int /*argc*/, char* /*argv*/ [])
75 {
76   while (1) {
77     msg_task_t task = NULL;
78     XBT_ATTRIB_UNUSED int res = MSG_task_receive(&(task), "worker_mailbox");
79     xbt_assert(res == MSG_OK, "MSG_task_get failed");
80     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
81
82     if (not strcmp(MSG_task_get_name(task), "finalize")) {
83       XBT_INFO("Destroying task \"%s\"", task->name);
84       MSG_task_destroy(task);
85       break;
86     }
87
88     if (not strcmp(MSG_task_get_name(task), "cancel")) {
89       MSG_process_create("worker1", worker_main, task, MSG_host_self());
90       MSG_process_sleep(0.1);
91       XBT_INFO("Canceling task \"%s\"", task->name);
92       MSG_task_cancel(task);
93       continue;
94     }
95
96     double start = MSG_get_clock();
97     MSG_task_execute(task);
98     double end = MSG_get_clock();
99     XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
100              MSG_task_get_flops_amount(task));
101
102     MSG_task_destroy(task);
103   }
104   XBT_INFO("I'm done. See you!");
105   return 0;
106 }
107
108 int main(int argc, char *argv[])
109 {
110   MSG_init(&argc, argv);
111   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
112
113   MSG_create_environment(argv[1]);
114
115   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
116   MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
117
118   msg_error_t res = MSG_main();
119
120   XBT_INFO("Simulation time %g", MSG_get_clock());
121
122   return res != MSG_OK;
123 }