X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/f3b7e5f4b4d7c87ee3e8827313ec966ea8fc8387..7e625e5e848a284b522d69ec28cb111f1f88515b:/examples/python/comm-wait/comm-wait.py diff --git a/examples/python/comm-wait/comm-wait.py b/examples/python/comm-wait/comm-wait.py index 667f01fc79..b79b2a287f 100644 --- a/examples/python/comm-wait/comm-wait.py +++ b/examples/python/comm-wait/comm-wait.py @@ -1,74 +1,62 @@ -# Copyright (c) 2010-2020. The SimGrid Team. All rights reserved. +# Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. + # # This program is free software; you can redistribute it and/or modify it # under the terms of the license (GNU LGPL) which comes with this package. -from simgrid import Engine, Mailbox, this_actor -import sys +""" +This example shows how to use simgrid::s4u::this_actor::wait() to wait for a given communication. -# This example shows how to use simgrid::s4u::this_actor::wait() to wait for a given communication. -# -# As for the other asynchronous examples, the sender initiate all the messages it wants to send and -# pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occurs concurrently. -# -# The sender then loops until there is no ongoing communication. +As for the other asynchronous examples, the sender initiate all the messages it wants to send and +pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occurs concurrently. +The sender then loops until there is no ongoing communication. +""" -class Sender: - def __init__(self, *args): - if len(args) != 3: - raise AssertionError( - "Actor sender requires 3 parameters, but got {:d}".format(len(args))) - self.messages_count = int(args[0]) # number of tasks - self.msg_size = int(args[1]) # communication cost (in bytes) - self.receivers_count = int(args[2]) # number of receivers - - def __call__(self): - # List in which we store all ongoing communications - pending_comms = [] +import sys +from simgrid import Actor, Engine, Host, Mailbox, this_actor - # Vector of the used mailboxes - mboxes = [Mailbox.by_name("receiver-{:d}".format(i)) for i in range(0, self.receivers_count)] - # Start dispatching all messages to receivers, in a round robin fashion - for i in range(0, self.messages_count): - content = "Message {:d}".format(i) - mbox = mboxes[i % self.receivers_count] +def sender(messages_count, msg_size, receivers_count): + # List in which we store all ongoing communications + pending_comms = [] - this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox))) + # Vector of the used mailboxes + mboxes = [Mailbox.by_name("receiver-{:d}".format(i)) for i in range(0, receivers_count)] - # Create a communication representing the ongoing communication, and store it in pending_comms - comm = mbox.put_async(content, self.msg_size) - pending_comms.append(comm) + # Start dispatching all messages to receivers, in a round robin fashion + for i in range(0, messages_count): + content = "Message {:d}".format(i) + mbox = mboxes[i % receivers_count] - # Start sending messages to let the workers know that they should stop - for i in range(0, self.receivers_count): - mbox = mboxes[i] - this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox))) - comm = mbox.put_async("finalize", 0) - pending_comms.append(comm) + this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox))) - this_actor.info("Done dispatching all messages") + # Create a communication representing the ongoing communication, and store it in pending_comms + comm = mbox.put_async(content, msg_size) + pending_comms.append(comm) - # Now that all message exchanges were initiated, wait for their completion, in order of creation. - for comm in pending_comms: - comm.wait() - this_actor.info("Goodbye now!") + # Start sending messages to let the workers know that they should stop + for i in range(0, receivers_count): + mbox = mboxes[i] + this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox))) + comm = mbox.put_async("finalize", 0) + pending_comms.append(comm) + this_actor.info("Done dispatching all messages") -class Receiver: - def __init__(self, *args): - if len(args) != 1: # Receiver actor expects 1 argument: its ID - raise AssertionError("Actor receiver requires 1 parameter, but got {:d}".format(len(args))) - self.mbox = Mailbox.by_name("receiver-{:s}".format(args[0])) + # Now that all message exchanges were initiated, wait for their completion, in order of creation. + for comm in pending_comms: + comm.wait() + this_actor.info("Goodbye now!") - def __call__(self): - this_actor.info("Wait for my first message") - while True: - received = self.mbox.get() - this_actor.info("I got a '{:s}'.".format(received)) - if received == "finalize": - break # If it's a finalize message, we're done. +def receiver(my_id): + mbox = Mailbox.by_name("receiver-{:d}".format(my_id)) + this_actor.info("Wait for my first message") + while True: + received = mbox.get() + this_actor.info("I got a '{:s}'.".format(received)) + if received == "finalize": + break # If it's a finalize message, we're done. if __name__ == '__main__': @@ -76,10 +64,7 @@ if __name__ == '__main__': e.load_platform(sys.argv[1]) # Load the platform description - # Register the classes representing the actors - e.register_actor("sender", Sender) - e.register_actor("receiver", Receiver) - - e.load_deployment(sys.argv[2]) + Actor.create("sender", Host.by_name("Tremblay"), sender, 3, 50000000, 1) + Actor.create("receiver", Host.by_name("Ruby"), receiver, 0) e.run()