Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1bc5891a0c24885ff89c9db23e84fdee8b64f6a1
[simgrid.git] / examples / python / async-waitany / async-waitany.py
1 # Copyright (c) 2010-2019. 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 import sys
7 from simgrid import *
8
9 # This example shows how to block on the completion of a set of communications.
10 #
11 # As for the other asynchronous examples, the sender initiate all the messages it wants to send and
12 # pack the resulting simgrid.Comm objects in a list. All messages thus occur concurrently.
13 #
14 # The sender then loops until there is no ongoing communication. Using wait_any() ensures that the sender
15 # will notice events as soon as they occur even if it does not follow the order of the container.
16 #
17 # Here, finalize messages will terminate earlier because their size is 0, so they travel faster than the
18 # other messages of this application.  As expected, the trace shows that the finalize of worker 1 is
19 # processed before 'Message 5' that is sent to worker 0.
20
21 class Sender:
22     def __init__(self, *args):
23         if len(args) != 3:
24             raise AssertionError("Actor sender requires 3 parameters, but got {:d}".format(len(args)))
25         self.messages_count = int(args[0])  # number of tasks
26         self.msg_size = int(args[1])  # communication cost (in bytes)
27         self.receivers_count = int(args[2])  # number of receivers
28
29     def __call__(self):
30         # List in which we store all ongoing communications
31         pending_comms = []
32
33         # Vector of the used mailboxes
34         mboxes = [Mailbox.by_name("receiver-{:d}".format(i))
35                   for i in range(0, self.receivers_count)]
36
37         # Start dispatching all messages to receivers, in a round robin fashion
38         for i in range(0, self.messages_count):
39             content = "Message {:d}".format(i)
40             mbox = mboxes[i % self.receivers_count]
41
42             this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox)))
43
44             # Create a communication representing the ongoing communication, and store it in pending_comms
45             comm = mbox.put_async(content, self.msg_size)
46             pending_comms.append(comm)
47
48         # Start sending messages to let the workers know that they should stop
49         for i in range(0, self.receivers_count):
50             mbox = mboxes[i]
51             this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox)))
52             comm = mbox.put_async("finalize", 0)
53             pending_comms.append(comm)
54
55         this_actor.info("Done dispatching all messages")
56
57         # Now that all message exchanges were initiated, wait for their completion, in order of completion.
58         #
59         # This loop waits for first terminating message with wait_any() and remove it with del, until all comms are
60         # terminated.
61         # Even in this simple example, the pending comms do not terminate in the exact same order of creation.
62         while pending_comms:
63           changed_pos = Comm.wait_any(pending_comms)
64           del pending_comms[changed_pos]
65           if (changed_pos != 0):
66             this_actor.info("Remove the {:d}th pending comm: it terminated earlier than another comm that was initiated first.".format(changed_pos));
67
68         this_actor.info("Goodbye now!")
69
70
71 class Receiver:
72     def __init__(self, *args):
73         if len(args) != 1:  # Receiver actor expects 1 argument: its ID
74             raise AssertionError(
75                 "Actor receiver requires 1 parameter, but got {:d}".format(len(args)))
76         self.mbox = Mailbox.by_name("receiver-{:s}".format(args[0]))
77
78     def __call__(self):
79         this_actor.info("Wait for my first message")
80         while True:
81             received = self.mbox.get()
82             this_actor.info("I got a '{:s}'.".format(received))
83             if received == "finalize":
84                 break  # If it's a finalize message, we're done.
85
86
87 if __name__ == '__main__':
88     e = Engine(sys.argv)
89
90     # Load the platform description
91     e.load_platform(sys.argv[1])
92
93     # Register the classes representing the actors
94     e.register_actor("sender", Sender)
95     e.register_actor("receiver", Receiver)
96
97     e.load_deployment(sys.argv[2])
98
99     e.run()