Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MSG_task_cancel and MSG_task_get_computation_remaining
[simgrid.git] / src / msg / task.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include"private.h"
9 #include"xbt/sysdep.h"
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(task, msg,
12                                 "Logging specific to MSG (task)");
13
14 static char sprint_buffer[64];
15
16 /** \defgroup m_task_management Managing functions of Tasks
17  *  \brief This section describes the task structure of MSG
18  *  (#m_task_t) and the functions for managing it.
19  *
20  *  Since most scheduling algorithms rely on a concept of task
21  *  that can be either <em>computed</em> locally or
22  *  <em>transferred</em> on another processor, it seems to be the
23  *  right level of abstraction for our purposes. A <em>task</em>
24  *  may then be defined by a <em>computing amount</em>, a
25  *  <em>message size</em> and some <em>private data</em>.
26  */
27
28 /********************************* Task **************************************/
29 /** \ingroup m_task_management
30  * \brief Creates a new #m_task_t.
31  *
32  * A constructor for #m_task_t taking four arguments and returning the 
33    corresponding object.
34  * \param name a name for the object. It is for user-level information
35    and can be NULL.
36  * \param compute_duration a value of the processing amount (in Mflop)
37    needed to process this new task. If 0, then it cannot be executed with
38    MSG_task_execute(). This value has to be >=0.
39  * \param message_size a value of the amount of data (in Mb) needed to
40    transfer this new task. If 0, then it cannot be transfered with
41    MSG_task_get() and MSG_task_put(). This value has to be >=0.
42  * \param data a pointer to any data may want to attach to the new
43    object.  It is for user-level information and can be NULL. It can
44    be retrieved with the function \ref MSG_task_get_data.
45  * \see m_task_t
46  * \return The new corresponding object.
47  */
48 m_task_t MSG_task_create(const char *name, double compute_duration,
49                          double message_size, void *data)
50 {
51   simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
52   m_task_t task = xbt_new0(s_m_task_t,1);
53   
54   /* Task structure */
55   task->name = xbt_strdup(name);
56   task->simdata = simdata;
57   task->data = data;
58
59   /* Simulator Data */
60   simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
61   simdata->computation_amount = compute_duration;
62   simdata->message_size = message_size;
63   simdata->rate = -1.0;
64   simdata->using = 1;
65   simdata->sender = NULL;
66
67   return task;
68 }
69
70 /** \ingroup m_task_management
71  * \brief Return the user data of a #m_task_t.
72  *
73  * This functions checks whether \a task is a valid pointer or not and return
74    the user data associated to \a task if it is possible.
75  */
76 void *MSG_task_get_data(m_task_t task)
77 {
78   xbt_assert0((task != NULL), "Invalid parameter");
79
80   return (task->data);
81 }
82
83 /** \ingroup m_task_management
84  * \brief Return the sender of a #m_task_t.
85  *
86  * This functions returns the #m_process_t which sent this task
87  */
88 m_process_t MSG_task_get_sender(m_task_t task)
89 {
90    xbt_assert0(task, "Invalid parameters");
91    return ((simdata_task_t) task->simdata)->sender;
92 }
93
94 /** \ingroup m_task_management
95  * \brief Return the name of a #m_task_t.
96  *
97  * This functions returns the name of a #m_task_t as specified on creation
98  */
99 const char *MSG_task_get_name(m_task_t task)
100 {
101    xbt_assert0(task, "Invalid parameters");
102    return task->name;
103 }
104
105
106 /** \ingroup m_task_management
107  * \brief Destroy a #m_task_t.
108  *
109  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
110    before calling this function.
111  */
112 MSG_error_t MSG_task_destroy(m_task_t task)
113 {
114   simdata_task_t simdata = NULL;
115   surf_action_t action = NULL;
116   int i;
117
118   xbt_assert0((task != NULL), "Invalid parameter");
119
120   task->simdata->using--;
121   if(task->simdata->using>0) return MSG_OK;
122
123   xbt_assert0((xbt_dynar_length(task->simdata->sleeping)==0), 
124               "Task still used. There is a problem. Cannot destroy it now!");
125
126   if(task->name) free(task->name);
127
128   xbt_dynar_free(&(task->simdata->sleeping));
129
130   action = task->simdata->compute;
131   if(action) action->resource_type->common_public->action_free(action);
132   action = task->simdata->comm;
133   if(action) action->resource_type->common_public->action_free(action);
134   if(task->simdata->host_list) xbt_free(task->simdata->host_list);
135
136   free(task->simdata);
137   free(task);
138
139   return MSG_OK;
140 }
141
142
143 /** \ingroup m_task_management
144  * \brief Cancel a #m_task_t.
145  * \param task the taskt to cancel. If it was executed or transfered, it 
146           stops the process that were working on it.
147  */
148 MSG_error_t MSG_task_cancel(m_task_t task)
149 {
150   xbt_assert0((task != NULL), "Invalid parameter");
151
152   if(task->simdata->compute) {
153     surf_workstation_resource->common_public->action_cancel(task->simdata->compute);
154     return MSG_OK;
155   }
156   if(task->simdata->comm) {
157     surf_workstation_resource->common_public->action_cancel(task->simdata->comm);
158     return MSG_OK;
159   }
160
161   return MSG_FATAL;
162 }
163
164 /** \ingroup m_task_management
165  * \brief Returns the computation amount needed to process a task #m_task_t.
166  *        Once a task has been processed, this amount is thus set to 0...
167  */
168 double MSG_task_get_compute_duration(m_task_t task)
169 {
170   simdata_task_t simdata = NULL;
171
172   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
173
174   return task->simdata->computation_amount;
175 }
176
177 /** \ingroup m_task_management
178  * \brief Returns the remaining computation amount of a task #m_task_t.
179  *
180  */
181 double MSG_task_get_remaining_computation(m_task_t task)
182 {
183   simdata_task_t simdata = NULL;
184
185   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
186
187   if(task->simdata->compute) {
188     return task->simdata->compute->remains;
189   } else {
190     return task->simdata->computation_amount;
191   }
192 }
193
194 /** \ingroup m_task_management
195  * \brief Returns the size of the data attached to a task #m_task_t.
196  *
197  */
198 double MSG_task_get_data_size(m_task_t task)
199 {
200   simdata_task_t simdata = NULL;
201
202   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
203
204   return task->simdata->message_size;
205 }
206
207 /* static MSG_error_t __MSG_task_check(m_task_t task) */
208 /* { */
209 /*   simdata_task_t simdata = NULL; */
210 /*   int warning = 0; */
211
212 /*   if (task == NULL) {                /\* Fatal *\/ */
213 /*     WARNING("Task uninitialized"); */
214 /*     return MSG_FATAL; */
215 /*   } */
216 /*   simdata = task->simdata; */
217
218 /*   if (simdata == NULL) {     /\* Fatal *\/ */
219 /*     WARNING("Simulator Data uninitialized"); */
220 /*     return MSG_FATAL; */
221 /*   } */
222
223 /*   if (simdata->compute == NULL) {    /\* Fatal if execute ... *\/ */
224 /*     WARNING("No duration set for this task"); */
225 /*     warning++; */
226 /*   } */
227
228 /*   if (simdata->message_size == 0) {  /\* Fatal if transfered ... *\/ */
229 /*     WARNING("No message_size set for this task"); */
230 /*     warning++; */
231 /*   } */
232
233 /* /\*    if (task->data == NULL) { *\/ */
234 /* /\*      WARNING("User Data uninitialized"); *\/ */
235 /* /\*      warning++; *\/ */
236 /* /\*    } *\/ */
237
238 /*   if (warning) */
239 /*     return MSG_WARNING; */
240 /*   return MSG_OK; */
241 /* } */
242
243 /* static m_task_t __MSG_task_copy(m_task_t src) */
244 /* { */
245 /*   m_task_t copy = NULL; */
246 /*   simdata_task_t simdata = NULL; */
247
248 /*   __MSG_task_check(src); */
249
250 /*   simdata = src->simdata; */
251 /*   copy = MSG_task_create(src->name, SG_getTaskCost(simdata->compute), */
252 /*                       simdata->message_size, MSG_task_get_data(src)); */
253
254 /*   return (copy); */
255 /* } */
256
257 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
258 {
259   int _cursor;
260   m_process_t proc = NULL;
261
262   xbt_assert0(((task != NULL)
263                && (task->simdata != NULL)), "Invalid parameters");
264
265   xbt_dynar_push(task->simdata->sleeping, &process);
266   process->simdata->waiting_task = task;
267   xbt_context_yield();
268   process->simdata->waiting_task = NULL;
269   xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
270     if(proc==process) 
271       xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);
272   }
273
274   return MSG_OK;
275 }