Logo AND Algorithmique Numérique Distribuée

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