3 /* Copyright (c) 2002-2007 Arnaud Legrand. */
4 /* Copyright (c) 2007 Bruno Donassolo. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
14 /** \defgroup m_task_management Managing functions of Tasks
15 * \brief This section describes the task structure of MSG
16 * (#m_task_t) and the functions for managing it.
18 /** @addtogroup m_task_management
19 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
21 * Since most scheduling algorithms rely on a concept of task
22 * that can be either <em>computed</em> locally or
23 * <em>transferred</em> on another processor, it seems to be the
24 * right level of abstraction for our purposes. A <em>task</em>
25 * may then be defined by a <em>computing amount</em>, a
26 * <em>message size</em> and some <em>private data</em>.
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);
60 simdata->computation_amount = compute_duration;
61 simdata->message_size = message_size;
63 simdata->priority = 1.0;
64 simdata->refcount = 1;
65 simdata->sender = NULL;
66 simdata->receiver = NULL;
67 simdata->cond = SIMIX_cond_init();
68 simdata->mutex = SIMIX_mutex_init();
69 simdata->compute = NULL;
72 simdata->host_list = NULL;
73 simdata->comp_amount = NULL;
74 simdata->comm_amount = NULL;
76 TRACE_msg_task_create (task);
82 /** prevent the task from being destroyed too quickly (but also prevent it from being sent). Mainly useful in bindings */
83 void MSG_task_ref(m_task_t t) {
84 t->simdata->refcount++;
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");
147 /** \ingroup m_task_management
148 * \brief Destroy a #m_task_t.
150 * Destructor for #m_task_t. Note that you should free user data, if any, \b
151 before calling this function.
153 MSG_error_t MSG_task_destroy(m_task_t task)
155 smx_action_t action = NULL;
156 xbt_assert0((task != NULL), "Invalid parameter");
158 /* 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? */
159 task->simdata->refcount--;
160 if (task->simdata->refcount > 0)
163 TRACE_msg_task_destroy (task);
169 SIMIX_cond_destroy(task->simdata->cond);
170 SIMIX_mutex_destroy(task->simdata->mutex);
172 action = task->simdata->compute;
174 SIMIX_action_destroy(action);
176 /* parallel tasks only */
177 if (task->simdata->host_list)
178 xbt_free(task->simdata->host_list);
180 /* free main structures */
181 xbt_free(task->simdata);
188 /** \ingroup m_task_management
189 * \brief Cancel a #m_task_t.
190 * \param task the taskt to cancel. If it was executed or transfered, it
191 stops the process that were working on it.
193 MSG_error_t MSG_task_cancel(m_task_t task)
195 xbt_assert0((task != NULL), "Invalid parameter");
197 if (task->simdata->compute) {
198 SIMIX_action_cancel(task->simdata->compute);
201 if (task->simdata->comm) {
202 SIMIX_communication_cancel(task->simdata->comm);
208 /** \ingroup m_task_management
209 * \brief Returns the computation amount needed to process a task #m_task_t.
210 * Once a task has been processed, this amount is thus set to 0...
212 double MSG_task_get_compute_duration(m_task_t task)
214 xbt_assert0((task != NULL)
215 && (task->simdata != NULL), "Invalid parameter");
217 return task->simdata->computation_amount;
220 /** \ingroup m_task_management
221 * \brief Returns the remaining computation amount of a task #m_task_t.
224 double MSG_task_get_remaining_computation(m_task_t task)
226 xbt_assert0((task != NULL)
227 && (task->simdata != NULL), "Invalid parameter");
229 if (task->simdata->compute) {
230 return SIMIX_action_get_remains(task->simdata->compute);
232 return task->simdata->computation_amount;
238 /** \ingroup m_task_management
239 * \brief Returns the total amount received by a task #m_task_t.
242 double MSG_task_get_remaining_communication(m_task_t task)
244 xbt_assert0((task != NULL)
245 && (task->simdata != NULL), "Invalid parameter");
247 return SIMIX_communication_get_remains(task->simdata->comm);
250 /** \ingroup m_task_management
251 * \brief Returns the size of the data attached to a task #m_task_t.
254 double MSG_task_get_data_size(m_task_t task)
256 xbt_assert0((task != NULL)
257 && (task->simdata != NULL), "Invalid parameter");
259 return task->simdata->message_size;
264 /** \ingroup m_task_management
265 * \brief Changes the priority of a computation task. This priority doesn't affect
266 * the transfer rate. A priority of 2 will make a task receive two times more
267 * cpu power than the other ones.
270 void MSG_task_set_priority(m_task_t task, double priority)
272 xbt_assert0((task != NULL)
273 && (task->simdata != NULL), "Invalid parameter");
275 task->simdata->priority = 1 / priority;
276 if (task->simdata->compute)
277 SIMIX_action_set_priority(task->simdata->compute,
278 task->simdata->priority);
282 /** \ingroup m_task_management
283 * \brief Sets the user data of a #m_task_t.
285 * This function allows to test if a task contains a not_null data
288 int MSG_task_has_data(m_task_t task)
291 return (task->data != NULL);