Logo AND Algorithmique Numérique Distribuée

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