class Receiver: def __init__(self, *args): if len(args) != 1: # Receiver actor expects 1 argument: its ID raise AssertionError( "Actor receiver requires 1 parameter, but got {:d}".format(len(args))) self.mbox = Mailbox.by_name("receiver-{:s}".format(args[0])) def __call__(self): this_actor.info("Wait for my first message") while True: received = self.mbox.get() this_actor.info("I got a '{:s}'.".format(received)) if received == "finalize": break # If it's a finalize message, we're done. if __name__ == '__main__': e = Engine(sys.argv) # Load the platform description e.load_platform(sys.argv[1]) # Register the classes representing the actors e.register_actor("sender", Sender) e.register_actor("receiver", Receiver) e.load_deployment(sys.argv[2]) e.run()
from simgrid import Engine, this_actor import sys class Sleeper: """This actor just sleeps until termination""" def __init__(self, *args): # sys.exit(1); simgrid.info("Exiting now (done sleeping or got killed).")) this_actor.on_exit(lambda: print("BAAA")) def __call__(self): this_actor.info("Hello! I go to sleep.") this_actor.sleep_for(10) this_actor.info("Done sleeping.") if __name__ == '__main__': e = Engine(sys.argv) if len(sys.argv) < 2: raise AssertionError( "Usage: actor-lifetime.py platform_file actor-lifetime_d.xml [other parameters]" ) e.load_platform(sys.argv[1]) # Load the platform description e.register_actor("sleeper", Sleeper) # Deploy the sleeper processes with explicit start/kill times e.load_deployment(sys.argv[2]) e.run()
# This serves as an example for the simgrid.yield() function, with which an actor can request # to be rescheduled after the other actor that are ready at the current timestamp. # # It can also be used to benchmark our context-switching mechanism. class Yielder: """Main function of the Yielder process""" number_of_yields = 0 def __init__(self, *args): self.number_of_yields = int(args[0]) def __call__(self): for _ in range(self.number_of_yields): this_actor.yield_() this_actor.info("I yielded {:d} times. Goodbye now!".format( self.number_of_yields)) if __name__ == '__main__': e = Engine(sys.argv) e.load_platform(sys.argv[1]) # Load the platform description # Register the class representing the actors e.register_actor("yielder", Yielder) e.load_deployment(sys.argv[2]) e.run() # - Run the simulation
import sys from simgrid import Engine, this_actor def sender(): this_actor.sleep_for(3) this_actor.info("Goodbye now!") def receiver(): this_actor.sleep_for(5) this_actor.info("Five seconds elapsed") if __name__ == '__main__': e = Engine(sys.argv) e.load_platform(sys.argv[1]) # Load the platform description # Register the classes representing the actors e.register_actor("sender", sender) e.register_actor("receiver", receiver) e.load_deployment(sys.argv[2]) e.run() this_actor.info("Dummy import...") import gc gc.collect() this_actor.info("done.")
# And now you have to ask SimGrid to actually start your actors. # # The easiest way to do so is to implement the behavior of your actor in a single function, # as we do here for the receiver actors. This function can take any kind of parameters, as # long as the last parameters of Actor::create() match what your function expects. Actor.create("receiver", Host.by_name("Fafard"), receiver, "mb42") # If your actor is getting more complex, you probably want to implement it as a class instead, # as we do here for the sender actors. The main behavior goes into operator()() of the class. # # You can then directly start your actor, as follows: Actor.create("sender1", Host.by_name("Tremblay"), Sender()) # If you want to pass parameters to your class, that's very easy: just use your constructors Actor.create("sender2", Host.by_name("Jupiter"), Sender("GloubiBoulga")) # But starting actors directly is considered as a bad experimental habit, since it ties the code # you want to test with the experimental scenario. Starting your actors from an external deployment # file in XML ensures that you can test your code in several scenarios without changing the code itself. # # For that, you first need to register your function or your actor as follows. e.register_actor("sender", Sender) e.register_actor("forwarder", forwarder) # Once actors and functions are registered, just load the deployment file e.load_deployment("actor-create_d.xml") # Once every actors are started in the engine, the simulation can start e.run() # Once the simulation is done, the program is ended
do_sleep1(int(i / 2), dur) this_actor.info("5-Done ({:d})".format(i)) def sleeper1(): do_sleep1(16, 1) def sleeper3(): do_sleep3(6, 3) def sleeper5(): do_sleep5(4, 5) if __name__ == '__main__': e = Engine(sys.argv) e.load_platform(sys.argv[1]) # Load the platform description # Register the classes representing the actors e.register_actor("sleeper1", sleeper1) e.register_actor("sleeper3", sleeper3) e.register_actor("sleeper5", sleeper5) e.load_deployment(sys.argv[2]) e.run() this_actor.info("Finalize!")