Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Last bits of convertion from xbt_error_t to exceptions. Some more cleanups (mainly...
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(task, msg,
12                                 "Logging specific to MSG (task)");
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  *  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 Mflop)
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 Mb) 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   simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
50   m_task_t task = xbt_new0(s_m_task_t,1);
51   
52   /* Task structure */
53   task->name = xbt_strdup(name);
54   task->simdata = simdata;
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->using = 1;
63   simdata->sender = NULL;
64
65   return task;
66 }
67
68 /** \ingroup m_task_management
69  * \brief Return the user data of a #m_task_t.
70  *
71  * This functions checks whether \a task is a valid pointer or not and return
72    the user data associated to \a task if it is possible.
73  */
74 void *MSG_task_get_data(m_task_t task)
75 {
76   xbt_assert0((task != NULL), "Invalid parameter");
77
78   return (task->data);
79 }
80
81 /** \ingroup m_task_management
82  * \brief Return the sender of a #m_task_t.
83  *
84  * This functions returns the #m_process_t which sent this task
85  */
86 m_process_t MSG_task_get_sender(m_task_t task)
87 {
88    xbt_assert0(task, "Invalid parameters");
89    return ((simdata_task_t) task->simdata)->sender;
90 }
91
92 /** \ingroup m_task_management
93  * \brief Return the name of a #m_task_t.
94  *
95  * This functions returns the name of a #m_task_t as specified on creation
96  */
97 const char *MSG_task_get_name(m_task_t task)
98 {
99    xbt_assert0(task, "Invalid parameters");
100    return task->name;
101 }
102
103
104 /** \ingroup m_task_management
105  * \brief Destroy a #m_task_t.
106  *
107  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
108    before calling this function.
109  */
110 MSG_error_t MSG_task_destroy(m_task_t task)
111 {
112   surf_action_t action = NULL;
113
114   xbt_assert0((task != NULL), "Invalid parameter");
115
116   task->simdata->using--;
117   if(task->simdata->using>0) return MSG_OK;
118
119   xbt_assert0((xbt_dynar_length(task->simdata->sleeping)==0), 
120               "Task still used. There is a problem. Cannot destroy it now!");
121
122   if(task->name) free(task->name);
123
124   xbt_dynar_free(&(task->simdata->sleeping));
125
126   action = task->simdata->compute;
127   if(action) action->resource_type->common_public->action_free(action);
128   action = task->simdata->comm;
129   if(action) action->resource_type->common_public->action_free(action);
130   if(task->simdata->host_list) xbt_free(task->simdata->host_list);
131
132   free(task->simdata);
133   free(task);
134
135   return MSG_OK;
136 }
137
138
139 /** \ingroup m_task_management
140  * \brief Cancel a #m_task_t.
141  * \param task the taskt to cancel. If it was executed or transfered, it 
142           stops the process that were working on it.
143  */
144 MSG_error_t MSG_task_cancel(m_task_t task)
145 {
146   xbt_assert0((task != NULL), "Invalid parameter");
147
148   if(task->simdata->compute) {
149     surf_workstation_resource->common_public->action_cancel(task->simdata->compute);
150     return MSG_OK;
151   }
152   if(task->simdata->comm) {
153     surf_workstation_resource->common_public->action_cancel(task->simdata->comm);
154     return MSG_OK;
155   }
156
157   return MSG_FATAL;
158 }
159
160 /** \ingroup m_task_management
161  * \brief Returns the computation amount needed to process a task #m_task_t.
162  *        Once a task has been processed, this amount is thus set to 0...
163  */
164 double MSG_task_get_compute_duration(m_task_t task) {
165   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
166
167   return task->simdata->computation_amount;
168 }
169
170 /** \ingroup m_task_management
171  * \brief Returns the remaining computation amount of a task #m_task_t.
172  *
173  */
174 double MSG_task_get_remaining_computation(m_task_t task)
175 {
176   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
177
178   if(task->simdata->compute) {
179     return task->simdata->compute->remains;
180   } else {
181     return task->simdata->computation_amount;
182   }
183 }
184
185 /** \ingroup m_task_management
186  * \brief Returns the size of the data attached to a task #m_task_t.
187  *
188  */
189 double MSG_task_get_data_size(m_task_t task) {
190   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
191
192   return task->simdata->message_size;
193 }
194
195 /* static MSG_error_t __MSG_task_check(m_task_t task) */
196 /* { */
197 /*   simdata_task_t simdata = NULL; */
198 /*   int warning = 0; */
199
200 /*   if (task == NULL) {                /\* Fatal *\/ */
201 /*     WARNING("Task uninitialized"); */
202 /*     return MSG_FATAL; */
203 /*   } */
204 /*   simdata = task->simdata; */
205
206 /*   if (simdata == NULL) {     /\* Fatal *\/ */
207 /*     WARNING("Simulator Data uninitialized"); */
208 /*     return MSG_FATAL; */
209 /*   } */
210
211 /*   if (simdata->compute == NULL) {    /\* Fatal if execute ... *\/ */
212 /*     WARNING("No duration set for this task"); */
213 /*     warning++; */
214 /*   } */
215
216 /*   if (simdata->message_size == 0) {  /\* Fatal if transfered ... *\/ */
217 /*     WARNING("No message_size set for this task"); */
218 /*     warning++; */
219 /*   } */
220
221 /* /\*    if (task->data == NULL) { *\/ */
222 /* /\*      WARNING("User Data uninitialized"); *\/ */
223 /* /\*      warning++; *\/ */
224 /* /\*    } *\/ */
225
226 /*   if (warning) */
227 /*     return MSG_WARNING; */
228 /*   return MSG_OK; */
229 /* } */
230
231 /* static m_task_t __MSG_task_copy(m_task_t src) */
232 /* { */
233 /*   m_task_t copy = NULL; */
234 /*   simdata_task_t simdata = NULL; */
235
236 /*   __MSG_task_check(src); */
237
238 /*   simdata = src->simdata; */
239 /*   copy = MSG_task_create(src->name, SG_getTaskCost(simdata->compute), */
240 /*                       simdata->message_size, MSG_task_get_data(src)); */
241
242 /*   return (copy); */
243 /* } */
244
245 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
246 {
247   int _cursor;
248   m_process_t proc = NULL;
249
250   xbt_assert0(((task != NULL)
251                && (task->simdata != NULL)), "Invalid parameters");
252
253   xbt_dynar_push(task->simdata->sleeping, &process);
254   process->simdata->waiting_task = task;
255   xbt_context_yield();
256   process->simdata->waiting_task = NULL;
257   xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
258     if(proc==process) 
259       xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);
260   }
261
262   return MSG_OK;
263 }