Logo AND Algorithmique Numérique Distribuée

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