Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d058cbe4762bb6152c11284b04e16157d9aa9bdf
[simgrid.git] / src / msg / task.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11
12 /** \defgroup m_task_management Managing functions of Tasks
13  *  \brief This section describes the task structure of MSG
14  */
15 /** \addtogroup m_task_management
16  *  (#m_task_t) and the functions for managing it.
17  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
18  * 
19  *  Since most scheduling algorithms rely on a concept of task
20  *  that can be either <em>computed</em> locally or
21  *  <em>transferred</em> on another processor, it seems to be the
22  *  right level of abstraction for our purposes. A <em>task</em>
23  *  may then be defined by a <em>computing amount</em>, a
24  *  <em>message size</em> and some <em>private data</em>.
25  */
26
27 /********************************* Task **************************************/
28 /** \ingroup m_task_management
29  * \brief Creates a new #m_task_t.
30  *
31  * A constructor for #m_task_t taking four arguments and returning the 
32    corresponding object.
33  * \param name a name for the object. It is for user-level information
34    and can be NULL.
35  * \param compute_duration a value of the processing amount (in flop)
36    needed to process this new task. If 0, then it cannot be executed with
37    MSG_task_execute(). This value has to be >=0.
38  * \param message_size a value of the amount of data (in bytes) needed to
39    transfer this new task. If 0, then it cannot be transfered with
40    MSG_task_get() and MSG_task_put(). This value has to be >=0.
41  * \param data a pointer to any data may want to attach to the new
42    object.  It is for user-level information and can be NULL. It can
43    be retrieved with the function \ref MSG_task_get_data.
44  * \see m_task_t
45  * \return The new corresponding object.
46  */
47 m_task_t MSG_task_create(const char *name, double compute_duration,
48                          double message_size, void *data)
49 {
50   m_task_t task = xbt_mallocator_get(msg_global->task_mallocator);
51   simdata_task_t simdata = task->simdata;
52   
53   /* Task structure */
54   task->name = xbt_strdup(name);
55   task->data = data;
56
57   /* Simulator Data */
58   simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
59   simdata->computation_amount = compute_duration;
60   simdata->message_size = message_size;
61   simdata->rate = -1.0;
62   simdata->priority = 1.0;
63   simdata->using = 1;
64   simdata->sender = NULL;
65
66   return task;
67 }
68
69 /** \ingroup m_task_management
70  * \brief Return the user data of a #m_task_t.
71  *
72  * This functions checks whether \a task is a valid pointer or not and return
73    the user data associated to \a task if it is possible.
74  */
75 void *MSG_task_get_data(m_task_t task)
76 {
77   xbt_assert0((task != NULL), "Invalid parameter");
78
79   return (task->data);
80 }
81
82 /** \ingroup m_task_management
83  * \brief Return the sender of a #m_task_t.
84  *
85  * This functions returns the #m_process_t which sent this task
86  */
87 m_process_t MSG_task_get_sender(m_task_t task)
88 {
89    xbt_assert0(task, "Invalid parameters");
90    return ((simdata_task_t) task->simdata)->sender;
91 }
92
93 /** \ingroup m_task_management
94  * \brief Return the source of a #m_task_t.
95  *
96  * This functions returns the #m_host_t from which this task was sent
97  */
98 m_host_t MSG_task_get_source(m_task_t task)
99 {
100    xbt_assert0(task, "Invalid parameters");
101    return ((simdata_task_t) task->simdata)->source;
102 }
103
104 /** \ingroup m_task_management
105  * \brief Return the name of a #m_task_t.
106  *
107  * This functions returns the name of a #m_task_t as specified on creation
108  */
109 const char *MSG_task_get_name(m_task_t task)
110 {
111    xbt_assert0(task, "Invalid parameters");
112    return task->name;
113 }
114
115
116 /** \ingroup m_task_management
117  * \brief Destroy a #m_task_t.
118  *
119  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
120    before calling this function.
121  */
122 MSG_error_t MSG_task_destroy(m_task_t task)
123 {
124   surf_action_t action = NULL;
125   xbt_assert0((task != NULL), "Invalid parameter");
126
127   task->simdata->using--;
128   if(task->simdata->using>0) return MSG_OK;
129
130   xbt_assert0((xbt_dynar_length(task->simdata->sleeping)==0), 
131               "Task still used. There is a problem. Cannot destroy it now!");
132
133   if(task->name) free(task->name);
134
135   xbt_dynar_free(&(task->simdata->sleeping));
136
137   action = task->simdata->compute;
138   if(action) action->resource_type->common_public->action_free(action);
139   action = task->simdata->comm;
140   if(action) action->resource_type->common_public->action_free(action);
141   if(task->simdata->host_list) xbt_free(task->simdata->host_list);
142
143   xbt_mallocator_release(msg_global->task_mallocator, task);
144   return MSG_OK;
145 }
146
147
148 /** \ingroup m_task_management
149  * \brief Cancel a #m_task_t.
150  * \param task the taskt to cancel. If it was executed or transfered, it 
151           stops the process that were working on it.
152  */
153 MSG_error_t MSG_task_cancel(m_task_t task)
154 {
155   xbt_assert0((task != NULL), "Invalid parameter");
156
157   if(task->simdata->compute) {
158     surf_workstation_resource->common_public->action_cancel(task->simdata->compute);
159     return MSG_OK;
160   }
161   if(task->simdata->comm) {
162     surf_workstation_resource->common_public->action_cancel(task->simdata->comm);
163     return MSG_OK;
164   }
165
166   return MSG_FATAL;
167 }
168
169 /** \ingroup m_task_management
170  * \brief Returns the computation amount needed to process a task #m_task_t.
171  *        Once a task has been processed, this amount is thus set to 0...
172  */
173 double MSG_task_get_compute_duration(m_task_t task) {
174   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
175
176   return task->simdata->computation_amount;
177 }
178
179 /** \ingroup m_task_management
180  * \brief Returns the remaining computation amount of a task #m_task_t.
181  *
182  */
183 double MSG_task_get_remaining_computation(m_task_t task)
184 {
185   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
186
187   if(task->simdata->compute) {
188     return task->simdata->compute->remains;
189   } else {
190     return task->simdata->computation_amount;
191   }
192 }
193
194 /** \ingroup m_task_management
195  * \brief Returns the size of the data attached to a task #m_task_t.
196  *
197  */
198 double MSG_task_get_data_size(m_task_t task) {
199   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
200
201   return task->simdata->message_size;
202 }
203
204 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
205 {
206   int _cursor;
207   m_process_t proc = NULL;
208
209   xbt_assert0(((task != NULL)
210                && (task->simdata != NULL)), "Invalid parameters");
211
212   xbt_dynar_push(task->simdata->sleeping, &process);
213   process->simdata->waiting_task = task;
214   xbt_context_yield();
215   process->simdata->waiting_task = NULL;
216   xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
217     if(proc==process) 
218       xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);
219   }
220
221   return MSG_OK;
222 }
223
224
225 /** \ingroup m_task_management
226  * \brief Changes the priority of a computation task. This priority doesn't affect 
227  *        the transfer rate. A priority of 2 will make a task receive two times more
228  *        cpu power than the other ones.
229  *
230  */
231 void MSG_task_set_priority(m_task_t task, double priority) {
232   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
233
234   task->simdata->priority = 1/priority;
235   if(task->simdata->compute)
236     surf_workstation_resource->common_public->
237       set_priority(task->simdata->compute, task->simdata->priority);
238 }
239
240 /* Mallocator functions */
241 m_task_t task_mallocator_new_f(void) {
242   m_task_t task = xbt_new(s_m_task_t, 1);
243   simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
244   task->simdata = simdata;
245   return task;
246 }
247
248 void task_mallocator_free_f(m_task_t task) {
249   xbt_assert0((task != NULL), "Invalid parameter");
250
251   xbt_free(task->simdata);
252   xbt_free(task);
253
254   return;
255 }
256
257 void task_mallocator_reset_f(m_task_t task) {
258   memset(task->simdata, 0, sizeof(s_simdata_task_t));  
259 }