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 #m_task_t.
29 * A constructor for #m_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 m_task_t MSG_task_create(const char *name, double compute_duration,
46 double message_size, void *data)
48 m_task_t task = xbt_new(s_m_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 /*************** Begin GPU ***************/
80 /** \ingroup m_task_management
81 * \brief Creates a new #m_gpu_task_t.
83 * A constructor for #m_gpu_task_t taking four arguments and returning
84 a pointer to the new created GPU task.
86 * \param name a name for the object. It is for user-level information
89 * \param compute_duration a value of the processing amount (in flop)
90 needed to process this new task. If 0, then it cannot be executed with
91 MSG_gpu_task_execute(). This value has to be >=0.
93 * \param dispatch_latency time in seconds to load this task on the GPU
95 * \param collect_latency time in seconds to transfer result from the GPU
96 back to the CPU (host) when done
99 * \return The new corresponding object.
101 m_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
102 double dispatch_latency, double collect_latency)
104 m_gpu_task_t task = xbt_new(s_m_gpu_task_t, 1);
105 simdata_gpu_task_t simdata = xbt_new(s_simdata_gpu_task_t, 1);
106 task->simdata = simdata;
108 task->name = xbt_strdup(name);
111 simdata->computation_amount = compute_duration;
112 simdata->dispatch_latency = dispatch_latency;
113 simdata->collect_latency = collect_latency;
117 /* TRACE_msg_gpu_task_create(task); */
122 /*************** End GPU ***************/
124 /** \ingroup m_task_management
125 * \brief Return the user data of a #m_task_t.
127 * This function checks whether \a task is a valid pointer or not and return
128 the user data associated to \a task if it is possible.
130 void *MSG_task_get_data(m_task_t task)
132 xbt_assert((task != NULL), "Invalid parameter");
137 /** \ingroup m_task_management
138 * \brief Sets the user data of a #m_task_t.
140 * This function allows to associate a new pointer to
141 the user data associated of \a task.
143 void MSG_task_set_data(m_task_t task, void *data)
145 xbt_assert((task != NULL), "Invalid parameter");
150 /** \ingroup m_task_management
151 * \brief Sets a function to be called when a task has just been copied.
152 * \param callback a callback function
154 void MSG_task_set_copy_callback(void (*callback)
155 (m_task_t task, m_process_t sender, m_process_t receiver)) {
157 msg_global->task_copy_callback = callback;
160 SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
163 SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
167 /** \ingroup m_task_management
168 * \brief Return the sender of a #m_task_t.
170 * This functions returns the #m_process_t which sent this task
172 m_process_t MSG_task_get_sender(m_task_t task)
174 xbt_assert(task, "Invalid parameters");
175 return ((simdata_task_t) task->simdata)->sender;
178 /** \ingroup m_task_management
179 * \brief Return the source of a #m_task_t.
181 * This functions returns the #m_host_t from which this task was sent
183 m_host_t MSG_task_get_source(m_task_t task)
185 xbt_assert(task, "Invalid parameters");
186 return ((simdata_task_t) task->simdata)->source;
189 /** \ingroup m_task_management
190 * \brief Return the name of a #m_task_t.
192 * This functions returns the name of a #m_task_t as specified on creation
194 const char *MSG_task_get_name(m_task_t task)
196 xbt_assert(task, "Invalid parameters");
200 /** \ingroup m_task_management
201 * \brief Sets the name of a #m_task_t.
203 * This functions allows to associate a name to a task
205 void MSG_task_set_name(m_task_t task, const char *name)
207 xbt_assert(task, "Invalid parameters");
208 task->name = xbt_strdup(name);
211 /** \ingroup m_task_management
212 * \brief Destroy a #m_task_t.
214 * Destructor for #m_task_t. Note that you should free user data, if any, \b
215 * before calling this function.
217 * Only the process that owns the task can destroy it.
218 * The owner changes after a successful send.
219 * If a task is successfully sent, the receiver becomes the owner and is
220 * supposed to destroy it. The sender should not use it anymore.
221 * If the task failed to be sent, the sender remains the owner of the task.
223 MSG_error_t MSG_task_destroy(m_task_t task)
225 smx_action_t action = NULL;
226 xbt_assert((task != NULL), "Invalid parameter");
228 if (task->simdata->isused) {
229 /* the task is being sent or executed: cancel it first */
230 MSG_task_cancel(task);
233 TRACE_msg_task_destroy(task);
236 xbt_free(task->name);
238 action = task->simdata->compute;
240 simcall_host_execution_destroy(action);
242 /* parallel tasks only */
243 xbt_free(task->simdata->host_list);
245 /* free main structures */
246 xbt_free(task->simdata);
253 /** \ingroup m_task_usage
254 * \brief Cancel a #m_task_t.
255 * \param task the task to cancel. If it was executed or transfered, it
256 stops the process that were working on it.
258 MSG_error_t MSG_task_cancel(m_task_t task)
260 xbt_assert((task != NULL), "Invalid parameter");
262 if (task->simdata->compute) {
263 simcall_host_execution_cancel(task->simdata->compute);
265 else if (task->simdata->comm) {
266 simcall_comm_cancel(task->simdata->comm);
267 task->simdata->isused = 0;
272 /** \ingroup m_task_management
273 * \brief Returns the computation amount needed to process a task #m_task_t.
274 * Once a task has been processed, this amount is thus set to 0...
276 double MSG_task_get_compute_duration(m_task_t task)
278 xbt_assert((task != NULL)
279 && (task->simdata != NULL), "Invalid parameter");
281 return task->simdata->computation_amount;
285 /** \ingroup m_task_management
286 * \brief set the computation amount needed to process a task #m_task_t.
289 void MSG_task_set_compute_duration(m_task_t task,
290 double computation_amount)
292 xbt_assert(task, "Invalid parameter");
293 task->simdata->computation_amount = computation_amount;
297 /** \ingroup m_task_management
298 * \brief Returns the remaining computation amount of a task #m_task_t.
301 double MSG_task_get_remaining_computation(m_task_t task)
303 xbt_assert((task != NULL)
304 && (task->simdata != NULL), "Invalid parameter");
306 if (task->simdata->compute) {
307 return simcall_host_execution_get_remains(task->simdata->compute);
309 return task->simdata->computation_amount;
313 /** \ingroup m_task_management
314 * \brief Returns the total amount received by a task #m_task_t.
315 * If the communication does not exist it will return 0.
316 * So, if the communication has FINISHED or FAILED it returns
319 double MSG_task_get_remaining_communication(m_task_t task)
321 xbt_assert((task != NULL)
322 && (task->simdata != NULL), "Invalid parameter");
323 XBT_DEBUG("calling simcall_communication_get_remains(%p)",
324 task->simdata->comm);
325 return simcall_comm_get_remains(task->simdata->comm);
328 #ifdef HAVE_LATENCY_BOUND_TRACKING
329 /** \ingroup m_task_management
330 * \brief Return 1 if communication task is limited by latency, 0 otherwise
333 int MSG_task_is_latency_bounded(m_task_t task)
335 xbt_assert((task != NULL)
336 && (task->simdata != NULL), "Invalid parameter");
337 XBT_DEBUG("calling simcall_communication_is_latency_bounded(%p)",
338 task->simdata->comm);
339 return simcall_comm_is_latency_bounded(task->simdata->comm);
343 /** \ingroup m_task_management
344 * \brief Returns the size of the data attached to a task #m_task_t.
347 double MSG_task_get_data_size(m_task_t task)
349 xbt_assert((task != NULL)
350 && (task->simdata != NULL), "Invalid parameter");
352 return task->simdata->message_size;
357 /** \ingroup m_task_management
358 * \brief Changes the priority of a computation task. This priority doesn't affect
359 * the transfer rate. A priority of 2 will make a task receive two times more
360 * cpu power than the other ones.
363 void MSG_task_set_priority(m_task_t task, double priority)
365 xbt_assert((task != NULL)
366 && (task->simdata != NULL), "Invalid parameter");
368 task->simdata->priority = 1 / priority;
369 if (task->simdata->compute)
370 simcall_host_execution_set_priority(task->simdata->compute,
371 task->simdata->priority);