Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pylint docs/source/tuto_s4u/*.py.
[simgrid.git] / docs / source / tuto_s4u / master-workers-lab1.py
1 # Copyright (c) 2010-2022. 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 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     worker_count = int(args[0])
20     tasks_count = int(args[1])
21     compute_cost = int(args[2])
22     communicate_cost = int(args[3])
23     this_actor.info(f"Got {worker_count} workers and {tasks_count} tasks to process")
24
25     for i in range(tasks_count): # For each task to be executed:
26         # - Select a worker in a round-robin way
27         mailbox = Mailbox.by_name(str(i % worker_count))
28
29         # - Send the computation amount to the worker
30         if (tasks_count < 10000 or (tasks_count < 100000 and i % 10000 == 0) or i % 100000 == 0):
31             this_actor.info(f"Sending task {i} of {tasks_count} to mailbox '{mailbox.name}'")
32         mailbox.put(compute_cost, communicate_cost)
33
34     this_actor.info("All tasks have been dispatched. Request all workers to stop.")
35     for i in range(worker_count):
36         # The workers stop when receiving a negative compute_cost
37         mailbox = Mailbox.by_name(str(i))
38         mailbox.put(-1, 0)
39 # master-end
40
41 # worker-begin
42 def worker(*args):
43     assert len(args) == 1, "The worker expects one argument"
44
45     mailbox = Mailbox.by_name(args[0])
46     done = False
47     while not done:
48         compute_cost = mailbox.get()
49         if compute_cost > 0: # If compute_cost is valid, execute a computation of that cost
50             this_actor.execute(compute_cost)
51         else: # Stop when receiving an invalid compute_cost
52             done = True
53
54     this_actor.info("Exiting now.")
55 # worker-end
56
57 # main-begin
58 if __name__ == '__main__':
59     assert len(sys.argv) > 2, f"Usage: python app-masterworkers.py platform_file deployment_file"
60
61     e = Engine(sys.argv)
62
63     # Register the classes representing the actors
64     e.register_actor("master", master)
65     e.register_actor("worker", worker)
66
67     # Load the platform description and then deploy the application
68     e.load_platform(sys.argv[1])
69     e.load_deployment(sys.argv[2])
70
71     # Run the simulation
72     e.run()
73
74     this_actor.info("Simulation is over")
75 # main-end