Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / python / actor-migrate / actor-migrate.py
1 # Copyright (c) 2017-2023. 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 """
7 This example demonstrate the actor migrations.
8
9 The worker actor first move by itself, and then start an execution.
10 During that execution, the monitor migrates the worker, that wakes up on another host.
11 The execution was of the right amount of flops to take exactly 5 seconds on the first host
12 and 5 other seconds on the second one, so it stops after 10 seconds.
13
14 Then another migration is done by the monitor while the worker is suspended.
15
16 Note that worker() takes an uncommon set of parameters,
17 and that this is perfectly accepted by create().
18 """
19
20 import sys
21 from simgrid import Actor, Engine, Host, this_actor
22
23
24 def worker(first_host, second_host):
25     flop_amount = first_host.speed * 5 + second_host.speed * 5
26
27     this_actor.info("Let's move to {:s} to execute {:.2f} Mflops (5sec on {:s} and 5sec on {:s})".format(
28         first_host.name, flop_amount / 1e6, first_host.name, second_host.name))
29
30     this_actor.set_host(first_host)
31     this_actor.execute(flop_amount)
32
33     this_actor.info("I wake up on {:s}. Let's suspend a bit".format(
34         this_actor.get_host().name))
35
36     this_actor.suspend()
37
38     this_actor.info("I wake up on {:s}".format(this_actor.get_host().name))
39     this_actor.info("Done")
40
41
42 def monitor():
43     boivin = Host.by_name("Boivin")
44     jacquelin = Host.by_name("Jacquelin")
45     fafard = Host.by_name("Fafard")
46
47     actor = Actor.create("worker", fafard, worker, boivin, jacquelin)
48
49     this_actor.sleep_for(5)
50
51     this_actor.info(
52         "After 5 seconds, move the actor to {:s}".format(jacquelin.name))
53     actor.host = jacquelin
54
55     this_actor.sleep_until(15)
56     this_actor.info(
57         "At t=15, move the actor to {:s} and resume it.".format(fafard.name))
58     actor.host = fafard
59     actor.resume()
60
61
62 if __name__ == '__main__':
63     e = Engine(sys.argv)
64     if len(sys.argv) < 2:
65         raise AssertionError(
66             "Usage: actor-migration.py platform_file [other parameters]")
67     e.load_platform(sys.argv[1])
68
69     Actor.create("monitor", Host.by_name("Boivin"), monitor)
70     e.run()