SimGrid  3.21
Versatile Simulation of Distributed Systems
simgrid::s4u::Actor Class Reference

Detailed Description

An actor is an independent stream of execution in your distributed application.

You can think of an actor as a process in your distributed application, or as a thread in a multithreaded program. This is the only component in SimGrid that actually does something on its own, executing its own code. A resource will not get used if you don't schedule activities on them. This is the code of Actors that create and schedule these activities.

An actor is located on a (simulated) host, but it can interact with the whole simulated platform.

The s4u::Actor API is strongly inspired from the C++11 threads. The documentation of this standard may help to understand the philosophy of the S4U Actors.

Defining the skeleton of an Actor

As in the C++11 standard, you can declare the code of your actor either as a pure function or as an object. It is very simple with functions:

#include <simgrid/s4u/actor.hpp>
// Declare the code of your worker
void worker() {
printf("Hello s4u");
simgrid::s4u::this_actor::execute(5*1024*1024); // Get the worker executing a task of 5 MFlops
};
// From your main or from another actor, create your actor on the host Jupiter
// The following line actually creates a new actor, even if there is no "new".
Actor("Alice", simgrid::s4u::Host::by_name("Jupiter"), worker);

But some people prefer to encapsulate their actors in classes and objects to save the actor state in a cleanly dedicated location. The syntax is slightly more complicated, but not much.

#include <simgrid/s4u/actor.hpp>
// Declare the class representing your actors
class Worker {
public:
void operator()() { // Two pairs of () because this defines the method called ()
printf("Hello s4u");
simgrid::s4u::this_actor::execute(5*1024*1024); // Get the worker executing a task of 5 MFlops
}
};
// From your main or from another actor, create your actor. Note the () after Worker
Actor("Bob", simgrid::s4u::Host::by_name("Jupiter"), Worker());

Fleshing your actor

The body of your actor can use the functions of the simgrid::s4u::this_actor namespace to interact with the world. This namespace contains the methods to start new activities (executions, communications, etc), and to get informations about the currently running thread (its location, etc).

Please refer to the full API .

Using a deployment file

Warning
This is currently not working with S4U. Sorry about that.

The best practice is to use an external deployment file as follows, because it makes it easier to test your application in differing settings. Load this file with s4u::Engine::loadDeployment() before the simulation starts. Refer to the Deploy the simulation section for more information.

<?xml version='1.0'?>
<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
<platform version="4">
<!-- Start an actor called 'master' on the host called 'Tremblay' -->
<actor host="Tremblay" function="master">
<!-- Here come the parameter that you want to feed to this instance of master -->
<argument value="20"/> <!-- argv[1] -->
<argument value="50000000"/> <!-- argv[2] -->
<argument value="1000000"/> <!-- argv[3] -->
<argument value="5"/> <!-- argv[4] -->
</actor>
<!-- Start an actor called 'worker' on the host called 'Jupiter' -->
<actor host="Jupiter" function="worker"/> <!-- Don't provide any parameter ->>
</platform>

Simulation Agent

#include <Actor.hpp>

Inherits simgrid::xbt::Extendable< Actor >.

Public Member Functions

 Actor (Actor const &)=delete
 
Actoroperator= (Actor const &)=delete
 
void daemonize ()
 This actor will be automatically terminated when the last non-daemon actor finishes. More...
 
bool is_daemon () const
 Returns whether or not this actor has been daemonized or not. More...
 
const simgrid::xbt::stringget_name () const
 Retrieves the name of that actor as a C++ string. More...
 
const char * get_cname () const
 Retrieves the name of that actor as a C string. More...
 
s4u::Hostget_host ()
 Retrieves the host on which that actor is running. More...
 
aid_t get_pid () const
 Retrieves the PID of that actor. More...
 
aid_t get_ppid () const
 Retrieves the PPID of that actor. More...
 
void suspend ()
 Suspend an actor by suspending the task on which it was waiting for the completion. More...
 
void resume ()
 Resume a suspended actor by resuming the task on which it was waiting for the completion. More...
 
void yield ()
 
bool is_suspended ()
 Returns true if the actor is suspended. More...
 
void set_auto_restart (bool autorestart)
 If set to true, the actor will automatically restart when its host reboots. More...
 
void on_exit (std::function< void(int, void *)> fun, void *data)
 Add a function to the list of "on_exit" functions for the current actor. More...
 
void set_kill_time (double time)
 Sets the time at which that actor should be killed. More...
 
double get_kill_time ()
 Retrieves the time at which that actor will be killed (or -1 if not set) More...
 
void migrate (Host *new_host)
 Moves the actor to another host. More...
 
void kill ()
 Ask the actor to die. More...
 
void join ()
 Wait for the actor to finish. More...
 
void join (double timeout)
 
Actorrestart ()
 
