1 # Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.
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.
7 This example demonstrates how to dynamically modify a graph of tasks.
9 Assuming we have two instances of a service placed on different hosts,
10 we want to send data alternatively to thoses instances.
12 We consider the following graph:
15 ┌────────────────────────┐
27 └────────────────────────┘
32 from argparse import ArgumentParser
34 from simgrid import Engine, Task, CommTask, ExecTask
37 parser = ArgumentParser()
42 help='path to the platform description'
44 return parser.parse_args()
47 print(f'[{Engine.clock}] {t} finished ({t.count})')
49 def switch(t, hosts, execs):
50 comm0.destination = hosts[t.count % 2]
51 comm0.remove_successor(execs[t.count % 2 - 1])
52 comm0.add_successor(execs[t.count % 2])
54 if __name__ == '__main__':
57 e.load_platform(args.platform)
60 tremblay = e.host_by_name('Tremblay')
61 jupiter = e.host_by_name('Jupiter')
62 fafard = e.host_by_name('Fafard')
65 comm0 = CommTask.init("comm0")
67 comm0.source = tremblay
68 exec1 = ExecTask.init("exec1", 1e9, jupiter)
69 exec2 = ExecTask.init("exec2", 1e9, fafard)
70 comm1 = CommTask.init("comm1", 1e7, jupiter, tremblay)
71 comm2 = CommTask.init("comm2", 1e7, fafard, tremblay)
73 # Create the initial graph by defining dependencies between tasks
74 exec1.add_successor(comm1)
75 exec2.add_successor(comm2)
77 # Add a function to be called when tasks end for log purpose
78 Task.on_completion_cb(callback)
80 # Add a function to be called before each firing of comm0
81 # This function modifies the graph of tasks by adding or removing
83 comm0.on_this_start_cb(lambda t: switch(t, [jupiter, fafard], [exec1,exec2]))
85 # Enqueue two firings for task exec1
86 comm0.enqueue_firings(4)