Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / docs / source / tuto_s4u / master-workers-lab2.py
1 # Copyright (c) 2010-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 # ##################################################################################
8 # Take this tutorial online: https://simgrid.org/doc/latest/Tutorial_Algorithms.html
9 # ##################################################################################
10 """
11
12 import sys
13 from simgrid import Actor, Engine, Mailbox, this_actor
14
15 # master-begin
16 def master(*args):
17     if len(args) == 2:
18         raise AssertionError(f"Actor master requires 4 parameters, but only {len(args)}")
19     tasks_count = int(args[0])
20     compute_cost = int(args[1])
21     communicate_cost = int(args[2])
22
23     this_actor.info(f"Got {tasks_count} tasks to process")
24
25     hosts = Engine.instance.get_all_hosts()
26
27     for h in hosts:
28         Actor.create(f'Worker-{h.name}', h, worker)
29
30     for i in range(tasks_count): # For each task to be executed:
31         # - Select a worker in a round-robin way
32         mailbox = Mailbox.by_name(f'Worker-{hosts[i%len(hosts)].name}')
33
34         # - Send the computation amount to the worker
35         if (tasks_count < 10000 or (tasks_count < 100000 and i % 10000 == 0) or i % 100000 == 0):
36             this_actor.info(f"Sending task {i} of {tasks_count} to mailbox '{mailbox.name}'")
37         mailbox.put(compute_cost, communicate_cost)
38
39     this_actor.info("All tasks have been dispatched. Request all workers to stop.")
40     for h in hosts:
41         # The workers stop when receiving a negative compute_cost
42         mailbox = Mailbox.by_name(f'Worker-{h.name}')
43         mailbox.put(-1, 0)
44 # master-end
45
46 # worker-begin
47 def worker(*args):
48     assert not args, "The worker expects no argument"
49
50     mailbox = Mailbox.by_name(f'Worker-{this_actor.get_host().name}')
51     done = False
52     while not done:
53         compute_cost = mailbox.get()
54         if compute_cost > 0: # If compute_cost is valid, execute a computation of that cost
55             this_actor.execute(compute_cost)
56         else: # Stop when receiving an invalid compute_cost
57             done = True
58
59     this_actor.info("Exiting now.")
60 # worker-end
61
62 # main-begin
63 if __name__ == '__main__':
64     assert len(sys.argv) > 2, f"Usage: python app-masterworkers.py platform_file deployment_file"
65
66     e = Engine(sys.argv)
67
68     # Register the classes representing the actors
69     e.register_actor("master", master)
70     e.register_actor("worker", worker)
71
72     # Load the platform description and then deploy the application
73     e.load_platform(sys.argv[1])
74     e.load_deployment(sys.argv[2])
75
76     # Run the simulation
77     e.run()
78
79     this_actor.info("Simulation is over")
80 # main-end