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 #ifdef HAVE_RUBY /* FIXME: KILLME */
27 XBT_LOG_EXTERNAL_CATEGORY(ruby);
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg,
33 "Logging specific to MSG (task)");
35 /********************************* Task **************************************/
36 /** \ingroup m_task_management
37 * \brief Creates a new #m_task_t.
39 * A constructor for #m_task_t taking four arguments and returning the
41 * \param name a name for the object. It is for user-level information
43 * \param compute_duration a value of the processing amount (in flop)
44 needed to process this new task. If 0, then it cannot be executed with
45 MSG_task_execute(). This value has to be >=0.
46 * \param message_size a value of the amount of data (in bytes) needed to
47 transfer this new task. If 0, then it cannot be transfered with
48 MSG_task_get() and MSG_task_put(). This value has to be >=0.
49 * \param data a pointer to any data may want to attach to the new
50 object. It is for user-level information and can be NULL. It can
51 be retrieved with the function \ref MSG_task_get_data.
53 * \return The new corresponding object.
55 m_task_t MSG_task_create(const char *name, double compute_duration,
56 double message_size, void *data)
58 m_task_t task = xbt_new(s_m_task_t, 1);
59 simdata_task_t simdata = xbt_new(s_simdata_task_t, 1);
60 task->simdata = simdata;
62 task->name = xbt_strdup(name);
67 simdata->computation_amount = compute_duration;
68 simdata->message_size = message_size;
70 simdata->priority = 1.0;
72 simdata->sender = NULL;
73 simdata->receiver = NULL;
74 simdata->compute = NULL;
77 simdata->host_list = NULL;
78 simdata->comp_amount = NULL;
79 simdata->comm_amount = NULL;
81 TRACE_msg_task_create(task);
87 /** \ingroup m_task_management
88 * \brief Return the user data of a #m_task_t.
90 * This function checks whether \a task is a valid pointer or not and return
91 the user data associated to \a task if it is possible.
93 void *MSG_task_get_data(m_task_t task)
95 xbt_assert0((task != NULL), "Invalid parameter");
100 /** \ingroup m_task_management
101 * \brief Sets the user data of a #m_task_t.
103 * This function allows to associate a new pointer to
104 the user data associated of \a task.
106 void MSG_task_set_data(m_task_t task, void *data)
108 xbt_assert0((task != NULL), "Invalid parameter");
113 /** \ingroup m_task_management
114 * \brief Return the sender of a #m_task_t.
116 * This functions returns the #m_process_t which sent this task
118 m_process_t MSG_task_get_sender(m_task_t task)
120 xbt_assert0(task, "Invalid parameters");
121 return ((simdata_task_t) task->simdata)->sender;
124 /** \ingroup m_task_management
125 * \brief Return the source of a #m_task_t.
127 * This functions returns the #m_host_t from which this task was sent
129 m_host_t MSG_task_get_source(m_task_t task)
131 xbt_assert0(task, "Invalid parameters");
132 return ((simdata_task_t) task->simdata)->source;
135 /** \ingroup m_task_management
136 * \brief Return the name of a #m_task_t.
138 * This functions returns the name of a #m_task_t as specified on creation
140 const char *MSG_task_get_name(m_task_t task)
142 xbt_assert0(task, "Invalid parameters");
146 /** \ingroup m_task_management
147 * \brief Return the name of a #m_task_t.
149 * This functions allows to associate a name to a task
151 void MSG_task_set_name(m_task_t task, const char *name)
153 xbt_assert0(task, "Invalid parameters");
154 task->name = xbt_strdup(name);
157 /** \ingroup m_task_management
158 * \brief Destroy a #m_task_t.
160 * Destructor for #m_task_t. Note that you should free user data, if any, \b
161 before calling this function.
163 MSG_error_t MSG_task_destroy(m_task_t task)
165 smx_action_t action = NULL;
166 xbt_assert0((task != NULL), "Invalid parameter");
168 /* why? if somebody is using, then you can't free! ok... but will return MSG_OK? when this task will be destroyed? isn't the user code wrong? */
169 if (task->simdata->isused > 0) {
170 DEBUG1("Cannot destroy task %p since somebody is using it", task);
174 TRACE_msg_task_destroy(task);
180 action = task->simdata->compute;
182 SIMIX_req_host_execution_destroy(action);
184 /* parallel tasks only */
185 if (task->simdata->host_list)
186 xbt_free(task->simdata->host_list);
188 /* free main structures */
189 xbt_free(task->simdata);
196 /** \ingroup m_task_management
197 * \brief Cancel a #m_task_t.
198 * \param task the taskt to cancel. If it was executed or transfered, it
199 stops the process that were working on it.
201 MSG_error_t MSG_task_cancel(m_task_t task)
203 xbt_assert0((task != NULL), "Invalid parameter");
205 if (task->simdata->compute) {
206 SIMIX_req_host_execution_cancel(task->simdata->compute);
209 if (task->simdata->comm) {
210 SIMIX_req_comm_cancel(task->simdata->comm);
216 /** \ingroup m_task_management
217 * \brief Returns the computation amount needed to process a task #m_task_t.
218 * Once a task has been processed, this amount is thus set to 0...
220 double MSG_task_get_compute_duration(m_task_t task)
222 xbt_assert0((task != NULL)
223 && (task->simdata != NULL), "Invalid parameter");
225 return task->simdata->computation_amount;
229 /** \ingroup m_task_management
230 * \brief set the computation amount needed to process a task #m_task_t.
233 void MSG_task_set_compute_duration(m_task_t task,
234 double computation_amount)
236 xbt_assert0(task, "Invalid parameter");
237 task->simdata->computation_amount = computation_amount;
241 /** \ingroup m_task_management
242 * \brief Returns the remaining computation amount of a task #m_task_t.
245 double MSG_task_get_remaining_computation(m_task_t task)
247 xbt_assert0((task != NULL)
248 && (task->simdata != NULL), "Invalid parameter");
250 if (task->simdata->compute) {
251 return SIMIX_req_host_execution_get_remains(task->simdata->compute);
253 return task->simdata->computation_amount;
257 /** \ingroup m_task_management
258 * \brief Returns the total amount received by a task #m_task_t.
259 * If the communication does not exist it will return 0.
260 * So, if the communication has FINISHED or FAILED it returns
263 double MSG_task_get_remaining_communication(m_task_t task)
265 xbt_assert0((task != NULL)
266 && (task->simdata != NULL), "Invalid parameter");
267 DEBUG1("calling SIMIX_req_communication_get_remains(%p)",
268 task->simdata->comm);
269 return SIMIX_req_comm_get_remains(task->simdata->comm);
272 #ifdef HAVE_LATENCY_BOUND_TRACKING
273 /** \ingroup m_task_management
274 * \brief Return 1 if communication task is limited by latency, 0 otherwise
277 int MSG_task_is_latency_bounded(m_task_t task)
279 xbt_assert0((task != NULL)
280 && (task->simdata != NULL), "Invalid parameter");
281 DEBUG1("calling SIMIX_req_communication_is_latency_bounded(%p)",
282 task->simdata->comm);
283 return SIMIX_req_comm_is_latency_bounded(task->simdata->comm);
287 /** \ingroup m_task_management
288 * \brief Returns the size of the data attached to a task #m_task_t.
291 double MSG_task_get_data_size(m_task_t task)
293 xbt_assert0((task != NULL)
294 && (task->simdata != NULL), "Invalid parameter");
296 return task->simdata->message_size;
301 /** \ingroup m_task_management
302 * \brief Changes the priority of a computation task. This priority doesn't affect
303 * the transfer rate. A priority of 2 will make a task receive two times more
304 * cpu power than the other ones.
307 void MSG_task_set_priority(m_task_t task, double priority)
309 xbt_assert0((task != NULL)
310 && (task->simdata != NULL), "Invalid parameter");
312 task->simdata->priority = 1 / priority;
313 if (task->simdata->compute)
314 SIMIX_req_host_execution_set_priority(task->simdata->compute,
315 task->simdata->priority);