Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b74c41169c1f0ed8347afb97fae2ac8c9d5667b3
[simgrid.git] / examples / python / task-io / task-io.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 from argparse import ArgumentParser
7 import sys
8 from simgrid import Engine, Task, ExecTask, IoTask, IoOpType
9
10 def parse():
11     parser = ArgumentParser()
12     parser.add_argument(
13         '--platform',
14         type=str,
15         required=True,
16         help='path to the platform description'
17     )
18     return parser.parse_args()
19
20 def callback(t):
21     print(f'[{Engine.clock}] {t} finished ({t.count})')
22
23 if __name__ == '__main__':
24     args = parse()
25     e = Engine(sys.argv)
26     e.load_platform(args.platform)
27     Task.init()
28
29     # Retrieve hosts
30     bob = e.host_by_name('bob')
31     carl = e.host_by_name('carl')
32
33     # Create tasks
34     exec1 = ExecTask.init("exec1", 1e9, bob)
35     exec2 = ExecTask.init("exec2", 1e9, carl)
36     write = IoTask.init("write", 1e7, bob.disks[0], IoOpType.WRITE)
37     read = IoTask.init("read", 1e7, carl.disks[0], IoOpType.READ)
38
39    # Create the graph by defining dependencies between tasks
40     exec1.add_successor(write)
41     write.add_successor(read)
42     read.add_successor(exec2)
43
44     # Add a function to be called when tasks end for log purpose
45     Task.on_end_cb(callback)
46
47     # Enqueue two executions for task exec1
48     exec1.enqueue_execs(2)
49
50     # runs the simulation
51     e.run()