Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rework the MSG navbar
[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  *  (#m_task_t) and the functions for managing it.
15  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
16  * 
17  *  Since most scheduling algorithms rely on a concept of task
18  *  that can be either <em>computed</em> locally or
19  *  <em>transferred</em> on another processor, it seems to be the
20  *  right level of abstraction for our purposes. A <em>task</em>
21  *  may then be defined by a <em>computing amount</em>, a
22  *  <em>message size</em> and some <em>private data</em>.
23  */
24
25 /********************************* Task **************************************/
26 /** \ingroup m_task_management
27  * \brief Creates a new #m_task_t.
28  *
29  * A constructor for #m_task_t taking four arguments and returning the 
30    corresponding object.
31  * \param name a name for the object. It is for user-level information
32    and can be NULL.
33  * \param compute_duration a value of the processing amount (in Mflop)
34    needed to process this new task. If 0, then it cannot be executed with
35    MSG_task_execute(). This value has to be >=0.
36  * \param message_size a value of the amount of data (in Mb) needed to
37    transfer this new task. If 0, then it cannot be transfered with
38    MSG_task_get() and MSG_task_put(). This value has to be >=0.
39  * \param data a pointer to any data may want to attach to the new
40    object.  It is for user-level information and can be NULL. It can
41    be retrieved with the function \ref MSG_task_get_data.
42  * \see m_task_t
43  * \return The new corresponding object.
44  */
45 m_task_t MSG_task_create(const char *name, double compute_duration,
46                          double message_size, void *data)
47 {
48   simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
49   m_task_t task = xbt_new0(s_m_task_t,1);
50   
51   /* Task structure */
52   task->name = xbt_strdup(name);
53   task->simdata = simdata;
54   task->data = data;
55
56   /* Simulator Data */
57   simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
58   simdata->computation_amount = compute_duration;
59   simdata->message_size = message_size;
60   simdata->rate = -1.0;
61   simdata->priority = 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 source of a #m_task_t.
94  *
95  * This functions returns the #m_host_t from which this task was sent
96  */
97 m_host_t MSG_task_get_source(m_task_t task)
98 {
99    xbt_assert0(task, "Invalid parameters");
100    return ((simdata_task_t) task->simdata)->source;
101 }
102
103 /** \ingroup m_task_management
104  * \brief Return the name of a #m_task_t.
105  *
106  * This functions returns the name of a #m_task_t as specified on creation
107  */
108 const char *MSG_task_get_name(m_task_t task)
109 {
110    xbt_assert0(task, "Invalid parameters");
111    return task->name;
112 }
113
114
115 /** \ingroup m_task_management
116  * \brief Destroy a #m_task_t.
117  *
118  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
119    before calling this function.
120  */
121 MSG_error_t MSG_task_destroy(m_task_t task)
122 {
123   surf_action_t action = NULL;
124
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   free(task->simdata);
144   free(task);
145
146   return MSG_OK;
147 }
148
149
150 /** \ingroup m_task_management
151  * \brief Cancel a #m_task_t.
152  * \param task the taskt to cancel. If it was executed or transfered, it 
153           stops the process that were working on it.
154  */
155 MSG_error_t MSG_task_cancel(m_task_t task)
156 {
157   xbt_assert0((task != NULL), "Invalid parameter");
158
159   if(task->simdata->compute) {
160     surf_workstation_resource->common_public->action_cancel(task->simdata->compute);
161     return MSG_OK;
162   }
163   if(task->simdata->comm) {
164     surf_workstation_resource->common_public->action_cancel(task->simdata->comm);
165     return MSG_OK;
166   }
167
168   return MSG_FATAL;
169 }
170
171 /** \ingroup m_task_management
172  * \brief Returns the computation amount needed to process a task #m_task_t.
173  *        Once a task has been processed, this amount is thus set to 0...
174  */
175 double MSG_task_get_compute_duration(m_task_t task) {
176   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
177
178   return task->simdata->computation_amount;
179 }
180
181 /** \ingroup m_task_management
182  * \brief Returns the remaining computation amount of a task #m_task_t.
183  *
184  */
185 double MSG_task_get_remaining_computation(m_task_t task)
186 {
187   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
188
189   if(task->simdata->compute) {
190     return task->simdata->compute->remains;
191   } else {
192     return task->simdata->computation_amount;
193   }
194 }
195
196 /** \ingroup m_task_management
197  * \brief Returns the size of the data attached to a task #m_task_t.
198  *
199  */
200 double MSG_task_get_data_size(m_task_t task) {
201   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
202
203   return task->simdata->message_size;
204 }
205
206 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
207 {
208   int _cursor;
209   m_process_t proc = NULL;
210
211   xbt_assert0(((task != NULL)
212                && (task->simdata != NULL)), "Invalid parameters");
213
214   xbt_dynar_push(task->simdata->sleeping, &process);
215   process->simdata->waiting_task = task;
216   xbt_context_yield();
217   process->simdata->waiting_task = NULL;
218   xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
219     if(proc==process) 
220       xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);
221   }
222
223   return MSG_OK;
224 }
225
226
227 /** \ingroup m_task_management
228  * \brief Changes the priority of a computation task. This priority doesn't affect 
229  *        the transfer rate. A priority of 2 will make a task receive two times more
230  *        cpu power than the other ones.
231  *
232  */
233 void MSG_task_set_priority(m_task_t task, double priority) {
234   xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
235
236   task->simdata->priority = 1/priority;
237   if(task->simdata->compute)
238     surf_workstation_resource->common_public->
239       set_priority(task->simdata->compute, task->simdata->priority);
240 }