Logo AND Algorithmique Numérique Distribuée

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