Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / teshsuite / msg / task_destroy_cancel / task_destroy_cancel.cpp
1 /* Copyright (c) 2010-2019. 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   if (MSG_comm_wait(comm, -1) != MSG_OK)
35     MSG_comm_destroy(comm);
36   MSG_task_destroy(task);
37
38   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
39   comm = MSG_task_isend(task, "worker_mailbox");
40   XBT_INFO("Destroying task \"%s\" during comm", task->name);
41   MSG_task_destroy(task);
42   if (MSG_comm_wait(comm, -1) != MSG_OK)
43     MSG_comm_destroy(comm);
44
45   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
46   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
47
48   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
49   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
50
51   XBT_INFO("Goodbye now!");
52   return 0;
53 }
54
55 static int worker_main(int /*argc*/, char* /*argv*/ [])
56 {
57   msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
58   msg_error_t res;
59   XBT_INFO("Start %s", task->name);
60   res = MSG_task_execute(task);
61   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
62   MSG_task_destroy(task);
63   return 0;
64 }
65
66 static int worker(int /*argc*/, char* /*argv*/ [])
67 {
68   while (1) {
69     msg_task_t task = NULL;
70     XBT_ATTRIB_UNUSED int res = MSG_task_receive(&(task), "worker_mailbox");
71     xbt_assert(res == MSG_OK, "MSG_task_get failed");
72     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
73
74     if (not strcmp(MSG_task_get_name(task), "finalize")) {
75       XBT_INFO("Destroying task \"%s\"", task->name);
76       MSG_task_destroy(task);
77       break;
78     }
79
80     if (not strcmp(MSG_task_get_name(task), "cancel")) {
81       MSG_process_create("worker1", worker_main, task, MSG_host_self());
82       MSG_process_sleep(0.1);
83       XBT_INFO("Canceling task \"%s\"", task->name);
84       MSG_task_cancel(task);
85       continue;
86     }
87
88     double start = MSG_get_clock();
89     MSG_task_execute(task);
90     double end = MSG_get_clock();
91     XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
92              MSG_task_get_flops_amount(task));
93
94     MSG_task_destroy(task);
95   }
96   XBT_INFO("I'm done. See you!");
97   return 0;
98 }
99
100 int main(int argc, char *argv[])
101 {
102   MSG_init(&argc, argv);
103   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
104
105   MSG_create_environment(argv[1]);
106
107   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
108   MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
109
110   msg_error_t res = MSG_main();
111
112   XBT_INFO("Simulation time %g", MSG_get_clock());
113
114   return res != MSG_OK;
115 }