1 # Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.
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.
9 # This example shows how to block on the completion of a set of communications.
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.
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.
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.
22 def __init__(self, *args):
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
30 # List in which we store all ongoing communications
33 # Vector of the used mailboxes
34 mboxes = [Mailbox.by_name("receiver-{:d}".format(i))
35 for i in range(0, self.receivers_count)]
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]
42 this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox)))
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)
48 # Start sending messages to let the workers know that they should stop
49 for i in range(0, self.receivers_count):
51 this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox)))
52 comm = mbox.put_async("finalize", 0)
53 pending_comms.append(comm)
55 this_actor.info("Done dispatching all messages")
57 # Now that all message exchanges were initiated, wait for their completion, in order of completion.
59 # This loop waits for first terminating message with wait_any() and remove it with del, until all comms are
61 # Even in this simple example, the pending comms do not terminate in the exact same order of creation.
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));
68 this_actor.info("Goodbye now!")
72 def __init__(self, *args):
73 if len(args) != 1: # Receiver actor expects 1 argument: its ID
75 "Actor receiver requires 1 parameter, but got {:d}".format(len(args)))
76 self.mbox = Mailbox.by_name("receiver-{:s}".format(args[0]))
79 this_actor.info("Wait for my first message")
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.
87 if __name__ == '__main__':
90 # Load the platform description
91 e.load_platform(sys.argv[1])
93 # Register the classes representing the actors
94 e.register_actor("sender", Sender)
95 e.register_actor("receiver", Receiver)
97 e.load_deployment(sys.argv[2])