Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8aae84d88364210d2ee8436ccdc4e2de9a969599
[simgrid.git] / src / msg / task.c
1 /*     $Id$      */
2
3 /* Copyright (c) 2002-2007 Arnaud Legrand.                                  */
4 /* Copyright (c) 2007 Bruno Donassolo.                                      */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13
14 /** \defgroup m_task_management Managing functions of Tasks
15  *  \brief This section describes the task structure of MSG
16  *  (#m_task_t) and the functions for managing it.
17  */
18 /** @addtogroup m_task_management
19  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
20  * 
21  *  Since most scheduling algorithms rely on a concept of task
22  *  that can be either <em>computed</em> locally or
23  *  <em>transferred</em> on another processor, it seems to be the
24  *  right level of abstraction for our purposes. A <em>task</em>
25  *  may then be defined by a <em>computing amount</em>, a
26  *  <em>message size</em> and some <em>private data</em>.
27  */
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->computation_amount = compute_duration;
61   simdata->message_size = message_size;
62   simdata->rate = -1.0;
63   simdata->priority = 1.0;
64   simdata->refcount = 1;
65   simdata->sender = NULL;
66   simdata->receiver = NULL;
67   simdata->cond = SIMIX_cond_init();
68   simdata->mutex = SIMIX_mutex_init();
69   simdata->compute = NULL;
70   simdata->comm = NULL;
71
72   simdata->host_list = NULL;
73   simdata->comp_amount = NULL;
74   simdata->comm_amount = NULL;
75
76   return task;
77 }
78
79 /** prevent the task from being destroyed too quickly (but also prevent it from being sent). Mainly useful in bindings */
80 void MSG_task_ref(m_task_t t) {
81   t->simdata->refcount++;
82 }
83
84 /** \ingroup m_task_management
85  * \brief Return the user data of a #m_task_t.
86  *
87  * This functions checks whether \a task is a valid pointer or not and return
88    the user data associated to \a task if it is possible.
89  */
90 void *MSG_task_get_data(m_task_t task)
91 {
92   xbt_assert0((task != NULL), "Invalid parameter");
93
94   return (task->data);
95 }
96
97 /** \ingroup m_task_management
98  * \brief Return the sender of a #m_task_t.
99  *
100  * This functions returns the #m_process_t which sent this task
101  */
102 m_process_t MSG_task_get_sender(m_task_t task)
103 {
104   xbt_assert0(task, "Invalid parameters");
105   return ((simdata_task_t) task->simdata)->sender;
106 }
107
108 /** \ingroup m_task_management
109  * \brief Return the source of a #m_task_t.
110  *
111  * This functions returns the #m_host_t from which this task was sent
112  */
113 m_host_t MSG_task_get_source(m_task_t task)
114 {
115   xbt_assert0(task, "Invalid parameters");
116   return ((simdata_task_t) task->simdata)->source;
117 }
118
119 /** \ingroup m_task_management
120  * \brief Return the name of a #m_task_t.
121  *
122  * This functions returns the name of a #m_task_t as specified on creation
123  */
124 const char *MSG_task_get_name(m_task_t task)
125 {
126   xbt_assert0(task, "Invalid parameters");
127   return task->name;
128 }
129
130
131 /** \ingroup m_task_management
132  * \brief Destroy a #m_task_t.
133  *
134  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
135    before calling this function.
136  */
137 MSG_error_t MSG_task_destroy(m_task_t task)
138 {
139   smx_action_t action = NULL;
140   xbt_assert0((task != NULL), "Invalid parameter");
141
142   /* 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? */
143   task->simdata->refcount--;
144   if (task->simdata->refcount > 0)
145     return MSG_OK;
146
147   if (task->name)
148     free(task->name);
149
150   SIMIX_cond_destroy(task->simdata->cond);
151   SIMIX_mutex_destroy(task->simdata->mutex);
152
153   action = task->simdata->compute;
154   if (action)
155     SIMIX_action_destroy(action);
156   
157   /* parallel tasks only */
158   if (task->simdata->host_list)
159     xbt_free(task->simdata->host_list);
160
161   /* free main structures */
162   xbt_free(task->simdata);
163   xbt_free(task);
164
165   return MSG_OK;
166 }
167
168
169 /** \ingroup m_task_management
170  * \brief Cancel a #m_task_t.
171  * \param task the taskt to cancel. If it was executed or transfered, it 
172           stops the process that were working on it.
173  */
174 MSG_error_t MSG_task_cancel(m_task_t task)
175 {
176   xbt_assert0((task != NULL), "Invalid parameter");
177
178   if (task->simdata->compute) {
179     SIMIX_action_cancel(task->simdata->compute);
180     return MSG_OK;
181   }
182   if (task->simdata->comm) {
183     SIMIX_communication_cancel(task->simdata->comm);
184     return MSG_OK;
185   }
186   THROW_IMPOSSIBLE;
187 }
188
189 /** \ingroup m_task_management
190  * \brief Returns the computation amount needed to process a task #m_task_t.
191  *        Once a task has been processed, this amount is thus set to 0...
192  */
193 double MSG_task_get_compute_duration(m_task_t task)
194 {
195   xbt_assert0((task != NULL)
196               && (task->simdata != NULL), "Invalid parameter");
197
198   return task->simdata->computation_amount;
199 }
200
201 /** \ingroup m_task_management
202  * \brief Returns the remaining computation amount of a task #m_task_t.
203  *
204  */
205 double MSG_task_get_remaining_computation(m_task_t task)
206 {
207   xbt_assert0((task != NULL)
208               && (task->simdata != NULL), "Invalid parameter");
209
210   if (task->simdata->compute) {
211     return SIMIX_action_get_remains(task->simdata->compute);
212   } else {
213     return task->simdata->computation_amount;
214   }
215 }
216
217
218
219 /** \ingroup m_task_management
220  * \brief Returns the total amount received by a task #m_task_t.
221  *
222  */
223 double MSG_task_get_remaining_communication(m_task_t task)
224 {
225   xbt_assert0((task != NULL)
226               && (task->simdata != NULL), "Invalid parameter");
227
228   return SIMIX_communication_get_remains(task->simdata->comm);
229 }
230
231 /** \ingroup m_task_management
232  * \brief Returns the size of the data attached to a task #m_task_t.
233  *
234  */
235 double MSG_task_get_data_size(m_task_t task)
236 {
237   xbt_assert0((task != NULL)
238               && (task->simdata != NULL), "Invalid parameter");
239
240   return task->simdata->message_size;
241 }
242
243
244
245 /** \ingroup m_task_management
246  * \brief Changes the priority of a computation task. This priority doesn't affect 
247  *        the transfer rate. A priority of 2 will make a task receive two times more
248  *        cpu power than the other ones.
249  *
250  */
251 void MSG_task_set_priority(m_task_t task, double priority)
252 {
253   xbt_assert0((task != NULL)
254               && (task->simdata != NULL), "Invalid parameter");
255
256   task->simdata->priority = 1 / priority;
257   if (task->simdata->compute)
258     SIMIX_action_set_priority(task->simdata->compute,
259                               task->simdata->priority);
260 }