Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fcd36ecc9b38c625b475abc748db0e0ad4b9d945
[simgrid.git] / examples / python / operation-simple / operation-simple.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 basic use of the operation plugin.
8 We model the following graph:
9
10 exec1 -> comm -> exec2
11
12 exec1 and exec2 are execution operations.
13 comm is a communication operation.
14 """
15
16 from argparse import ArgumentParser
17 import sys
18 from simgrid import Engine, Operation, CommOp, ExecOp
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 if __name__ == '__main__':
34     args = parse()
35     e = Engine(sys.argv)
36     e.load_platform(args.platform)
37     Operation.init()
38
39     # Retrieve hosts
40     tremblay = e.host_by_name('Tremblay')
41     jupiter = e.host_by_name('Jupiter')
42
43     # Create operations
44     exec1 = ExecOp.init("exec1", 1e9, tremblay)
45     exec2 = ExecOp.init("exec2", 1e9, jupiter)
46     comm = CommOp.init("comm", 1e7, tremblay, jupiter)
47
48     # Create the graph by defining dependencies between operations
49     exec1.add_successor(comm)
50     comm.add_successor(exec2)
51
52     # Add a function to be called when operations end for log purpose
53     Operation.on_end_cb(callback)
54
55     # Enqueue two executions for operation exec1
56     exec1.enqueue_execs(2)
57
58     # runs the simulation
59     e.run()
60