Logo AND Algorithmique Numérique Distribuée

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