Logo AND Algorithmique Numérique Distribuée

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