1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "msg_private.h"
8 #include "xbt/sysdep.h"
11 /** @addtogroup m_task_management
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>.
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
23 "Logging specific to MSG (task)");
25 /********************************* Task **************************************/
26 /** \ingroup m_task_management
27 * \brief Creates a new #msg_task_t.
29 * A constructor for #msg_task_t taking four arguments and returning the
31 * \param name a name for the object. It is for user-level information
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.
43 * \return The new corresponding object.
45 msg_task_t MSG_task_create(const char *name, double compute_duration,
46 double message_size, void *data)
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;
53 task->name = xbt_strdup(name);
57 simdata->compute = 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;
69 simdata->host_list = NULL;
70 simdata->comp_amount = NULL;
71 simdata->comm_amount = NULL;
73 TRACE_msg_task_create(task);
79 /** \ingroup m_task_management
80 * \brief Creates a new #msg_task_t (a parallel one....).
82 * A constructor for #msg_task_t taking six arguments and returning the
84 * \param name a name for the object. It is for user-level information
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.
96 * \return The new corresponding object.
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)
104 msg_task_t task = MSG_task_create(name, 0, 0, data);
105 simdata_task_t simdata = task->simdata;
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;
114 for (i = 0; i < host_nb; i++)
115 simdata->host_list[i] = host_list[i];
120 /*************** Begin GPU ***************/
121 /** \ingroup m_task_management
122 * \brief Creates a new #msg_gpu_task_t.
124 * A constructor for #msg_gpu_task_t taking four arguments and returning
125 a pointer to the new created GPU task.
127 * \param name a name for the object. It is for user-level information
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.
134 * \param dispatch_latency time in seconds to load this task on the GPU
136 * \param collect_latency time in seconds to transfer result from the GPU
137 back to the CPU (host) when done
139 * \see msg_gpu_task_t
140 * \return The new corresponding object.
142 msg_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
143 double dispatch_latency, double collect_latency)
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;
149 task->name = xbt_strdup(name);
152 simdata->computation_amount = compute_duration;
153 simdata->dispatch_latency = dispatch_latency;
154 simdata->collect_latency = collect_latency;
158 /* TRACE_msg_gpu_task_create(task); */
163 /*************** End GPU ***************/
165 /** \ingroup m_task_management
166 * \brief Return the user data of a #msg_task_t.
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.
171 void *MSG_task_get_data(msg_task_t task)
173 xbt_assert((task != NULL), "Invalid parameter");
178 /** \ingroup m_task_management
179 * \brief Sets the user data of a #msg_task_t.
181 * This function allows to associate a new pointer to
182 the user data associated of \a task.
184 void MSG_task_set_data(msg_task_t task, void *data)
186 xbt_assert((task != NULL), "Invalid parameter");
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
195 void MSG_task_set_copy_callback(void (*callback)
196 (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
198 msg_global->task_copy_callback = callback;
201 SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
204 SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
208 /** \ingroup m_task_management
209 * \brief Return the sender of a #msg_task_t.
211 * This functions returns the #msg_process_t which sent this task
213 msg_process_t MSG_task_get_sender(msg_task_t task)
215 xbt_assert(task, "Invalid parameters");
216 return ((simdata_task_t) task->simdata)->sender;
219 /** \ingroup m_task_management
220 * \brief Return the source of a #msg_task_t.
222 * This functions returns the #msg_host_t from which this task was sent
224 msg_host_t MSG_task_get_source(msg_task_t task)
226 xbt_assert(task, "Invalid parameters");
227 return ((simdata_task_t) task->simdata)->source;
230 /** \ingroup m_task_management
231 * \brief Return the name of a #msg_task_t.
233 * This functions returns the name of a #msg_task_t as specified on creation
235 const char *MSG_task_get_name(msg_task_t task)
237 xbt_assert(task, "Invalid parameters");
241 /** \ingroup m_task_management
242 * \brief Sets the name of a #msg_task_t.
244 * This functions allows to associate a name to a task
246 void MSG_task_set_name(msg_task_t task, const char *name)
248 xbt_assert(task, "Invalid parameters");
249 task->name = xbt_strdup(name);
252 /** \ingroup m_task_management
253 * \brief Destroy a #msg_task_t.
255 * Destructor for #msg_task_t. Note that you should free user data, if any, \b
256 * before calling this function.
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.
264 msg_error_t MSG_task_destroy(msg_task_t task)
266 smx_action_t action = NULL;
267 xbt_assert((task != NULL), "Invalid parameter");
269 if (task->simdata->isused) {
270 /* the task is being sent or executed: cancel it first */
271 MSG_task_cancel(task);
274 TRACE_msg_task_destroy(task);
277 xbt_free(task->name);
279 action = task->simdata->compute;
281 simcall_host_execution_destroy(action);
283 /* parallel tasks only */
284 xbt_free(task->simdata->host_list);
286 /* free main structures */
287 xbt_free(task->simdata);
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.
299 msg_error_t MSG_task_cancel(msg_task_t task)
301 xbt_assert((task != NULL), "Cannot cancel a NULL task");
303 if (task->simdata->compute) {
304 simcall_host_execution_cancel(task->simdata->compute);
306 else if (task->simdata->comm) {
307 simcall_comm_cancel(task->simdata->comm);
308 task->simdata->isused = 0;
313 /** \ingroup m_task_management
314 * \brief Returns the computation amount needed to process a task #msg_task_t.
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.
319 double MSG_task_get_compute_duration(msg_task_t task)
321 xbt_assert((task != NULL)
322 && (task->simdata != NULL), "Invalid parameter");
324 return task->simdata->computation_amount;
328 /** \ingroup m_task_management
329 * \brief set the computation amount needed to process a task #msg_task_t.
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
337 void MSG_task_set_compute_duration(msg_task_t task,
338 double computation_amount)
340 xbt_assert(task, "Invalid parameter");
341 task->simdata->computation_amount = computation_amount;
345 /** \ingroup m_task_management
346 * \brief set the amount data attached with a task #msg_task_t.
348 * \warning If the transfer is ongoing (already started and not finished),
349 * it is not modified by this call.
352 void MSG_task_set_data_size(msg_task_t task,
355 xbt_assert(task, "Invalid parameter");
356 task->simdata->message_size = data_size;
362 /** \ingroup m_task_management
363 * \brief Returns the remaining computation amount of a task #msg_task_t.
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.
369 double MSG_task_get_remaining_computation(msg_task_t task)
371 xbt_assert((task != NULL)
372 && (task->simdata != NULL), "Invalid parameter");
374 if (task->simdata->compute) {
375 return simcall_host_execution_get_remains(task->simdata->compute);
377 return task->simdata->computation_amount;
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
387 double MSG_task_get_remaining_communication(msg_task_t task)
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);
396 #ifdef HAVE_LATENCY_BOUND_TRACKING
397 /** \ingroup m_task_management
398 * \brief Return 1 if communication task is limited by latency, 0 otherwise
401 int MSG_task_is_latency_bounded(msg_task_t task)
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);
411 /** \ingroup m_task_management
412 * \brief Returns the size of the data attached to a task #msg_task_t.
415 double MSG_task_get_data_size(msg_task_t task)
417 xbt_assert((task != NULL)
418 && (task->simdata != NULL), "Invalid parameter");
420 return task->simdata->message_size;
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.
431 void MSG_task_set_priority(msg_task_t task, double priority)
433 xbt_assert((task != NULL)
434 && (task->simdata != NULL), "Invalid parameter");
436 task->simdata->priority = 1 / priority;
437 if (task->simdata->compute)
438 simcall_host_execution_set_priority(task->simdata->compute,
439 task->simdata->priority);