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. */
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(task, msg,
12 "Logging specific to MSG (task)");
14 static char sprint_buffer[64];
16 /** \defgroup m_task_management Managing functions of Tasks
17 * \brief This section describes the task structure of MSG
18 * (#m_task_t) and the functions for managing it.
20 * Since most scheduling algorithms rely on a concept of task
21 * that can be either <em>computed</em> locally or
22 * <em>transferred</em> on another processor, it seems to be the
23 * right level of abstraction for our purposes. A <em>task</em>
24 * may then be defined by a <em>computing amount</em>, a
25 * <em>message size</em> and some <em>private data</em>.
28 /********************************* Task **************************************/
29 /** \ingroup m_task_management
30 * \brief Creates a new #m_task_t.
32 * A constructor for #m_task_t taking four arguments and returning the
34 * \param name a name for the object. It is for user-level information
36 * \param compute_duration a value of the processing amount (in Mflop)
37 needed to process this new task. If 0, then it cannot be executed with
38 MSG_task_execute(). This value has to be >=0.
39 * \param message_size a value of the amount of data (in Mb) needed to
40 transfer this new task. If 0, then it cannot be transfered with
41 MSG_task_get() and MSG_task_put(). This value has to be >=0.
42 * \param data a pointer to any data may want to attach to the new
43 object. It is for user-level information and can be NULL. It can
44 be retrieved with the function \ref MSG_task_get_data.
46 * \return The new corresponding object.
48 m_task_t MSG_task_create(const char *name, double compute_duration,
49 double message_size, void *data)
51 simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
52 m_task_t task = xbt_new0(s_m_task_t,1);
55 task->name = xbt_strdup(name);
56 task->simdata = simdata;
60 simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
61 simdata->computation_amount = compute_duration;
62 simdata->message_size = message_size;
65 simdata->sender = NULL;
71 /** \ingroup m_task_management
72 * \brief Return the user data of a #m_task_t.
74 * This functions checks whether \a task is a valid pointer or not and return
75 the user data associated to \a task if it is possible.
77 void *MSG_task_get_data(m_task_t task)
79 xbt_assert0((task != NULL), "Invalid parameter");
84 /** \ingroup m_task_management
85 * \brief Return the sender of a #m_task_t.
87 * This functions returns the #m_process_t which sent this task
89 m_process_t MSG_task_get_sender(m_task_t task)
91 xbt_assert0(task, "Invalid parameters");
92 return ((simdata_task_t) task->simdata)->sender;
95 /** \ingroup m_task_management
96 * \brief Return the name of a #m_task_t.
98 * This functions returns the name of a #m_task_t as specified on creation
100 const char *MSG_task_get_name(m_task_t task)
102 xbt_assert0(task, "Invalid parameters");
107 /** \ingroup m_task_management
108 * \brief Destroy a #m_task_t.
110 * Destructor for #m_task_t. Note that you should free user data, if any, \b
111 before calling this function.
113 MSG_error_t MSG_task_destroy(m_task_t task)
115 simdata_task_t simdata = NULL;
116 surf_action_t action = NULL;
119 xbt_assert0((task != NULL), "Invalid parameter");
121 task->simdata->using--;
122 if(task->simdata->using>0) return MSG_OK;
124 xbt_assert0((xbt_dynar_length(task->simdata->sleeping)==0),
125 "Task still used. There is a problem. Cannot destroy it now!");
127 if(task->name) free(task->name);
129 xbt_dynar_free(&(task->simdata->sleeping));
131 action = task->simdata->compute;
132 if(action) action->resource_type->common_public->action_free(action);
133 action = task->simdata->comm;
134 if(action) action->resource_type->common_public->action_free(action);
143 /** \ingroup m_task_management
144 * \brief Returns the computation amount needed to process a task #m_task_t.
147 double MSG_task_get_compute_duration(m_task_t task)
149 simdata_task_t simdata = NULL;
151 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
153 return task->simdata->computation_amount;
156 /** \ingroup m_task_management
157 * \brief Returns the size of the data attached to a task #m_task_t.
160 double MSG_task_get_data_size(m_task_t task)
162 simdata_task_t simdata = NULL;
164 xbt_assert0((task != NULL) && (task->simdata != NULL), "Invalid parameter");
166 return task->simdata->message_size;
169 /* static MSG_error_t __MSG_task_check(m_task_t task) */
171 /* simdata_task_t simdata = NULL; */
172 /* int warning = 0; */
174 /* if (task == NULL) { /\* Fatal *\/ */
175 /* WARNING("Task uninitialized"); */
176 /* return MSG_FATAL; */
178 /* simdata = task->simdata; */
180 /* if (simdata == NULL) { /\* Fatal *\/ */
181 /* WARNING("Simulator Data uninitialized"); */
182 /* return MSG_FATAL; */
185 /* if (simdata->compute == NULL) { /\* Fatal if execute ... *\/ */
186 /* WARNING("No duration set for this task"); */
190 /* if (simdata->message_size == 0) { /\* Fatal if transfered ... *\/ */
191 /* WARNING("No message_size set for this task"); */
195 /* /\* if (task->data == NULL) { *\/ */
196 /* /\* WARNING("User Data uninitialized"); *\/ */
197 /* /\* warning++; *\/ */
201 /* return MSG_WARNING; */
205 /* static m_task_t __MSG_task_copy(m_task_t src) */
207 /* m_task_t copy = NULL; */
208 /* simdata_task_t simdata = NULL; */
210 /* __MSG_task_check(src); */
212 /* simdata = src->simdata; */
213 /* copy = MSG_task_create(src->name, SG_getTaskCost(simdata->compute), */
214 /* simdata->message_size, MSG_task_get_data(src)); */
219 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
222 m_process_t proc = NULL;
224 xbt_assert0(((task != NULL)
225 && (task->simdata != NULL)), "Invalid parameters");
227 xbt_dynar_push(task->simdata->sleeping, &process);
228 process->simdata->waiting_task = task;
230 process->simdata->waiting_task = NULL;
231 xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
233 xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);