kernel::actor::ActorImpl * get_impl ()
 Returns the internal implementation of this actor. More...
 
std::unordered_map< std::string, std::string > * get_properties ()
 Retrieve the property value (or nullptr if not set) More...
 
const char * get_property (std::string key)
 
void set_property (std::string key, std::string value)
 
- Public Member Functions inherited from simgrid::xbt::Extendable< Actor >
 Extendable ()
 
 ~Extendable ()
 
void * extension (std::size_t rank)
 
U * extension (Extension< Actor, U > rank)
 
U * extension ()
 
void extension_set (std::size_t rank, void *value, bool use_dtor=true)
 
void extension_set (Extension< Actor, U > rank, U *value, bool use_dtor=true)
 
void extension_set (U *p)
 

Static Public Member Functions

static ActorPtr self ()
 Retrieve a reference to myself. More...
 
static ActorPtr create (std::string name, s4u::Host *host, std::function< void()> code)
 Create an actor from a std::function<void()> More...
 
template<class F >
static ActorPtr create (std::string name, s4u::Host *host, F code)
 Create an actor from a std::function. More...
 
template<class F , class... Args, typename = typename std::result_of<F(Args...)>::type>
static ActorPtr create (std::string name, s4u::Host *host, F code, Args... args)
 Create an actor using a callable thing and its arguments. More...
 
static ActorPtr create (std::string name, s4u::Host *host, std::string function, std::vector< std::string > args)
 
static void kill (aid_t pid)
 Kill an actor from its ID. More...
 
static ActorPtr by_pid (aid_t pid)
 Retrieves the actor that have the given PID (or nullptr if not existing) More...
 
static void kill_all ()
 Ask kindly to all actors to die. More...
 
- Static Public Member Functions inherited from simgrid::xbt::Extendable< Actor >
static size_t extension_create (void(*deleter)(void *))
 
static Extension< Actor, U > extension_create (void(*deleter)(void *))
 
static Extension< Actor, U > extension_create ()
 

Static Public Attributes

static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_creation
 Signal to others that a new actor has been created. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_suspend
 Signal to others that an actor has been suspended. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_resume
 Signal to others that an actor has been resumed. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_sleep
 Signal to others that an actor is sleeping. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_wake_up
 Signal to others that an actor wakes up for a sleep. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_migration_start
 Signal to others that an actor is going to migrated to another host. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_migration_end
 Signal to others that an actor is has been migrated to another host. More...
 
static simgrid::xbt::signal< void(simgrid::s4u::ActorPtr)> on_destruction
 Signal indicating that an actor is about to disappear. More...
 

Friends

void intrusive_ptr_add_ref (Actor *actor)
 
void intrusive_ptr_release (Actor *actor)
 

Constructor & Destructor Documentation

◆ Actor()

simgrid::s4u::Actor::Actor ( Actor const &  )
delete

Member Function Documentation

◆ operator=()

Actor& simgrid::s4u::Actor::operator= ( Actor const &  )
delete

◆ self()

static ActorPtr simgrid::s4u::Actor::self ( )
static

Retrieve a reference to myself.

◆ create() [1/4]

static ActorPtr simgrid::s4u::Actor::create ( std::string  name,
s4u::Host host,
std::function< void()>  code 
)
static

Create an actor from a std::function<void()>

If the actor is restarted, the actor has a fresh copy of the function.

◆ create() [2/4]

template<class F >
static ActorPtr simgrid::s4u::Actor::create ( std::string  name,
s4u::Host host,
code 
)
inlinestatic

Create an actor from a std::function.

If the actor is restarted, the actor has a fresh copy of the function.

◆ create() [3/4]

template<class F , class... Args, typename = typename std::result_of<F(Args...)>::type>
static ActorPtr simgrid::s4u::Actor::create ( std::string  name,
s4u::Host host,
code,
Args...  args 
)
inlinestatic

Create an actor using a callable thing and its arguments.

Note that the arguments will be copied, so move-only parameters are forbidden

◆ create() [4/4]

static ActorPtr simgrid::s4u::Actor::create ( std::string  name,
s4u::Host host,
std::string  function,
std::vector< std::string >  args 
)
static

◆ daemonize()

void simgrid::s4u::Actor::daemonize ( )

This actor will be automatically terminated when the last non-daemon actor finishes.

◆ is_daemon()

bool simgrid::s4u::Actor::is_daemon ( ) const

Returns whether or not this actor has been daemonized or not.

◆ get_name()

const simgrid::xbt::string& simgrid::s4u::Actor::get_name ( ) const

Retrieves the name of that actor as a C++ string.

◆ get_cname()

const char* simgrid::s4u::Actor::get_cname ( ) const

Retrieves the name of that actor as a C string.

◆ get_host()

s4u::Host* simgrid::s4u::Actor::get_host ( )

Retrieves the host on which that actor is running.

