Logo AND Algorithmique Numérique Distribuée

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