3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(task, msg,
12 "Logging specific to MSG (task)");
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 * 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 /********************************* Task **************************************/
27 /** \ingroup m_task_management
28 * \brief Creates a new #m_task_t.
30 * A constructor for #m_task_t taking four arguments and returning the
32 * \param name a name for the object. It is for user-level information
34 * \param compute_duration a value of the processing amount (in Mflop)
35 needed to process this new task. If 0, then it cannot be executed with
36 MSG_task_execute(). This value has to be >=0.
37 * \param message_size a value of the amount of data (in Mb) needed to
38 transfer this new task. If 0, then it cannot be transfered with
39 MSG_task_get() and MSG_task_put(). This value has to be >=0.
40 * \param data a pointer to any data may want to attach to the new
41 object. It is for user-level information and can be NULL. It can
42 be retrieved with the function \ref MSG_task_get_data.
44 * \return The new corresponding object.
46 m_task_t MSG_task_create(const char *name, double compute_duration,
47 double message_size, void *data)
49 simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
50 m_task_t task = xbt_new0(s_m_task_t,1);
53 task->name = xbt_strdup(name);
54 task->simdata = simdata;
58 simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
59 simdata->computation_amount = compute_duration;
60 simdata->message_size = message_size;
62 simdata->priority = 1.0;
64 simdata->sender = NULL;
69 /** \ingroup m_task_management
70 * \brief Return the user data of a #m_task_t.
72 * This functions checks whether \a task is a valid pointer or not and return
73 the user data associated to \a task if it is possible.
75 void *MSG_task_get_data(m_task_t task)
77 xbt_assert0((task != NULL), "Invalid parameter");
82 /** \ingroup m_task_management
83 * \brief Return the sender of a #m_task_t.
85 * This functions returns the #m_process_t which sent this task
87 m_process_t MSG_task_get_sender(m_task_t task)
89 xbt_assert0(task, "Invalid parameters");
90 return ((simdata_task_t) task->simdata)->sender;
93 /** \ingroup m_task_management
94 * \brief Return the name of a #m_task_t.
96 * This functions returns the name of a #m_task_t as specified on creation
98 const char *MSG_task_get_name(m_task_t task)
100 xbt_assert0(task, "Invalid parameters");
105 /** \ingroup m_task_management
106 * \brief Destroy a #m_task_t.
108 * Destructor for #m_task_t. Note that you should free user data, if any, \b
109 before calling this function.
111 MSG_error_t MSG_task_destroy(m_task_t task)
113 surf_action_t action = NULL;
115 xbt_assert0((task != NULL), "Invalid parameter");
117 task->simdata->using--;
118 if(task->simdata->using>0) return MSG_OK;
120 xbt_assert0((xbt_dynar_length(task->simdata->sleeping)==0),
121 "Task still used. There is a problem. Cannot destroy it now!");
123 if(task->name) free(task->name);
125 xbt_dynar_free(&(task->simdata->sleeping));
127 action = task->simdata->compute;
128 if(action) action->resource_type->common_public->action_free(action);
129 action = task->simdata->comm;
130 if(action) action->resource_type->common_public->action_free(action);
131 if(task->simdata->host_list) xbt_free(task->simdata->host_list);
140 /** \ingroup m_task_management
141 * \brief Cancel a #m_task_t.
142 * \param task the taskt to cancel. If it was executed or transfered, it
143 stops the process that were working on it.
145 MSG_error_t MSG_task_cancel(m_task_t task)
147 xbt_assert0((task != NULL), "Invalid parameter");
149 if(task->simdata->compute) {
150 surf_workstation_resource->common_public->action_cancel(task->simdata->compute);
153 if(task->simdata->comm) {
154 surf_workstation_resource->common_public->action_cancel(task->simdata->comm);
161 /** \ingroup m_task_management
162 * \brief Returns the computation amount needed to process a task #m_task_t.
163 * Once a task has been processed, this amount is thus set to 0...
165 double MSG_task_get_compute_duration(m_task_t task) {
166 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
168 return task->simdata->computation_amount;
171 /** \ingroup m_task_management
172 * \brief Returns the remaining computation amount of a task #m_task_t.
175 double MSG_task_get_remaining_computation(m_task_t task)
177 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
179 if(task->simdata->compute) {
180 return task->simdata->compute->remains;
182 return task->simdata->computation_amount;
186 /** \ingroup m_task_management
187 * \brief Returns the size of the data attached to a task #m_task_t.
190 double MSG_task_get_data_size(m_task_t task) {
191 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
193 return task->simdata->message_size;
196 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
199 m_process_t proc = NULL;
201 xbt_assert0(((task != NULL)
202 && (task->simdata != NULL)), "Invalid parameters");
204 xbt_dynar_push(task->simdata->sleeping, &process);
205 process->simdata->waiting_task = task;
207 process->simdata->waiting_task = NULL;
208 xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
210 xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);
217 /** \ingroup m_task_management
218 * \brief Changes the priority of a computation task. This priority doesn't affect
219 * the transfer rate. A priority of 2 will make a task receive two times more
220 * cpu power than the other ones.
223 void MSG_task_set_priority(m_task_t task, double priority) {
224 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
226 task->simdata->priority = 1/priority;
227 if(task->simdata->compute)
228 surf_workstation_resource->common_public->
229 set_priority(task->simdata->compute, task->simdata->priority);