Logo AND Algorithmique Numérique Distribuée

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