Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert the MasterWorker to python, in preparation to the tutorial PyConvertion
[simgrid.git] / examples / python / app-masterworkers / app-masterworkers.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 # Take this tutorial online: https://simgrid.org/doc/latest/Tutorial_Algorithms.html
8 # ##################################################################################
9
10 from simgrid import Actor, Engine, Host, Mailbox, this_actor
11 import sys
12
13 def master(*args):
14   if len(args) < 2:
15     raise AssertionError(
16             f"Actor master requires 3 parameters plus the workers' names, but got only {len(args)}")
17   tasks_count = int(args[0])
18   compute_cost = int(args[1])
19   communicate_cost = int(args[2])
20   workers = []
21   for i in range(3, len(args)): 
22     workers.append(Mailbox.by_name(args[i]))
23   this_actor.info(f"Got {len(workers)} 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 = workers[i % len(workers)]
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 (len(workers)):
36       # The workers stop when receiving a negative compute_cost
37       mailbox = workers[i]
38       mailbox.put(-1, 0)
39
40 def worker(*args):
41   assert len(args) == 0, "The worker expects to not get any argument"
42
43   mailbox = Mailbox.by_name(this_actor.get_host().name)
44   done = False
45   while not done:
46     compute_cost = mailbox.get()
47     if compute_cost > 0: # If compute_cost is valid, execute a computation of that cost 
48       this_actor.execute(compute_cost)
49     else: # Stop when receiving an invalid compute_cost
50       done = True
51
52   this_actor.info("Exiting now.")
53
54 if __name__ == '__main__':
55     assert len(sys.argv) > 2, f"Usage: python app-masterworkers.py platform_file deployment_file"
56
57     e = Engine(sys.argv)
58
59     # Register the classes representing the actors
60     e.register_actor("master", master)
61     e.register_actor("worker", worker)
62
63     # Load the platform description and then deploy the application
64     e.load_platform(sys.argv[1]) 
65     e.load_deployment(sys.argv[2])
66
67     # Run the simulation
68     e.run()
69
70     this_actor.info("Simulation is over")