Logo AND Algorithmique Numérique Distribuée

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