1 #include "msg_simix_private.h"
2 #include "xbt/sysdep.h"
5 /** \defgroup m_task_management Managing functions of Tasks
6 * \brief This section describes the task structure of MSG
7 * (#m_task_t) and the functions for managing it.
8 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Tasks" --> \endhtmlonly
10 * Since most scheduling algorithms rely on a concept of task
11 * that can be either <em>computed</em> locally or
12 * <em>transferred</em> on another processor, it seems to be the
13 * right level of abstraction for our purposes. A <em>task</em>
14 * may then be defined by a <em>computing amount</em>, a
15 * <em>message size</em> and some <em>private data</em>.
18 /********************************* Task **************************************/
19 /** \ingroup m_task_management
20 * \brief Creates a new #m_task_t.
22 * A constructor for #m_task_t taking four arguments and returning the
24 * \param name a name for the object. It is for user-level information
26 * \param compute_duration a value of the processing amount (in flop)
27 needed to process this new task. If 0, then it cannot be executed with
28 MSG_task_execute(). This value has to be >=0.
29 * \param message_size a value of the amount of data (in bytes) needed to
30 transfer this new task. If 0, then it cannot be transfered with
31 MSG_task_get() and MSG_task_put(). This value has to be >=0.
32 * \param data a pointer to any data may want to attach to the new
33 object. It is for user-level information and can be NULL. It can
34 be retrieved with the function \ref MSG_task_get_data.
36 * \return The new corresponding object.
38 m_task_t MSG_task_create(const char *name, double compute_duration,
39 double message_size, void *data)
41 m_task_t task = xbt_new(s_m_task_t,1);
42 simdata_task_t simdata = xbt_new(s_simdata_task_t,1);
43 task->simdata = simdata;
45 task->name = xbt_strdup(name);
49 simdata->computation_amount = compute_duration;
50 simdata->message_size = message_size;
52 simdata->priority = 1.0;
54 simdata->sender = NULL;
55 simdata->receiver = NULL;
56 simdata->cond = SIMIX_cond_init();
57 simdata->mutex = SIMIX_mutex_init();
58 simdata->compute = NULL;
61 simdata->host_list = NULL;
62 simdata->comp_amount = NULL;
63 simdata->comm_amount = NULL;
68 /** \ingroup m_task_management
69 * \brief Return the user data of a #m_task_t.
71 * This functions checks whether \a task is a valid pointer or not and return
72 the user data associated to \a task if it is possible.
74 void *MSG_task_get_data(m_task_t task)
76 xbt_assert0((task != NULL), "Invalid parameter");
81 /** \ingroup m_task_management
82 * \brief Return the sender of a #m_task_t.
84 * This functions returns the #m_process_t which sent this task
86 m_process_t MSG_task_get_sender(m_task_t task)
88 xbt_assert0(task, "Invalid parameters");
89 return ((simdata_task_t) task->simdata)->sender;
92 /** \ingroup m_task_management
93 * \brief Return the source of a #m_task_t.
95 * This functions returns the #m_host_t from which this task was sent
97 m_host_t MSG_task_get_source(m_task_t task)
99 xbt_assert0(task, "Invalid parameters");
100 return ((simdata_task_t) task->simdata)->source;
103 /** \ingroup m_task_management
104 * \brief Return the name of a #m_task_t.
106 * This functions returns the name of a #m_task_t as specified on creation
108 const char *MSG_task_get_name(m_task_t task)
110 xbt_assert0(task, "Invalid parameters");
115 /** \ingroup m_task_management
116 * \brief Destroy a #m_task_t.
118 * Destructor for #m_task_t. Note that you should free user data, if any, \b
119 before calling this function.
121 MSG_error_t MSG_task_destroy(m_task_t task)
123 smx_action_t action = NULL;
124 xbt_assert0((task != NULL), "Invalid parameter");
126 /* 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? */
127 task->simdata->using--;
128 if(task->simdata->using>0) return MSG_OK;
130 if(task->name) free(task->name);
132 SIMIX_cond_destroy(task->simdata->cond);
133 SIMIX_mutex_destroy(task->simdata->mutex);
135 action = task->simdata->compute;
136 if(action) SIMIX_action_destroy(action);
137 action = task->simdata->comm;
138 if(action) SIMIX_action_destroy(action);
139 /* parallel tasks only */
140 if(task->simdata->host_list) xbt_free(task->simdata->host_list);
142 /* free main structures */
143 xbt_free(task->simdata);
150 /** \ingroup m_task_management
151 * \brief Cancel a #m_task_t.
152 * \param task the taskt to cancel. If it was executed or transfered, it
153 stops the process that were working on it.
155 MSG_error_t MSG_task_cancel(m_task_t task)
157 xbt_assert0((task != NULL), "Invalid parameter");
159 if(task->simdata->compute) {
160 SIMIX_action_cancel(task->simdata->compute);
163 if(task->simdata->comm) {
164 SIMIX_action_cancel(task->simdata->comm);
171 /** \ingroup m_task_management
172 * \brief Returns the computation amount needed to process a task #m_task_t.
173 * Once a task has been processed, this amount is thus set to 0...
175 double MSG_task_get_compute_duration(m_task_t task)
177 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
179 return task->simdata->computation_amount;
182 /** \ingroup m_task_management
183 * \brief Returns the remaining computation amount of a task #m_task_t.
186 double MSG_task_get_remaining_computation(m_task_t task)
188 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
190 if(task->simdata->compute) {
191 return SIMIX_action_get_remains(task->simdata->compute);
193 return task->simdata->computation_amount;
197 /** \ingroup m_task_management
198 * \brief Returns the size of the data attached to a task #m_task_t.
201 double MSG_task_get_data_size(m_task_t task)
203 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
205 return task->simdata->message_size;
210 /** \ingroup m_task_management
211 * \brief Changes the priority of a computation task. This priority doesn't affect
212 * the transfer rate. A priority of 2 will make a task receive two times more
213 * cpu power than the other ones.
216 void MSG_task_set_priority(m_task_t task, double priority)
218 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
220 task->simdata->priority = 1/priority;
221 if(task->simdata->compute)
222 SIMIX_action_set_priority(task->simdata->compute, task->simdata->priority);