Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add python bindings for operations
[simgrid.git] / examples / python / operation-variable-load / operation-variable-load.py
1 # Copyright (c) 2006-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 demonstrates how to create a variable load for operations.
8 We consider the following graph:
9
10 comm -> exec
11
12 With a small load each comm operation is followed by an exec operation.
13 With a heavy load there is a burst of comm before the exec operation can even finish once.
14 """
15
16 from argparse import ArgumentParser
17 import sys
18 from simgrid import Engine, Operation, CommOp, ExecOp, Actor, this_actor
19
20 def parse():
21     parser = ArgumentParser()
22     parser.add_argument(
23         '--platform',
24         type=str,
25         required=True,
26         help='path to the platform description'
27     )
28     return parser.parse_args()
29
30 def callback(op):
31     print(f'[{Engine.clock}] Operation {op} finished ({op.count})')
32
33 def variable_load(op):
34     print('--- Small load ---')
35     for i in range(3):
36         op.enqueue_execs(1)
37         this_actor.sleep_for(100)
38     this_actor.sleep_for(1000)
39     print('--- Heavy load ---')
40     for i in range(3):
41         op.enqueue_execs(1)
42         this_actor.sleep_for(1)
43
44 if __name__ == '__main__':
45     args = parse()
46     e = Engine(sys.argv)
47     e.load_platform(args.platform)
48     Operation.init()
49
50     # Retrieve hosts
51     tremblay = e.host_by_name('Tremblay')
52     jupiter = e.host_by_name('Jupiter')
53
54     # Create operations
55     comm = CommOp.init("comm", 1e7, tremblay, jupiter)
56     exec = ExecOp.init("exec", 1e9, jupiter)
57
58     # Create the graph by defining dependencies between operations
59     comm.add_successor(exec)
60
61     # Add a function to be called when operations end for log purpose
62     Operation.on_end_cb(callback)
63
64     # Create the actor that will inject load during the simulation
65     Actor.create("input", tremblay, variable_load, comm)
66
67     # runs the simulation
68     e.run()