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;
70 simdata->host_list = NULL;
71 simdata->comp_amount = NULL;
72 simdata->comm_amount = NULL;
74 TRACE_msg_task_create(task);
80 /** \ingroup m_task_management
81 * \brief Creates a new #msg_task_t (a parallel one....).
83 * A constructor for #msg_task_t taking six arguments and returning the
85 * \param name a name for the object. It is for user-level information
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.
97 * \return The new corresponding object.
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)
105 msg_task_t task = MSG_task_create(name, 0, 0, data);
106 simdata_task_t simdata = task->simdata;
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;
115 for (i = 0; i < host_nb; i++)
116 simdata->host_list[i] = host_list[i];
121 /*************** Begin GPU ***************/
122 /** \ingroup m_task_management
123 * \brief Creates a new #msg_gpu_task_t.
125 * A constructor for #msg_gpu_task_t taking four arguments and returning
126 a pointer to the new created GPU task.
128 * \param name a name for the object. It is for user-level information
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.
135 * \param dispatch_latency time in seconds to load this task on the GPU
137 * \param collect_latency time in seconds to transfer result from the GPU
138 back to the CPU (host) when done
140 * \see msg_gpu_task_t
141 * \return The new corresponding object.
143 msg_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
144 double dispatch_latency, double collect_latency)
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;
150 task->name = xbt_strdup(name);
153 simdata->computation_amount = compute_duration;
154 simdata->dispatch_latency = dispatch_latency;
155 simdata->collect_latency = collect_latency;
159 /* TRACE_msg_gpu_task_create(task); */
164 /*************** End GPU ***************/
166 /** \ingroup m_task_management
167 * \brief Return the user data of a #msg_task_t.
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.
172 void *MSG_task_get_data(msg_task_t task)
174 xbt_assert((task != NULL), "Invalid parameter");
179 /** \ingroup m_task_management
180 * \brief Sets the user data of a #msg_task_t.
182 * This function allows to associate a new pointer to
183 the user data associated of \a task.
185 void MSG_task_set_data(msg_task_t task, void *data)
187 xbt_assert((task != NULL), "Invalid parameter");
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
196 void MSG_task_set_copy_callback(void (*callback)
197 (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
199 msg_global->task_copy_callback = callback;
202 SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
205 SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
209 /** \ingroup m_task_management
210 * \brief Return the sender of a #msg_task_t.
212 * This functions returns the #msg_process_t which sent this task
214 msg_process_t MSG_task_get_sender(msg_task_t task)
216 xbt_assert(task, "Invalid parameters");
217 return ((simdata_task_t) task->simdata)->sender;
220 /** \ingroup m_task_management
221 * \brief Return the source of a #msg_task_t.
223 * This functions returns the #msg_host_t from which this task was sent
225 msg_host_t MSG_task_get_source(msg_task_t task)
227 xbt_assert(task, "Invalid parameters");
228 return ((simdata_task_t) task->simdata)->source;
231 /** \ingroup m_task_management
232 * \brief Return the name of a #msg_task_t.
234 * This functions returns the name of a #msg_task_t as specified on creation
236 const char *MSG_task_get_name(msg_task_t task)
238 xbt_assert(task, "Invalid parameters");
242 /** \ingroup m_task_management
243 * \brief Sets the name of a #msg_task_t.
245 * This functions allows to associate a name to a task
247 void MSG_task_set_name(msg_task_t task, const char *name)
249 xbt_assert(task, "Invalid parameters");
250 task->name = xbt_strdup(name);
253 /** \ingroup m_task_management
254 * \brief Destroy a #msg_task_t.
256 * Destructor for #msg_task_t. Note that you should free user data, if any, \b
257 * before calling this function.
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.
265 msg_error_t MSG_task_destroy(msg_task_t task)
267 smx_action_t action = NULL;
268 xbt_assert((task != NULL), "Invalid parameter");
270 if (task->simdata->isused) {
271 /* the task is being sent or executed: cancel it first */
272 MSG_task_cancel(task);
275 TRACE_msg_task_destroy(task);
278 xbt_free(task->name);
280 action = task->simdata->compute;
282 simcall_host_execution_destroy(action);
284 /* parallel tasks only */
285 xbt_free(task->simdata->host_list);
287 /* free main structures */
288 xbt_free(task->simdata);
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.
300 msg_error_t MSG_task_cancel(msg_task_t task)
302 xbt_assert((task != NULL), "Cannot cancel a NULL task");
304 if (task->simdata->compute) {
305 simcall_host_execution_cancel(task->simdata->compute);
307 else if (task->simdata->comm) {
308 simcall_comm_cancel(task->simdata->comm);
309 task->simdata->isused = 0;
314 /** \ingroup m_task_management
315 * \brief Returns the computation amount needed to process a task #msg_task_t.
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.
320 double MSG_task_get_compute_duration(msg_task_t task)
322 xbt_assert((task != NULL)
323 && (task->simdata != NULL), "Invalid parameter");
325 return task->simdata->computation_amount;
329 /** \ingroup m_task_management
330 * \brief set the computation amount needed to process a task #msg_task_t.
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
338 void MSG_task_set_compute_duration(msg_task_t task,
339 double computation_amount)
341 xbt_assert(task, "Invalid parameter");
342 task->simdata->computation_amount = computation_amount;
346 /** \ingroup m_task_management
347 * \brief set the amount data attached with a task #msg_task_t.
349 * \warning If the transfer is ongoing (already started and not finished),
350 * it is not modified by this call.
353 void MSG_task_set_data_size(msg_task_t task,
356 xbt_assert(task, "Invalid parameter");
357 task->simdata->message_size = data_size;
363 /** \ingroup m_task_management
364 * \brief Returns the remaining computation amount of a task #msg_task_t.
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.
370 double MSG_task_get_remaining_computation(msg_task_t task)
372 xbt_assert((task != NULL)
373 && (task->simdata != NULL), "Invalid parameter");
375 if (task->simdata->compute) {
376 return simcall_host_execution_get_remains(task->simdata->compute);
378 return task->simdata->computation_amount;
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
388 double MSG_task_get_remaining_communication(msg_task_t task)
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);
397 #ifdef HAVE_LATENCY_BOUND_TRACKING
398 /** \ingroup m_task_management
399 * \brief Return 1 if communication task is limited by latency, 0 otherwise
402 int MSG_task_is_latency_bounded(msg_task_t task)
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);
412 /** \ingroup m_task_management
413 * \brief Returns the size of the data attached to a task #msg_task_t.
416 double MSG_task_get_data_size(msg_task_t task)
418 xbt_assert((task != NULL)
419 && (task->simdata != NULL), "Invalid parameter");
421 return task->simdata->message_size;
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.
432 void MSG_task_set_priority(msg_task_t task, double priority)
434 xbt_assert((task != NULL)
435 && (task->simdata != NULL), "Invalid parameter");
437 task->simdata->priority = 1 / priority;
438 if (task->simdata->compute)
439 simcall_host_execution_set_priority(task->simdata->compute,
440 task->simdata->priority);
444 /** \ingroup m_task_management
445 * \brief Changes the maximum CPU utilization of a computation task.
448 * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
450 void MSG_task_set_bound(msg_task_t task, double bound)
452 xbt_assert(task, "Invalid parameter");
453 xbt_assert(task->simdata, "Invalid parameter");
456 XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
458 task->simdata->bound = bound;
459 if (task->simdata->compute)
460 simcall_host_execution_set_bound(task->simdata->compute,
461 task->simdata->bound);