Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
73105847cecfb8e05b70af76a263b0287827c632
[simgrid.git] / examples / python / task-variable-load / task-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 tasks.
8 We consider the following graph:
9
10 comm -> exec
11
12 With a small load each comm task is followed by an exec task.
13 With a heavy load there is a burst of comm before the exec task can even finish once.
14 """
15
16 from argparse import ArgumentParser
17 import sys
18 from simgrid import Engine, Task, CommTask, ExecTask, 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(t):
31     print(f'[{Engine.clock}] {t} finished ({t.count})')
32
33 def variable_load(t):
34     print('--- Small load ---')
35     for _ in range(3):
36         t.enqueue_execs(1)
37         this_actor.sleep_for(100)
38     this_actor.sleep_for(1000)
39     print('--- Heavy load ---')
40     for _ in range(3):
41         t.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     Task.init()
49
50     # Retrieve hosts
51     tremblay = e.host_by_name('Tremblay')
52     jupiter = e.host_by_name('Jupiter')
53
54     # Create tasks
55     comm = CommTask.init("comm", 1e7, tremblay, jupiter)
56     exec = ExecTask.init("exec", 1e9, jupiter)
57
58     # Create the graph by defining dependencies between tasks
59     comm.add_successor(exec)
60
61     # Add a function to be called when tasks end for log purpose
62     Task.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()