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 /** \defgroup m_task_management Managing functions of Tasks
12 * \brief This section describes the task structure of MSG
13 * (#m_task_t) and the functions for managing it.
15 /** @addtogroup m_task_management
16 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
18 * Since most scheduling algorithms rely on a concept of task
19 * that can be either <em>computed</em> locally or
20 * <em>transferred</em> on another processor, it seems to be the
21 * right level of abstraction for our purposes. A <em>task</em>
22 * may then be defined by a <em>computing amount</em>, a
23 * <em>message size</em> and some <em>private data</em>.
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
27 "Logging specific to MSG (task)");
29 /********************************* Task **************************************/
30 /** \ingroup m_task_management
31 * \brief Creates a new #m_task_t.
33 * A constructor for #m_task_t taking four arguments and returning the
35 * \param name a name for the object. It is for user-level information
37 * \param compute_duration a value of the processing amount (in flop)
38 needed to process this new task. If 0, then it cannot be executed with
39 MSG_task_execute(). This value has to be >=0.
40 * \param message_size a value of the amount of data (in bytes) needed to
41 transfer this new task. If 0, then it cannot be transfered with
42 MSG_task_get() and MSG_task_put(). This value has to be >=0.
43 * \param data a pointer to any data may want to attach to the new
44 object. It is for user-level information and can be NULL. It can
45 be retrieved with the function \ref MSG_task_get_data.
47 * \return The new corresponding object.
49 m_task_t MSG_task_create(const char *name, double compute_duration,
50 double message_size, void *data)
52 m_task_t task = xbt_new(s_m_task_t, 1);
53 simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
54 task->simdata = simdata;
56 task->name = xbt_strdup(name);
61 simdata->computation_amount = compute_duration;
62 simdata->message_size = message_size;
64 simdata->priority = 1.0;
66 simdata->sender = NULL;
67 simdata->receiver = NULL;
68 simdata->compute = NULL;
71 simdata->host_list = NULL;
72 simdata->comp_amount = NULL;
73 simdata->comm_amount = NULL;
75 TRACE_msg_task_create(task);
81 /*************** Begin GPU ***************/
82 /** \ingroup m_task_management
83 * \brief Creates a new #m_gpu_task_t.
85 * A constructor for #m_gpu_task_t taking four arguments and returning
86 a pointer to the new created GPU task.
88 * \param name a name for the object. It is for user-level information
91 * \param compute_duration a value of the processing amount (in flop)
92 needed to process this new task. If 0, then it cannot be executed with
93 MSG_gpu_task_execute(). This value has to be >=0.
95 * \param dispatch_latency time in seconds to load this task on the GPU
97 * \param collect_latency time in seconds to transfer result from the GPU
98 back to the CPU (host) when done
101 * \return The new corresponding object.
103 m_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
104 double dispatch_latency, double collect_latency)
106 m_gpu_task_t task = xbt_new(s_m_gpu_task_t, 1);
107 simdata_gpu_task_t simdata = xbt_new(s_simdata_gpu_task_t, 1);
108 task->simdata = simdata;
110 task->name = xbt_strdup(name);
113 simdata->computation_amount = compute_duration;
114 simdata->dispatch_latency = dispatch_latency;
115 simdata->collect_latency = collect_latency;
119 /* TRACE_msg_gpu_task_create(task); */
124 /*************** End GPU ***************/
126 /** \ingroup m_task_management
127 * \brief Return the user data of a #m_task_t.
129 * This function checks whether \a task is a valid pointer or not and return
130 the user data associated to \a task if it is possible.
132 void *MSG_task_get_data(m_task_t task)
134 xbt_assert((task != NULL), "Invalid parameter");
139 /** \ingroup m_task_management
140 * \brief Sets the user data of a #m_task_t.
142 * This function allows to associate a new pointer to
143 the user data associated of \a task.
145 void MSG_task_set_data(m_task_t task, void *data)
147 xbt_assert((task != NULL), "Invalid parameter");
152 /** \ingroup m_task_management
153 * \brief Sets a function to be called when a task has just been copied.
154 * \param callback a callback function
156 void MSG_task_set_copy_callback(void (*callback)
157 (m_task_t task, m_process_t sender, m_process_t receiver)) {
159 msg_global->task_copy_callback = callback;
162 SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
165 SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
169 /** \ingroup m_task_management
170 * \brief Return the sender of a #m_task_t.
172 * This functions returns the #m_process_t which sent this task
174 m_process_t MSG_task_get_sender(m_task_t task)
176 xbt_assert(task, "Invalid parameters");
177 return ((simdata_task_t) task->simdata)->sender;
180 /** \ingroup m_task_management
181 * \brief Return the source of a #m_task_t.
183 * This functions returns the #m_host_t from which this task was sent
185 m_host_t MSG_task_get_source(m_task_t task)
187 xbt_assert(task, "Invalid parameters");
188 return ((simdata_task_t) task->simdata)->source;
191 /** \ingroup m_task_management
192 * \brief Return the name of a #m_task_t.
194 * This functions returns the name of a #m_task_t as specified on creation
196 const char *MSG_task_get_name(m_task_t task)
198 xbt_assert(task, "Invalid parameters");
202 /** \ingroup m_task_management
203 * \brief Return the name of a #m_task_t.
205 * This functions allows to associate a name to a task
207 void MSG_task_set_name(m_task_t task, const char *name)
209 xbt_assert(task, "Invalid parameters");
210 task->name = xbt_strdup(name);
213 /** \ingroup m_task_management
214 * \brief Destroy a #m_task_t.
216 * Destructor for #m_task_t. Note that you should free user data, if any, \b
217 * before calling this function.
219 * Only the process that owns the task can destroy it.
220 * The owner changes after a successful send.
221 * If a task is successfully sent, the receiver becomes the owner and is
222 * supposed to destroy it. The sender should not use it anymore.
223 * If the task failed to be sent, the sender remains the owner of the task.
225 MSG_error_t MSG_task_destroy(m_task_t task)
227 smx_action_t action = NULL;
228 xbt_assert((task != NULL), "Invalid parameter");
230 if (task->simdata->isused) {
231 /* the task is being sent or executed: cancel it first */
232 MSG_task_cancel(task);
235 TRACE_msg_task_destroy(task);
238 xbt_free(task->name);
240 action = task->simdata->compute;
242 simcall_host_execution_destroy(action);
244 /* parallel tasks only */
245 xbt_free(task->simdata->host_list);
247 /* free main structures */
248 xbt_free(task->simdata);
255 /** \ingroup m_task_management
256 * \brief Cancel a #m_task_t.
257 * \param task the task to cancel. If it was executed or transfered, it
258 stops the process that were working on it.
260 MSG_error_t MSG_task_cancel(m_task_t task)
262 xbt_assert((task != NULL), "Invalid parameter");
264 if (task->simdata->compute) {
265 simcall_host_execution_cancel(task->simdata->compute);
267 else if (task->simdata->comm) {
268 simcall_comm_cancel(task->simdata->comm);
269 task->simdata->isused = 0;
274 /** \ingroup m_task_management
275 * \brief Returns the computation amount needed to process a task #m_task_t.
276 * Once a task has been processed, this amount is thus set to 0...
278 double MSG_task_get_compute_duration(m_task_t task)
280 xbt_assert((task != NULL)
281 && (task->simdata != NULL), "Invalid parameter");
283 return task->simdata->computation_amount;
287 /** \ingroup m_task_management
288 * \brief set the computation amount needed to process a task #m_task_t.
291 void MSG_task_set_compute_duration(m_task_t task,
292 double computation_amount)
294 xbt_assert(task, "Invalid parameter");
295 task->simdata->computation_amount = computation_amount;
299 /** \ingroup m_task_management
300 * \brief Returns the remaining computation amount of a task #m_task_t.
303 double MSG_task_get_remaining_computation(m_task_t task)
305 xbt_assert((task != NULL)
306 && (task->simdata != NULL), "Invalid parameter");
308 if (task->simdata->compute) {
309 return simcall_host_execution_get_remains(task->simdata->compute);
311 return task->simdata->computation_amount;
315 /** \ingroup m_task_management
316 * \brief Returns the total amount received by a task #m_task_t.
317 * If the communication does not exist it will return 0.
318 * So, if the communication has FINISHED or FAILED it returns
321 double MSG_task_get_remaining_communication(m_task_t task)
323 xbt_assert((task != NULL)
324 && (task->simdata != NULL), "Invalid parameter");
325 XBT_DEBUG("calling simcall_communication_get_remains(%p)",
326 task->simdata->comm);
327 return simcall_comm_get_remains(task->simdata->comm);
330 #ifdef HAVE_LATENCY_BOUND_TRACKING
331 /** \ingroup m_task_management
332 * \brief Return 1 if communication task is limited by latency, 0 otherwise
335 int MSG_task_is_latency_bounded(m_task_t task)
337 xbt_assert((task != NULL)
338 && (task->simdata != NULL), "Invalid parameter");
339 XBT_DEBUG("calling simcall_communication_is_latency_bounded(%p)",
340 task->simdata->comm);
341 return simcall_comm_is_latency_bounded(task->simdata->comm);
345 /** \ingroup m_task_management
346 * \brief Returns the size of the data attached to a task #m_task_t.
349 double MSG_task_get_data_size(m_task_t task)
351 xbt_assert((task != NULL)
352 && (task->simdata != NULL), "Invalid parameter");
354 return task->simdata->message_size;
359 /** \ingroup m_task_management
360 * \brief Changes the priority of a computation task. This priority doesn't affect
361 * the transfer rate. A priority of 2 will make a task receive two times more
362 * cpu power than the other ones.
365 void MSG_task_set_priority(m_task_t task, double priority)
367 xbt_assert((task != NULL)
368 && (task->simdata != NULL), "Invalid parameter");
370 task->simdata->priority = 1 / priority;
371 if (task->simdata->compute)
372 simcall_host_execution_set_priority(task->simdata->compute,
373 task->simdata->priority);