Logo AND Algorithmique Numérique Distribuée

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