◆ get_pid()

aid_t simgrid::s4u::Actor::get_pid ( ) const

Retrieves the PID of that actor.

aid_t is an alias for long

◆ get_ppid()

aid_t simgrid::s4u::Actor::get_ppid ( ) const

Retrieves the PPID of that actor.

aid_t is an alias for long

◆ suspend()

void simgrid::s4u::Actor::suspend ( )

Suspend an actor by suspending the task on which it was waiting for the completion.

◆ resume()

void simgrid::s4u::Actor::resume ( )

Resume a suspended actor by resuming the task on which it was waiting for the completion.

◆ yield()

void simgrid::s4u::Actor::yield ( )

◆ is_suspended()

bool simgrid::s4u::Actor::is_suspended ( )

Returns true if the actor is suspended.

◆ set_auto_restart()

void simgrid::s4u::Actor::set_auto_restart ( bool  autorestart)

If set to true, the actor will automatically restart when its host reboots.

◆ on_exit()

void simgrid::s4u::Actor::on_exit ( std::function< void(int, void *)>  fun,
void *  data 
)

Add a function to the list of "on_exit" functions for the current actor.

The on_exit functions are the functions executed when your actor is killed. You should use them to free the data used by your actor.

◆ set_kill_time()

void simgrid::s4u::Actor::set_kill_time ( double  time)

Sets the time at which that actor should be killed.

◆ get_kill_time()

double simgrid::s4u::Actor::get_kill_time ( )

Retrieves the time at which that actor will be killed (or -1 if not set)

◆ migrate()

void simgrid::s4u::Actor::migrate ( Host new_host)

Moves the actor to another host.

If the actor is currently blocked on an execution activity, the activity is also migrated to the new host. If it's blocked on another kind of activity, an error is raised as the mandated code is not written yet. Please report that bug if you need it.

Asynchronous activities started by the actor are not migrated automatically, so you have to take care of this yourself (only you knows which ones should be migrated).

◆ kill() [1/2]

void simgrid::s4u::Actor::kill ( )

Ask the actor to die.

Any blocking activity will be canceled, and it will be rescheduled to free its memory. Being killed is not something that actors can defer or avoid.

SimGrid still have sometimes issues when you kill actors that are currently communicating and such. Still. Please report any bug that you may encounter with a minimal working example.

◆ kill() [2/2]

static void simgrid::s4u::Actor::kill ( aid_t  pid)
static

Kill an actor from its ID.

◆ by_pid()

static ActorPtr simgrid::s4u::Actor::by_pid ( aid_t  pid)
static

Retrieves the actor that have the given PID (or nullptr if not existing)

◆ join() [1/2]

void simgrid::s4u::Actor::join ( )

Wait for the actor to finish.

This blocks the calling actor until the actor on which we call join() is terminated

◆ join() [2/2]

void simgrid::s4u::Actor::join ( double  timeout)

◆ restart()

Actor* simgrid::s4u::Actor::restart ( )

◆ kill_all()

static void simgrid::s4u::Actor::kill_all ( )
static

Ask kindly to all actors to die.

Only the issuer will survive.

◆ get_impl()

kernel::actor::ActorImpl* simgrid::s4u::Actor::get_impl ( )

Returns the internal implementation of this actor.

◆ get_properties()

std::unordered_map<std::string, std::string>* simgrid::s4u::Actor::get_properties ( )

Retrieve the property value (or nullptr if not set)

◆ get_property()

const char* simgrid::s4u::Actor::get_property ( std::string  key)

◆ set_property()

void simgrid::s4u::Actor::set_property ( std::string  key,
std::string  value 
)

Friends And Related Function Documentation

◆ intrusive_ptr_add_ref

void intrusive_ptr_add_ref ( Actor actor)
friend

◆ intrusive_ptr_release

void intrusive_ptr_release ( Actor actor)
friend

Member Data Documentation

◆ on_creation

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_creation
static

Signal to others that a new actor has been created.

◆ on_suspend

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_suspend
static

Signal to others that an actor has been suspended.

◆ on_resume

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_resume
static

Signal to others that an actor has been resumed.

◆ on_sleep

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_sleep
static

Signal to others that an actor is sleeping.

◆ on_wake_up

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_wake_up
static

Signal to others that an actor wakes up for a sleep.

◆ on_migration_start

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_migration_start
static

Signal to others that an actor is going to migrated to another host.

◆ on_migration_end

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_migration_end
static

Signal to others that an actor is has been migrated to another host.

◆ on_destruction

simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> simgrid::s4u::Actor::on_destruction
static

Signal indicating that an actor is about to disappear.

This signal is fired for any dying actor, which is mostly useful when designing plugins and extensions. If you want to register to the termination of a given actor, use this_actor::on_exit() instead.


The documentation for this class was generated from the following file: