Logo AND Algorithmique Numérique Distribuée

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