Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanups: there is no random in the platforms since a while
[simgrid.git] / src / simdag / sd_task.cpp
1 /* Copyright (c) 2006-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/surf/surf_interface.hpp"
8 #include "src/surf/host_interface.hpp"
9 #include "src/simdag/simdag_private.h"
10 #include "simgrid/simdag.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/dynar.h"
13 #include "src/instr/instr_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task, sd, "Logging specific to SimDag (task)");
16
17 static void __SD_task_remove_dependencies(SD_task_t task);
18 static void __SD_task_destroy_scheduling_data(SD_task_t task);
19
20 void* SD_task_new_f(void)
21 {
22   SD_task_t task = xbt_new0(s_SD_task_t, 1);
23   task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
24   task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
25
26   return task;
27 }
28
29 void SD_task_recycle_f(void *t)
30 {
31   SD_task_t task = (SD_task_t) t;
32
33   /* Reset the content */
34   task->kind = SD_TASK_NOT_TYPED;
35   task->state= SD_NOT_SCHEDULED;
36   xbt_dynar_push(sd_global->initial_task_set,&task);
37
38
39   task->marked = 0;
40
41   task->start_time = -1.0;
42   task->finish_time = -1.0;
43   task->surf_action = NULL;
44   task->watch_points = 0;
45
46   /* dependencies */
47   xbt_dynar_reset(task->tasks_before);
48   xbt_dynar_reset(task->tasks_after);
49   task->unsatisfied_dependencies = 0;
50   task->is_not_ready = 0;
51
52   /* scheduling parameters */
53   task->host_count = 0;
54   task->host_list = NULL;
55   task->flops_amount = NULL;
56   task->bytes_amount = NULL;
57   task->rate = -1;
58 }
59
60 void SD_task_free_f(void *t)
61 {
62   SD_task_t task = (SD_task_t)t;
63
64   xbt_dynar_free(&task->tasks_before);
65   xbt_dynar_free(&task->tasks_after);
66   xbt_free(task);
67 }
68
69 /**
70  * \brief Creates a new task.
71  *
72  * \param name the name of the task (can be \c NULL)
73  * \param data the user data you want to associate with the task (can be \c NULL)
74  * \param amount amount of the task
75  * \return the new task
76  * \see SD_task_destroy()
77  */
78 SD_task_t SD_task_create(const char *name, void *data, double amount)
79 {
80   SD_task_t task = (SD_task_t)xbt_mallocator_get(sd_global->task_mallocator);
81
82   /* general information */
83   task->data = data;            /* user data */
84   task->name = xbt_strdup(name);
85   task->amount = amount;
86   task->remains = amount;
87
88   return task;
89 }
90
91 static XBT_INLINE SD_task_t SD_task_create_sized(const char *name, void *data, double amount, int ws_count)
92 {
93   SD_task_t task = SD_task_create(name, data, amount);
94   task->bytes_amount = xbt_new0(double, ws_count * ws_count);
95   task->flops_amount = xbt_new0(double, ws_count);
96   task->host_count = ws_count;
97   task->host_list = xbt_new0(sg_host_t, ws_count);
98   return task;
99 }
100
101 /** @brief create a end-to-end communication task that can then be auto-scheduled
102  *
103  * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
104  * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
105  * mandatory power.
106  *
107  * A end-to-end communication must be scheduled on 2 hosts, and the amount specified at creation is sent from hosts[0]
108  * to hosts[1].
109  */
110 SD_task_t SD_task_create_comm_e2e(const char *name, void *data, double amount)
111 {
112   SD_task_t res = SD_task_create_sized(name, data, amount, 2);
113   res->bytes_amount[2] = amount;
114   res->kind = SD_TASK_COMM_E2E;
115
116   return res;
117 }
118
119 /** @brief create a sequential computation task that can then be auto-scheduled
120  *
121  * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
122  * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
123  * mandatory power.
124  *
125  * A sequential computation must be scheduled on 1 host, and the amount specified at creation to be run on hosts[0].
126  *
127  * \param name the name of the task (can be \c NULL)
128  * \param data the user data you want to associate with the task (can be \c NULL)
129  * \param flops_amount amount of compute work to be done by the task
130  * \return the new SD_TASK_COMP_SEQ typed task
131  */
132 SD_task_t SD_task_create_comp_seq(const char *name, void *data, double flops_amount)
133 {
134   SD_task_t res = SD_task_create_sized(name, data, flops_amount, 1);
135   res->flops_amount[0] = flops_amount;
136   res->kind = SD_TASK_COMP_SEQ;
137
138   return res;
139 }
140
141 /** @brief create a parallel computation task that can then be auto-scheduled
142  *
143  * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
144  * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
145  * mandatory power.
146  *
147  * A parallel computation can be scheduled on any number of host.
148  * The underlying speedup model is Amdahl's law.
149  * To be auto-scheduled, \see SD_task_distribute_comp_amdahl has to be called first.
150  * \param name the name of the task (can be \c NULL)
151  * \param data the user data you want to associate with the task (can be \c NULL)
152  * \param flops_amount amount of compute work to be done by the task
153  * \param alpha purely serial fraction of the work to be done (in [0.;1.[)
154  * \return the new task
155  */
156 SD_task_t SD_task_create_comp_par_amdahl(const char *name, void *data, double flops_amount, double alpha)
157 {
158   xbt_assert(alpha < 1. && alpha >= 0., "Invalid parameter: alpha must be in [0.;1.[");
159   
160   SD_task_t res = SD_task_create(name, data, flops_amount);
161   res->alpha = alpha;
162   res->kind = SD_TASK_COMP_PAR_AMDAHL;
163
164   return res;
165 }
166
167 /** @brief create a complex data redistribution task that can then be  auto-scheduled
168  *
169  * Auto-scheduling mean that the task can be used with SD_task_schedulev(). 
170  * This allows to specify the task costs at creation, and decouple them from the scheduling process where you just
171  * specify which resource should communicate.
172  *
173  * A data redistribution can be scheduled on any number of host.
174  * The assumed distribution is a 1D block distribution. Each host owns the same share of the \see amount.
175  * To be auto-scheduled, \see SD_task_distribute_comm_mxn_1d_block has to be  called first.
176  * \param name the name of the task (can be \c NULL)
177  * \param data the user data you want to associate with the task (can be \c NULL)
178  * \param amount amount of data to redistribute by the task
179  * \return the new task
180  */
181 SD_task_t SD_task_create_comm_par_mxn_1d_block(const char *name, void *data, double amount)
182 {
183   SD_task_t res = SD_task_create(name, data, amount);
184   res->host_list=NULL;
185   res->kind = SD_TASK_COMM_PAR_MXN_1D_BLOCK;
186
187   return res;
188 }
189
190 /**
191  * \brief Destroys a task.
192  *
193  * The user data (if any) should have been destroyed first.
194  *
195  * \param task the task you want to destroy
196  * \see SD_task_create()
197  */
198 void SD_task_destroy(SD_task_t task)
199 {
200   int idx;
201   XBT_DEBUG("Destroying task %s...", SD_task_get_name(task));
202
203   __SD_task_remove_dependencies(task);
204
205   if (task->state == SD_SCHEDULED || task->state == SD_RUNNABLE)
206     __SD_task_destroy_scheduling_data(task);
207
208   idx = xbt_dynar_search_or_negative(sd_global->return_set, &task);
209   if (idx >=0) {
210     xbt_dynar_remove_at(sd_global->return_set, idx, NULL);
211   }
212
213   xbt_free(task->name);
214
215   if (task->surf_action != NULL)
216     task->surf_action->unref();
217
218   xbt_free(task->host_list);
219   xbt_free(task->bytes_amount);
220   xbt_free(task->flops_amount);
221
222   xbt_mallocator_release(sd_global->task_mallocator,task);
223
224   XBT_DEBUG("Task destroyed.");
225 }
226
227 /**
228  * \brief Returns the user data of a task
229  *
230  * \param task a task
231  * \return the user data associated with this task (can be \c NULL)
232  * \see SD_task_set_data()
233  */
234 void *SD_task_get_data(SD_task_t task)
235 {
236   return task->data;
237 }
238
239 /**
240  * \brief Sets the user data of a task
241  *
242  * The new data can be \c NULL. The old data should have been freed first
243  * if it was not \c NULL.
244  *
245  * \param task a task
246  * \param data the new data you want to associate with this task
247  * \see SD_task_get_data()
248  */
249 void SD_task_set_data(SD_task_t task, void *data)
250 {
251   task->data = data;
252 }
253
254 /**
255  * \brief Sets the rate of a task
256  *
257  * This will change the network bandwidth a task can use. This rate  cannot be dynamically changed. Once the task has
258  * started, this call is ineffective. This rate depends on both the nominal bandwidth on the route onto which the task
259  * is scheduled (\see SD_task_get_current_bandwidth) and the amount of data to transfer.
260  *
261  * To divide the nominal bandwidth by 2, the rate then has to be :
262  *    rate = bandwidth/(2*amount)
263  *
264  * \param task a \see SD_TASK_COMM_E2E task (end-to-end communication)
265  * \param rate the new rate you want to associate with this task.
266  */
267 void SD_task_set_rate(SD_task_t task, double rate)
268 {
269   xbt_assert(task->kind == SD_TASK_COMM_E2E, "The rate can be modified for end-to-end communications only.");
270   if(task->start_time<0) {
271     task->rate = rate;
272   } else {
273     XBT_WARN("Task %p has started. Changing rate is ineffective.", task);
274   }
275 }
276
277 /**
278  * \brief Returns the state of a task
279  *
280  * \param task a task
281  * \return the current \ref e_SD_task_state_t "state" of this task:
282  * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING, #SD_DONE or #SD_FAILED
283  * \see e_SD_task_state_t
284  */
285 e_SD_task_state_t SD_task_get_state(SD_task_t task)
286 {
287   return task->state;
288 }
289
290 /* Changes the state of a task. Updates the sd_global->watch_point_reached flag.
291  */
292 void SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state)
293 {
294   int idx;
295   switch (new_state) {
296   case SD_NOT_SCHEDULED:
297   case SD_SCHEDULABLE:
298     if (SD_task_get_state(task) == SD_FAILED){
299         xbt_dynar_remove_at(sd_global->completed_task_set,
300             xbt_dynar_search(sd_global->completed_task_set, &task), NULL);
301         xbt_dynar_push(sd_global->initial_task_set,&task);
302     }
303     break;
304   case SD_SCHEDULED:
305     if (SD_task_get_state(task) == SD_RUNNABLE){
306       xbt_dynar_remove_at(sd_global->executable_task_set,
307           xbt_dynar_search(sd_global->executable_task_set, &task), NULL);
308       xbt_dynar_push(sd_global->initial_task_set,&task);
309     }
310     break;
311   case SD_RUNNABLE:
312     idx = xbt_dynar_search_or_negative(sd_global->initial_task_set, &task);
313     if (idx >= 0) {
314       xbt_dynar_remove_at(sd_global->initial_task_set, idx, NULL);
315       xbt_dynar_push(sd_global->executable_task_set,&task);
316     }
317     break;
318   case SD_RUNNING:
319       xbt_dynar_remove_at(sd_global->executable_task_set,
320          xbt_dynar_search(sd_global->executable_task_set, &task), NULL);
321     break;
322   case SD_DONE:
323     xbt_dynar_push(sd_global->completed_task_set,&task);
324     task->finish_time = task->surf_action->getFinishTime();
325     task->remains = 0;
326 #ifdef HAVE_JEDULE
327     jedule_log_sd_event(task);
328 #endif
329     break;
330   case SD_FAILED:
331     xbt_dynar_push(sd_global->completed_task_set,&task);
332     break;
333   default:
334     xbt_die( "Invalid state");
335   }
336
337   task->state = new_state;
338
339   if (task->watch_points & new_state) {
340     XBT_VERB("Watch point reached with task '%s'!", SD_task_get_name(task));
341     sd_global->watch_point_reached = 1;
342     SD_task_unwatch(task, new_state);   /* remove the watch point */
343   }
344 }
345
346 /**
347  * \brief Returns the name of a task
348  *
349  * \param task a task
350  * \return the name of this task (can be \c NULL)
351  */
352 const char *SD_task_get_name(SD_task_t task)
353 {
354   return task->name;
355 }
356
357 /** @brief Allows to change the name of a task */
358 void SD_task_set_name(SD_task_t task, const char *name)
359 {
360   xbt_free(task->name);
361   task->name = xbt_strdup(name);
362 }
363
364 /** @brief Returns the dynar of the parents of a task
365  *
366  * \param task a task
367  * \return a newly allocated dynar comprising the parents of this task
368  */
369
370 xbt_dynar_t SD_task_get_parents(SD_task_t task)
371 {
372   unsigned int i;
373   xbt_dynar_t parents;
374   SD_dependency_t dep;
375
376   parents = xbt_dynar_new(sizeof(SD_task_t), NULL);
377   xbt_dynar_foreach(task->tasks_before, i, dep) {
378     xbt_dynar_push(parents, &(dep->src));
379   }
380   return parents;
381 }
382
383 /** @brief Returns the dynar of the parents of a task
384  *
385  * \param task a task
386  * \return a newly allocated dynar comprising the parents of this task
387  */
388 xbt_dynar_t SD_task_get_children(SD_task_t task)
389 {
390   unsigned int i;
391   xbt_dynar_t children;
392   SD_dependency_t dep;
393
394   children = xbt_dynar_new(sizeof(SD_task_t), NULL);
395   xbt_dynar_foreach(task->tasks_after, i, dep) {
396     xbt_dynar_push(children, &(dep->dst));
397   }
398   return children;
399 }
400
401 /**
402  * \brief Returns the amount of workstations involved in a task
403  *
404  * Only call this on already scheduled tasks!
405  * \param task a task
406  */
407 int SD_task_get_workstation_count(SD_task_t task)
408 {
409   return task->host_count;
410 }
411
412 /**
413  * \brief Returns the list of workstations involved in a task
414  *
415  * Only call this on already scheduled tasks!
416  * \param task a task
417  */
418 sg_host_t *SD_task_get_workstation_list(SD_task_t task)
419 {
420   return task->host_list;
421 }
422
423 /**
424  * \brief Returns the total amount of work contained in a task
425  *
426  * \param task a task
427  * \return the total amount of work (computation or data transfer) for this task
428  * \see SD_task_get_remaining_amount()
429  */
430 double SD_task_get_amount(SD_task_t task)
431 {
432   return task->amount;
433 }
434
435 /** @brief Sets the total amount of work of a task
436  * For sequential typed tasks (COMP_SEQ and COMM_E2E), it also sets the appropriate values in the flops_amount and
437  * bytes_amount arrays respectively. Nothing more than modifying task->amount is done for parallel  typed tasks
438  * (COMP_PAR_AMDAHL and COMM_PAR_MXN_1D_BLOCK) as the distribution of the amount of work is done at scheduling time.
439  *
440  * \param task a task
441  * \param amount the new amount of work to execute
442  */
443 void SD_task_set_amount(SD_task_t task, double amount)
444 {
445   task->amount = amount;
446   if (task->kind == SD_TASK_COMP_SEQ)
447     task->flops_amount[0] = amount;
448   if (task->kind == SD_TASK_COMM_E2E)
449     task->bytes_amount[2] = amount;
450 }
451
452 /**
453  * \brief Returns the alpha parameter of a SD_TASK_COMP_PAR_AMDAHL task
454  *
455  * \param task a parallel task assuming Amdahl's law as speedup model
456  * \return the alpha parameter (serial part of a task in percent) for this task
457  */
458 double SD_task_get_alpha(SD_task_t task)
459 {
460   xbt_assert(SD_task_get_kind(task) == SD_TASK_COMP_PAR_AMDAHL, "Alpha parameter is not defined for this kind of task");
461   return task->alpha;
462 }
463
464
465 /**
466  * \brief Returns the remaining amount work to do till the completion of a task
467  *
468  * \param task a task
469  * \return the remaining amount of work (computation or data transfer) of this task
470  * \see SD_task_get_amount()
471  */
472 double SD_task_get_remaining_amount(SD_task_t task)
473 {
474   if (task->surf_action)
475     return surf_action_get_remains(task->surf_action);
476   else
477     return task->remains;
478 }
479
480 e_SD_task_kind_t SD_task_get_kind(SD_task_t task)
481 {
482   return task->kind;
483 }
484
485 /** @brief Displays debugging informations about a task */
486 void SD_task_dump(SD_task_t task)
487 {
488   unsigned int counter;
489   SD_dependency_t dependency;
490   char *statename;
491
492   XBT_INFO("Displaying task %s", SD_task_get_name(task));
493   statename = bprintf("%s%s%s%s%s%s%s",
494                       (task->state == SD_NOT_SCHEDULED ? " not scheduled" : ""),
495                       (task->state == SD_SCHEDULABLE ? " schedulable" : ""),
496                       (task->state == SD_SCHEDULED ? " scheduled" : ""),
497                       (task->state == SD_RUNNABLE ? " runnable" : " not runnable"),
498                       (task->state == SD_RUNNING ? " running" : ""),
499                       (task->state == SD_DONE ? " done" : ""),
500                       (task->state == SD_FAILED ? " failed" : ""));
501   XBT_INFO("  - state:%s", statename);
502   free(statename);
503
504   if (task->kind != 0) {
505     switch (task->kind) {
506     case SD_TASK_COMM_E2E:
507       XBT_INFO("  - kind: end-to-end communication");
508       break;
509     case SD_TASK_COMP_SEQ:
510       XBT_INFO("  - kind: sequential computation");
511       break;
512     case SD_TASK_COMP_PAR_AMDAHL:
513       XBT_INFO("  - kind: parallel computation following Amdahl's law");
514       break;
515     case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
516       XBT_INFO("  - kind: MxN data redistribution assuming 1D block distribution");
517       break;
518     default:
519       XBT_INFO("  - (unknown kind %d)", task->kind);
520     }
521   }
522
523   if (task->category)
524     XBT_INFO("  - tracing category: %s", task->category);
525
526   XBT_INFO("  - amount: %.0f", SD_task_get_amount(task));
527   if (task->kind == SD_TASK_COMP_PAR_AMDAHL)
528     XBT_INFO("  - alpha: %.2f", task->alpha);
529   XBT_INFO("  - Dependencies to satisfy: %d", task->unsatisfied_dependencies);
530   if (!xbt_dynar_is_empty(task->tasks_before)) {
531     XBT_INFO("  - pre-dependencies:");
532     xbt_dynar_foreach(task->tasks_before, counter, dependency) {
533       XBT_INFO("    %s", SD_task_get_name(dependency->src));
534     }
535   }
536   if (!xbt_dynar_is_empty(task->tasks_after)) {
537     XBT_INFO("  - post-dependencies:");
538     xbt_dynar_foreach(task->tasks_after, counter, dependency) {
539       XBT_INFO("    %s", SD_task_get_name(dependency->dst));
540     }
541   }
542 }
543
544 /** @brief Dumps the task in dotty formalism into the FILE* passed as second argument */
545 void SD_task_dotty(SD_task_t task, void *out)
546 {
547   unsigned int counter;
548   SD_dependency_t dependency;
549   FILE *fout = (FILE*)out;
550   fprintf(fout, "  T%p [label=\"%.20s\"", task, task->name);
551   switch (task->kind) {
552   case SD_TASK_COMM_E2E:
553   case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
554     fprintf(fout, ", shape=box");
555     break;
556   case SD_TASK_COMP_SEQ:
557   case SD_TASK_COMP_PAR_AMDAHL:
558     fprintf(fout, ", shape=circle");
559     break;
560   default:
561     xbt_die("Unknown task type!");
562   }
563   fprintf(fout, "];\n");
564   xbt_dynar_foreach(task->tasks_before, counter, dependency) {
565     fprintf(fout, " T%p -> T%p;\n", dependency->src, dependency->dst);
566   }
567 }
568
569 /* Destroys a dependency between two tasks.
570  */
571 static void __SD_task_dependency_destroy(void *dependency)
572 {
573   xbt_free(((SD_dependency_t)dependency)->name);
574   xbt_free(dependency);
575 }
576
577 /**
578  * \brief Adds a dependency between two tasks
579  *
580  * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
581  * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_RUNNABLE.
582  *
583  * \param name the name of the new dependency (can be \c NULL)
584  * \param data the user data you want to associate with this dependency (can be \c NULL)
585  * \param src the task which must be executed first
586  * \param dst the task you want to make depend on \a src
587  * \see SD_task_dependency_remove()
588  */
589 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst)
590 {
591   xbt_dynar_t dynar;
592   unsigned long length;
593   int found = 0;
594   unsigned long i;
595   SD_dependency_t dependency;
596   e_SD_task_state_t state;
597
598   dynar = src->tasks_after;
599   length = xbt_dynar_length(dynar);
600
601   if (src == dst)
602     THROWF(arg_error, 0, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
603
604   state = SD_task_get_state(src);
605   if (state != SD_NOT_SCHEDULED && state != SD_SCHEDULABLE && state != SD_RUNNING && state != SD_SCHEDULED &&
606        state != SD_RUNNABLE)
607     THROWF(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, SD_RUNNABLE, or SD_RUNNING",
608            SD_task_get_name(src));
609
610   state = SD_task_get_state(dst);
611   if (state != SD_NOT_SCHEDULED && state != SD_SCHEDULABLE && state != SD_SCHEDULED && state != SD_RUNNABLE)
612     THROWF(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, or SD_RUNNABLE",
613            SD_task_get_name(dst));
614
615   XBT_DEBUG("SD_task_dependency_add: src = %s, dst = %s", SD_task_get_name(src), SD_task_get_name(dst));
616   for (i = 0; i < length && !found; i++) {
617     xbt_dynar_get_cpy(dynar, i, &dependency);
618     found = (dependency->dst == dst);
619     XBT_DEBUG("Dependency %lu: dependency->dst = %s", i, SD_task_get_name(dependency->dst));
620   }
621
622   if (found)
623     THROWF(arg_error, 0, "A dependency already exists between task '%s' and task '%s'",
624            SD_task_get_name(src), SD_task_get_name(dst));
625
626   dependency = xbt_new(s_SD_dependency_t, 1);
627
628   dependency->name = xbt_strdup(name);  /* xbt_strdup is cleaver enough to deal with NULL args itself */
629   dependency->data = data;
630   dependency->src = src;
631   dependency->dst = dst;
632
633   /* src must be executed before dst */
634   xbt_dynar_push(src->tasks_after, &dependency);
635   xbt_dynar_push(dst->tasks_before, &dependency);
636
637   dst->unsatisfied_dependencies++;
638   dst->is_not_ready++;
639
640   /* if the task was runnable, then dst->tasks_before is not empty anymore, so we must go back to state SD_SCHEDULED */
641   if (SD_task_get_state(dst) == SD_RUNNABLE) {
642     XBT_DEBUG("SD_task_dependency_add: %s was runnable and becomes scheduled!", SD_task_get_name(dst));
643     SD_task_set_state(dst, SD_SCHEDULED);
644   }
645 }
646 /**
647  * \brief Returns the name given as input when dependency has been created..
648  *
649  * \param src a task
650  * \param dst a task depending on \a src
651  *
652  */
653 const char *SD_task_dependency_get_name(SD_task_t src, SD_task_t dst){
654   unsigned int i;
655   SD_dependency_t dependency;
656
657   xbt_dynar_foreach(src->tasks_after, i, dependency){
658     if (dependency->dst == dst)
659       return dependency->name;
660   }
661   return NULL;
662 }
663
664 /**
665  * \brief Indicates whether there is a dependency between two tasks.
666  *
667  * \param src a task
668  * \param dst a task depending on \a src
669  *
670  * If src is NULL, checks whether dst has any pre-dependency.
671  * If dst is NULL, checks whether src has any post-dependency.
672  */
673 int SD_task_dependency_exists(SD_task_t src, SD_task_t dst)
674 {
675   unsigned int counter;
676   SD_dependency_t dependency;
677
678   xbt_assert(src != NULL || dst != NULL, "Invalid parameter: both src and dst are NULL");
679
680   if (src) {
681     if (dst) {
682       xbt_dynar_foreach(src->tasks_after, counter, dependency) {
683         if (dependency->dst == dst)
684           return 1;
685       }
686     } else {
687       return xbt_dynar_length(src->tasks_after);
688     }
689   } else {
690     return xbt_dynar_length(dst->tasks_before);
691   }
692   return 0;
693 }
694
695 /**
696  * \brief Remove a dependency between two tasks
697  *
698  * \param src a task
699  * \param dst a task depending on \a src
700  * \see SD_task_dependency_add()
701  */
702 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst)
703 {
704   xbt_dynar_t dynar;
705   unsigned long length;
706   int found = 0;
707   unsigned long i;
708   SD_dependency_t dependency;
709
710   /* remove the dependency from src->tasks_after */
711   dynar = src->tasks_after;
712   length = xbt_dynar_length(dynar);
713
714   for (i = 0; i < length && !found; i++) {
715     xbt_dynar_get_cpy(dynar, i, &dependency);
716     if (dependency->dst == dst) {
717       xbt_dynar_remove_at(dynar, i, NULL);
718       found = 1;
719     }
720   }
721   if (!found)
722     THROWF(arg_error, 0, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
723            SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
724
725   /* remove the dependency from dst->tasks_before */
726   dynar = dst->tasks_before;
727   length = xbt_dynar_length(dynar);
728   found = 0;
729
730   for (i = 0; i < length && !found; i++) {
731     xbt_dynar_get_cpy(dynar, i, &dependency);
732     if (dependency->src == src) {
733       xbt_dynar_remove_at(dynar, i, NULL);
734       __SD_task_dependency_destroy(dependency);
735       dst->unsatisfied_dependencies--;
736       dst->is_not_ready--;
737       found = 1;
738     }
739   }
740   /* should never happen... */
741   xbt_assert(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
742               SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst));
743
744   /* if the task was scheduled and dst->tasks_before is empty now, we can make it runnable */
745
746   if (dst->unsatisfied_dependencies == 0) {
747     if (SD_task_get_state(dst) == SD_SCHEDULED)
748       SD_task_set_state(dst, SD_RUNNABLE);
749     else
750       SD_task_set_state(dst, SD_SCHEDULABLE);
751   }
752
753   if (dst->is_not_ready == 0)
754     SD_task_set_state(dst, SD_SCHEDULABLE);
755 }
756
757 /**
758  * \brief Returns the user data associated with a dependency between two tasks
759  *
760  * \param src a task
761  * \param dst a task depending on \a src
762  * \return the user data associated with this dependency (can be \c NULL)
763  * \see SD_task_dependency_add()
764  */
765 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst)
766 {
767   xbt_dynar_t dynar;
768   unsigned long length;
769   int found = 0;
770   unsigned long i;
771   SD_dependency_t dependency;
772
773   dynar = src->tasks_after;
774   length = xbt_dynar_length(dynar);
775
776   for (i = 0; i < length && !found; i++) {
777     xbt_dynar_get_cpy(dynar, i, &dependency);
778     found = (dependency->dst == dst);
779   }
780   if (!found)
781     THROWF(arg_error, 0, "No dependency found between task '%s' and '%s'",
782            SD_task_get_name(src), SD_task_get_name(dst));
783   return dependency->data;
784 }
785
786 /**
787  * \brief Adds a watch point to a task
788  *
789  * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this task becomes the one given in argument.
790  * The watch point is then automatically removed.
791  *
792  * \param task a task
793  * \param state the \ref e_SD_task_state_t "state" you want to watch
794  * (cannot be #SD_NOT_SCHEDULED)
795  * \see SD_task_unwatch()
796  */
797 void SD_task_watch(SD_task_t task, e_SD_task_state_t state)
798 {
799   if (state & SD_NOT_SCHEDULED)
800     THROWF(arg_error, 0, "Cannot add a watch point for state SD_NOT_SCHEDULED");
801
802   task->watch_points = task->watch_points | state;
803 }
804
805 /**
806  * \brief Removes a watch point from a task
807  *
808  * \param task a task
809  * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
810  * \see SD_task_watch()
811  */
812 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state)
813 {
814   xbt_assert(state != SD_NOT_SCHEDULED, "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
815
816   task->watch_points = task->watch_points & ~state;
817 }
818
819 /**
820  * \brief Returns an approximative estimation of the execution time of a task.
821  *
822  * The estimation is very approximative because the value returned is the time the task would take if it was executed
823  * now and if it was the only task.
824  *
825  * \param task the task to evaluate
826  * \param workstation_nb number of workstations on which the task would be executed
827  * \param workstation_list the workstations on which the task would be executed
828  * \param flops_amount computation amount for each workstation
829  * \param bytes_amount communication amount between each pair of workstations
830  * \see SD_schedule()
831  */
832 double SD_task_get_execution_time(SD_task_t task, int workstation_nb, const sg_host_t *workstation_list,
833                                   const double *flops_amount, const double *bytes_amount)
834 {
835   double time, max_time = 0.0;
836   int i, j;
837   xbt_assert(workstation_nb > 0, "Invalid parameter");
838
839   /* the task execution time is the maximum execution time of the parallel tasks */
840
841   for (i = 0; i < workstation_nb; i++) {
842     time = 0.0;
843     if (flops_amount != NULL)
844       time = flops_amount[i] / sg_host_speed(workstation_list[i]);
845
846     if (bytes_amount != NULL)
847       for (j = 0; j < workstation_nb; j++) {
848         if (bytes_amount[i * workstation_nb + j] !=0 ) {
849           time += (SD_route_get_latency(workstation_list[i], workstation_list[j]) +
850                    bytes_amount[i * workstation_nb + j] /
851                    SD_route_get_bandwidth(workstation_list[i], workstation_list[j]));
852         }
853       }
854
855     if (time > max_time) {
856       max_time = time;
857     }
858   }
859   return max_time;
860 }
861
862 static XBT_INLINE void SD_task_do_schedule(SD_task_t task)
863 {
864   if (SD_task_get_state(task) > SD_SCHEDULABLE)
865     THROWF(arg_error, 0, "Task '%s' has already been scheduled", SD_task_get_name(task));
866
867   /* update the task state */
868   if (task->unsatisfied_dependencies == 0)
869     SD_task_set_state(task, SD_RUNNABLE);
870   else
871     SD_task_set_state(task, SD_SCHEDULED);
872 }
873
874 /**
875  * \brief Schedules a task
876  *
877  * The task state must be #SD_NOT_SCHEDULED.
878  * Once scheduled, a task is executed as soon as possible in \see SD_simulate, i.e. when its dependencies are satisfied.
879  *
880  * \param task the task you want to schedule
881  * \param workstation_count number of workstations on which the task will be executed
882  * \param workstation_list the workstations on which the task will be executed
883  * \param flops_amount computation amount for each workstation
884  * \param bytes_amount communication amount between each pair of workstations
885  * \param rate task execution speed rate
886  * \see SD_task_unschedule()
887  */
888 void SD_task_schedule(SD_task_t task, int workstation_count, const sg_host_t * workstation_list,
889                       const double *flops_amount, const double *bytes_amount, double rate)
890 {
891   xbt_assert(workstation_count > 0, "workstation_nb must be positive");
892
893   task->host_count = workstation_count;
894   task->rate = rate;
895
896   if (flops_amount) {
897     task->flops_amount = (double*)xbt_realloc(task->flops_amount, sizeof(double) * workstation_count);
898     memcpy(task->flops_amount, flops_amount, sizeof(double) * workstation_count);
899   } else {
900     xbt_free(task->flops_amount);
901     task->flops_amount = NULL;
902   }
903
904   int communication_nb = workstation_count * workstation_count;
905   if (bytes_amount) {
906     task->bytes_amount = (double*)xbt_realloc(task->bytes_amount, sizeof(double) * communication_nb);
907     memcpy(task->bytes_amount, bytes_amount, sizeof(double) * communication_nb);
908   } else {
909     xbt_free(task->bytes_amount);
910     task->bytes_amount = NULL;
911   }
912
913   task->host_list = (sg_host_t*) xbt_realloc(task->host_list, sizeof(sg_host_t) * workstation_count);
914   memcpy(task->host_list, workstation_list, sizeof(sg_host_t) * workstation_count);
915
916   SD_task_do_schedule(task);
917 }
918
919 /**
920  * \brief Unschedules a task
921  *
922  * The task state must be #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING or #SD_FAILED.
923  * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
924  * Call SD_task_schedule() to schedule it again.
925  *
926  * \param task the task you want to unschedule
927  * \see SD_task_schedule()
928  */
929 void SD_task_unschedule(SD_task_t task)
930 {
931   if (task->state != SD_SCHEDULED && task->state != SD_RUNNABLE && task->state != SD_RUNNING &&
932       task->state != SD_FAILED)
933     THROWF(arg_error, 0, "Task %s: the state must be SD_SCHEDULED, SD_RUNNABLE, SD_RUNNING or SD_FAILED",
934            SD_task_get_name(task));
935
936   if ((task->state == SD_SCHEDULED || task->state == SD_RUNNABLE)
937       /* if the task is scheduled or runnable */
938       && ((task->kind == SD_TASK_COMP_PAR_AMDAHL) || (task->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK))) {
939           /* Don't free scheduling data for typed tasks */
940     __SD_task_destroy_scheduling_data(task);
941     xbt_free(task->host_list);
942     task->host_list=NULL;
943     task->host_count = 0;
944   }
945
946   if (SD_task_get_state(task) == SD_RUNNING)
947     /* the task should become SD_FAILED */
948     task->surf_action->cancel();
949   else {
950     if (task->unsatisfied_dependencies == 0)
951       SD_task_set_state(task, SD_SCHEDULABLE);
952     else
953       SD_task_set_state(task, SD_NOT_SCHEDULED);
954   }
955   task->remains = task->amount;
956   task->start_time = -1.0;
957 }
958
959 /* Destroys the data memorized by SD_task_schedule. Task state must be SD_SCHEDULED or SD_RUNNABLE.
960  */
961 static void __SD_task_destroy_scheduling_data(SD_task_t task)
962 {
963   if (task->state != SD_SCHEDULED && task->state != SD_RUNNABLE)
964     THROWF(arg_error, 0, "Task '%s' must be SD_SCHEDULED or SD_RUNNABLE", SD_task_get_name(task));
965
966   xbt_free(task->flops_amount);
967   xbt_free(task->bytes_amount);
968   task->flops_amount = task->bytes_amount = NULL;
969 }
970
971 /* Runs a task. */
972 void SD_task_run(SD_task_t task)
973 {
974   int i;
975   sg_host_t *hosts;
976
977   xbt_assert(SD_task_get_state(task) == SD_RUNNABLE, "Task '%s' is not runnable! Task state: %d",
978              SD_task_get_name(task), (int)SD_task_get_state(task));
979   xbt_assert(task->host_list != NULL, "Task '%s': workstation_list is NULL!", SD_task_get_name(task));
980
981   XBT_DEBUG("Running task '%s'", SD_task_get_name(task));
982
983   /* Copy the elements of the task into the action */
984   int host_nb = task->host_count;
985   hosts = xbt_new(sg_host_t, host_nb);
986
987   for (i = 0; i < host_nb; i++)
988     hosts[i] =  task->host_list[i];
989
990   double *flops_amount = xbt_new0(double, host_nb);
991   double *bytes_amount = xbt_new0(double, host_nb * host_nb);
992
993   if(task->flops_amount)
994     memcpy(flops_amount, task->flops_amount, sizeof(double) * host_nb);
995   if(task->bytes_amount)
996     memcpy(bytes_amount, task->bytes_amount, sizeof(double) * host_nb * host_nb);
997
998   task->surf_action = surf_host_model->executeParallelTask(host_nb, hosts, flops_amount, bytes_amount, task->rate);
999
1000   task->surf_action->setData(task);
1001
1002   XBT_DEBUG("surf_action = %p", task->surf_action);
1003
1004   if (task->category)
1005     TRACE_surf_action(task->surf_action, task->category);
1006
1007   __SD_task_destroy_scheduling_data(task);      /* now the scheduling data are not useful anymore */
1008   SD_task_set_state(task, SD_RUNNING);
1009   xbt_assert(SD_task_get_state(task) == SD_RUNNING, "Bad state of task '%s': %d",
1010              SD_task_get_name(task), (int)SD_task_get_state(task));
1011
1012 }
1013
1014 /* 
1015  * Remove all dependencies associated with a task. This function is called when the task is destroyed.
1016  */
1017 static void __SD_task_remove_dependencies(SD_task_t task)
1018 {
1019   /* we must destroy the dependencies carefuly (with SD_dependency_remove) because each one is stored twice */
1020   SD_dependency_t dependency;
1021   while (!xbt_dynar_is_empty(task->tasks_before)) {
1022     xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
1023     SD_task_dependency_remove(dependency->src, dependency->dst);
1024   }
1025
1026   while (!xbt_dynar_is_empty(task->tasks_after)) {
1027     xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
1028     SD_task_dependency_remove(dependency->src, dependency->dst);
1029   }
1030 }
1031
1032 /**
1033  * \brief Returns the start time of a task
1034  *
1035  * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1036  *
1037  * \param task: a task
1038  * \return the start time of this task
1039  */
1040 double SD_task_get_start_time(SD_task_t task)
1041 {
1042   if (task->surf_action)
1043     return task->surf_action->getStartTime();
1044   else
1045     return task->start_time;
1046 }
1047
1048 /**
1049  * \brief Returns the finish time of a task
1050  *
1051  * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1052  * If the state is not completed yet, the returned value is an estimation of the task finish time. This value can
1053  * vary until the task is completed.
1054  *
1055  * \param task: a task
1056  * \return the start time of this task
1057  */
1058 double SD_task_get_finish_time(SD_task_t task)
1059 {
1060   if (task->surf_action)        /* should never happen as actions are destroyed right after their completion */
1061     return task->surf_action->getFinishTime();
1062   else
1063     return task->finish_time;
1064 }
1065
1066 void SD_task_distribute_comp_amdahl(SD_task_t task, int ws_count)
1067 {
1068   int i;
1069   xbt_assert(task->kind == SD_TASK_COMP_PAR_AMDAHL, "Task %s is not a SD_TASK_COMP_PAR_AMDAHL typed task."
1070               "Cannot use this function.", SD_task_get_name(task));
1071   task->flops_amount = xbt_new0(double, ws_count);
1072   task->bytes_amount = xbt_new0(double, ws_count * ws_count);
1073   xbt_free(task->host_list);
1074   task->host_count = ws_count;
1075   task->host_list = xbt_new0(sg_host_t, ws_count);
1076   
1077   for(i=0;i<ws_count;i++){
1078     task->flops_amount[i] = (task->alpha + (1 - task->alpha)/ws_count) * task->amount;
1079   }
1080
1081
1082
1083 /** @brief Auto-schedules a task.
1084  *
1085  * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
1086  * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
1087  * mandatory power.
1088  *
1089  * To be auto-schedulable, a task must be type and created with one of the specialized creation functions.
1090  *
1091  * @todo
1092  * We should create tasks kind for the following categories:
1093  *  - Point to point communication (done)
1094  *  - Sequential computation       (done)
1095  *  - group communication (redistribution, several kinds)
1096  *  - parallel tasks with no internal communication (one kind per speedup    model such as Amdahl)
1097  *  - idem+ internal communication. Task type not enough since we cannot store comm cost alongside to comp one)
1098  */
1099 void SD_task_schedulev(SD_task_t task, int count, const sg_host_t * list)
1100 {
1101   int i, j;
1102   SD_dependency_t dep;
1103   unsigned int cpt;
1104   xbt_assert(task->kind != 0, "Task %s is not typed. Cannot automatically schedule it.", SD_task_get_name(task));
1105   switch (task->kind) {
1106   case SD_TASK_COMP_PAR_AMDAHL:
1107     SD_task_distribute_comp_amdahl(task, count);
1108   case SD_TASK_COMM_E2E:
1109   case SD_TASK_COMP_SEQ:
1110     xbt_assert(task->host_count == count, "Got %d locations, but were expecting %d locations", count,task->host_count);
1111     for (i = 0; i < count; i++)
1112       task->host_list[i] = list[i];
1113     if (SD_task_get_kind(task)== SD_TASK_COMP_SEQ && !task->flops_amount){
1114       /*This task has failed and is rescheduled. Reset the flops_amount*/
1115       task->flops_amount = xbt_new0(double, 1);
1116       task->flops_amount[0] = task->remains;
1117     }
1118     SD_task_do_schedule(task);
1119     break;
1120   default:
1121     xbt_die("Kind of task %s not supported by SD_task_schedulev()", SD_task_get_name(task));
1122   }
1123   if (task->kind == SD_TASK_COMM_E2E) {
1124     XBT_VERB("Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(task),
1125           sg_host_get_name(task->host_list[0]), sg_host_get_name(task->host_list[1]), task->bytes_amount[2]);
1126
1127   }
1128
1129   /* Iterate over all children and parents being COMM_E2E to say where I am located (and start them if runnable) */
1130   if (task->kind == SD_TASK_COMP_SEQ) {
1131     XBT_VERB("Schedule computation task %s on %s. It costs %.f flops", SD_task_get_name(task),
1132           sg_host_get_name(task->host_list[0]), task->flops_amount[0]);
1133
1134     xbt_dynar_foreach(task->tasks_before, cpt, dep) {
1135       SD_task_t before = dep->src;
1136       if (before->kind == SD_TASK_COMM_E2E) {
1137         before->host_list[1] = task->host_list[0];
1138
1139         if (before->host_list[0] && (SD_task_get_state(before) < SD_SCHEDULED)) {
1140           SD_task_do_schedule(before);
1141           XBT_VERB ("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(before),
1142                sg_host_get_name(before->host_list[0]), sg_host_get_name(before->host_list[1]), before->bytes_amount[2]);
1143         }
1144       }
1145     }
1146     xbt_dynar_foreach(task->tasks_after, cpt, dep) {
1147       SD_task_t after = dep->dst;
1148       if (after->kind == SD_TASK_COMM_E2E) {
1149         after->host_list[0] = task->host_list[0];
1150         if (after->host_list[1] && (SD_task_get_state(after) < SD_SCHEDULED)) {
1151           SD_task_do_schedule(after);
1152           XBT_VERB ("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(after),
1153                sg_host_get_name(after->host_list[0]), sg_host_get_name(after->host_list[1]), after->bytes_amount[2]);
1154         }
1155       }
1156     }
1157   }
1158   /* Iterate over all children and parents being MXN_1D_BLOCK to say where I am located (and start them if runnable) */
1159   if (task->kind == SD_TASK_COMP_PAR_AMDAHL) {
1160     XBT_VERB("Schedule computation task %s on %d workstations. %.f flops will be distributed following Amdahl's Law",
1161           SD_task_get_name(task), task->host_count, task->flops_amount[0]);
1162     xbt_dynar_foreach(task->tasks_before, cpt, dep) {
1163       SD_task_t before = dep->src;
1164       if (before->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK){
1165         if (!before->host_list){
1166           XBT_VERB("Sender side of Task %s is not scheduled yet", SD_task_get_name(before));
1167           before->host_list = xbt_new0(sg_host_t, count);
1168           before->host_count = count;
1169           XBT_VERB("Fill the workstation list with list of Task '%s'", SD_task_get_name(task));
1170           for (i=0;i<count;i++)
1171             before->host_list[i] = task->host_list[i];
1172         } else {
1173           XBT_VERB("Build communication matrix for task '%s'", SD_task_get_name(before));
1174           int src_nb, dst_nb;
1175           double src_start, src_end, dst_start, dst_end;
1176           src_nb = before->host_count;
1177           dst_nb = count;
1178           before->host_list = (sg_host_t*) xbt_realloc(before->host_list, (before->host_count+count)*sizeof(sg_host_t));
1179           for(i=0; i<count; i++)
1180             before->host_list[before->host_count+i] = task->host_list[i];
1181
1182           before->host_count += count;
1183           xbt_free(before->flops_amount);
1184           xbt_free(before->bytes_amount);
1185           before->flops_amount = xbt_new0(double, before->host_count);
1186           before->bytes_amount = xbt_new0(double, before->host_count* before->host_count);
1187
1188           for(i=0;i<src_nb;i++){
1189             src_start = i*before->amount/src_nb;
1190             src_end = src_start + before->amount/src_nb;
1191             for(j=0; j<dst_nb; j++){
1192               dst_start = j*before->amount/dst_nb;
1193               dst_end = dst_start + before->amount/dst_nb;
1194               XBT_VERB("(%s->%s): (%.2f, %.2f)-> (%.2f, %.2f)", sg_host_get_name(before->host_list[i]),
1195                   sg_host_get_name(before->host_list[src_nb+j]), src_start, src_end, dst_start, dst_end);
1196               if ((src_end <= dst_start) || (dst_end <= src_start)) {
1197                 before->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]=0.0;
1198               } else {
1199                 before->bytes_amount[i*(src_nb+dst_nb)+src_nb+j] = MIN(src_end, dst_end) - MAX(src_start, dst_start);
1200               }
1201               XBT_VERB("==> %.2f", before->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]);
1202             }
1203           }
1204
1205           if (SD_task_get_state(before)< SD_SCHEDULED) {
1206             SD_task_do_schedule(before);
1207             XBT_VERB
1208               ("Auto-Schedule redistribution task %s. Send %.f bytes from %d hosts to %d hosts.",
1209                   SD_task_get_name(before),before->amount, src_nb, dst_nb);
1210             }
1211         }
1212       }
1213     }
1214     xbt_dynar_foreach(task->tasks_after, cpt, dep) {
1215       SD_task_t after = dep->dst;
1216       if (after->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK){
1217         if (!after->host_list){
1218           XBT_VERB("Receiver side of Task '%s' is not scheduled yet", SD_task_get_name(after));
1219           after->host_list = xbt_new0(sg_host_t, count);
1220           after->host_count = count;
1221           XBT_VERB("Fill the workstation list with list of Task '%s'", SD_task_get_name(task));
1222           for (i=0;i<count;i++)
1223             after->host_list[i] = task->host_list[i];
1224         } else {
1225           int src_nb, dst_nb;
1226           double src_start, src_end, dst_start, dst_end;
1227           src_nb = count;
1228           dst_nb = after->host_count;
1229           after->host_list = (sg_host_t*) xbt_realloc(after->host_list, (after->host_count+count)*sizeof(sg_host_t));
1230           for(i=after->host_count - 1; i>=0; i--)
1231             after->host_list[count+i] = after->host_list[i];
1232           for(i=0; i<count; i++)
1233             after->host_list[i] = task->host_list[i];
1234
1235           after->host_count += count;
1236
1237           xbt_free(after->flops_amount);
1238           xbt_free(after->bytes_amount);
1239
1240           after->flops_amount = xbt_new0(double, after->host_count);
1241           after->bytes_amount = xbt_new0(double, after->host_count* after->host_count);
1242
1243           for(i=0;i<src_nb;i++){
1244             src_start = i*after->amount/src_nb;
1245             src_end = src_start + after->amount/src_nb;
1246             for(j=0; j<dst_nb; j++){
1247               dst_start = j*after->amount/dst_nb;
1248               dst_end = dst_start + after->amount/dst_nb;
1249               XBT_VERB("(%d->%d): (%.2f, %.2f)-> (%.2f, %.2f)", i, j, src_start, src_end, dst_start, dst_end);
1250               if ((src_end <= dst_start) || (dst_end <= src_start)) {
1251                 after->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]=0.0;
1252               } else {
1253                 after->bytes_amount[i*(src_nb+dst_nb)+src_nb+j] = MIN(src_end, dst_end)- MAX(src_start, dst_start);
1254               }
1255               XBT_VERB("==> %.2f", after->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]);
1256             }
1257           }
1258
1259           if (SD_task_get_state(after)< SD_SCHEDULED) {
1260             SD_task_do_schedule(after);
1261             XBT_VERB ("Auto-Schedule redistribution task %s. Send %.f bytes from %d hosts to %d hosts.",
1262               SD_task_get_name(after),after->amount, src_nb, dst_nb);
1263           }
1264         }
1265       }
1266     }
1267   }
1268 }
1269
1270 /** @brief autoschedule a task on a list of workstations
1271  *
1272  * This function is very similar to SD_task_schedulev(), but takes the list of workstations to schedule onto as
1273  * separate parameters.
1274  * It builds a proper vector of workstations and then call SD_task_schedulev()
1275  */
1276 void SD_task_schedulel(SD_task_t task, int count, ...)
1277 {
1278   va_list ap;
1279   sg_host_t *list = xbt_new(sg_host_t, count);
1280   int i;
1281   va_start(ap, count);
1282   for (i = 0; i < count; i++) {
1283     list[i] = va_arg(ap, sg_host_t);
1284   }
1285   va_end(ap);
1286   SD_task_schedulev(task, count, list);
1287   free(list);
1288 }