Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[simgrid.git] / src / msg / msg_task.c
1 /* Copyright (c) 2004-2014. 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 "simix/smx_private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11
12 /** @addtogroup m_task_management
13  *    
14  * 
15  *  Since most scheduling algorithms rely on a concept of task
16  *  that can be either <em>computed</em> locally or
17  *  <em>transferred</em> on another processor, it seems to be the
18  *  right level of abstraction for our purposes. A <em>task</em>
19  *  may then be defined by a <em>computing amount</em>, a
20  *  <em>message size</em> and some <em>private data</em>.
21  */
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
24                                 "Logging specific to MSG (task)");
25
26 /********************************* Task **************************************/
27 /** \ingroup m_task_management
28  * \brief Creates a new #msg_task_t.
29  *
30  * A constructor for #msg_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 flop)
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 bytes) needed to
38    transfer this new task. If 0, then it cannot be transfered with
39    MSG_task_send() and MSG_task_recv(). 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 msg_task_t
44  * \return The new corresponding object.
45  */
46 msg_task_t MSG_task_create(const char *name, double compute_duration,
47                          double message_size, void *data)
48 {
49   msg_task_t task = xbt_new(s_msg_task_t, 1);
50   simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
51   task->simdata = simdata;
52
53   /* Task structure */
54   task->name = xbt_strdup(name);
55   task->data = data;
56
57   /* Simulator Data */
58   simdata->compute = NULL;
59   simdata->comm = NULL;
60   simdata->message_size = message_size;
61   simdata->computation_amount = compute_duration;
62   simdata->sender = NULL;
63   simdata->receiver = NULL;
64   simdata->source = NULL;
65   simdata->priority = 1.0;
66   simdata->bound = 0;
67   simdata->affinity_mask_db = xbt_dict_new_homogeneous(NULL);
68   simdata->rate = -1.0;
69   simdata->isused = 0;
70
71   simdata->host_nb = 0;
72   simdata->host_list = NULL;
73   simdata->comp_amount = NULL;
74   simdata->comm_amount = NULL;
75 #ifdef HAVE_TRACING
76   TRACE_msg_task_create(task);
77 #endif
78
79   return task;
80 }
81
82 /** \ingroup m_task_management
83  * \brief Creates a new #msg_task_t (a parallel one....).
84  *
85  * A constructor for #msg_task_t taking six arguments and returning the
86  corresponding object.
87  * \param name a name for the object. It is for user-level information
88  and can be NULL.
89  * \param host_nb the number of hosts implied in the parallel task.
90  * \param host_list an array of \p host_nb msg_host_t.
91  * \param computation_amount an array of \p host_nb
92  doubles. computation_amount[i] is the total number of operations
93  that have to be performed on host_list[i].
94  * \param communication_amount an array of \p host_nb* \p host_nb doubles.
95  * \param data a pointer to any data may want to attach to the new
96  object.  It is for user-level information and can be NULL. It can
97  be retrieved with the function \ref MSG_task_get_data.
98  * \see msg_task_t
99  * \return The new corresponding object.
100  */
101 msg_task_t
102 MSG_parallel_task_create(const char *name, int host_nb,
103                          const msg_host_t * host_list,
104                          double *computation_amount,
105                          double *communication_amount, void *data)
106 {
107   msg_task_t task = MSG_task_create(name, 0, 0, data);
108   simdata_task_t simdata = task->simdata;
109   int i;
110
111   /* Simulator Data specific to parallel tasks */
112   simdata->host_nb = host_nb;
113   simdata->host_list = xbt_new0(smx_host_t, host_nb);
114   simdata->comp_amount = computation_amount;
115   simdata->comm_amount = communication_amount;
116
117   for (i = 0; i < host_nb; i++)
118     simdata->host_list[i] = host_list[i];
119
120   return task;
121 }
122
123 /*************** Begin GPU ***************/
124 /** \ingroup m_task_management
125  * \brief Creates a new #msg_gpu_task_t.
126
127  * A constructor for #msg_gpu_task_t taking four arguments and returning
128    a pointer to the new created GPU task.
129
130  * \param name a name for the object. It is for user-level information
131    and can be NULL.
132
133  * \param compute_duration a value of the processing amount (in flop)
134    needed to process this new task. If 0, then it cannot be executed with
135    MSG_gpu_task_execute(). This value has to be >=0.
136
137  * \param dispatch_latency time in seconds to load this task on the GPU
138
139  * \param collect_latency time in seconds to transfer result from the GPU
140    back to the CPU (host) when done
141
142  * \see msg_gpu_task_t
143  * \return The new corresponding object.
144  */
145 msg_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
146                          double dispatch_latency, double collect_latency)
147 {
148   msg_gpu_task_t task = xbt_new(s_msg_gpu_task_t, 1);
149   simdata_gpu_task_t simdata = xbt_new(s_simdata_gpu_task_t, 1);
150   task->simdata = simdata;
151   /* Task structure */
152   task->name = xbt_strdup(name);
153
154   /* Simulator Data */
155   simdata->computation_amount = compute_duration;
156   simdata->dispatch_latency   = dispatch_latency;
157   simdata->collect_latency    = collect_latency;
158
159 #ifdef HAVE_TRACING
160   //FIXME
161   /* TRACE_msg_gpu_task_create(task); */
162 #endif
163
164   return task;
165 }
166 /*************** End GPU ***************/
167
168 /** \ingroup m_task_management
169  * \brief Return the user data of a #msg_task_t.
170  *
171  * This function checks whether \a task is a valid pointer or not and return
172    the user data associated to \a task if it is possible.
173  */
174 void *MSG_task_get_data(msg_task_t task)
175 {
176   xbt_assert((task != NULL), "Invalid parameter");
177
178   return (task->data);
179 }
180
181 /** \ingroup m_task_management
182  * \brief Sets the user data of a #msg_task_t.
183  *
184  * This function allows to associate a new pointer to
185    the user data associated of \a task.
186  */
187 void MSG_task_set_data(msg_task_t task, void *data)
188 {
189   xbt_assert((task != NULL), "Invalid parameter");
190
191   task->data = data;
192 }
193
194 /** \ingroup m_task_management
195  * \brief Sets a function to be called when a task has just been copied.
196  * \param callback a callback function
197  */
198 void MSG_task_set_copy_callback(void (*callback)
199     (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
200
201   msg_global->task_copy_callback = callback;
202
203   if (callback) {
204     SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
205   }
206   else {
207     SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
208   }
209 }
210
211 /** \ingroup m_task_management
212  * \brief Return the sender of a #msg_task_t.
213  *
214  * This functions returns the #msg_process_t which sent this task
215  */
216 msg_process_t MSG_task_get_sender(msg_task_t task)
217 {
218   xbt_assert(task, "Invalid parameters");
219   return ((simdata_task_t) task->simdata)->sender;
220 }
221
222 /** \ingroup m_task_management
223  * \brief Return the source of a #msg_task_t.
224  *
225  * This functions returns the #msg_host_t from which this task was sent
226  */
227 msg_host_t MSG_task_get_source(msg_task_t task)
228 {
229   xbt_assert(task, "Invalid parameters");
230   return ((simdata_task_t) task->simdata)->source;
231 }
232
233 /** \ingroup m_task_management
234  * \brief Return the name of a #msg_task_t.
235  *
236  * This functions returns the name of a #msg_task_t as specified on creation
237  */
238 const char *MSG_task_get_name(msg_task_t task)
239 {
240   xbt_assert(task, "Invalid parameters");
241   return task->name;
242 }
243
244 /** \ingroup m_task_management
245  * \brief Sets the name of a #msg_task_t.
246  *
247  * This functions allows to associate a name to a task
248  */
249 void MSG_task_set_name(msg_task_t task, const char *name)
250 {
251   xbt_assert(task, "Invalid parameters");
252   task->name = xbt_strdup(name);
253 }
254
255 /** \ingroup m_task_management
256  * \brief Destroy a #msg_task_t.
257  *
258  * Destructor for #msg_task_t. Note that you should free user data, if any, \b
259  * before calling this function.
260  *
261  * Only the process that owns the task can destroy it.
262  * The owner changes after a successful send.
263  * If a task is successfully sent, the receiver becomes the owner and is
264  * supposed to destroy it. The sender should not use it anymore.
265  * If the task failed to be sent, the sender remains the owner of the task.
266  */
267 msg_error_t MSG_task_destroy(msg_task_t task)
268 {
269   smx_action_t action = NULL;
270   xbt_assert((task != NULL), "Invalid parameter");
271
272   if (task->simdata->isused) {
273     /* the task is being sent or executed: cancel it first */
274     MSG_task_cancel(task);
275   }
276 #ifdef HAVE_TRACING
277   TRACE_msg_task_destroy(task);
278 #endif
279
280   xbt_free(task->name);
281
282   action = task->simdata->compute;
283   if (action)
284     simcall_host_execution_destroy(action);
285
286   /* parallel tasks only */
287   xbt_free(task->simdata->host_list);
288
289   xbt_dict_free(&task->simdata->affinity_mask_db);
290
291   /* free main structures */
292   xbt_free(task->simdata);
293   xbt_free(task);
294
295   return MSG_OK;
296 }
297
298
299 /** \ingroup m_task_usage
300  * \brief Cancel a #msg_task_t.
301  * \param task the task to cancel. If it was executed or transfered, it
302           stops the process that were working on it.
303  */
304 msg_error_t MSG_task_cancel(msg_task_t task)
305 {
306   xbt_assert((task != NULL), "Cannot cancel a NULL task");
307
308   if (task->simdata->compute) {
309     simcall_host_execution_cancel(task->simdata->compute);
310   }
311   else if (task->simdata->comm) {
312     simcall_comm_cancel(task->simdata->comm);
313     task->simdata->isused = 0;
314   }
315   return MSG_OK;
316 }
317
318 /** \ingroup m_task_management
319  * \brief Returns the computation amount needed to process a task #msg_task_t.
320  *
321  * Once a task has been processed, this amount is set to 0. If you want, you
322  * can reset this value with #MSG_task_set_compute_duration before restarting the task.
323  */
324 double MSG_task_get_compute_duration(msg_task_t task)
325 {
326   xbt_assert((task != NULL)
327               && (task->simdata != NULL), "Invalid parameter");
328
329   return task->simdata->computation_amount;
330 }
331
332
333 /** \ingroup m_task_management
334  * \brief set the computation amount needed to process a task #msg_task_t.
335  *
336  * \warning If the computation is ongoing (already started and not finished),
337  * it is not modified by this call. And the termination of the ongoing task with
338  * set the computation_amount to zero, overriding any value set during the
339  * execution.
340  */
341
342 void MSG_task_set_compute_duration(msg_task_t task,
343                                    double computation_amount)
344 {
345   xbt_assert(task, "Invalid parameter");
346   task->simdata->computation_amount = computation_amount;
347
348 }
349
350 /** \ingroup m_task_management
351  * \brief set the amount data attached with a task #msg_task_t.
352  *
353  * \warning If the transfer is ongoing (already started and not finished),
354  * it is not modified by this call. 
355  */
356
357 void MSG_task_set_data_size(msg_task_t task,
358                                    double data_size)
359 {
360   xbt_assert(task, "Invalid parameter");
361   task->simdata->message_size = data_size;
362
363 }
364
365
366
367 /** \ingroup m_task_management
368  * \brief Returns the remaining computation amount of a task #msg_task_t.
369  *
370  * If the task is ongoing, this call retrieves the remaining amount of work.
371  * If it is not ongoing, it returns the total amount of work that will be
372  * executed when the task starts.
373  */
374 double MSG_task_get_remaining_computation(msg_task_t task)
375 {
376   xbt_assert((task != NULL)
377               && (task->simdata != NULL), "Invalid parameter");
378
379   if (task->simdata->compute) {
380     return simcall_host_execution_get_remains(task->simdata->compute);
381   } else {
382     return task->simdata->computation_amount;
383   }
384 }
385
386 /** \ingroup m_task_management
387  * \brief Returns the total amount received by a task #msg_task_t.
388  *        If the communication does not exist it will return 0.
389  *        So, if the communication has FINISHED or FAILED it returns
390  *        zero.
391  */
392 double MSG_task_get_remaining_communication(msg_task_t task)
393 {
394   xbt_assert((task != NULL)
395               && (task->simdata != NULL), "Invalid parameter");
396   XBT_DEBUG("calling simcall_communication_get_remains(%p)",
397          task->simdata->comm);
398   return simcall_comm_get_remains(task->simdata->comm);
399 }
400
401 #ifdef HAVE_LATENCY_BOUND_TRACKING
402 /** \ingroup m_task_management
403  * \brief Return 1 if communication task is limited by latency, 0 otherwise
404  *
405  */
406 int MSG_task_is_latency_bounded(msg_task_t task)
407 {
408   xbt_assert((task != NULL)
409               && (task->simdata != NULL), "Invalid parameter");
410   XBT_DEBUG("calling simcall_communication_is_latency_bounded(%p)",
411          task->simdata->comm);
412   return simcall_comm_is_latency_bounded(task->simdata->comm);
413 }
414 #endif
415
416 /** \ingroup m_task_management
417  * \brief Returns the size of the data attached to a task #msg_task_t.
418  *
419  */
420 double MSG_task_get_data_size(msg_task_t task)
421 {
422   xbt_assert((task != NULL)
423               && (task->simdata != NULL), "Invalid parameter");
424
425   return task->simdata->message_size;
426 }
427
428
429
430 /** \ingroup m_task_management
431  * \brief Changes the priority of a computation task. This priority doesn't affect 
432  *        the transfer rate. A priority of 2 will make a task receive two times more
433  *        cpu power than the other ones.
434  *
435  */
436 void MSG_task_set_priority(msg_task_t task, double priority)
437 {
438   xbt_assert((task != NULL)
439               && (task->simdata != NULL), "Invalid parameter");
440
441   task->simdata->priority = 1 / priority;
442   if (task->simdata->compute)
443     simcall_host_execution_set_priority(task->simdata->compute,
444                                       task->simdata->priority);
445 }
446
447
448 /** \ingroup m_task_management
449  * \brief Changes the maximum CPU utilization of a computation task.
450  *        Unit is flops/s.
451  *
452  * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
453  */
454 void MSG_task_set_bound(msg_task_t task, double bound)
455 {
456   xbt_assert(task, "Invalid parameter");
457   xbt_assert(task->simdata, "Invalid parameter");
458
459   if (bound == 0)
460     XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
461
462   task->simdata->bound = bound;
463   if (task->simdata->compute)
464     simcall_host_execution_set_bound(task->simdata->compute,
465                                       task->simdata->bound);
466 }
467
468
469 /** \ingroup m_task_management
470  * \brief Changes the CPU affinity of a computation task.
471  *
472  * When pinning the given task to the first CPU core of the given host, use
473  * 0x01 for the mask value. Each bit of the mask value corresponds to each CPU
474  * core. See taskset(1) on Linux.
475  *
476  * \param task a target task
477  * \param host the host having a multi-core CPU
478  * \param mask the bit mask of a new CPU affinity setting for the task
479  *
480  *
481  * Usage:
482  * 0. Define a host with multiple cores.
483  *    \<host id="PM0" power="1E8" core="2"/\>
484  *
485  * 1. Pin a given task to the first CPU core of a host.
486  *   MSG_task_set_affinity(task, pm0, 0x01);
487  *
488  * 2. Pin a given task to the third CPU core of a host. Turn on the third bit of the mask.
489  *   MSG_task_set_affinity(task, pm0, 0x04); // 0x04 == 100B
490  *
491  * 3. Pin a given VM to the first CPU core of a host.
492  *   MSG_vm_set_affinity(vm, pm0, 0x01);
493  *
494  * See examples/msg/cloud/multicore.c for more information.
495  *
496  *
497  * Note:
498  * 1. The current code does not allow an affinity of a task to multiple cores.
499  * The mask value 0x03 (i.e., a given task will be executed on the first core
500  * or the second core) is not allowed. The mask value 0x01 or 0x02 works. See
501  * cpu_cas01.c for details.
502  *
503  * 2. It is recommended to first compare simulation results in both the Lazy
504  * and Full calculation modes (using --cfg=cpu/optim:Full or not). Fix
505  * cpu_cas01.c if you find wrong results in the Lazy mode.
506  *
507  */
508 void MSG_task_set_affinity(msg_task_t task, msg_host_t host, unsigned long mask)
509 {
510   xbt_assert(task, "Invalid parameter");
511   xbt_assert(task->simdata, "Invalid parameter");
512
513   if (mask == 0) {
514     /* 0 means clear */
515     {
516       /* We need remove_ext() not throwing exception. */
517       void *ret = xbt_dict_get_or_null_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(msg_host_t));
518       if (ret != NULL)
519         xbt_dict_remove_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(host));
520     }
521   } else
522     xbt_dict_set_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(host), (void *) mask, NULL);
523
524   /* We set affinity data of this task. If the task is being executed, we
525    * actually change the affinity setting of the task. Otherwise, this change
526    * will be applied when the task is executed. */
527
528   if (!task->simdata->compute) {
529     /* task is not yet executed */
530     XBT_INFO("set affinity(0x%04lx@%s) for %s (not active now)", mask, MSG_host_get_name(host), MSG_task_get_name(task));
531     return;
532   }
533
534   {
535     smx_action_t compute = task->simdata->compute;
536     msg_host_t host_now = compute->execution.host;  // simix_private.h is necessary
537     if (host_now != host) {
538       /* task is not yet executed on this host */
539       XBT_INFO("set affinity(0x%04lx@%s) for %s (not active now)", mask, MSG_host_get_name(host), MSG_task_get_name(task));
540       return;
541     }
542
543     /* task is being executed on this host. so change the affinity now */
544     {
545       /* check it works. remove me if it works. */
546       unsigned long affinity_mask = (unsigned long) xbt_dict_get_or_null_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(msg_host_t));
547       xbt_assert(affinity_mask == mask);
548     }
549
550     XBT_INFO("set affinity(0x%04lx@%s) for %s", mask, MSG_host_get_name(host), MSG_task_get_name(task));
551     simcall_host_execution_set_affinity(task->simdata->compute, host, mask);
552   }
553 }