Logo AND Algorithmique Numérique Distribuée

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