Logo AND Algorithmique Numérique Distribuée

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