Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1578ff18817186dbabaacfb33d452174a61c8bea
[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 static void __SD_task_remove_dependencies(SD_task_t task);
7
8 /**
9  * \brief Creates a new task.
10  *
11  * \param name the name of the task (can be \c NULL)
12  * \param data the user data you want to associate with the task (can be \c NULL)
13  * \param amount the computing amount necessary to do this task
14  * \return the new task
15  * \see SD_task_destroy()
16  */
17 SD_task_t SD_task_create(const char *name, void *data, double amount) {
18   SD_CHECK_INIT_DONE();
19   xbt_assert0(amount > 0, "amount must be positive");
20
21   SD_task_t task = xbt_new0(s_SD_task_t, 1);
22
23   /* general information */
24   task->data = data; /* user data */
25   if (name != NULL)
26     task->name = xbt_strdup(name);
27   else
28     task->name = NULL;
29
30   task->state_set = sd_global->not_scheduled_task_set;
31   xbt_swag_insert(task,task->state_set);
32
33   task->amount = amount;
34   task->surf_action = NULL;
35   task->watch_points = 0;
36   task->state_changed = 0;
37
38   /* dependencies */
39   task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
40   task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
41
42   /* scheduling parameters */
43   task->workstation_nb = 0;
44   task->workstation_list = NULL;
45   task->computation_amount = NULL;
46   task->communication_amount = NULL;
47   task->rate = 0;
48
49   return task;
50 }
51
52 /**
53  * \brief Returns the user data of a task
54  *
55  * \param task a task
56  * \return the user data associated with this task (can be \c NULL)
57  * \see SD_task_set_data()
58  */
59 void* SD_task_get_data(SD_task_t task) {
60   SD_CHECK_INIT_DONE();
61   xbt_assert0(task != NULL, "Invalid parameter");
62   return task->data;
63 }
64
65 /**
66  * \brief Sets the user data of a task
67  *
68  * The new data can be \c NULL. The old data should have been freed first
69  * if it was not \c NULL.
70  *
71  * \param task a task
72  * \param data the new data you want to associate with this task
73  * \see SD_task_get_data()
74  */
75 void SD_task_set_data(SD_task_t task, void *data) {
76   SD_CHECK_INIT_DONE();
77   xbt_assert0(task != NULL, "Invalid parameter");
78   task->data = data;
79 }
80
81 /**
82  * \brief Returns the state of a task
83  *
84  * \param task a task
85  * \return the current \ref e_SD_task_state_t "state" of this task:
86  * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_READY, #SD_RUNNING, #SD_DONE or #SD_FAILED
87  * \see e_SD_task_state_t
88  */
89 e_SD_task_state_t SD_task_get_state(SD_task_t task) {
90   SD_CHECK_INIT_DONE();
91   xbt_assert0(task != NULL, "Invalid parameter");
92
93   if (task->state_set == sd_global->scheduled_task_set)
94     return SD_SCHEDULED;
95   if (task->state_set == sd_global->done_task_set)
96     return SD_DONE;
97   if (task->state_set == sd_global->running_task_set)
98     return SD_RUNNING;
99   if (task->state_set == sd_global->ready_task_set)
100     return SD_READY;
101   if (task->state_set == sd_global->not_scheduled_task_set)
102     return SD_NOT_SCHEDULED;
103   return SD_FAILED;
104 }
105
106 /* Changes the state of a task. Updates the swags and the flag sd_global->watch_point_reached.
107  */
108 void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state) {
109   xbt_swag_remove(task, task->state_set);
110   switch (new_state) {
111   case SD_NOT_SCHEDULED:
112     task->state_set = sd_global->not_scheduled_task_set;
113     break;
114   case SD_SCHEDULED:
115     task->state_set = sd_global->scheduled_task_set;
116     break;
117   case SD_READY:
118     task->state_set = sd_global->ready_task_set;
119     break;
120   case SD_RUNNING:
121     task->state_set = sd_global->running_task_set;
122     break;
123   case SD_DONE:
124     task->state_set = sd_global->done_task_set;
125     break;
126   case SD_FAILED:
127     task->state_set = sd_global->failed_task_set;
128     break;
129   default:
130     xbt_assert0(0, "Invalid state");
131   }
132   xbt_swag_insert(task, task->state_set);
133
134   if (task->watch_points & new_state) {
135     printf("Watch point reached with task '%s'!\n", SD_task_get_name(task));
136     sd_global->watch_point_reached = 1;
137     SD_task_unwatch(task, new_state); /* remove the watch point */
138   }
139 }
140
141 /**
142  * \brief Returns the name of a task
143  *
144  * \param task a task
145  * \return the name of this task (can be \c NULL)
146  */
147 const char* SD_task_get_name(SD_task_t task) {
148   SD_CHECK_INIT_DONE();
149   xbt_assert0(task != NULL, "Invalid parameter");
150   return task->name;
151 }
152
153 /**
154  * \brief Returns the computing amount of a task
155  *
156  * \param task a task
157  * \return the total computing amount of this task
158  * \see SD_task_get_remaining_amount()
159  */
160 double SD_task_get_amount(SD_task_t task) {
161   SD_CHECK_INIT_DONE();
162   xbt_assert0(task != NULL, "Invalid parameter");
163   return task->amount;
164 }
165
166 /**
167  * \brief Returns the remaining computing amount of a task
168  *
169  * \param task a task
170  * \return the remaining computing amount of this task
171  * \see SD_task_get_amount()
172  */
173 double SD_task_get_remaining_amount(SD_task_t task) {
174   SD_CHECK_INIT_DONE();
175   xbt_assert0(task != NULL, "Invalid parameter");
176   
177   if (task->surf_action)
178     return task->amount;
179   else
180     return task->surf_action->remains;
181 }
182
183 /* temporary function for debbuging */
184 static void __SD_print_dependencies(SD_task_t task) {
185   printf("The following tasks must be executed before %s:", SD_task_get_name(task));
186   xbt_dynar_t dynar = task->tasks_before;
187   int length = xbt_dynar_length(dynar);
188   int i;
189   SD_dependency_t dependency;
190   for (i = 0; i < length; i++) {
191     xbt_dynar_get_cpy(dynar, i, &dependency);
192     printf(" %s", SD_task_get_name(dependency->src));
193   }
194
195   printf("\nThe following tasks must be executed after %s:", SD_task_get_name(task));
196
197   dynar = task->tasks_after;
198   length = xbt_dynar_length(dynar);
199   for (i = 0; i < length; i++) {
200     xbt_dynar_get_cpy(dynar, i, &dependency);
201     printf(" %s", SD_task_get_name(dependency->dst));
202   }
203   printf("\n----------------------------\n");
204 }
205
206 /* Destroys a dependency between two tasks.
207  */
208 static void __SD_task_dependency_destroy(void *dependency) {
209   if (((SD_dependency_t) dependency)->name != NULL)
210     xbt_free(((SD_dependency_t) dependency)->name);
211   xbt_free(dependency);
212 }
213
214 /**
215  * \brief Adds a dependency between two tasks
216  *
217  * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
218  * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_READY.
219  *
220  * \param name the name of the new dependency (can be \c NULL)
221  * \param data the user data you want to associate with this dependency (can be \c NULL)
222  * \param src the task which must be executed first
223  * \param dst the task you want to make depend on \a src
224  * \see SD_task_dependency_remove()
225  */
226 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst) {
227   SD_CHECK_INIT_DONE();
228   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
229   xbt_assert1(src != dst, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
230   xbt_assert1(__SD_task_is_not_scheduled(src) || __SD_task_is_scheduled_or_ready(src),
231               "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(src));
232   xbt_assert1(__SD_task_is_not_scheduled(dst) || __SD_task_is_scheduled_or_ready(dst),
233               "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(dst));
234
235   xbt_dynar_t dynar = src->tasks_after;
236   int length = xbt_dynar_length(dynar);
237   int found = 0;
238   int i;
239   SD_dependency_t dependency;
240   for (i = 0; i < length && !found; i++) {
241     xbt_dynar_get_cpy(dynar, i, &dependency);
242     found = (dependency->dst == dst);
243   }
244   xbt_assert2(!found, "A dependency already exists between task '%s' and task '%s'",
245               SD_task_get_name(src), SD_task_get_name(dst));
246
247   dependency = xbt_new0(s_SD_dependency_t, 1);
248
249   if (name != NULL)
250     dependency->name = xbt_strdup(name);
251   dependency->data = data;
252   dependency->src = src;
253   dependency->dst = dst;
254
255   /* src must be executed before dst */
256   xbt_dynar_push(src->tasks_after, &dependency);
257   xbt_dynar_push(dst->tasks_before, &dependency);
258
259   /* if the task was ready, then dst->tasks_before is not empty anymore,
260      so we must go back to state SD_SCHEDULED */
261   if (__SD_task_is_ready(dst)) {
262     printf("SD_task_dependency_add: %s was ready and becomes scheduled!\n", SD_task_get_name(dst));
263     __SD_task_set_state(dst, SD_SCHEDULED);
264   }
265
266   /*  __SD_print_dependencies(src);
267       __SD_print_dependencies(dst); */
268 }
269
270 /**
271  * \brief Remove a dependency between two tasks
272  *
273  * \param src a task
274  * \param dst a task depending on \a src
275  * \see SD_task_dependency_add()
276  */
277 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
278   SD_CHECK_INIT_DONE();
279   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
280
281   /* remove the dependency from src->tasks_after */
282   xbt_dynar_t dynar = src->tasks_after;
283   int length = xbt_dynar_length(dynar);
284   int found = 0;
285   int i;
286   SD_dependency_t dependency;
287   for (i = 0; i < length && !found; i++) {
288     xbt_dynar_get_cpy(dynar, i, &dependency);
289     if (dependency->dst == dst) {
290       xbt_dynar_remove_at(dynar, i, NULL);
291       found = 1;
292     }
293   }
294   xbt_assert4(found, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
295               SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
296
297   /* remove the dependency from dst->tasks_before */
298   dynar = dst->tasks_before;
299   length = xbt_dynar_length(dynar);
300   found = 0;
301   
302   for (i = 0; i < length && !found; i++) {
303     xbt_dynar_get_cpy(dynar, i, &dependency);
304     if (dependency->src == src) {
305       xbt_dynar_remove_at(dynar, i, NULL);
306       __SD_task_dependency_destroy(dependency);
307       found = 1;
308     }
309   }
310   xbt_assert4(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
311               SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst)); /* should never happen... */
312
313   /* if the task was scheduled and dst->tasks_before is empty now, we can make it ready */
314   if (xbt_dynar_length(dst->tasks_before) == 0 && __SD_task_is_scheduled(dst))
315     __SD_task_set_state(dst, SD_READY);
316
317   /*  __SD_print_dependencies(src); 
318       __SD_print_dependencies(dst);*/
319 }
320
321 /**
322  * \brief Returns the user data associated with a dependency between two tasks
323  *
324  * \param src a task
325  * \param dst a task depending on \a src
326  * \return the user data associated with this dependency (can be \c NULL)
327  * \see SD_task_dependency_add()
328  */
329 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
330   SD_CHECK_INIT_DONE();
331   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
332
333   xbt_dynar_t dynar = src->tasks_after;
334   int length = xbt_dynar_length(dynar);
335   int found = 0;
336   int i;
337   SD_dependency_t dependency;
338   for (i = 0; i < length && !found; i++) {
339     xbt_dynar_get_cpy(dynar, i, &dependency);
340     found = (dependency->dst == dst);
341   }
342   xbt_assert2(found, "No dependency found between task '%s' and '%s'", SD_task_get_name(src), SD_task_get_name(dst));
343   return dependency->data;
344 }
345
346 /* temporary function for debugging */
347 static void __SD_print_watch_points(SD_task_t task) {
348   static const int state_masks[] = {SD_SCHEDULED, SD_RUNNING, SD_READY, SD_DONE, SD_FAILED};
349   static const char* state_names[] = {"scheduled", "running", "ready", "done", "failed"};
350
351   printf("Task '%s' watch points (%x): ", SD_task_get_name(task), task->watch_points);
352
353   int i;
354   for (i = 0; i < 5; i++) {
355     if (task->watch_points & state_masks[i])
356       printf("%s ", state_names[i]);
357   }
358   printf("\n");
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, double *computation_amount,
424                      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   /*printf("Destroying task %s...\n", 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   /*printf("Task destroyed.\n");*/
550 }