Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove declarations for never used signal slots.
[simgrid.git] / examples / python / async-wait / async-wait.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 use simgrid::s4u::this_actor::wait() to wait for a given communication.
10 #
11 # As for the other asynchronous examples, the sender initiate all the messages it wants to send and
12 # pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occurs concurrently.
13 #
14 # The sender then loops until there is no ongoing communication.
15
16 class Sender:
17     def __init__(self, *args):
18         if len(args) != 3:
19             raise AssertionError(
20                 "Actor sender requires 4 parameters, but got {:d}".format(len(args)))
21         self.messages_count  = int(args[0]) # number of tasks
22         self.msg_size        = int(args[1]) # communication cost (in bytes)
23         self.receivers_count = int(args[2]) # number of receivers
24
25     def __call__(self):
26         # List in which we store all ongoing communications
27         pending_comms = []
28
29         # Vector of the used mailboxes
30         mboxes = [Mailbox.by_name("receiver-{:d}".format(i)) for i in range(0, self.receivers_count)]
31
32         # Start dispatching all messages to receivers, in a round robin fashion
33         for i in range(0, self.messages_count):
34             content = "Message {:d}".format(i)
35             mbox = mboxes[i % self.receivers_count]
36
37             this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox)))
38
39             # Create a communication representing the ongoing communication, and store it in pending_comms
40             comm = mbox.put_async(content, self.msg_size)
41             pending_comms.append(comm)
42
43         # Start sending messages to let the workers know that they should stop
44         for i in range(0, self.receivers_count):
45             mbox = mboxes[i]
46             this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox)))
47             comm = mbox.put_async("finalize", 0)
48             pending_comms.append(comm)
49
50         this_actor.info("Done dispatching all messages")
51
52         # Now that all message exchanges were initiated, wait for their completion, in order of creation.
53         for comm in pending_comms:
54             comm.wait()
55         this_actor.info("Goodbye now!")
56
57 class Receiver:
58     def __init__(self, *args):
59         if len(args) != 1: # Receiver actor expects 1 argument: its ID
60             raise AssertionError("Actor receiver requires 1 parameter, but got {:d}".format(len(args)))
61         self.mbox = Mailbox.by_name("receiver-{:s}".format(args[0]))
62
63     def __call__(self):
64         this_actor.info("Wait for my first message")
65         while True:
66             received = self.mbox.get()
67             this_actor.info("I got a '{:s}'.".format(received))
68             if received == "finalize":
69                 break # If it's a finalize message, we're done.
70
71
72 if __name__ == '__main__':
73     e = Engine(sys.argv)
74
75     e.load_platform(sys.argv[1])             # Load the platform description
76
77     # Register the classes representing the actors
78     e.register_actor("sender", Sender)
79     e.register_actor("receiver", Receiver)
80
81     e.load_deployment(sys.argv[2])
82
83     e.run()