SimGrid  3.14.159
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 "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 "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 a process called 'master' on the host called 'Tremblay' -->
<process 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] -->
</process>
<!-- Start a process called 'worker' on the host called 'Jupiter' -->
<process host="Jupiter" function="worker"/> <!-- Don't provide any parameter ->>
</platform>

Simulation Agent

#include <Actor.hpp>

Public Member Functions

 Actor (Actor const &)=delete
 
Actoroperator= (Actor const &)=delete
 
simgrid::xbt::string getName ()
 Retrieves the actor that have the given PID (or NULL if not existing) More...
 
s4u::HostgetHost ()
 Retrieves the host on which that actor is running. More...
 
int getPid ()
 Retrieves the PID of that actor. More...
 
int getPpid ()
 Retrieves the PPID of that actor. More...
 
void setAutoRestart (bool autorestart)
 If set to true, the actor will automatically restart when its host reboots. More...
 
void setKillTime (double time)
 Sets the time at which that actor should be killed. More...
 
double getKillTime ()
 Retrieves the time at which that actor will be killed (or -1 if not set) More...
 
void kill ()
 Ask the actor to die. More...
 
void join ()
 Wait for the actor to finish. More...
 

Static Public Member Functions

static ActorPtr self ()
 Retrieve a reference to myself. More...
 
static ActorPtr createActor (const char *name, s4u::Host *host, double killTime, std::function< void()> code)
 Create an actor using a function. More...
 
static ActorPtr createActor (const char *name, s4u::Host *host, std::function< void()> code)
 
template<class F , class... Args, typename = typename std::result_of<F(Args...)>::type>
static ActorPtr createActor (const char *name, s4u::Host *host, F code, Args... args)
 Create an actor using code. More...
 
static ActorPtr createActor (const char *name, s4u::Host *host, double killTime, const char *function, std::vector< std::string > args)
 
static ActorPtr createActor (const char *name, s4u::Host *host, const char *function, std::vector< std::string > args)
 
static void kill (int pid)
 
static ActorPtr forPid (int pid)
 
static void killAll ()
 Ask kindly to all actors to die. More...
 

Protected Member Functions

smx_actor_t getImpl ()
 Returns the internal implementation of this actor. 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()

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

Retrieve a reference to myself.

◆ createActor() [1/5]

ActorPtr simgrid::s4u::Actor::createActor ( const char *  name,
s4u::Host host,
double  killTime,
std::function< void()>  code 
)
static

Create an actor using a function.

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

Examples:
examples/s4u/launching/s4u_launching.cpp, and examples/s4u/mutex/s4u_mutex.cpp.

◆ createActor() [2/5]

static ActorPtr simgrid::s4u::Actor::createActor ( const char *  name,
s4u::Host host,
std::function< void()>  code 
)
inlinestatic

◆ createActor() [3/5]

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

Create an actor using code.

Using this constructor, move-only type can be used. The consequence is that we cannot copy the value and restart the process in its initial state. In order to use auto-restart, an explicit function must be passed instead.

◆ createActor() [4/5]

ActorPtr simgrid::s4u::Actor::createActor ( const char *  name,
s4u::Host host,
double  killTime,
const char *  function,
std::vector< std::string args 
)
static

◆ createActor() [5/5]

static ActorPtr simgrid::s4u::Actor::createActor ( const char *  name,
s4u::Host host,
const char *  function,
std::vector< std::string args 
)
inlinestatic

◆ getName()

simgrid::xbt::string simgrid::s4u::Actor::getName ( )

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

Retrieves the name of that actor

◆ getHost()

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

Retrieves the host on which that actor is running.

◆ getPid()

int simgrid::s4u::Actor::getPid ( )

Retrieves the PID of that actor.

◆ getPpid()

int simgrid::s4u::Actor::getPpid ( )

Retrieves the PPID of that actor.

◆ setAutoRestart()

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

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

◆ setKillTime()

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

Sets the time at which that actor should be killed.

◆ getKillTime()

double simgrid::s4u::Actor::getKillTime ( )

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

◆ kill() [1/2]

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

Ask the actor to die.

It will only notice your request when doing a simcall next time (a communication or similar). SimGrid sometimes have issues when you kill actors that are currently communicating and such. We are working on it to fix the issues.

◆ kill() [2/2]

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

◆ forPid()

ActorPtr simgrid::s4u::Actor::forPid ( int  pid)
static

◆ join()

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

Wait for the actor to finish.

◆ killAll()

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

Ask kindly to all actors to die.

Only the issuer will survive.

◆ getImpl()

smx_actor_t simgrid::s4u::Actor::getImpl ( )
protected

Returns the internal implementation of this actor.

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

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