Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b0df136d09123f576bf80979f1da1e5a61dd5ce7
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
27                                 "Logging specific to MSG (task)");
28
29 /********************************* Task **************************************/
30 /** \ingroup m_task_management
31  * \brief Creates a new #m_task_t.
32  *
33  * A constructor for #m_task_t taking four arguments and returning the 
34    corresponding object.
35  * \param name a name for the object. It is for user-level information
36    and can be NULL.
37  * \param compute_duration a value of the processing amount (in flop)
38    needed to process this new task. If 0, then it cannot be executed with
39    MSG_task_execute(). This value has to be >=0.
40  * \param message_size a value of the amount of data (in bytes) needed to
41    transfer this new task. If 0, then it cannot be transfered with
42    MSG_task_get() and MSG_task_put(). This value has to be >=0.
43  * \param data a pointer to any data may want to attach to the new
44    object.  It is for user-level information and can be NULL. It can
45    be retrieved with the function \ref MSG_task_get_data.
46  * \see m_task_t
47  * \return The new corresponding object.
48  */
49 m_task_t MSG_task_create(const char *name, double compute_duration,
50                          double message_size, void *data)
51 {
52   m_task_t task = xbt_new(s_m_task_t, 1);
53   simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
54   task->simdata = simdata;
55   /* Task structure */
56   task->name = xbt_strdup(name);
57   task->data = data;
58
59   /* Simulator Data */
60   simdata->host_nb = 0;
61   simdata->computation_amount = compute_duration;
62   simdata->message_size = message_size;
63   simdata->rate = -1.0;
64   simdata->priority = 1.0;
65   simdata->isused = 0;
66   simdata->sender = NULL;
67   simdata->receiver = NULL;
68   simdata->compute = NULL;
69   simdata->comm = NULL;
70
71   simdata->host_list = NULL;
72   simdata->comp_amount = NULL;
73   simdata->comm_amount = NULL;
74 #ifdef HAVE_TRACING
75   TRACE_msg_task_create(task);
76 #endif
77
78   return task;
79 }
80
81 /** \ingroup m_task_management
82  * \brief Return the user data of a #m_task_t.
83  *
84  * This function checks whether \a task is a valid pointer or not and return
85    the user data associated to \a task if it is possible.
86  */
87 void *MSG_task_get_data(m_task_t task)
88 {
89   xbt_assert((task != NULL), "Invalid parameter");
90
91   return (task->data);
92 }
93
94 /** \ingroup m_task_management
95  * \brief Sets the user data of a #m_task_t.
96  *
97  * This function allows to associate a new pointer to
98    the user data associated of \a task.
99  */
100 void MSG_task_set_data(m_task_t task, void *data)
101 {
102   xbt_assert((task != NULL), "Invalid parameter");
103
104   task->data = data;
105 }
106
107 /** \ingroup m_task_management
108  * \brief Return the sender of a #m_task_t.
109  *
110  * This functions returns the #m_process_t which sent this task
111  */
112 m_process_t MSG_task_get_sender(m_task_t task)
113 {
114   xbt_assert(task, "Invalid parameters");
115   return ((simdata_task_t) task->simdata)->sender;
116 }
117
118 /** \ingroup m_task_management
119  * \brief Return the source of a #m_task_t.
120  *
121  * This functions returns the #m_host_t from which this task was sent
122  */
123 m_host_t MSG_task_get_source(m_task_t task)
124 {
125   xbt_assert(task, "Invalid parameters");
126   return ((simdata_task_t) task->simdata)->source;
127 }
128
129 /** \ingroup m_task_management
130  * \brief Return the name of a #m_task_t.
131  *
132  * This functions returns the name of a #m_task_t as specified on creation
133  */
134 const char *MSG_task_get_name(m_task_t task)
135 {
136   xbt_assert(task, "Invalid parameters");
137   return task->name;
138 }
139
140 /** \ingroup m_task_management
141  * \brief Return the name of a #m_task_t.
142  *
143  * This functions allows to associate a name to a task
144  */
145 void MSG_task_set_name(m_task_t task, const char *name)
146 {
147   xbt_assert(task, "Invalid parameters");
148   task->name = xbt_strdup(name);
149 }
150
151 /** \ingroup m_task_management
152  * \brief Destroy a #m_task_t.
153  *
154  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
155    before calling this function.
156  */
157 MSG_error_t MSG_task_destroy(m_task_t task)
158 {
159   smx_action_t action = NULL;
160   xbt_assert((task != NULL), "Invalid parameter");
161
162   if (task->simdata->isused) {
163     /* the task is still being used, it may be an unfinished dsend */
164     MSG_task_cancel(task);
165   }
166 #ifdef HAVE_TRACING
167   TRACE_msg_task_destroy(task);
168 #endif
169
170   xbt_free(task->name);
171
172   action = task->simdata->compute;
173   if (action)
174     SIMIX_req_host_execution_destroy(action);
175
176   /* parallel tasks only */
177   xbt_free(task->simdata->host_list);
178
179   /* free main structures */
180   xbt_free(task->simdata);
181   xbt_free(task);
182
183   return MSG_OK;
184 }
185
186
187 /** \ingroup m_task_management
188  * \brief Cancel a #m_task_t.
189  * \param task the task to cancel. If it was executed or transfered, it
190           stops the process that were working on it.
191  */
192 MSG_error_t MSG_task_cancel(m_task_t task)
193 {
194   xbt_assert((task != NULL), "Invalid parameter");
195
196   if (task->simdata->compute) {
197     SIMIX_req_host_execution_cancel(task->simdata->compute);
198   }
199   else if (task->simdata->comm) {
200     SIMIX_req_comm_cancel(task->simdata->comm);
201     task->simdata->isused = 0;
202   }
203   else {
204     static int warned = 0;
205     if (!warned) {
206       XBT_WARN("Cannot cancel a non-running task");
207       warned = 1;
208     }
209   }
210   return MSG_OK;
211 }
212
213 /** \ingroup m_task_management
214  * \brief Returns the computation amount needed to process a task #m_task_t.
215  *        Once a task has been processed, this amount is thus set to 0...
216  */
217 double MSG_task_get_compute_duration(m_task_t task)
218 {
219   xbt_assert((task != NULL)
220               && (task->simdata != NULL), "Invalid parameter");
221
222   return task->simdata->computation_amount;
223 }
224
225
226 /** \ingroup m_task_management
227  * \brief set the computation amount needed to process a task #m_task_t.
228  */
229
230 void MSG_task_set_compute_duration(m_task_t task,
231                                    double computation_amount)
232 {
233   xbt_assert(task, "Invalid parameter");
234   task->simdata->computation_amount = computation_amount;
235
236 }
237
238 /** \ingroup m_task_management
239  * \brief Returns the remaining computation amount of a task #m_task_t.
240  *
241  */
242 double MSG_task_get_remaining_computation(m_task_t task)
243 {
244   xbt_assert((task != NULL)
245               && (task->simdata != NULL), "Invalid parameter");
246
247   if (task->simdata->compute) {
248     return SIMIX_req_host_execution_get_remains(task->simdata->compute);
249   } else {
250     return task->simdata->computation_amount;
251   }
252 }
253
254 /** \ingroup m_task_management
255  * \brief Returns the total amount received by a task #m_task_t.
256  *        If the communication does not exist it will return 0.
257  *        So, if the communication has FINISHED or FAILED it returns
258  *        zero.
259  */
260 double MSG_task_get_remaining_communication(m_task_t task)
261 {
262   xbt_assert((task != NULL)
263               && (task->simdata != NULL), "Invalid parameter");
264   XBT_DEBUG("calling SIMIX_req_communication_get_remains(%p)",
265          task->simdata->comm);
266   return SIMIX_req_comm_get_remains(task->simdata->comm);
267 }
268
269 #ifdef HAVE_LATENCY_BOUND_TRACKING
270 /** \ingroup m_task_management
271  * \brief Return 1 if communication task is limited by latency, 0 otherwise
272  *
273  */
274 int MSG_task_is_latency_bounded(m_task_t task)
275 {
276   xbt_assert((task != NULL)
277               && (task->simdata != NULL), "Invalid parameter");
278   XBT_DEBUG("calling SIMIX_req_communication_is_latency_bounded(%p)",
279          task->simdata->comm);
280   return SIMIX_req_comm_is_latency_bounded(task->simdata->comm);
281 }
282 #endif
283
284 /** \ingroup m_task_management
285  * \brief Returns the size of the data attached to a task #m_task_t.
286  *
287  */
288 double MSG_task_get_data_size(m_task_t task)
289 {
290   xbt_assert((task != NULL)
291               && (task->simdata != NULL), "Invalid parameter");
292
293   return task->simdata->message_size;
294 }
295
296
297
298 /** \ingroup m_task_management
299  * \brief Changes the priority of a computation task. This priority doesn't affect 
300  *        the transfer rate. A priority of 2 will make a task receive two times more
301  *        cpu power than the other ones.
302  *
303  */
304 void MSG_task_set_priority(m_task_t task, double priority)
305 {
306   xbt_assert((task != NULL)
307               && (task->simdata != NULL), "Invalid parameter");
308
309   task->simdata->priority = 1 / priority;
310   if (task->simdata->compute)
311     SIMIX_req_host_execution_set_priority(task->simdata->compute,
312                                       task->simdata->priority);
313 }