Logo AND Algorithmique Numérique Distribuée

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