Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a missing file and add MSG_vm_set_bound
[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 #msg_task_t.
28  *
29  * A constructor for #msg_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 msg_task_t
43  * \return The new corresponding object.
44  */
45 msg_task_t MSG_task_create(const char *name, double compute_duration,
46                          double message_size, void *data)
47 {
48   msg_task_t task = xbt_new(s_msg_task_t, 1);
49   simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
50   task->simdata = simdata;
51
52   /* Task structure */
53   task->name = xbt_strdup(name);
54   task->data = data;
55
56   /* Simulator Data */
57   simdata->compute = NULL;
58   simdata->comm = NULL;
59   simdata->message_size = message_size;
60   simdata->computation_amount = compute_duration;
61   simdata->sender = NULL;
62   simdata->receiver = NULL;
63   simdata->source = NULL;
64   simdata->priority = 1.0;
65   simdata->bound = 0;
66   simdata->rate = -1.0;
67   simdata->isused = 0;
68
69   simdata->host_nb = 0;
70   simdata->host_list = NULL;
71   simdata->comp_amount = NULL;
72   simdata->comm_amount = NULL;
73 #ifdef HAVE_TRACING
74   TRACE_msg_task_create(task);
75 #endif
76
77   return task;
78 }
79
80 /** \ingroup m_task_management
81  * \brief Creates a new #msg_task_t (a parallel one....).
82  *
83  * A constructor for #msg_task_t taking six arguments and returning the
84  corresponding object.
85  * \param name a name for the object. It is for user-level information
86  and can be NULL.
87  * \param host_nb the number of hosts implied in the parallel task.
88  * \param host_list an array of \p host_nb msg_host_t.
89  * \param computation_amount an array of \p host_nb
90  doubles. computation_amount[i] is the total number of operations
91  that have to be performed on host_list[i].
92  * \param communication_amount an array of \p host_nb* \p host_nb doubles.
93  * \param data a pointer to any data may want to attach to the new
94  object.  It is for user-level information and can be NULL. It can
95  be retrieved with the function \ref MSG_task_get_data.
96  * \see msg_task_t
97  * \return The new corresponding object.
98  */
99 msg_task_t
100 MSG_parallel_task_create(const char *name, int host_nb,
101                          const msg_host_t * host_list,
102                          double *computation_amount,
103                          double *communication_amount, void *data)
104 {
105   msg_task_t task = MSG_task_create(name, 0, 0, data);
106   simdata_task_t simdata = task->simdata;
107   int i;
108
109   /* Simulator Data specific to parallel tasks */
110   simdata->host_nb = host_nb;
111   simdata->host_list = xbt_new0(smx_host_t, host_nb);
112   simdata->comp_amount = computation_amount;
113   simdata->comm_amount = communication_amount;
114
115   for (i = 0; i < host_nb; i++)
116     simdata->host_list[i] = host_list[i];
117
118   return task;
119 }
120
121 /*************** Begin GPU ***************/
122 /** \ingroup m_task_management
123  * \brief Creates a new #msg_gpu_task_t.
124
125  * A constructor for #msg_gpu_task_t taking four arguments and returning
126    a pointer to the new created GPU task.
127
128  * \param name a name for the object. It is for user-level information
129    and can be NULL.
130
131  * \param compute_duration a value of the processing amount (in flop)
132    needed to process this new task. If 0, then it cannot be executed with
133    MSG_gpu_task_execute(). This value has to be >=0.
134
135  * \param dispatch_latency time in seconds to load this task on the GPU
136
137  * \param collect_latency time in seconds to transfer result from the GPU
138    back to the CPU (host) when done
139
140  * \see msg_gpu_task_t
141  * \return The new corresponding object.
142  */
143 msg_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
144                          double dispatch_latency, double collect_latency)
145 {
146   msg_gpu_task_t task = xbt_new(s_msg_gpu_task_t, 1);
147   simdata_gpu_task_t simdata = xbt_new(s_simdata_gpu_task_t, 1);
148   task->simdata = simdata;
149   /* Task structure */
150   task->name = xbt_strdup(name);
151
152   /* Simulator Data */
153   simdata->computation_amount = compute_duration;
154   simdata->dispatch_latency   = dispatch_latency;
155   simdata->collect_latency    = collect_latency;
156
157 #ifdef HAVE_TRACING
158   //FIXME
159   /* TRACE_msg_gpu_task_create(task); */
160 #endif
161
162   return task;
163 }
164 /*************** End GPU ***************/
165
166 /** \ingroup m_task_management
167  * \brief Return the user data of a #msg_task_t.
168  *
169  * This function checks whether \a task is a valid pointer or not and return
170    the user data associated to \a task if it is possible.
171  */
172 void *MSG_task_get_data(msg_task_t task)
173 {
174   xbt_assert((task != NULL), "Invalid parameter");
175
176   return (task->data);
177 }
178
179 /** \ingroup m_task_management
180  * \brief Sets the user data of a #msg_task_t.
181  *
182  * This function allows to associate a new pointer to
183    the user data associated of \a task.
184  */
185 void MSG_task_set_data(msg_task_t task, void *data)
186 {
187   xbt_assert((task != NULL), "Invalid parameter");
188
189   task->data = data;
190 }
191
192 /** \ingroup m_task_management
193  * \brief Sets a function to be called when a task has just been copied.
194  * \param callback a callback function
195  */
196 void MSG_task_set_copy_callback(void (*callback)
197     (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
198
199   msg_global->task_copy_callback = callback;
200
201   if (callback) {
202     SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
203   }
204   else {
205     SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
206   }
207 }
208
209 /** \ingroup m_task_management
210  * \brief Return the sender of a #msg_task_t.
211  *
212  * This functions returns the #msg_process_t which sent this task
213  */
214 msg_process_t MSG_task_get_sender(msg_task_t task)
215 {
216   xbt_assert(task, "Invalid parameters");
217   return ((simdata_task_t) task->simdata)->sender;
218 }
219
220 /** \ingroup m_task_management
221  * \brief Return the source of a #msg_task_t.
222  *
223  * This functions returns the #msg_host_t from which this task was sent
224  */
225 msg_host_t MSG_task_get_source(msg_task_t task)
226 {
227   xbt_assert(task, "Invalid parameters");
228   return ((simdata_task_t) task->simdata)->source;
229 }
230
231 /** \ingroup m_task_management
232  * \brief Return the name of a #msg_task_t.
233  *
234  * This functions returns the name of a #msg_task_t as specified on creation
235  */
236 const char *MSG_task_get_name(msg_task_t task)
237 {
238   xbt_assert(task, "Invalid parameters");
239   return task->name;
240 }
241
242 /** \ingroup m_task_management
243  * \brief Sets the name of a #msg_task_t.
244  *
245  * This functions allows to associate a name to a task
246  */
247 void MSG_task_set_name(msg_task_t task, const char *name)
248 {
249   xbt_assert(task, "Invalid parameters");
250   task->name = xbt_strdup(name);
251 }
252
253 /** \ingroup m_task_management
254  * \brief Destroy a #msg_task_t.
255  *
256  * Destructor for #msg_task_t. Note that you should free user data, if any, \b
257  * before calling this function.
258  *
259  * Only the process that owns the task can destroy it.
260  * The owner changes after a successful send.
261  * If a task is successfully sent, the receiver becomes the owner and is
262  * supposed to destroy it. The sender should not use it anymore.
263  * If the task failed to be sent, the sender remains the owner of the task.
264  */
265 msg_error_t MSG_task_destroy(msg_task_t task)
266 {
267   smx_action_t action = NULL;
268   xbt_assert((task != NULL), "Invalid parameter");
269
270   if (task->simdata->isused) {
271     /* the task is being sent or executed: cancel it first */
272     MSG_task_cancel(task);
273   }
274 #ifdef HAVE_TRACING
275   TRACE_msg_task_destroy(task);
276 #endif
277
278   xbt_free(task->name);
279
280   action = task->simdata->compute;
281   if (action)
282     simcall_host_execution_destroy(action);
283
284   /* parallel tasks only */
285   xbt_free(task->simdata->host_list);
286
287   /* free main structures */
288   xbt_free(task->simdata);
289   xbt_free(task);
290
291   return MSG_OK;
292 }
293
294
295 /** \ingroup m_task_usage
296  * \brief Cancel a #msg_task_t.
297  * \param task the task to cancel. If it was executed or transfered, it
298           stops the process that were working on it.
299  */
300 msg_error_t MSG_task_cancel(msg_task_t task)
301 {
302   xbt_assert((task != NULL), "Cannot cancel a NULL task");
303
304   if (task->simdata->compute) {
305     simcall_host_execution_cancel(task->simdata->compute);
306   }
307   else if (task->simdata->comm) {
308     simcall_comm_cancel(task->simdata->comm);
309     task->simdata->isused = 0;
310   }
311   return MSG_OK;
312 }
313
314 /** \ingroup m_task_management
315  * \brief Returns the computation amount needed to process a task #msg_task_t.
316  *
317  * Once a task has been processed, this amount is set to 0. If you want, you
318  * can reset this value with #MSG_task_set_compute_duration before restarting the task.
319  */
320 double MSG_task_get_compute_duration(msg_task_t task)
321 {
322   xbt_assert((task != NULL)
323               && (task->simdata != NULL), "Invalid parameter");
324
325   return task->simdata->computation_amount;
326 }
327
328
329 /** \ingroup m_task_management
330  * \brief set the computation amount needed to process a task #msg_task_t.
331  *
332  * \warning If the computation is ongoing (already started and not finished),
333  * it is not modified by this call. And the termination of the ongoing task with
334  * set the computation_amount to zero, overriding any value set during the
335  * execution.
336  */
337
338 void MSG_task_set_compute_duration(msg_task_t task,
339                                    double computation_amount)
340 {
341   xbt_assert(task, "Invalid parameter");
342   task->simdata->computation_amount = computation_amount;
343
344 }
345
346 /** \ingroup m_task_management
347  * \brief set the amount data attached with a task #msg_task_t.
348  *
349  * \warning If the transfer is ongoing (already started and not finished),
350  * it is not modified by this call. 
351  */
352
353 void MSG_task_set_data_size(msg_task_t task,
354                                    double data_size)
355 {
356   xbt_assert(task, "Invalid parameter");
357   task->simdata->message_size = data_size;
358
359 }
360
361
362
363 /** \ingroup m_task_management
364  * \brief Returns the remaining computation amount of a task #msg_task_t.
365  *
366  * If the task is ongoing, this call retrieves the remaining amount of work.
367  * If it is not ongoing, it returns the total amount of work that will be
368  * executed when the task starts.
369  */
370 double MSG_task_get_remaining_computation(msg_task_t task)
371 {
372   xbt_assert((task != NULL)
373               && (task->simdata != NULL), "Invalid parameter");
374
375   if (task->simdata->compute) {
376     return simcall_host_execution_get_remains(task->simdata->compute);
377   } else {
378     return task->simdata->computation_amount;
379   }
380 }
381
382 /** \ingroup m_task_management
383  * \brief Returns the total amount received by a task #msg_task_t.
384  *        If the communication does not exist it will return 0.
385  *        So, if the communication has FINISHED or FAILED it returns
386  *        zero.
387  */
388 double MSG_task_get_remaining_communication(msg_task_t task)
389 {
390   xbt_assert((task != NULL)
391               && (task->simdata != NULL), "Invalid parameter");
392   XBT_DEBUG("calling simcall_communication_get_remains(%p)",
393          task->simdata->comm);
394   return simcall_comm_get_remains(task->simdata->comm);
395 }
396
397 #ifdef HAVE_LATENCY_BOUND_TRACKING
398 /** \ingroup m_task_management
399  * \brief Return 1 if communication task is limited by latency, 0 otherwise
400  *
401  */
402 int MSG_task_is_latency_bounded(msg_task_t task)
403 {
404   xbt_assert((task != NULL)
405               && (task->simdata != NULL), "Invalid parameter");
406   XBT_DEBUG("calling simcall_communication_is_latency_bounded(%p)",
407          task->simdata->comm);
408   return simcall_comm_is_latency_bounded(task->simdata->comm);
409 }
410 #endif
411
412 /** \ingroup m_task_management
413  * \brief Returns the size of the data attached to a task #msg_task_t.
414  *
415  */
416 double MSG_task_get_data_size(msg_task_t task)
417 {
418   xbt_assert((task != NULL)
419               && (task->simdata != NULL), "Invalid parameter");
420
421   return task->simdata->message_size;
422 }
423
424
425
426 /** \ingroup m_task_management
427  * \brief Changes the priority of a computation task. This priority doesn't affect 
428  *        the transfer rate. A priority of 2 will make a task receive two times more
429  *        cpu power than the other ones.
430  *
431  */
432 void MSG_task_set_priority(msg_task_t task, double priority)
433 {
434   xbt_assert((task != NULL)
435               && (task->simdata != NULL), "Invalid parameter");
436
437   task->simdata->priority = 1 / priority;
438   if (task->simdata->compute)
439     simcall_host_execution_set_priority(task->simdata->compute,
440                                       task->simdata->priority);
441 }
442
443
444 /** \ingroup m_task_management
445  * \brief Changes the maximum CPU utilization of a computation task.
446  *        Unit is flops/s.
447  *
448  * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
449  */
450 void MSG_task_set_bound(msg_task_t task, double bound)
451 {
452   xbt_assert(task, "Invalid parameter");
453   xbt_assert(task->simdata, "Invalid parameter");
454
455   if (bound == 0)
456     XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
457
458   task->simdata->bound = bound;
459   if (task->simdata->compute)
460     simcall_host_execution_set_bound(task->simdata->compute,
461                                       task->simdata->bound);
462 }