Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Doc improvement: rename GOS into task usage
[simgrid.git] / src / msg / msg_task.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "msg_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10
11 /** @addtogroup m_task_management
12  *    
13  * 
14  *  Since most scheduling algorithms rely on a concept of task
15  *  that can be either <em>computed</em> locally or
16  *  <em>transferred</em> on another processor, it seems to be the
17  *  right level of abstraction for our purposes. A <em>task</em>
18  *  may then be defined by a <em>computing amount</em>, a
19  *  <em>message size</em> and some <em>private data</em>.
20  */
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
23                                 "Logging specific to MSG (task)");
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 flop)
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 bytes) needed to
37    transfer this new task. If 0, then it cannot be transfered with
38    MSG_task_send() and MSG_task_recv(). 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   m_task_t task = xbt_new(s_m_task_t, 1);
49   simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
50   task->simdata = simdata;
51   /* Task structure */
52   task->name = xbt_strdup(name);
53   task->data = data;
54
55   /* Simulator Data */
56   simdata->host_nb = 0;
57   simdata->computation_amount = compute_duration;
58   simdata->message_size = message_size;
59   simdata->rate = -1.0;
60   simdata->priority = 1.0;
61   simdata->isused = 0;
62   simdata->sender = NULL;
63   simdata->receiver = NULL;
64   simdata->compute = NULL;
65   simdata->comm = NULL;
66
67   simdata->host_list = NULL;
68   simdata->comp_amount = NULL;
69   simdata->comm_amount = NULL;
70 #ifdef HAVE_TRACING
71   TRACE_msg_task_create(task);
72 #endif
73
74   return task;
75 }
76
77 /*************** Begin GPU ***************/
78 /** \ingroup m_task_management
79  * \brief Creates a new #m_gpu_task_t.
80
81  * A constructor for #m_gpu_task_t taking four arguments and returning
82    a pointer to the new created GPU task.
83
84  * \param name a name for the object. It is for user-level information
85    and can be NULL.
86
87  * \param compute_duration a value of the processing amount (in flop)
88    needed to process this new task. If 0, then it cannot be executed with
89    MSG_gpu_task_execute(). This value has to be >=0.
90
91  * \param dispatch_latency time in seconds to load this task on the GPU
92
93  * \param collect_latency time in seconds to transfer result from the GPU
94    back to the CPU (host) when done
95
96  * \see m_gpu_task_t
97  * \return The new corresponding object.
98  */
99 m_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
100                          double dispatch_latency, double collect_latency)
101 {
102   m_gpu_task_t task = xbt_new(s_m_gpu_task_t, 1);
103   simdata_gpu_task_t simdata = xbt_new(s_simdata_gpu_task_t, 1);
104   task->simdata = simdata;
105   /* Task structure */
106   task->name = xbt_strdup(name);
107
108   /* Simulator Data */
109   simdata->computation_amount = compute_duration;
110   simdata->dispatch_latency   = dispatch_latency;
111   simdata->collect_latency    = collect_latency;
112
113 #ifdef HAVE_TRACING
114   //FIXME
115   /* TRACE_msg_gpu_task_create(task); */
116 #endif
117
118   return task;
119 }
120 /*************** End GPU ***************/
121
122 /** \ingroup m_task_management
123  * \brief Return the user data of a #m_task_t.
124  *
125  * This function checks whether \a task is a valid pointer or not and return
126    the user data associated to \a task if it is possible.
127  */
128 void *MSG_task_get_data(m_task_t task)
129 {
130   xbt_assert((task != NULL), "Invalid parameter");
131
132   return (task->data);
133 }
134
135 /** \ingroup m_task_management
136  * \brief Sets the user data of a #m_task_t.
137  *
138  * This function allows to associate a new pointer to
139    the user data associated of \a task.
140  */
141 void MSG_task_set_data(m_task_t task, void *data)
142 {
143   xbt_assert((task != NULL), "Invalid parameter");
144
145   task->data = data;
146 }
147
148 /** \ingroup m_task_management
149  * \brief Sets a function to be called when a task has just been copied.
150  * \param callback a callback function
151  */
152 void MSG_task_set_copy_callback(void (*callback)
153     (m_task_t task, m_process_t sender, m_process_t receiver)) {
154
155   msg_global->task_copy_callback = callback;
156
157   if (callback) {
158     SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
159   }
160   else {
161     SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
162   }
163 }
164
165 /** \ingroup m_task_management
166  * \brief Return the sender of a #m_task_t.
167  *
168  * This functions returns the #m_process_t which sent this task
169  */
170 m_process_t MSG_task_get_sender(m_task_t task)
171 {
172   xbt_assert(task, "Invalid parameters");
173   return ((simdata_task_t) task->simdata)->sender;
174 }
175
176 /** \ingroup m_task_management
177  * \brief Return the source of a #m_task_t.
178  *
179  * This functions returns the #m_host_t from which this task was sent
180  */
181 m_host_t MSG_task_get_source(m_task_t task)
182 {
183   xbt_assert(task, "Invalid parameters");
184   return ((simdata_task_t) task->simdata)->source;
185 }
186
187 /** \ingroup m_task_management
188  * \brief Return the name of a #m_task_t.
189  *
190  * This functions returns the name of a #m_task_t as specified on creation
191  */
192 const char *MSG_task_get_name(m_task_t task)
193 {
194   xbt_assert(task, "Invalid parameters");
195   return task->name;
196 }
197
198 /** \ingroup m_task_management
199  * \brief Return the name of a #m_task_t.
200  *
201  * This functions allows to associate a name to a task
202  */
203 void MSG_task_set_name(m_task_t task, const char *name)
204 {
205   xbt_assert(task, "Invalid parameters");
206   task->name = xbt_strdup(name);
207 }
208
209 /** \ingroup m_task_management
210  * \brief Destroy a #m_task_t.
211  *
212  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
213  * before calling this function.
214  *
215  * Only the process that owns the task can destroy it.
216  * The owner changes after a successful send.
217  * If a task is successfully sent, the receiver becomes the owner and is
218  * supposed to destroy it. The sender should not use it anymore.
219  * If the task failed to be sent, the sender remains the owner of the task.
220  */
221 MSG_error_t MSG_task_destroy(m_task_t task)
222 {
223   smx_action_t action = NULL;
224   xbt_assert((task != NULL), "Invalid parameter");
225
226   if (task->simdata->isused) {
227     /* the task is being sent or executed: cancel it first */
228     MSG_task_cancel(task);
229   }
230 #ifdef HAVE_TRACING
231   TRACE_msg_task_destroy(task);
232 #endif
233
234   xbt_free(task->name);
235
236   action = task->simdata->compute;
237   if (action)
238     simcall_host_execution_destroy(action);
239
240   /* parallel tasks only */
241   xbt_free(task->simdata->host_list);
242
243   /* free main structures */
244   xbt_free(task->simdata);
245   xbt_free(task);
246
247   return MSG_OK;
248 }
249
250
251 /** \ingroup m_task_management
252  * \brief Cancel a #m_task_t.
253  * \param task the task to cancel. If it was executed or transfered, it
254           stops the process that were working on it.
255  */
256 MSG_error_t MSG_task_cancel(m_task_t task)
257 {
258   xbt_assert((task != NULL), "Invalid parameter");
259
260   if (task->simdata->compute) {
261     simcall_host_execution_cancel(task->simdata->compute);
262   }
263   else if (task->simdata->comm) {
264     simcall_comm_cancel(task->simdata->comm);
265     task->simdata->isused = 0;
266   }
267   return MSG_OK;
268 }
269
270 /** \ingroup m_task_management
271  * \brief Returns the computation amount needed to process a task #m_task_t.
272  *        Once a task has been processed, this amount is thus set to 0...
273  */
274 double MSG_task_get_compute_duration(m_task_t task)
275 {
276   xbt_assert((task != NULL)
277               && (task->simdata != NULL), "Invalid parameter");
278
279   return task->simdata->computation_amount;
280 }
281
282
283 /** \ingroup m_task_management
284  * \brief set the computation amount needed to process a task #m_task_t.
285  */
286
287 void MSG_task_set_compute_duration(m_task_t task,
288                                    double computation_amount)
289 {
290   xbt_assert(task, "Invalid parameter");
291   task->simdata->computation_amount = computation_amount;
292
293 }
294
295 /** \ingroup m_task_management
296  * \brief Returns the remaining computation amount of a task #m_task_t.
297  *
298  */
299 double MSG_task_get_remaining_computation(m_task_t task)
300 {
301   xbt_assert((task != NULL)
302               && (task->simdata != NULL), "Invalid parameter");
303
304   if (task->simdata->compute) {
305     return simcall_host_execution_get_remains(task->simdata->compute);
306   } else {
307     return task->simdata->computation_amount;
308   }
309 }
310
311 /** \ingroup m_task_management
312  * \brief Returns the total amount received by a task #m_task_t.
313  *        If the communication does not exist it will return 0.
314  *        So, if the communication has FINISHED or FAILED it returns
315  *        zero.
316  */
317 double MSG_task_get_remaining_communication(m_task_t task)
318 {
319   xbt_assert((task != NULL)
320               && (task->simdata != NULL), "Invalid parameter");
321   XBT_DEBUG("calling simcall_communication_get_remains(%p)",
322          task->simdata->comm);
323   return simcall_comm_get_remains(task->simdata->comm);
324 }
325
326 #ifdef HAVE_LATENCY_BOUND_TRACKING
327 /** \ingroup m_task_management
328  * \brief Return 1 if communication task is limited by latency, 0 otherwise
329  *
330  */
331 int MSG_task_is_latency_bounded(m_task_t task)
332 {
333   xbt_assert((task != NULL)
334               && (task->simdata != NULL), "Invalid parameter");
335   XBT_DEBUG("calling simcall_communication_is_latency_bounded(%p)",
336          task->simdata->comm);
337   return simcall_comm_is_latency_bounded(task->simdata->comm);
338 }
339 #endif
340
341 /** \ingroup m_task_management
342  * \brief Returns the size of the data attached to a task #m_task_t.
343  *
344  */
345 double MSG_task_get_data_size(m_task_t task)
346 {
347   xbt_assert((task != NULL)
348               && (task->simdata != NULL), "Invalid parameter");
349
350   return task->simdata->message_size;
351 }
352
353
354
355 /** \ingroup m_task_management
356  * \brief Changes the priority of a computation task. This priority doesn't affect 
357  *        the transfer rate. A priority of 2 will make a task receive two times more
358  *        cpu power than the other ones.
359  *
360  */
361 void MSG_task_set_priority(m_task_t task, double priority)
362 {
363   xbt_assert((task != NULL)
364               && (task->simdata != NULL), "Invalid parameter");
365
366   task->simdata->priority = 1 / priority;
367   if (task->simdata->compute)
368     simcall_host_execution_set_priority(task->simdata->compute,
369                                       task->simdata->priority);
370 }