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;
52 task->name = xbt_strdup(name);
57 simdata->computation_amount = compute_duration;
58 simdata->message_size = message_size;
60 simdata->priority = 1.0;
62 simdata->sender = NULL;
63 simdata->receiver = NULL;
64 simdata->compute = NULL;
67 simdata->host_list = NULL;
68 simdata->comp_amount = NULL;
69 simdata->comm_amount = NULL;
71 TRACE_msg_task_create(task);
77 /*************** Begin GPU ***************/
78 /** \ingroup m_task_management
79 * \brief Creates a new #m_gpu_task_t.
81 * A constructor for #m_gpu_task_t taking four arguments and returning
82 a pointer to the new created GPU task.
84 * \param name a name for the object. It is for user-level information
87 * \param compute_duration a value of the processing amount (in flop)
88 needed to process this new task. If 0, then it cannot be executed with
89 MSG_gpu_task_execute(). This value has to be >=0.
91 * \param dispatch_latency time in seconds to load this task on the GPU
93 * \param collect_latency time in seconds to transfer result from the GPU
94 back to the CPU (host) when done
97 * \return The new corresponding object.
99 m_gpu_task_t MSG_gpu_task_create(const char *name, double compute_duration,
100 double dispatch_latency, double collect_latency)
102 m_gpu_task_t task = xbt_new(s_m_gpu_task_t, 1);
103 simdata_gpu_task_t simdata = xbt_new(s_simdata_gpu_task_t, 1);
104 task->simdata = simdata;
106 task->name = xbt_strdup(name);
109 simdata->computation_amount = compute_duration;
110 simdata->dispatch_latency = dispatch_latency;
111 simdata->collect_latency = collect_latency;
115 /* TRACE_msg_gpu_task_create(task); */
120 /*************** End GPU ***************/
122 /** \ingroup m_task_management
123 * \brief Return the user data of a #m_task_t.
125 * This function checks whether \a task is a valid pointer or not and return
126 the user data associated to \a task if it is possible.
128 void *MSG_task_get_data(m_task_t task)
130 xbt_assert((task != NULL), "Invalid parameter");
135 /** \ingroup m_task_management
136 * \brief Sets the user data of a #m_task_t.
138 * This function allows to associate a new pointer to
139 the user data associated of \a task.
141 void MSG_task_set_data(m_task_t task, void *data)
143 xbt_assert((task != NULL), "Invalid parameter");
148 /** \ingroup m_task_management
149 * \brief Sets a function to be called when a task has just been copied.
150 * \param callback a callback function
152 void MSG_task_set_copy_callback(void (*callback)
153 (m_task_t task, m_process_t sender, m_process_t receiver)) {
155 msg_global->task_copy_callback = callback;
158 SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
161 SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
165 /** \ingroup m_task_management
166 * \brief Return the sender of a #m_task_t.
168 * This functions returns the #m_process_t which sent this task
170 m_process_t MSG_task_get_sender(m_task_t task)
172 xbt_assert(task, "Invalid parameters");
173 return ((simdata_task_t) task->simdata)->sender;
176 /** \ingroup m_task_management
177 * \brief Return the source of a #m_task_t.
179 * This functions returns the #m_host_t from which this task was sent
181 m_host_t MSG_task_get_source(m_task_t task)
183 xbt_assert(task, "Invalid parameters");
184 return ((simdata_task_t) task->simdata)->source;
187 /** \ingroup m_task_management
188 * \brief Return the name of a #m_task_t.
190 * This functions returns the name of a #m_task_t as specified on creation
192 const char *MSG_task_get_name(m_task_t task)
194 xbt_assert(task, "Invalid parameters");
198 /** \ingroup m_task_management
199 * \brief Sets the name of a #m_task_t.
201 * This functions allows to associate a name to a task
203 void MSG_task_set_name(m_task_t task, const char *name)
205 xbt_assert(task, "Invalid parameters");
206 task->name = xbt_strdup(name);
209 /** \ingroup m_task_management
210 * \brief Destroy a #m_task_t.
212 * Destructor for #m_task_t. Note that you should free user data, if any, \b
213 * before calling this function.
215 * Only the process that owns the task can destroy it.
216 * The owner changes after a successful send.
217 * If a task is successfully sent, the receiver becomes the owner and is
218 * supposed to destroy it. The sender should not use it anymore.
219 * If the task failed to be sent, the sender remains the owner of the task.
221 MSG_error_t MSG_task_destroy(m_task_t task)
223 smx_action_t action = NULL;
224 xbt_assert((task != NULL), "Invalid parameter");
226 if (task->simdata->isused) {
227 /* the task is being sent or executed: cancel it first */
228 MSG_task_cancel(task);
231 TRACE_msg_task_destroy(task);
234 xbt_free(task->name);
236 action = task->simdata->compute;
238 simcall_host_execution_destroy(action);
240 /* parallel tasks only */
241 xbt_free(task->simdata->host_list);
243 /* free main structures */
244 xbt_free(task->simdata);
251 /** \ingroup m_task_usage
252 * \brief Cancel a #m_task_t.
253 * \param task the task to cancel. If it was executed or transfered, it
254 stops the process that were working on it.
256 MSG_error_t MSG_task_cancel(m_task_t task)
258 xbt_assert((task != NULL), "Invalid parameter");
260 if (task->simdata->compute) {
261 simcall_host_execution_cancel(task->simdata->compute);
263 else if (task->simdata->comm) {
264 simcall_comm_cancel(task->simdata->comm);
265 task->simdata->isused = 0;
270 /** \ingroup m_task_management
271 * \brief Returns the computation amount needed to process a task #m_task_t.
272 * Once a task has been processed, this amount is thus set to 0...
274 double MSG_task_get_compute_duration(m_task_t task)
276 xbt_assert((task != NULL)
277 && (task->simdata != NULL), "Invalid parameter");
279 return task->simdata->computation_amount;
283 /** \ingroup m_task_management
284 * \brief set the computation amount needed to process a task #m_task_t.
287 void MSG_task_set_compute_duration(m_task_t task,
288 double computation_amount)
290 xbt_assert(task, "Invalid parameter");
291 task->simdata->computation_amount = computation_amount;
295 /** \ingroup m_task_management
296 * \brief Returns the remaining computation amount of a task #m_task_t.
299 double MSG_task_get_remaining_computation(m_task_t task)
301 xbt_assert((task != NULL)
302 && (task->simdata != NULL), "Invalid parameter");
304 if (task->simdata->compute) {
305 return simcall_host_execution_get_remains(task->simdata->compute);
307 return task->simdata->computation_amount;
311 /** \ingroup m_task_management
312 * \brief Returns the total amount received by a task #m_task_t.
313 * If the communication does not exist it will return 0.
314 * So, if the communication has FINISHED or FAILED it returns
317 double MSG_task_get_remaining_communication(m_task_t task)
319 xbt_assert((task != NULL)
320 && (task->simdata != NULL), "Invalid parameter");
321 XBT_DEBUG("calling simcall_communication_get_remains(%p)",
322 task->simdata->comm);
323 return simcall_comm_get_remains(task->simdata->comm);
326 #ifdef HAVE_LATENCY_BOUND_TRACKING
327 /** \ingroup m_task_management
328 * \brief Return 1 if communication task is limited by latency, 0 otherwise
331 int MSG_task_is_latency_bounded(m_task_t task)
333 xbt_assert((task != NULL)
334 && (task->simdata != NULL), "Invalid parameter");
335 XBT_DEBUG("calling simcall_communication_is_latency_bounded(%p)",
336 task->simdata->comm);
337 return simcall_comm_is_latency_bounded(task->simdata->comm);
341 /** \ingroup m_task_management
342 * \brief Returns the size of the data attached to a task #m_task_t.
345 double MSG_task_get_data_size(m_task_t task)
347 xbt_assert((task != NULL)
348 && (task->simdata != NULL), "Invalid parameter");
350 return task->simdata->message_size;
355 /** \ingroup m_task_management
356 * \brief Changes the priority of a computation task. This priority doesn't affect
357 * the transfer rate. A priority of 2 will make a task receive two times more
358 * cpu power than the other ones.
361 void MSG_task_set_priority(m_task_t task, double priority)
363 xbt_assert((task != NULL)
364 && (task->simdata != NULL), "Invalid parameter");
366 task->simdata->priority = 1 / priority;
367 if (task->simdata->compute)
368 simcall_host_execution_set_priority(task->simdata->compute,
369 task->simdata->priority);