Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / python / comm-waitfor / comm-waitfor.py
1 # Copyright (c) 2010-2023. 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 This example shows how to block on the completion of a set of communications.
8
9 As for the other asynchronous examples, the sender initiate all the messages it wants to send and
10 pack the resulting simgrid.Comm objects in a list. All messages thus occur concurrently.
11
12 The sender then loops until there is no ongoing communication. Using wait_any() ensures that the sender
13 will notice events as soon as they occur even if it does not follow the order of the container.
14
15 Here, finalize messages will terminate earlier because their size is 0, so they travel faster than the
16 other messages of this application.  As expected, the trace shows that the finalize of worker 1 is
17 processed before 'Message 5' that is sent to worker 0.
18 """
19
20 import sys
21 from simgrid import Actor, Comm, Engine, Host, Mailbox, this_actor
22
23
24 FINALIZE_TAG = "finalize"
25
26
27 def sender(receiver_id: str, messages_count: int, payload_size: int) -> None:
28     # List in which we store all ongoing communications
29     pending_comms = []
30     mbox = Mailbox.by_name(receiver_id)
31
32     # Asynchronously send `messages_count` message(s) to the receiver
33     for i in range(0, messages_count):
34         payload = "Message {:d}".format(i)
35         this_actor.info("Send '{:s}' to '{:s}'".format(payload, receiver_id))
36
37         # Create a communication representing the ongoing communication, and store it in pending_comms
38         comm = mbox.put_async(payload, payload_size)
39         pending_comms.append(comm)
40
41     # Send the final message to the receiver
42     payload = FINALIZE_TAG
43     final_payload_size = 0
44     final_comm = mbox.put_async(payload, final_payload_size)
45     pending_comms.append(final_comm)
46     this_actor.info("Send '{:s}' to '{:s}".format(payload, receiver_id))
47     this_actor.info("Done dispatching all messages")
48
49     this_actor.info("Waiting for all outstanding communications to complete")
50     while pending_comms:
51         current_comm: Comm = pending_comms[-1]
52         current_comm.wait_for(1.0)
53         pending_comms.pop()
54     this_actor.info("Goodbye now!")
55
56
57 def receiver(identifier: str) -> None:
58     mbox: Mailbox = Mailbox.by_name(identifier)
59     this_actor.info("Wait for my first message")
60     while True:
61         received = mbox.get()
62         this_actor.info("I got a '{:s}'.".format(received))
63         if received == FINALIZE_TAG:
64             break
65     this_actor.info("Goodbye now!")
66
67
68 def main():
69     e = Engine(sys.argv)
70
71     # Load the platform description
72     e.load_platform(sys.argv[1])
73
74     receiver_id = "receiver-0"
75     Actor.create("sender", Host.by_name("Tremblay"), sender, receiver_id, 3, int(5e7))
76     Actor.create("receiver", Host.by_name("Ruby"), receiver, receiver_id)
77
78     e.run()
79
80
81 if __name__ == '__main__':
82     main()