Logo AND Algorithmique Numérique Distribuée

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