Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2bc05b0783c34b7588b549982dd5455ea912e9d5
[simgrid.git] / examples / python / actor-create / actor-create.py
1 # Copyright (c) 2006-2018. 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 # This example shows how to declare and start your actors.
7 #
8 # The first step is to declare the code of your actors (what they do exactly does not matter to this example) and then
9 # you ask SimGrid to start your actors. There is three ways of doing so:
10 # - Directly, by instantiating your actor as paramter to Actor::create()
11 # - By first registering your actors before instantiating it;
12 # - Through the deployment file.
13 #
14 # This example shows all these solutions, even if you obviously should use only one of these solutions to start your
15 # actors. The most advised solution is to use a deployment file, as it creates a clear separation between your
16 # application and the settings to test it. This is a better scientific methodology. Actually, starting an actor with
17 # Actor.create() is mostly useful to start an actor from another actor.
18
19 import simgrid, sys
20
21 # Our first class of actors is simply implemented with a function, that takes a single string as parameter.
22 #
23 # Later, this actor class is instantiated within the simulation.
24 def receiver(mailbox_name):
25   mailbox = simgrid.Mailbox.by_name(mailbox_name)
26
27   simgrid.info("Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.get_name()))
28
29   msg1 = mailbox.get()
30   msg2 = mailbox.get()
31   msg3 = mailbox.get()
32   simgrid.info("I received '{:s}', '{:s}' and '{:s}'".format(msg1, msg2, msg3))
33   simgrid.info("I'm done. See you.")
34
35 # Our second class of actors is also a function
36 def forwarder(*args):
37   if len(args) < 2: raise AssertionError("Actor forwarder requires 2 parameters, but got only {:d}".format(len(args)))
38   mb_in  = simgrid.Mailbox.by_name(args[0])
39   mb_out = simgrid.Mailbox.by_name(args[1])
40   
41   msg = mb_in.get()
42   simgrid.info("Forward '{:s}'.".format(msg))
43   mb_out.put(msg, len(msg))
44
45 # Declares a third class of actors which sends a message to the mailbox 'mb42'.
46 # The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default)
47 #
48 # Later, this actor class is instantiated twice in the simulation.
49 class Sender:
50   mbox  = "mb42"
51   msg = "GaBuZoMeu";
52   def __init__(self, *args):
53       if len(args) > 0: self.msg  = args[0];
54       if len(args) > 1: self.mbox = args[1];
55       if len(args) > 2: raise AssertionError("Actor sender requires 2 parameters, but got only {:d}".format(len(args)))
56
57   def __call__(self):
58       simgrid.info("Hello s4u, I have something to send")
59       mailbox = simgrid.Mailbox.by_name(self.mbox)
60
61       mailbox.put(self.msg, len(self.msg))
62       simgrid.info("I'm done. See you.")
63
64 # Here comes the main function of your program
65 if __name__ == '__main__':
66   # When your program starts, you have to first start a new simulation engine, as follows
67   e = simgrid.Engine(sys.argv)
68
69   # Then you should load a platform file, describing your simulated platform
70   e.load_platform("../platforms/small_platform.xml");
71
72   # And now you have to ask SimGrid to actually start your actors.
73   #
74   # The easiest way to do so is to implement the behavior of your actor in a single function,
75   # as we do here for the receiver actors. This function can take any kind of parameters, as
76   # long as the last parameters of Actor::create() match what your function expects.
77   simgrid.Actor.create("receiver", simgrid.Host.by_name("Fafard"), receiver, "mb42")
78
79   # If your actor is getting more complex, you probably want to implement it as a class instead,
80   # as we do here for the sender actors. The main behavior goes into operator()() of the class.
81   #
82   # You can then directly start your actor, as follows:
83   simgrid.Actor.create("sender1", simgrid.Host.by_name("Tremblay"), Sender())
84   # If you want to pass parameters to your class, that's very easy: just use your constructors
85   simgrid.Actor.create("sender2", simgrid.Host.by_name("Jupiter"), Sender("GloubiBoulga"));
86
87   # But starting actors directly is considered as a bad experimental habit, since it ties the code
88   # you want to test with the experimental scenario. Starting your actors from an external deployment
89   # file in XML ensures that you can test your code in several scenarios without changing the code itself.
90   #
91   # For that, you first need to register your function or your actor as follows.
92   e.register_actor("sender", Sender)
93   e.register_actor("forwarder", forwarder)
94   # Once actors and functions are registered, just load the deployment file 
95   e.load_deployment("actor-create/actor-create_d.xml")
96
97   # Once every actors are started in the engine, the simulation can start
98   e.run();
99
100   # Once the simulation is done, the program is ended