Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
maxmin_bench, use stderr only (not stdout)
[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   const char * mailbox = "jupi";
18   xbt_ex_t ex;
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 (ex) {
41     xbt_ex_free(ex);
42     MSG_comm_destroy(comm);
43   }
44   MSG_task_destroy(task);
45
46   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
47   comm = MSG_task_isend(task, mailbox);
48   XBT_INFO("Destroying task \"%s\" during comm", task->name);
49   MSG_task_destroy(task);
50   TRY {
51     MSG_comm_wait(comm, -1);
52   }
53   CATCH (ex) {
54     xbt_ex_free(ex);
55     MSG_comm_destroy(comm);
56   }
57
58   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
59   MSG_task_send_with_timeout(task, mailbox, timeout);
60
61   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
62   MSG_task_send_with_timeout(task, mailbox, timeout);
63
64   XBT_INFO("Goodbye now!");
65   return 0;
66 }
67
68 static int worker_main(int argc, char *argv[])
69 {
70   msg_task_t task = MSG_process_get_data(MSG_process_self());
71   msg_error_t res;
72   XBT_INFO("Start %s", task->name);
73   res = MSG_task_execute(task);
74   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
75   MSG_task_destroy(task);
76   return 0;
77 }
78
79 static int slave(int argc, char *argv[])
80 {
81   msg_task_t task;
82   XBT_ATTRIB_UNUSED int res;
83   int id = -1;
84   const char * mailbox = "jupi";
85   double start, end;
86  
87   while (1) {
88     task = NULL;
89     res = MSG_task_receive(&(task), mailbox);
90     xbt_assert(res == MSG_OK, "MSG_task_get failed");
91     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
92
93     if (!strcmp(MSG_task_get_name(task), "finalize")) {
94       XBT_INFO("Destroying task \"%s\"", task->name);
95       MSG_task_destroy(task);
96       break;
97     }
98
99     if (!strcmp(MSG_task_get_name(task), "cancel")) {
100       MSG_process_create("worker1", worker_main, task, MSG_host_self());
101       MSG_process_sleep(0.1);
102       XBT_INFO("Canceling task \"%s\"", task->name);
103       MSG_task_cancel(task);
104       continue;
105     }
106
107     start = MSG_get_clock();
108     MSG_task_execute(task);
109     end = MSG_get_clock();
110     XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
111              MSG_task_get_flops_amount(task));
112
113     MSG_task_destroy(task);
114     task = NULL;
115     id--;
116   }
117   XBT_INFO("I'm done. See you!");
118   return 0;
119 }
120
121 int main(int argc, char *argv[])
122 {
123   msg_error_t res;
124
125   MSG_init(&argc, argv);
126   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
127
128   MSG_create_environment(argv[1]);
129
130   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
131   MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
132
133   res = MSG_main();
134
135   XBT_INFO("Simulation time %g", MSG_get_clock());
136
137   return res != MSG_OK;
138 }