Logo AND Algorithmique Numérique Distribuée

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