1 # Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the license (GNU LGPL) which comes with this package.
8 This example shows how to use simgrid::s4u::this_actor::wait() to wait for a given communication.
10 As for the other asynchronous examples, the sender initiate all the messages it wants to send and
11 pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occurs concurrently.
13 The sender then loops until there is no ongoing communication.
17 from simgrid import Actor, Engine, Host, Mailbox, this_actor
20 def sender(messages_count, msg_size, receivers_count):
21 # List in which we store all ongoing communications
24 # Vector of the used mailboxes
25 mboxes = [Mailbox.by_name("receiver-{:d}".format(i)) for i in range(0, receivers_count)]
27 # Start dispatching all messages to receivers, in a round robin fashion
28 for i in range(0, messages_count):
29 content = "Message {:d}".format(i)
30 mbox = mboxes[i % receivers_count]
32 this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox)))
34 # Create a communication representing the ongoing communication, and store it in pending_comms
35 comm = mbox.put_async(content, msg_size)
36 pending_comms.append(comm)
38 # Start sending messages to let the workers know that they should stop
39 for i in range(0, receivers_count):
41 this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox)))
42 comm = mbox.put_async("finalize", 0)
43 pending_comms.append(comm)
45 this_actor.info("Done dispatching all messages")
47 # Now that all message exchanges were initiated, wait for their completion, in order of creation.
48 for comm in pending_comms:
50 this_actor.info("Goodbye now!")
53 mbox = Mailbox.by_name("receiver-{:d}".format(my_id))
54 this_actor.info("Wait for my first message")
57 this_actor.info("I got a '{:s}'.".format(received))
58 if received == "finalize":
59 break # If it's a finalize message, we're done.
62 if __name__ == '__main__':
65 e.load_platform(sys.argv[1]) # Load the platform description
67 Actor.create("sender", Host.by_name("Tremblay"), sender, 3, 50000000, 1)
68 Actor.create("receiver", Host.by_name("Ruby"), receiver, 0)