Logo AND Algorithmique Numérique Distribuée

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