Logo AND Algorithmique Numérique Distribuée

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