Logo AND Algorithmique Numérique Distribuée

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