Logo AND Algorithmique Numérique Distribuée

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