Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c9b17021b7b8936cbd420c9eab6f8cf8be45ca35
[simgrid.git] / teshsuite / msg / task_destroy_cancel.c
1 /* Copyright (c) 2010-2014. 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 <stdio.h>
8 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   double task_comp_size = 5E7;
24   double task_comm_size = 1E6;
25   double timeout = 1;
26
27   char mailbox[256];
28   msg_task_t task = NULL;
29   msg_comm_t comm = NULL;
30   sprintf(mailbox, "jupi");
31
32   task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
33   XBT_INFO("Sending task: \"%s\"", task->name);
34   MSG_task_send_with_timeout(task, mailbox, timeout);
35
36   task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
37   XBT_INFO("Canceling task \"%s\" directly", task->name);
38   MSG_task_cancel(task);
39
40   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
41   XBT_INFO("Destroying task \"%s\" directly", task->name);
42   MSG_task_destroy(task);
43
44   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
45   comm = MSG_task_isend(task, mailbox);
46   XBT_INFO("Canceling task \"%s\" during comm", task->name);
47   MSG_task_cancel(task);
48
49   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
50   comm = MSG_task_isend(task, mailbox);
51   XBT_INFO("Destroying task \"%s\" during comm", task->name);
52   MSG_task_destroy(task);
53
54   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
55   MSG_task_send_with_timeout(task, mailbox, timeout);
56
57   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
58   MSG_task_send_with_timeout(task, mailbox, timeout);
59
60   XBT_INFO("Goodbye now!");
61   return 0;
62 }                               /* end_of_master */
63
64 static int worker_main(int argc, char *argv[])
65 {
66   msg_task_t task = MSG_process_get_data(MSG_process_self());
67   XBT_INFO("Start %s", task->name);
68   MSG_task_execute(task);
69   XBT_INFO("Task done");
70   return 0;
71 }
72
73 /** Receiver function  */
74 int slave(int argc, char *argv[])
75 {
76   msg_task_t task;
77   _XBT_GNUC_UNUSED int res;
78   int id = -1;
79   char mailbox[80];
80   double start, end;
81   sprintf(mailbox, "jupi");
82
83   while (1) {
84     task = NULL;
85     res = MSG_task_receive(&(task), mailbox);
86     xbt_assert(res == MSG_OK, "MSG_task_get failed");
87     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
88
89     if (!strcmp(MSG_task_get_name(task), "finalize")) {
90       XBT_INFO("Destroying task \"%s\"", task->name);
91       MSG_task_destroy(task);
92       break;
93     }
94
95     if (!strcmp(MSG_task_get_name(task), "cancel")) {
96       MSG_process_create("worker1", worker_main, task, MSG_host_self());
97       XBT_INFO("Canceling task \"%s\"", task->name);
98       MSG_task_cancel(task);
99       continue;
100     }
101
102     start = MSG_get_clock();
103     MSG_task_execute(task);
104     end = MSG_get_clock();
105     XBT_INFO("Task \"%s\" done in %f (amount %f)"
106                 , MSG_task_get_name(task)
107                 , end - start
108                 , MSG_task_get_remaining_computation(task));
109
110     MSG_task_destroy(task);
111     task = NULL;
112     id--;
113   }
114   XBT_INFO("I'm done. See you!");
115   return 0;
116 }                               /* end_of_slave */
117
118 /** Main function */
119 int main(int argc, char *argv[])
120 {
121   msg_error_t res;
122   const char *platform_file;
123   const char *application_file;
124
125   MSG_init(&argc, argv);
126   if (argc != 3) {
127     printf("Usage: %s platform_file deployment_file\n", argv[0]);
128     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
129     exit(1);
130   }
131   platform_file = argv[1];
132   application_file = argv[2];
133
134   /* MSG_config("workstation/model","KCCFLN05"); */
135   {                             /*  Simulation setting */
136     MSG_create_environment(platform_file);
137   }
138   {                             /*   Application deployment */
139     MSG_function_register("master", master);
140     MSG_function_register("slave", slave);
141
142     MSG_launch_application(application_file);
143   }
144   res = MSG_main();
145
146   XBT_INFO("Simulation time %g", MSG_get_clock());
147
148   if (res == MSG_OK)
149     return 0;
150   else
151     return 1;
152 }                               /* end_of_main */