Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a40dd23854de565fb58d0cca651d6da54117ca0b
[simgrid.git] / src / simdag / sd_task.c
1 #include "private.h"
2 #include "simdag/simdag.h"
3 #include "xbt/sysdep.h"
4 #include "xbt/dynar.h"
5
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task,sd,
7                                 "Logging specific to SimDag (task)");
8
9 static void __SD_task_remove_dependencies(SD_task_t task);
10
11 /**
12  * \brief Creates a new task.
13  *
14  * \param name the name of the task (can be \c NULL)
15  * \param data the user data you want to associate with the task (can be \c NULL)
16  * \return the new task
17  * \see SD_task_destroy()
18  */
19 SD_task_t SD_task_create(const char *name, void *data, double amount) {
20   SD_CHECK_INIT_DONE();
21
22   SD_task_t task = xbt_new0(s_SD_task_t, 1);
23
24   /* general information */
25   task->data = data; /* user data */
26   if (name != NULL)
27     task->name = xbt_strdup(name);
28   else
29     task->name = NULL;
30
31   task->state_set = sd_global->not_scheduled_task_set;
32   xbt_swag_insert(task,task->state_set);
33
34   task->amount = amount;
35   task->surf_action = NULL;
36   task->watch_points = 0;
37   task->state_changed = 0;
38
39   /* dependencies */
40   task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
41   task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
42
43   /* scheduling parameters */
44   task->workstation_nb = 0;
45   task->workstation_list = NULL;
46   task->computation_amount = NULL;
47   task->communication_amount = NULL;
48   task->rate = 0;
49
50   return task;
51 }
52
53 /**
54  * \brief Returns the user data of a task
55  *
56  * \param task a task
57  * \return the user data associated with this task (can be \c NULL)
58  * \see SD_task_set_data()
59  */
60 void* SD_task_get_data(SD_task_t task) {
61   SD_CHECK_INIT_DONE();
62   xbt_assert0(task != NULL, "Invalid parameter");
63   return task->data;
64 }
65
66 /**
67  * \brief Sets the user data of a task
68  *
69  * The new data can be \c NULL. The old data should have been freed first
70  * if it was not \c NULL.
71  *
72  * \param task a task
73  * \param data the new data you want to associate with this task
74  * \see SD_task_get_data()
75  */
76 void SD_task_set_data(SD_task_t task, void *data) {
77   SD_CHECK_INIT_DONE();
78   xbt_assert0(task != NULL, "Invalid parameter");
79   task->data = data;
80 }
81
82 /**
83  * \brief Returns the state of a task
84  *
85  * \param task a task
86  * \return the current \ref e_SD_task_state_t "state" of this task:
87  * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_READY, #SD_RUNNING, #SD_DONE or #SD_FAILED
88  * \see e_SD_task_state_t
89  */
90 e_SD_task_state_t SD_task_get_state(SD_task_t task) {
91   SD_CHECK_INIT_DONE();
92   xbt_assert0(task != NULL, "Invalid parameter");
93
94   if (task->state_set == sd_global->scheduled_task_set)
95     return SD_SCHEDULED;
96   if (task->state_set == sd_global->done_task_set)
97     return SD_DONE;
98   if (task->state_set == sd_global->running_task_set)
99     return SD_RUNNING;
100   if (task->state_set == sd_global->ready_task_set)
101     return SD_READY;
102   if (task->state_set == sd_global->not_scheduled_task_set)
103     return SD_NOT_SCHEDULED;
104   return SD_FAILED;
105 }
106
107 /* Changes the state of a task. Updates the swags and the flag sd_global->watch_point_reached.
108  */
109 void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state) {
110   xbt_swag_remove(task, task->state_set);
111   switch (new_state) {
112   case SD_NOT_SCHEDULED:
113     task->state_set = sd_global->not_scheduled_task_set;
114     break;
115   case SD_SCHEDULED:
116     task->state_set = sd_global->scheduled_task_set;
117     break;
118   case SD_READY:
119     task->state_set = sd_global->ready_task_set;
120     break;
121   case SD_RUNNING:
122     task->state_set = sd_global->running_task_set;
123     break;
124   case SD_DONE:
125     task->state_set = sd_global->done_task_set;
126     break;
127   case SD_FAILED:
128     task->state_set = sd_global->failed_task_set;
129     break;
130   default:
131     xbt_assert0(0, "Invalid state");
132   }
133   xbt_swag_insert(task, task->state_set);
134
135   if (task->watch_points & new_state) {
136     INFO1("Watch point reached with task '%s'!", SD_task_get_name(task));
137     sd_global->watch_point_reached = 1;
138     SD_task_unwatch(task, new_state); /* remove the watch point */
139   }
140 }
141
142 /**
143  * \brief Returns the name of a task
144  *
145  * \param task a task
146  * \return the name of this task (can be \c NULL)
147  */
148 const char* SD_task_get_name(SD_task_t task) {
149   SD_CHECK_INIT_DONE();
150   xbt_assert0(task != NULL, "Invalid parameter");
151   return task->name;
152 }
153
154 /**
155  * \brief Returns the total amount of a task
156  *
157  * \param task a task
158  * \return the total amount of this task
159  * \see SD_task_get_remaining_amount()
160  */
161 double SD_task_get_amount(SD_task_t task) {
162   SD_CHECK_INIT_DONE();
163   xbt_assert0(task != NULL, "Invalid parameter");
164   return task->amount;
165 }
166
167 /**
168  * \brief Returns the remaining amount of a task
169  *
170  * \param task a task
171  * \return the remaining amount of this task
172  * \see SD_task_get_amount()
173  */
174 double SD_task_get_remaining_amount(SD_task_t task) {
175   SD_CHECK_INIT_DONE();
176   xbt_assert0(task != NULL, "Invalid parameter");
177
178   if (task->surf_action)
179     return task->surf_action->remains;
180   else
181     return task->amount;
182 }
183
184 /* temporary function for debbuging */
185 static void __SD_print_dependencies(SD_task_t task) {
186   INFO1("The following tasks must be executed before %s:", SD_task_get_name(task));
187   xbt_dynar_t dynar = task->tasks_before;
188   int length = xbt_dynar_length(dynar);
189   int i;
190   SD_dependency_t dependency;
191   for (i = 0; i < length; i++) {
192     xbt_dynar_get_cpy(dynar, i, &dependency);
193     INFO1(" %s", SD_task_get_name(dependency->src));
194   }
195
196   INFO1("The following tasks must be executed after %s:", SD_task_get_name(task));
197
198   dynar = task->tasks_after;
199   length = xbt_dynar_length(dynar);
200   for (i = 0; i < length; i++) {
201     xbt_dynar_get_cpy(dynar, i, &dependency);
202     INFO1(" %s", SD_task_get_name(dependency->dst));
203   }
204   INFO0("----------------------------");
205 }
206
207 /* Destroys a dependency between two tasks.
208  */
209 static void __SD_task_dependency_destroy(void *dependency) {
210   if (((SD_dependency_t) dependency)->name != NULL)
211     xbt_free(((SD_dependency_t) dependency)->name);
212   xbt_free(dependency);
213 }
214
215 /**
216  * \brief Adds a dependency between two tasks
217  *
218  * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
219  * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_READY.
220  *
221  * \param name the name of the new dependency (can be \c NULL)
222  * \param data the user data you want to associate with this dependency (can be \c NULL)
223  * \param src the task which must be executed first
224  * \param dst the task you want to make depend on \a src
225  * \see SD_task_dependency_remove()
226  */
227 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst) {
228   SD_CHECK_INIT_DONE();
229   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
230   xbt_assert1(src != dst, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
231   xbt_assert1(__SD_task_is_not_scheduled(src) || __SD_task_is_scheduled_or_ready(src),
232               "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(src));
233   xbt_assert1(__SD_task_is_not_scheduled(dst) || __SD_task_is_scheduled_or_ready(dst),
234               "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(dst));
235
236   xbt_dynar_t dynar = src->tasks_after;
237   int length = xbt_dynar_length(dynar);
238   int found = 0;
239   int i;
240   SD_dependency_t dependency;
241   for (i = 0; i < length && !found; i++) {
242     xbt_dynar_get_cpy(dynar, i, &dependency);
243     found = (dependency->dst == dst);
244   }
245   xbt_assert2(!found, "A dependency already exists between task '%s' and task '%s'",
246               SD_task_get_name(src), SD_task_get_name(dst));
247
248   dependency = xbt_new0(s_SD_dependency_t, 1);
249
250   if (name != NULL)
251     dependency->name = xbt_strdup(name);
252   dependency->data = data;
253   dependency->src = src;
254   dependency->dst = dst;
255
256   /* src must be executed before dst */
257   xbt_dynar_push(src->tasks_after, &dependency);
258   xbt_dynar_push(dst->tasks_before, &dependency);
259
260   /* if the task was ready, then dst->tasks_before is not empty anymore,
261      so we must go back to state SD_SCHEDULED */
262   if (__SD_task_is_ready(dst)) {
263     DEBUG1("SD_task_dependency_add: %s was ready and becomes scheduled!", SD_task_get_name(dst));
264     __SD_task_set_state(dst, SD_SCHEDULED);
265   }
266
267   /*  __SD_print_dependencies(src);
268       __SD_print_dependencies(dst); */
269 }
270
271 /**
272  * \brief Remove a dependency between two tasks
273  *
274  * \param src a task
275  * \param dst a task depending on \a src
276  * \see SD_task_dependency_add()
277  */
278 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
279   SD_CHECK_INIT_DONE();
280   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
281
282   /* remove the dependency from src->tasks_after */
283   xbt_dynar_t dynar = src->tasks_after;
284   int length = xbt_dynar_length(dynar);
285   int found = 0;
286   int i;
287   SD_dependency_t dependency;
288   for (i = 0; i < length && !found; i++) {
289     xbt_dynar_get_cpy(dynar, i, &dependency);
290     if (dependency->dst == dst) {
291       xbt_dynar_remove_at(dynar, i, NULL);
292       found = 1;
293     }
294   }
295   xbt_assert4(found, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
296               SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
297
298   /* remove the dependency from dst->tasks_before */
299   dynar = dst->tasks_before;
300   length = xbt_dynar_length(dynar);
301   found = 0;
302   
303   for (i = 0; i < length && !found; i++) {
304     xbt_dynar_get_cpy(dynar, i, &dependency);
305     if (dependency->src == src) {
306       xbt_dynar_remove_at(dynar, i, NULL);
307       __SD_task_dependency_destroy(dependency);
308       found = 1;
309     }
310   }
311   xbt_assert4(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
312               SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst)); /* should never happen... */
313
314   /* if the task was scheduled and dst->tasks_before is empty now, we can make it ready */
315   if (xbt_dynar_length(dst->tasks_before) == 0 && __SD_task_is_scheduled(dst))
316     __SD_task_set_state(dst, SD_READY);
317
318   /*  __SD_print_dependencies(src); 
319       __SD_print_dependencies(dst);*/
320 }
321
322 /**
323  * \brief Returns the user data associated with a dependency between two tasks
324  *
325  * \param src a task
326  * \param dst a task depending on \a src
327  * \return the user data associated with this dependency (can be \c NULL)
328  * \see SD_task_dependency_add()
329  */
330 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
331   SD_CHECK_INIT_DONE();
332   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
333
334   xbt_dynar_t dynar = src->tasks_after;
335   int length = xbt_dynar_length(dynar);
336   int found = 0;
337   int i;
338   SD_dependency_t dependency;
339   for (i = 0; i < length && !found; i++) {
340     xbt_dynar_get_cpy(dynar, i, &dependency);
341     found = (dependency->dst == dst);
342   }
343   xbt_assert2(found, "No dependency found between task '%s' and '%s'", SD_task_get_name(src), SD_task_get_name(dst));
344   return dependency->data;
345 }
346
347 /* temporary function for debugging */
348 static void __SD_print_watch_points(SD_task_t task) {
349   static const int state_masks[] = {SD_SCHEDULED, SD_RUNNING, SD_READY, SD_DONE, SD_FAILED};
350   static const char* state_names[] = {"scheduled", "running", "ready", "done", "failed"};
351
352   INFO2("Task '%s' watch points (%x): ", SD_task_get_name(task), task->watch_points);
353
354   int i;
355   for (i = 0; i < 5; i++) {
356     if (task->watch_points & state_masks[i])
357       INFO1("%s ", state_names[i]);
358   }
359 }
360
361 /**
362  * \brief Adds a watch point to a task
363  *
364  * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this task becomes the one given in argument. The
365  * watch point is then automatically removed.
366  * 
367  * \param task a task
368  * \param state the \ref e_SD_task_state_t "state" you want to watch (cannot be #SD_NOT_SCHEDULED)
369  * \see SD_task_unwatch()
370  */
371 void SD_task_watch(SD_task_t task, e_SD_task_state_t state) {
372   SD_CHECK_INIT_DONE();
373   xbt_assert0(task != NULL, "Invalid parameter");
374   xbt_assert0(state != SD_NOT_SCHEDULED, "Cannot add a watch point for state SD_NOT_SCHEDULED");
375
376   task->watch_points = task->watch_points | state;
377   /*  __SD_print_watch_points(task);*/
378 }
379
380 /**
381  * \brief Removes a watch point from a task
382  * 
383  * \param task a task
384  * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
385  * \see SD_task_watch()
386  */
387 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state) {
388   SD_CHECK_INIT_DONE();
389   xbt_assert0(task != NULL, "Invalid parameter");
390   xbt_assert0(state != SD_NOT_SCHEDULED, "Cannot have a watch point for state SD_NOT_SCHEDULED");
391   
392   task->watch_points = task->watch_points & ~state;
393   /*  __SD_print_watch_points(task);*/
394 }
395
396 /* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED or SD_READY.
397  */
398 static void __SD_task_destroy_scheduling_data(SD_task_t task) {
399   SD_CHECK_INIT_DONE();
400   xbt_assert1(__SD_task_is_scheduled_or_ready(task),
401               "Task '%s' must be SD_SCHEDULED or SD_READY", SD_task_get_name(task));
402   xbt_free(task->workstation_list);
403   xbt_free(task->computation_amount);
404   xbt_free(task->communication_amount);
405 }
406
407 /**
408  * \brief Schedules a task
409  *
410  * The task state must be #SD_NOT_SCHEDULED.
411  * Once scheduled, a task will be executed as soon as possible in SD_simulate(),
412  * i.e. when its dependencies are satisfied.
413  * 
414  * \param task the task you want to schedule
415  * \param workstation_nb number of workstations on which the task will be executed
416  * \param workstation_list the workstations on which the task will be executed
417  * \param computation_amount computation amount for each workstation
418  * \param communication_amount communication amount between each pair of workstations
419  * \param rate task execution speed rate
420  * \see SD_task_unschedule()
421  */
422 void SD_task_schedule(SD_task_t task, int workstation_nb,
423                      const SD_workstation_t *workstation_list, const double *computation_amount,
424                      const double *communication_amount, double rate) {
425   SD_CHECK_INIT_DONE();
426   xbt_assert0(task, "Invalid parameter");
427   xbt_assert1(__SD_task_is_not_scheduled(task), "Task '%s' has already been scheduled.", SD_task_get_name(task));
428   xbt_assert0(workstation_nb > 0, "workstation_nb must be positive");
429
430   task->workstation_nb = workstation_nb;
431   task->rate = rate;
432
433   task->computation_amount = xbt_new0(double, workstation_nb);
434   memcpy(task->computation_amount, computation_amount, sizeof(double) * workstation_nb);
435
436   int communication_nb = workstation_nb * workstation_nb;
437   task->communication_amount = xbt_new0(double, communication_nb);
438   memcpy(task->communication_amount, communication_amount, sizeof(double) * communication_nb);
439
440   /* we have to create a Surf workstation array instead of the SimDag workstation array */
441   task->workstation_list = xbt_new0(void*, workstation_nb);
442   int i;
443   for (i = 0; i < workstation_nb; i++) {
444     task->workstation_list[i] = workstation_list[i]->surf_workstation;
445   }
446
447   /* update the task state */
448   if (xbt_dynar_length(task->tasks_before) == 0)
449     __SD_task_set_state(task, SD_READY);
450   else
451     __SD_task_set_state(task, SD_SCHEDULED);
452 }
453
454 /**
455  * \brief Unschedules a task
456  *
457  * The task state must be #SD_SCHEDULED, #SD_READY, #SD_RUNNING or #SD_FAILED.
458  * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
459  * Call SD_task_schedule() to schedule it again.
460  *
461  * \param task the task you want to unschedule
462  * \see SD_task_schedule()
463  */
464 void SD_task_unschedule(SD_task_t task) {
465   SD_CHECK_INIT_DONE();
466   xbt_assert0(task != NULL, "Invalid parameter");
467   xbt_assert1(task->state_set == sd_global->scheduled_task_set ||
468               task->state_set == sd_global->ready_task_set ||
469               task->state_set == sd_global->running_task_set ||
470               task->state_set == sd_global->failed_task_set,
471               "Task %s: the state must be SD_SCHEDULED, SD_READY, SD_RUNNING or SD_FAILED",
472               SD_task_get_name(task));
473
474   if (__SD_task_is_scheduled_or_ready(task)) /* if the task is scheduled or ready */
475     __SD_task_destroy_scheduling_data(task);
476
477   if (__SD_task_is_running(task)) /* the task should become SD_FAILED */
478     surf_workstation_resource->common_public->action_cancel(task->surf_action);
479   else
480     __SD_task_set_state(task, SD_NOT_SCHEDULED);
481 }
482
483 /* Runs a task. This function is called by SD_simulate when a scheduled task can start
484  * (ie when its dependencies are satisfied).
485  */
486 surf_action_t __SD_task_run(SD_task_t task) {
487   SD_CHECK_INIT_DONE();
488   xbt_assert0(task != NULL, "Invalid parameter");
489   xbt_assert2(__SD_task_is_ready(task), "Task '%s' is not ready! Task state: %d",
490               SD_task_get_name(task), SD_task_get_state(task));
491
492   surf_action_t surf_action = surf_workstation_resource->extension_public->
493     execute_parallel_task(task->workstation_nb,
494                           task->workstation_list,
495                           task->computation_amount,
496                           task->communication_amount,
497                           task->amount,
498                           task->rate);
499
500   __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
501   __SD_task_set_state(task, SD_RUNNING);
502
503   return surf_action;
504 }
505 /* Remove all dependencies associated with a task. This function is called when the task is destroyed.
506  */
507 static void __SD_task_remove_dependencies(SD_task_t task) {
508   /* we must destroy the dependencies carefuly (with SD_dependency_remove)
509      because each one is stored twice */
510   SD_dependency_t dependency;
511   while (xbt_dynar_length(task->tasks_before) > 0) {
512     xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
513     SD_task_dependency_remove(dependency->src, dependency->dst);
514   }
515
516   while (xbt_dynar_length(task->tasks_after) > 0) {
517     xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
518     SD_task_dependency_remove(dependency->src, dependency->dst);
519   }
520 }
521
522 /**
523  * \brief Destroys a task.
524  *
525  * The user data (if any) should have been destroyed first.
526  *
527  * \param task the task you want to destroy
528  * \see SD_task_create()
529  */
530 void SD_task_destroy(SD_task_t task) {
531   SD_CHECK_INIT_DONE();
532   xbt_assert0(task != NULL, "Invalid parameter");
533
534   DEBUG1("Destroying task %s...", SD_task_get_name(task));
535
536   __SD_task_remove_dependencies(task);
537
538   /* if the task was scheduled or ready we have to free the scheduling parameters */
539   if (__SD_task_is_scheduled_or_ready(task))
540     __SD_task_destroy_scheduling_data(task);
541
542   if (task->name != NULL)
543     xbt_free(task->name);
544
545   xbt_dynar_free(&task->tasks_before);
546   xbt_dynar_free(&task->tasks_after);
547   xbt_free(task);
548
549   DEBUG0("Task destroyed.");
550 }