Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
def186271ffa41be19ea97b71a818221a873005a
[simgrid.git] / src / s4u / s4u_Actor.cpp
1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/actor.h>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Actor.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/Exec.hpp>
12 #include <simgrid/s4u/VirtualMachine.hpp>
13
14 #include "src/include/mc/mc.h"
15 #include "src/kernel/EngineImpl.hpp"
16 #include "src/kernel/actor/ActorImpl.hpp"
17 #include "src/kernel/context/Context.hpp"
18 #include "src/mc/mc_replay.hpp"
19 #include "src/surf/HostImpl.hpp"
20
21 #include <algorithm>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_actor, s4u, "S4U actors");
24
25 namespace simgrid {
26
27 template class xbt::Extendable<s4u::Actor>;
28
29 namespace s4u {
30
31 xbt::signal<void(Actor&)> s4u::Actor::on_creation;
32 xbt::signal<void(Actor const&)> s4u::Actor::on_suspend;
33 xbt::signal<void(Actor const&)> s4u::Actor::on_resume;
34 xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
35 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
36 xbt::signal<void(Actor const&, Host const& previous_location)> s4u::Actor::on_host_change;
37 xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
38 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
39
40 // ***** Actor creation *****
41 Actor* Actor::self()
42 {
43   const kernel::context::Context* self_context = kernel::context::Context::self();
44   if (self_context == nullptr)
45     return nullptr;
46
47   return self_context->get_actor()->get_ciface();
48 }
49
50 ActorPtr Actor::init(const std::string& name, s4u::Host* host)
51 {
52   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
53   kernel::actor::ActorImpl* actor =
54       kernel::actor::simcall_answered([self, &name, host] { return self->init(name, host).get(); });
55   return actor->get_iface();
56 }
57
58 /** Set a non-default stack size for this context (in Kb)
59  *
60  * This must be done before starting the actor, and it won't work with the thread factory. */
61 ActorPtr Actor::set_stacksize(unsigned stacksize)
62 {
63   pimpl_->set_stacksize(stacksize * 1024);
64   return this;
65 }
66
67 ActorPtr Actor::start(const std::function<void()>& code)
68 {
69   simgrid::kernel::actor::simcall_answered([this, &code] { pimpl_->start(code); });
70   return this;
71 }
72
73 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::function<void()>& code)
74 {
75   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
76   kernel::actor::ActorImpl* actor =
77       kernel::actor::simcall_answered([self, &name, host, &code] { return self->init(name, host)->start(code); });
78
79   return actor->get_iface();
80 }
81
82 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::string& function,
83                        std::vector<std::string> args)
84 {
85   const simgrid::kernel::actor::ActorCodeFactory& factory =
86       simgrid::kernel::EngineImpl::get_instance()->get_function(function);
87   return create(name, host, factory(std::move(args)));
88 }
89
90 void intrusive_ptr_add_ref(const Actor* actor)
91 {
92   intrusive_ptr_add_ref(actor->pimpl_);
93 }
94 void intrusive_ptr_release(const Actor* actor)
95 {
96   intrusive_ptr_release(actor->pimpl_);
97 }
98 int Actor::get_refcount() const
99 {
100   return pimpl_->get_refcount();
101 }
102
103 // ***** Actor methods *****
104
105 void Actor::join() const
106 {
107   join(-1);
108 }
109
110 void Actor::join(double timeout) const
111 {
112   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
113   const kernel::actor::ActorImpl* target = pimpl_;
114   kernel::actor::simcall_blocking([issuer, target, timeout] {
115     if (target->context_->wannadie()) {
116       // The joined actor is already finished, just wake up the issuer right away
117       issuer->simcall_answer();
118     } else {
119       kernel::activity::ActivityImplPtr sync = issuer->join(target, timeout);
120       sync->register_simcall(&issuer->simcall_);
121     }
122   });
123 }
124
125 Actor* Actor::set_auto_restart(bool autorestart)
126 {
127   if (autorestart == pimpl_->has_to_auto_restart()) // not changed
128     return this;
129
130   kernel::actor::simcall_answered([this, autorestart]() {
131     xbt_assert(autorestart, "Asking an actor to stop being autorestart is not implemented yet. Ask us if you need it.");
132     pimpl_->set_auto_restart(autorestart);
133
134     auto* arg = new kernel::actor::ProcessArg(pimpl_->get_host(), pimpl_);
135     XBT_DEBUG("Adding %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
136     pimpl_->get_host()->get_impl()->add_actor_at_boot(arg);
137   });
138   return this;
139 }
140 int Actor::get_restart_count() const
141 {
142   return pimpl_->get_restart_count();
143 }
144
145 void Actor::on_exit(const std::function<void(bool /*failed*/)>& fun) const
146 {
147   kernel::actor::simcall_answered([this, &fun] { pimpl_->on_exit->emplace_back(fun); });
148 }
149
150 void Actor::set_host(Host* new_host)
151 {
152   const s4u::Host* previous_location = get_host();
153
154   kernel::actor::simcall_answered([this, new_host]() {
155     for (auto const& activity : pimpl_->activities_) {
156       // FIXME: implement the migration of other kinds of activities
157       if (auto exec = boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(activity))
158         exec->migrate(new_host);
159     }
160     this->pimpl_->set_host(new_host);
161   });
162
163   s4u::Actor::on_host_change(*this, *previous_location);
164 }
165
166 s4u::Host* Actor::get_host() const
167 {
168   return this->pimpl_->get_host();
169 }
170
171 Actor* Actor::daemonize()
172 {
173   kernel::actor::simcall_answered([this]() { pimpl_->daemonize(); });
174   return this;
175 }
176
177 bool Actor::is_daemon() const
178 {
179   return this->pimpl_->is_daemon();
180 }
181
182 bool Actor::is_maestro()
183 {
184   const auto* self = kernel::actor::ActorImpl::self();
185   return self == nullptr || kernel::EngineImpl::get_instance()->is_maestro(self);
186 }
187
188 const simgrid::xbt::string& Actor::get_name() const
189 {
190   return this->pimpl_->get_name();
191 }
192
193 const char* Actor::get_cname() const
194 {
195   return this->pimpl_->get_cname();
196 }
197
198 aid_t Actor::get_pid() const
199 {
200   return this->pimpl_->get_pid();
201 }
202
203 aid_t Actor::get_ppid() const
204 {
205   return this->pimpl_->get_ppid();
206 }
207
208 void Actor::suspend()
209 {
210   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
211   kernel::actor::ActorImpl* target = pimpl_;
212   s4u::Actor::on_suspend(*this);
213   kernel::actor::simcall_blocking([issuer, target]() {
214     target->suspend();
215     if (target != issuer) {
216       /* If we are suspending ourselves, then just do not finish the simcall now */
217       issuer->simcall_answer();
218     }
219   });
220 }
221
222 void Actor::resume()
223 {
224   kernel::actor::simcall_answered([this] { pimpl_->resume(); });
225   s4u::Actor::on_resume(*this);
226 }
227
228 bool Actor::is_suspended() const
229 {
230   return pimpl_->is_suspended();
231 }
232
233 void Actor::set_kill_time(double kill_time)
234 {
235   kernel::actor::simcall_answered([this, kill_time] { pimpl_->set_kill_time(kill_time); });
236 }
237
238 /** @brief Get the kill time of an actor(or 0 if unset). */
239 double Actor::get_kill_time() const
240 {
241   return pimpl_->get_kill_time();
242 }
243
244 void Actor::kill()
245 {
246   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
247   kernel::actor::simcall_answered([this, self] { self->kill(pimpl_); });
248 }
249
250 // ***** Static functions *****
251
252 ActorPtr Actor::by_pid(aid_t pid)
253 {
254   kernel::actor::ActorImpl* actor = kernel::actor::ActorImpl::by_pid(pid);
255   if (actor != nullptr)
256     return actor->get_iface();
257   else
258     return ActorPtr();
259 }
260
261 void Actor::kill_all()
262 {
263   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
264   kernel::actor::simcall_answered([self] { self->kill_all(); });
265 }
266
267 const std::unordered_map<std::string, std::string>* Actor::get_properties() const
268 {
269   return pimpl_->get_properties();
270 }
271
272 /** Retrieve the property value (or nullptr if not set) */
273 const char* Actor::get_property(const std::string& key) const
274 {
275   return pimpl_->get_property(key);
276 }
277
278 void Actor::set_property(const std::string& key, const std::string& value)
279 {
280   kernel::actor::simcall_answered([this, &key, &value] { pimpl_->set_property(key, value); });
281 }
282
283 Actor* Actor::restart()
284 {
285   return kernel::actor::simcall_answered([this]() { return pimpl_->restart(); });
286 }
287
288 // ***** this_actor *****
289
290 namespace this_actor {
291
292 /** Returns true if run from the kernel mode, and false if run from a real actor
293  *
294  * Everything that is run out of any actor (simulation setup before the engine is run,
295  * computing the model evolutions as a result to the actors' action, etc) is run in
296  * kernel mode, just as in any operating systems.
297  *
298  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
299  * because it is the one scheduling when the others should move or wait.
300  */
301 bool is_maestro()
302 {
303   return Actor::is_maestro();
304 }
305
306 void sleep_for(double duration)
307 {
308   xbt_assert(std::isfinite(duration), "duration is not finite!");
309
310   if (duration <= 0) /* that's a no-op */
311     return;
312
313   if (duration < sg_surf_precision) {
314     static unsigned int warned = 0; // At most 20 such warnings
315     warned++;
316     if (warned <= 20)
317       XBT_INFO("The parameter to sleep_for() is smaller than the SimGrid numerical accuracy (%g < %g). "
318                "Please refer to https://simgrid.org/doc/latest/Configuring_SimGrid.html#numerical-precision",
319                duration, sg_surf_precision);
320     if (warned == 20)
321       XBT_VERB("(further warnings about the numerical accuracy of sleep_for() will be omitted).");
322   }
323
324   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
325   Actor::on_sleep(*issuer->get_ciface());
326
327   kernel::actor::simcall_blocking([issuer, duration]() {
328     if (MC_is_active() || MC_record_replay_is_active()) {
329       MC_process_clock_add(issuer, duration);
330       issuer->simcall_answer();
331       return;
332     }
333     kernel::activity::ActivityImplPtr sync = issuer->sleep(duration);
334     sync->register_simcall(&issuer->simcall_);
335   });
336
337   Actor::on_wake_up(*issuer->get_ciface());
338 }
339
340 void yield()
341 {
342   kernel::actor::simcall_answered([] { /* do nothing*/ });
343 }
344
345 XBT_PUBLIC void sleep_until(double wakeup_time)
346 {
347   double now = s4u::Engine::get_clock();
348   if (wakeup_time > now)
349     sleep_for(wakeup_time - now);
350 }
351
352 void execute(double flops)
353 {
354   execute(flops, 1.0 /* priority */);
355 }
356
357 void execute(double flops, double priority)
358 {
359   exec_init(flops)->set_priority(priority)->vetoable_start()->wait();
360 }
361
362 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
363                       const std::vector<double>& bytes_amounts)
364 {
365   exec_init(hosts, flops_amounts, bytes_amounts)->wait();
366 }
367
368 void thread_execute(s4u::Host* host, double flops_amount, int thread_count)
369 {
370   Exec::init()->set_flops_amount(flops_amount)->set_host(host)->set_thread_count(thread_count)->wait();
371 }
372
373 ExecPtr exec_init(double flops_amount)
374 {
375   return Exec::init()->set_flops_amount(flops_amount)->set_host(get_host());
376 }
377
378 ExecPtr exec_init(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
379                   const std::vector<double>& bytes_amounts)
380 {
381   xbt_assert(not hosts.empty(), "Your parallel executions must span over at least one host.");
382   xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
383              "Host count (%zu) does not match flops_amount count (%zu).", hosts.size(), flops_amounts.size());
384   xbt_assert(hosts.size() * hosts.size() == bytes_amounts.size() || bytes_amounts.empty(),
385              "bytes_amounts must be a matrix of size host_count * host_count (%zu*%zu), but it's of size %zu.",
386              hosts.size(), hosts.size(), bytes_amounts.size());
387   /* Check that we are not mixing VMs and PMs in the parallel task */
388   bool is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(hosts.front()));
389   xbt_assert(std::all_of(hosts.begin(), hosts.end(),
390                          [is_a_vm](s4u::Host* elm) {
391                            bool tmp_is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(elm));
392                            return is_a_vm == tmp_is_a_vm;
393                          }),
394              "parallel_execute: mixing VMs and PMs is not supported (yet).");
395   /* checking for infinite values */
396   xbt_assert(std::all_of(flops_amounts.begin(), flops_amounts.end(), [](double elm) { return std::isfinite(elm); }),
397              "flops_amounts comprises infinite values!");
398   xbt_assert(std::all_of(bytes_amounts.begin(), bytes_amounts.end(), [](double elm) { return std::isfinite(elm); }),
399              "flops_amounts comprises infinite values!");
400
401   return Exec::init()->set_flops_amounts(flops_amounts)->set_bytes_amounts(bytes_amounts)->set_hosts(hosts);
402 }
403
404 ExecPtr exec_async(double flops)
405 {
406   ExecPtr res = exec_init(flops);
407   res->vetoable_start();
408   return res;
409 }
410
411 aid_t get_pid()
412 {
413   return simgrid::kernel::actor::ActorImpl::self()->get_pid();
414 }
415
416 aid_t get_ppid()
417 {
418   return simgrid::kernel::actor::ActorImpl::self()->get_ppid();
419 }
420
421 std::string get_name()
422 {
423   return simgrid::kernel::actor::ActorImpl::self()->get_name();
424 }
425
426 const char* get_cname()
427 {
428   return simgrid::kernel::actor::ActorImpl::self()->get_cname();
429 }
430
431 Host* get_host()
432 {
433   return simgrid::kernel::actor::ActorImpl::self()->get_host();
434 }
435
436 void suspend()
437 {
438   kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
439   s4u::Actor::on_suspend(*self->get_ciface());
440   kernel::actor::simcall_blocking([self] { self->suspend(); });
441 }
442
443 void exit()
444 {
445   kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
446   simgrid::kernel::actor::simcall_answered([self] { self->exit(); });
447   THROW_IMPOSSIBLE;
448 }
449
450 void on_exit(const std::function<void(bool)>& fun)
451 {
452   simgrid::kernel::actor::ActorImpl::self()->get_iface()->on_exit(fun);
453 }
454
455 /** @brief Moves the current actor to another host
456  *
457  * @see simgrid::s4u::Actor::migrate() for more information
458  */
459 void set_host(Host* new_host)
460 {
461   simgrid::kernel::actor::ActorImpl::self()->get_iface()->set_host(new_host);
462 }
463
464 } // namespace this_actor
465 } // namespace s4u
466 } // namespace simgrid
467
468 /* **************************** Public C interface *************************** */
469 size_t sg_actor_count()
470 {
471   return simgrid::s4u::Engine::get_instance()->get_actor_count();
472 }
473
474 sg_actor_t* sg_actor_list()
475 {
476   const simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
477   size_t actor_count      = e->get_actor_count();
478   xbt_assert(actor_count > 0, "There is no actor!");
479   std::vector<simgrid::s4u::ActorPtr> actors = e->get_all_actors();
480
481   auto* res = xbt_new(sg_actor_t, actors.size());
482   for (size_t i = 0; i < actor_count; i++)
483     res[i] = actors[i].get();
484   return res;
485 }
486
487 sg_actor_t sg_actor_init(const char* name, sg_host_t host)
488 {
489   return simgrid::s4u::Actor::init(name, host).get();
490 }
491
492 void sg_actor_start_(sg_actor_t actor, xbt_main_func_t code, int argc, const char* const* argv)
493 {
494   simgrid::kernel::actor::ActorCode function;
495   if (code)
496     function = simgrid::xbt::wrap_main(code, argc, argv);
497   actor->start(function);
498 }
499
500 sg_actor_t sg_actor_create_(const char* name, sg_host_t host, xbt_main_func_t code, int argc, const char* const* argv)
501 {
502   simgrid::kernel::actor::ActorCode function = simgrid::xbt::wrap_main(code, argc, argv);
503   return simgrid::s4u::Actor::init(name, host)->start(function).get();
504 }
505
506 void sg_actor_set_stacksize(sg_actor_t actor, unsigned size)
507 {
508   actor->set_stacksize(size);
509 }
510
511 void sg_actor_exit()
512 {
513   simgrid::s4u::this_actor::exit();
514 }
515
516 /**
517  * @brief Returns the process ID of @a actor.
518  *
519  * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
520  */
521
522 aid_t sg_actor_get_pid(const_sg_actor_t actor)
523 {
524   /* Do not raise an exception here: this function is called by the logs
525    * and the exceptions, so it would be called back again and again */
526   if (actor == nullptr || actor->get_impl() == nullptr)
527     return 0;
528   return actor->get_pid();
529 }
530
531 /**
532  * @brief Returns the process ID of the parent of @a actor.
533  *
534  * This function checks whether @a actor is a valid pointer and return its parent's PID.
535  * Returns -1 if the actor has not been created by any other actor.
536  */
537 aid_t sg_actor_get_ppid(const_sg_actor_t actor)
538 {
539   return actor->get_ppid();
540 }
541
542 /**
543  * @brief Return a #sg_actor_t given its PID.
544  *
545  * This function search in the list of all the created sg_actor_t for a sg_actor_t  whose PID is equal to @a PID.
546  * If none is found, @c nullptr is returned.
547    Note that the PID are unique in the whole simulation, not only on a given host.
548  */
549 sg_actor_t sg_actor_by_pid(aid_t pid)
550 {
551   return simgrid::s4u::Actor::by_pid(pid).get();
552 }
553
554 /** @brief Return the name of an actor. */
555 const char* sg_actor_get_name(const_sg_actor_t actor)
556 {
557   return actor->get_cname();
558 }
559
560 sg_host_t sg_actor_get_host(const_sg_actor_t actor)
561 {
562   return actor->get_host();
563 }
564
565 /**
566  * @brief Returns the value of a given actor property
567  *
568  * @param actor an actor
569  * @param name a property name
570  * @return value of a property (or nullptr if the property is not set)
571  */
572 const char* sg_actor_get_property_value(const_sg_actor_t actor, const char* name)
573 {
574   return actor->get_property(name);
575 }
576
577 /**
578  * @brief Return the list of properties
579  *
580  * This function returns all the parameters associated with an actor
581  */
582 xbt_dict_t sg_actor_get_properties(const_sg_actor_t actor)
583 {
584   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
585   xbt_dict_t as_dict                        = xbt_dict_new_homogeneous(xbt_free_f);
586   const std::unordered_map<std::string, std::string>* props = actor->get_properties();
587   if (props == nullptr)
588     return nullptr;
589   for (auto const& kv : *props) {
590     xbt_dict_set(as_dict, kv.first.c_str(), xbt_strdup(kv.second.c_str()));
591   }
592   return as_dict;
593 }
594
595 /**
596  * @brief Suspend the actor.
597  *
598  * This function suspends the actor by suspending the task on which it was waiting for the completion.
599  */
600 void sg_actor_suspend(sg_actor_t actor)
601 {
602   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
603   actor->suspend();
604 }
605
606 /**
607  * @brief Resume a suspended actor.
608  *
609  * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
610  */
611 void sg_actor_resume(sg_actor_t actor)
612 {
613   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
614   actor->resume();
615 }
616
617 /**
618  * @brief Returns true if the actor is suspended .
619  *
620  * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
621  */
622 int sg_actor_is_suspended(const_sg_actor_t actor)
623 {
624   return actor->is_suspended();
625 }
626
627 /** @brief Restarts an actor from the beginning. */
628 sg_actor_t sg_actor_restart(sg_actor_t actor)
629 {
630   return actor->restart();
631 }
632
633 /**
634  * @brief Sets the "auto-restart" flag of the actor.
635  * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
636  */
637 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
638 {
639   actor->set_auto_restart(auto_restart);
640 }
641
642 /** @brief This actor will be terminated automatically when the last non-daemon actor finishes */
643 void sg_actor_daemonize(sg_actor_t actor)
644 {
645   actor->daemonize();
646 }
647
648 /** Returns whether or not this actor has been daemonized or not */
649 int sg_actor_is_daemon(const_sg_actor_t actor)
650 {
651   return actor->is_daemon();
652 }
653
654 /**
655  * @brief Migrates an actor to another location.
656  *
657  * This function changes the value of the #sg_host_t on  which @a actor is running.
658  */
659 void sg_actor_set_host(sg_actor_t actor, sg_host_t host)
660 {
661   actor->set_host(host);
662 }
663
664 /**
665  * @brief Wait for the completion of a #sg_actor_t.
666  *
667  * @param actor the actor to wait for
668  * @param timeout wait until the actor is over, or the timeout expires
669  */
670 void sg_actor_join(const_sg_actor_t actor, double timeout)
671 {
672   actor->join(timeout);
673 }
674
675 void sg_actor_kill(sg_actor_t actor)
676 {
677   actor->kill();
678 }
679
680 void sg_actor_kill_all()
681 {
682   simgrid::s4u::Actor::kill_all();
683 }
684
685 /**
686  * @brief Set the kill time of an actor.
687  *
688  * @param actor an actor
689  * @param kill_time the time when the actor is killed.
690  */
691 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
692 {
693   actor->set_kill_time(kill_time);
694 }
695
696 /** Yield the current actor; let the other actors execute first */
697 void sg_actor_yield()
698 {
699   simgrid::s4u::this_actor::yield();
700 }
701
702 void sg_actor_sleep_for(double duration)
703 {
704   simgrid::s4u::this_actor::sleep_for(duration);
705 }
706
707 void sg_actor_sleep_until(double wakeup_time)
708 {
709   simgrid::s4u::this_actor::sleep_until(wakeup_time);
710 }
711
712 sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
713 {
714   xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
715   std::unordered_map<std::string, std::string> props;
716   xbt_dict_cursor_t cursor = nullptr;
717   char* key;
718   char* value;
719   xbt_dict_foreach (properties, cursor, key, value)
720     props[key] = value;
721   xbt_dict_free(&properties);
722
723   /* Let's create the actor: SIMIX may decide to start it right now, even before returning the flow control to us */
724   smx_actor_t actor = nullptr;
725   try {
726     actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host).get();
727     actor->set_properties(props);
728   } catch (simgrid::HostFailureException const&) {
729     xbt_die("Could not attach");
730   }
731
732   simgrid::s4u::this_actor::yield();
733   return actor->get_ciface();
734 }
735
736 void sg_actor_detach()
737 {
738   simgrid::kernel::actor::ActorImpl::detach();
739 }
740
741 aid_t sg_actor_self_get_pid()
742 {
743   return simgrid::s4u::this_actor::get_pid();
744 }
745
746 aid_t sg_actor_self_get_ppid()
747 {
748   return simgrid::s4u::this_actor::get_ppid();
749 }
750
751 const char* sg_actor_self_get_name()
752 {
753   return simgrid::s4u::this_actor::get_cname();
754 }
755
756 void* sg_actor_self_get_data()
757 {
758   return simgrid::s4u::Actor::self()->get_data<void>();
759 }
760
761 void sg_actor_self_set_data(void* userdata)
762 {
763   simgrid::s4u::Actor::self()->set_data(userdata);
764 }
765
766 sg_actor_t sg_actor_self()
767 {
768   return simgrid::s4u::Actor::self();
769 }
770
771 void sg_actor_execute(double flops)
772 {
773   simgrid::s4u::this_actor::execute(flops);
774 }
775 void sg_actor_execute_with_priority(double flops, double priority)
776 {
777   simgrid::s4u::this_actor::exec_init(flops)->set_priority(priority)->wait();
778 }
779
780 void sg_actor_parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
781 {
782   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
783   std::vector<double> flops;
784   std::vector<double> bytes;
785   if (flops_amount != nullptr)
786     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
787   if (bytes_amount != nullptr)
788     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
789
790   simgrid::s4u::this_actor::parallel_execute(hosts, flops, bytes);
791 }
792
793 /** @brief Take an extra reference on that actor to prevent it to be garbage-collected */
794 void sg_actor_ref(const_sg_actor_t actor)
795 {
796   intrusive_ptr_add_ref(actor);
797 }
798 /** @brief Release a reference on that actor so that it can get be garbage-collected */
799 void sg_actor_unref(const_sg_actor_t actor)
800 {
801   intrusive_ptr_release(actor);
802 }
803
804 /** @brief Return the user data of a #sg_actor_t */
805 void* sg_actor_get_data(const_sg_actor_t actor)
806 {
807   return actor->get_data<void>();
808 }
809
810 /** @brief Set the user data of a #sg_actor_t */
811 void sg_actor_set_data(sg_actor_t actor, void* userdata)
812 {
813   actor->set_data(userdata);
814 }
815
816 /** @brief Add a function to the list of "on_exit" functions for the current actor.
817  *  The on_exit functions are the functions executed when your actor is killed.
818  *  You should use them to free the data used by your actor.
819  */
820 void sg_actor_on_exit(void_f_int_pvoid_t fun, void* data)
821 {
822   simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); });
823 }
824
825 sg_exec_t sg_actor_exec_init(double computation_amount)
826 {
827   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(computation_amount);
828   exec->add_ref();
829   return exec.get();
830 }
831
832 sg_exec_t sg_actor_parallel_exec_init(int host_nb, const sg_host_t* host_list, double* flops_amount,
833                                       double* bytes_amount)
834 {
835   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
836   std::vector<double> flops;
837   std::vector<double> bytes;
838   if (flops_amount != nullptr)
839     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
840   if (bytes_amount != nullptr)
841     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
842
843   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(hosts, flops, bytes);
844   exec->add_ref();
845   return exec.get();
846 }
847
848 sg_exec_t sg_actor_exec_async(double computation_amount)
849 {
850   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_async(computation_amount);
851   exec->add_ref();
852   return exec.get();
853 }