Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
python: add exec-dvfs example
[simgrid.git] / examples / python / exec-async / exec-async.py
1 # Copyright (c) 2018-2019. 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 import sys
7 from simgrid import *
8
9
10 class Waiter:
11     """ This actor simply waits for its task completion after starting it. That's exactly equivalent to synchronous execution. """
12
13     def __call__(self):
14         computation_amount = this_actor.get_host().speed
15         this_actor.info("Execute {:.0f} flops, should take 1 second.".format(computation_amount))
16         activity = this_actor.exec_init(computation_amount)
17         activity.start()
18         activity.wait()
19
20         this_actor.info("Goodbye now!")
21
22
23 class Monitor:
24     """This actor tests the ongoing execution until its completion, and don't wait before it's terminated."""
25
26     def __call__(self):
27         computation_amount = this_actor.get_host().speed
28         this_actor.info("Execute {:.0f} flops, should take 1 second.".format(computation_amount))
29         activity = this_actor.exec_init(computation_amount).start()
30
31         while not activity.test():
32             this_actor.info("Remaining amount of flops: {:.0f} ({:.0f}%)".format(
33                 activity.remaining, 100 * activity.remaining_ratio))
34             this_actor.sleep_for(0.3)
35         activity.wait()
36
37         this_actor.info("Goodbye now!")
38
39
40 class Canceller:
41     """This actor cancels the ongoing execution after a while."""
42
43     def __call__(self):
44         computation_amount = this_actor.get_host().speed
45         this_actor.info("Execute {:.0f} flops, should take 1 second.".format(computation_amount))
46         activity = this_actor.exec_init(computation_amount).start()
47
48         this_actor.sleep_for(0.5)
49         this_actor.info("I changed my mind, cancel!")
50         activity.cancel()
51
52         this_actor.info("Goodbye now!")
53
54
55 if __name__ == '__main__':
56     e = Engine(sys.argv)
57     if len(sys.argv) < 2:
58         raise AssertionError("Usage: exec-async.py platform_file [other parameters]")
59
60     e.load_platform(sys.argv[1])
61
62     Actor.create("wait", Host.by_name("Fafard"), Waiter())
63     Actor.create("monitor", Host.by_name("Ginette"), Monitor())
64     Actor.create("cancel", Host.by_name("Boivin"), Canceller())
65
66     e.run()