Logo AND Algorithmique Numérique Distribuée

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