Logo AND Algorithmique Numérique Distribuée

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