Logo AND Algorithmique Numérique Distribuée

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