Logo AND Algorithmique Numérique Distribuée

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