Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
38a34f148d030c25a5f2dbfcf9fda4bc26c0eb95
[simgrid.git] / examples / python / comm-waitall / comm-waitall.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 from simgrid import Actor,Comm, Engine, Host, Mailbox, this_actor
7 import sys
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 blocks until all ongoing communication terminate, using simgrid.Comm.wait_all()
15
16
17 def sender(messages_count, msg_size, receivers_count):
18      # List in which we store all ongoing communications
19      pending_comms = []
20
21      # Vector of the used mailboxes
22      mboxes = [Mailbox.by_name("receiver-{:d}".format(i))
23                for i in range(0, receivers_count)]
24
25      # Start dispatching all messages to receivers, in a round robin fashion
26      for i in range(0, messages_count):
27          content = "Message {:d}".format(i)
28          mbox = mboxes[i % receivers_count]
29
30          this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox)))
31
32          # Create a communication representing the ongoing communication, and store it in pending_comms
33          comm = mbox.put_async(content, msg_size)
34          pending_comms.append(comm)
35
36      # Start sending messages to let the workers know that they should stop
37      for i in range(0, receivers_count):
38          mbox = mboxes[i]
39          this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox)))
40          comm = mbox.put_async("finalize", 0)
41          pending_comms.append(comm)
42
43      this_actor.info("Done dispatching all messages")
44
45      # Now that all message exchanges were initiated, wait for their completion in one single call
46      Comm.wait_all(pending_comms)
47
48      this_actor.info("Goodbye now!")
49
50
51 def receiver(id):
52     mbox = Mailbox.by_name("receiver-{:d}".format(id))
53
54     this_actor.info("Wait for my first message")
55     while True:
56         received = mbox.get()
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.
60
61
62 if __name__ == '__main__':
63     e = Engine(sys.argv)
64
65     # Load the platform description
66     e.load_platform(sys.argv[1])
67
68     Actor.create("sender", Host.by_name("Tremblay"), sender, 5, 1000000, 2)
69     Actor.create("receiver", Host.by_name("Ruby"), receiver, 0)
70     Actor.create("receiver", Host.by_name("Perl"), receiver, 1)
71
72     e.run()