Logo AND Algorithmique Numérique Distribuée

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