Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Necessity to change the code to implement MSG..
[simgrid.git] / src / msg_simix / task.c
1 #include "private.h"
2 #include "xbt/sysdep.h"
3 #include "xbt/log.h"
4
5 /** \defgroup m_task_management Managing functions of Tasks
6  *  \brief This section describes the task structure of MSG
7  *  (#m_task_t) and the functions for managing it.
8  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
9  * 
10  *  Since most scheduling algorithms rely on a concept of task
11  *  that can be either <em>computed</em> locally or
12  *  <em>transferred</em> on another processor, it seems to be the
13  *  right level of abstraction for our purposes. A <em>task</em>
14  *  may then be defined by a <em>computing amount</em>, a
15  *  <em>message size</em> and some <em>private data</em>.
16  */
17
18 /********************************* Task **************************************/
19 /** \ingroup m_task_management
20  * \brief Creates a new #m_task_t.
21  *
22  * A constructor for #m_task_t taking four arguments and returning the 
23    corresponding object.
24  * \param name a name for the object. It is for user-level information
25    and can be NULL.
26  * \param compute_duration a value of the processing amount (in flop)
27    needed to process this new task. If 0, then it cannot be executed with
28    MSG_task_execute(). This value has to be >=0.
29  * \param message_size a value of the amount of data (in bytes) needed to
30    transfer this new task. If 0, then it cannot be transfered with
31    MSG_task_get() and MSG_task_put(). This value has to be >=0.
32  * \param data a pointer to any data may want to attach to the new
33    object.  It is for user-level information and can be NULL. It can
34    be retrieved with the function \ref MSG_task_get_data.
35  * \see m_task_t
36  * \return The new corresponding object.
37  */
38 m_task_t MSG_task_create(const char *name, double compute_duration,
39                          double message_size, void *data)
40 {
41         m_task_t task = xbt_new(s_m_task_t,1);
42   simdata_task_t simdata = task->simdata;
43   task->simdata = simdata;
44   /* Task structure */
45   task->name = xbt_strdup(name);
46   task->data = data;
47
48   /* Simulator Data */
49   simdata->computation_amount = compute_duration;
50   simdata->message_size = message_size;
51   simdata->rate = -1.0;
52   simdata->priority = 1.0;
53   simdata->using = 1;
54   simdata->sender = NULL;
55         simdata->cond = SIMIX_cond_init();
56         simdata->mutex = SIMIX_mutex_init();
57
58   return task;
59 }
60
61 /** \ingroup m_task_management
62  * \brief Return the user data of a #m_task_t.
63  *
64  * This functions checks whether \a task is a valid pointer or not and return
65    the user data associated to \a task if it is possible.
66  */
67 void *MSG_task_get_data(m_task_t task)
68 {
69   xbt_assert0((task != NULL), "Invalid parameter");
70
71   return (task->data);
72 }
73
74 /** \ingroup m_task_management
75  * \brief Return the sender of a #m_task_t.
76  *
77  * This functions returns the #m_process_t which sent this task
78  */
79 m_process_t MSG_task_get_sender(m_task_t task)
80 {
81    xbt_assert0(task, "Invalid parameters");
82    return ((simdata_task_t) task->simdata)->sender;
83 }
84
85 /** \ingroup m_task_management
86  * \brief Return the source of a #m_task_t.
87  *
88  * This functions returns the #m_host_t from which this task was sent
89  */
90 m_host_t MSG_task_get_source(m_task_t task)
91 {
92    xbt_assert0(task, "Invalid parameters");
93    return ((simdata_task_t) task->simdata)->source;
94 }
95
96 /** \ingroup m_task_management
97  * \brief Return the name of a #m_task_t.
98  *
99  * This functions returns the name of a #m_task_t as specified on creation
100  */
101 const char *MSG_task_get_name(m_task_t task)
102 {
103    xbt_assert0(task, "Invalid parameters");
104    return task->name;
105 }
106
107
108 /** \ingroup m_task_management
109  * \brief Destroy a #m_task_t.
110  *
111  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
112    before calling this function.
113  */
114 MSG_error_t MSG_task_destroy(m_task_t task)
115 {
116   smx_action_t action = NULL;
117   xbt_assert0((task != NULL), "Invalid parameter");
118
119         /* 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 code wrong? */
120   task->simdata->using--;
121   if(task->simdata->using>0) return MSG_OK;
122
123   if(task->name) free(task->name);
124
125         SIMIX_cond_destroy(task->simdata->cond);
126         SIMIX_mutex_destroy(task->simdata->mutex);
127
128   action = task->simdata->compute;
129   if(action) SIMIX_action_destroy(action);
130   action = task->simdata->comm;
131   if(action) SIMIX_action_destroy(action);
132         /* parallel tasks only */ 
133   if(task->simdata->host_list) xbt_free(task->simdata->host_list);
134         
135         /* free main structures */
136         xbt_free(task->simdata);
137         xbt_free(task);
138
139   return MSG_OK;
140 }
141
142
143 /** \ingroup m_task_management
144  * \brief Cancel a #m_task_t.
145  * \param task the taskt to cancel. If it was executed or transfered, it 
146           stops the process that were working on it.
147  */
148 MSG_error_t MSG_task_cancel(m_task_t task)
149 {
150   xbt_assert0((task != NULL), "Invalid parameter");
151
152   if(task->simdata->compute) {
153                 SIMIX_action_cancel(task->simdata->compute);
154     return MSG_OK;
155   }
156   if(task->simdata->comm) {
157                 SIMIX_action_cancel(task->simdata->comm);
158     return MSG_OK;
159   }
160
161   return MSG_FATAL;
162 }
163
164 /** \ingroup m_task_management
165  * \brief Returns the computation amount needed to process a task #m_task_t.
166  *        Once a task has been processed, this amount is thus set to 0...
167  */
168 double MSG_task_get_compute_duration(m_task_t task) 
169 {
170   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
171
172   return task->simdata->computation_amount;
173 }
174
175 /** \ingroup m_task_management
176  * \brief Returns the remaining computation amount of a task #m_task_t.
177  *
178  */
179 double MSG_task_get_remaining_computation(m_task_t task)
180 {
181   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
182
183   if(task->simdata->compute) {
184     return SIMIX_action_get_remains(task->simdata->compute);
185   } else {
186     return task->simdata->computation_amount;
187   }
188 }
189
190 /** \ingroup m_task_management
191  * \brief Returns the size of the data attached to a task #m_task_t.
192  *
193  */
194 double MSG_task_get_data_size(m_task_t task) 
195 {
196   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
197
198   return task->simdata->message_size;
199 }
200
201
202
203 /** \ingroup m_task_management
204  * \brief Changes the priority of a computation task. This priority doesn't affect 
205  *        the transfer rate. A priority of 2 will make a task receive two times more
206  *        cpu power than the other ones.
207  *
208  */
209 void MSG_task_set_priority(m_task_t task, double priority) 
210 {
211   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
212
213   task->simdata->priority = 1/priority;
214   if(task->simdata->compute)
215                 SIMIX_action_set_priority(task->simdata->compute, task->simdata->priority);
216 }
217