Logo AND Algorithmique Numérique Distribuée

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