Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ridiculously tiny economy of a stupid useless allocation.
[simgrid.git] / src / msg / task.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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 "msg/private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10
11 /** \defgroup m_task_management Managing functions of Tasks
12  *  \brief This section describes the task structure of MSG
13  *  (#m_task_t) and the functions for managing it.
14  */
15 /** @addtogroup m_task_management
16  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
17  * 
18  *  Since most scheduling algorithms rely on a concept of task
19  *  that can be either <em>computed</em> locally or
20  *  <em>transferred</em> on another processor, it seems to be the
21  *  right level of abstraction for our purposes. A <em>task</em>
22  *  may then be defined by a <em>computing amount</em>, a
23  *  <em>message size</em> and some <em>private data</em>.
24  */
25
26 #ifdef HAVE_RUBY                /* FIXME: KILLME */
27 XBT_LOG_EXTERNAL_CATEGORY(ruby);
28 #endif
29
30
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
33                                 "Logging specific to MSG (task)");
34
35 /********************************* Task **************************************/
36 /** \ingroup m_task_management
37  * \brief Creates a new #m_task_t.
38  *
39  * A constructor for #m_task_t taking four arguments and returning the 
40    corresponding object.
41  * \param name a name for the object. It is for user-level information
42    and can be NULL.
43  * \param compute_duration a value of the processing amount (in flop)
44    needed to process this new task. If 0, then it cannot be executed with
45    MSG_task_execute(). This value has to be >=0.
46  * \param message_size a value of the amount of data (in bytes) needed to
47    transfer this new task. If 0, then it cannot be transfered with
48    MSG_task_get() and MSG_task_put(). This value has to be >=0.
49  * \param data a pointer to any data may want to attach to the new
50    object.  It is for user-level information and can be NULL. It can
51    be retrieved with the function \ref MSG_task_get_data.
52  * \see m_task_t
53  * \return The new corresponding object.
54  */
55 m_task_t MSG_task_create(const char *name, double compute_duration,
56                          double message_size, void *data)
57 {
58   m_task_t task = xbt_new(s_m_task_t, 1);
59   simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
60   task->simdata = simdata;
61   /* Task structure */
62   task->name = xbt_strdup(name);
63   task->data = data;
64
65   /* Simulator Data */
66   simdata->host_nb = 0;
67   simdata->computation_amount = compute_duration;
68   simdata->message_size = message_size;
69   simdata->rate = -1.0;
70   simdata->priority = 1.0;
71   simdata->refcount = 1;
72   simdata->sender = NULL;
73   simdata->receiver = NULL;
74   simdata->cond = SIMIX_cond_init();
75   simdata->mutex = SIMIX_mutex_init();
76   simdata->compute = NULL;
77   simdata->comm = NULL;
78
79   simdata->host_list = NULL;
80   simdata->comp_amount = NULL;
81   simdata->comm_amount = NULL;
82 #ifdef HAVE_TRACING
83   TRACE_msg_task_create(task);
84 #endif
85
86   return task;
87 }
88
89 /** \ingroup m_task_management
90  * \brief Return the user data of a #m_task_t.
91  *
92  * This function checks whether \a task is a valid pointer or not and return
93    the user data associated to \a task if it is possible.
94  */
95 void *MSG_task_get_data(m_task_t task)
96 {
97   xbt_assert0((task != NULL), "Invalid parameter");
98
99   return (task->data);
100 }
101
102 /** \ingroup m_task_management
103  * \brief Sets the user data of a #m_task_t.
104  *
105  * This function allows to associate a new pointer to
106    the user data associated of \a task.
107  */
108 void MSG_task_set_data(m_task_t task, void *data)
109 {
110   xbt_assert0((task != NULL), "Invalid parameter");
111
112   task->data = data;
113 }
114
115 /** \ingroup m_task_management
116  * \brief Return the sender of a #m_task_t.
117  *
118  * This functions returns the #m_process_t which sent this task
119  */
120 m_process_t MSG_task_get_sender(m_task_t task)
121 {
122   xbt_assert0(task, "Invalid parameters");
123   return ((simdata_task_t) task->simdata)->sender;
124 }
125
126 /** \ingroup m_task_management
127  * \brief Return the source of a #m_task_t.
128  *
129  * This functions returns the #m_host_t from which this task was sent
130  */
131 m_host_t MSG_task_get_source(m_task_t task)
132 {
133   xbt_assert0(task, "Invalid parameters");
134   return ((simdata_task_t) task->simdata)->source;
135 }
136
137 /** \ingroup m_task_management
138  * \brief Return the name of a #m_task_t.
139  *
140  * This functions returns the name of a #m_task_t as specified on creation
141  */
142 const char *MSG_task_get_name(m_task_t task)
143 {
144   xbt_assert0(task, "Invalid parameters");
145   return task->name;
146 }
147
148 /** \ingroup m_task_management
149  * \brief Return the name of a #m_task_t.
150  *
151  * This functions allows to associate a name to a task
152  */
153 void MSG_task_set_name(m_task_t task, const char *name)
154 {
155   xbt_assert0(task, "Invalid parameters");
156   task->name = xbt_strdup(name);
157 }
158
159
160 void MSG_task_refcount_dec(m_task_t task)
161 {
162   task->simdata->refcount--;
163 }
164
165 /** \ingroup m_task_management
166  * \brief Destroy a #m_task_t.
167  *
168  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
169    before calling this function.
170  */
171 MSG_error_t MSG_task_destroy(m_task_t task)
172 {
173   smx_action_t action = NULL;
174   xbt_assert0((task != NULL), "Invalid parameter");
175
176   /* why? if somebody is using, then you can't free! ok... but will return MSG_OK? when this task will be destroyed? isn't the user code wrong? */
177   task->simdata->refcount--;
178   if (task->simdata->refcount > 0)
179     return MSG_OK;
180 #ifdef HAVE_TRACING
181   TRACE_msg_task_destroy(task);
182 #endif
183
184   if (task->name)
185     free(task->name);
186
187   SIMIX_cond_destroy(task->simdata->cond);
188   SIMIX_mutex_destroy(task->simdata->mutex);
189
190   action = task->simdata->compute;
191   if (action)
192     SIMIX_action_destroy(action);
193
194   /* parallel tasks only */
195   if (task->simdata->host_list)
196     xbt_free(task->simdata->host_list);
197
198   /* free main structures */
199   xbt_free(task->simdata);
200   xbt_free(task);
201
202   return MSG_OK;
203 }
204
205
206 /** \ingroup m_task_management
207  * \brief Cancel a #m_task_t.
208  * \param task the taskt to cancel. If it was executed or transfered, it 
209           stops the process that were working on it.
210  */
211 MSG_error_t MSG_task_cancel(m_task_t task)
212 {
213   xbt_assert0((task != NULL), "Invalid parameter");
214
215   if (task->simdata->compute) {
216     SIMIX_action_cancel(task->simdata->compute);
217     return MSG_OK;
218   }
219   if (task->simdata->comm) {
220     SIMIX_communication_cancel(task->simdata->comm);
221     return MSG_OK;
222   }
223   THROW_IMPOSSIBLE;
224 }
225
226 /** \ingroup m_task_management
227  * \brief Returns the computation amount needed to process a task #m_task_t.
228  *        Once a task has been processed, this amount is thus set to 0...
229  */
230 double MSG_task_get_compute_duration(m_task_t task)
231 {
232   xbt_assert0((task != NULL)
233               && (task->simdata != NULL), "Invalid parameter");
234
235   return task->simdata->computation_amount;
236 }
237
238
239 /** \ingroup m_task_management
240  * \brief set the computation amount needed to process a task #m_task_t.
241  */
242
243 void MSG_task_set_compute_duration(m_task_t task,
244                                    double computation_amount)
245 {
246   xbt_assert0(task, "Invalid parameter");
247   task->simdata->computation_amount = computation_amount;
248
249 }
250
251 /** \ingroup m_task_management
252  * \brief Returns the remaining computation amount of a task #m_task_t.
253  *
254  */
255 double MSG_task_get_remaining_computation(m_task_t task)
256 {
257   xbt_assert0((task != NULL)
258               && (task->simdata != NULL), "Invalid parameter");
259
260   if (task->simdata->compute) {
261     return SIMIX_action_get_remains(task->simdata->compute);
262   } else {
263     return task->simdata->computation_amount;
264   }
265 }
266
267 /** \ingroup m_task_management
268  * \brief Returns the total amount received by a task #m_task_t.
269  *        If the communication does not exist it will return 0.
270  *        So, if the communication has FINISHED or FAILED it returns
271  *        zero.
272  */
273 double MSG_task_get_remaining_communication(m_task_t task)
274 {
275   xbt_assert0((task != NULL)
276               && (task->simdata != NULL), "Invalid parameter");
277
278   if(!task->simdata->comm){
279           DEBUG1("you are trying to retrive remaining information on a NULL action, assuming it is zero",
280                                   0);
281           return 0;
282   }else{
283           DEBUG1("calling SIMIX_communication_get_remains(%p)",
284                           task->simdata->comm);
285   }
286
287   return SIMIX_communication_get_remains(task->simdata->comm);
288 }
289
290 #ifdef HAVE_LATENCY_BOUND_TRACKING
291 /** \ingroup m_task_management
292  * \brief Return 1 if communication task is limited by latency, 0 otherwise
293  *
294  */
295 int MSG_task_is_latency_bounded(m_task_t task)
296 {
297   xbt_assert0((task != NULL)
298               && (task->simdata != NULL), "Invalid parameter");
299   DEBUG1("calling SIMIX_communication_is_latency_bounded(%p)",
300          task->simdata->comm);
301   return SIMIX_communication_is_latency_bounded(task->simdata->comm);
302 }
303 #endif
304
305 /** \ingroup m_task_management
306  * \brief Returns the size of the data attached to a task #m_task_t.
307  *
308  */
309 double MSG_task_get_data_size(m_task_t task)
310 {
311   xbt_assert0((task != NULL)
312               && (task->simdata != NULL), "Invalid parameter");
313
314   return task->simdata->message_size;
315 }
316
317
318
319 /** \ingroup m_task_management
320  * \brief Changes the priority of a computation task. This priority doesn't affect 
321  *        the transfer rate. A priority of 2 will make a task receive two times more
322  *        cpu power than the other ones.
323  *
324  */
325 void MSG_task_set_priority(m_task_t task, double priority)
326 {
327   xbt_assert0((task != NULL)
328               && (task->simdata != NULL), "Invalid parameter");
329
330   task->simdata->priority = 1 / priority;
331   if (task->simdata->compute)
332     SIMIX_action_set_priority(task->simdata->compute,
333                               task->simdata->priority);
334 }