1 # Copyright (c) 2017-2023. The SimGrid Team. All rights reserved.
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.
7 Usage: activityset-waitany.py platform_file [other parameters]
11 from simgrid import Actor, ActivitySet, Engine, Comm, Exec, Io, Host, Mailbox, this_actor
14 mbox = Mailbox.by_name("mbox")
15 disk = Host.current().get_disks()[0]
17 this_actor.info("Create my asynchronous activities")
18 exec = this_actor.exec_async(5e9)
19 comm, payload = mbox.get_async()
20 io = disk.read_async(300000000)
22 pending_activities = ActivitySet([exec, comm])
23 pending_activities.push(io) # Activities can be pushed after creation, too
25 this_actor.info("Wait for asynchronous activities to complete")
26 while not pending_activities.empty():
27 completed_one = pending_activities.wait_any()
29 if isinstance(completed_one, Comm):
30 this_actor.info("Completed a Comm")
31 elif isinstance(completed_one, Exec):
32 this_actor.info("Completed an Exec")
33 elif isinstance(completed_one, Io):
34 this_actor.info("Completed an I/O")
36 this_actor.info("Last activity is complete")
39 this_actor.info("Send 'Message'")
40 Mailbox.by_name("mbox").put("Message", 600000000)
42 if __name__ == '__main__':
44 e.set_log_control("root.fmt:[%4.6r]%e[%5a]%e%m%n")
46 # Load the platform description
47 e.load_platform(sys.argv[1])
49 Actor.create("bob", Host.by_name("bob"), bob)
50 Actor.create("alice", Host.by_name("alice"), alice)
56 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */
58 /* This program is free software; you can redistribute it and/or modify it
59 * under the terms of the license (GNU LGPL) which comes with this package. */
61 #include "simgrid/s4u.hpp"
65 namespace sg4 = simgrid::s4u;
67 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_activity_waittany, "Messages specific for this s4u example");
71 sg4::Mailbox* mbox = sg4::Mailbox::by_name("mbox");
72 const sg4::Disk* disk = sg4::Host::current()->get_disks().front();
75 XBT_INFO("Create my asynchronous activities");
76 auto exec = sg4::this_actor::exec_async(5e9);
77 auto comm = mbox->get_async(&payload);
78 auto io = disk->read_async(3e8);
80 sg4::ActivitySet pending_activities({boost::dynamic_pointer_cast<sg4::Activity>(exec),
81 boost::dynamic_pointer_cast<sg4::Activity>(comm),
82 boost::dynamic_pointer_cast<sg4::Activity>(io)});
84 XBT_INFO("Wait for asynchronous activities to complete");
85 while (not pending_activities.empty()) {
86 auto completed_one = pending_activities.wait_any();
87 if (completed_one != nullptr) {
88 if (boost::dynamic_pointer_cast<sg4::Comm>(completed_one))
89 XBT_INFO("Completed a Comm");
90 if (boost::dynamic_pointer_cast<sg4::Exec>(completed_one))
91 XBT_INFO("Completed an Exec");
92 if (boost::dynamic_pointer_cast<sg4::Io>(completed_one))
93 XBT_INFO("Completed an I/O");
96 XBT_INFO("Last activity is complete");
102 auto* payload = new std::string("Message");
103 XBT_INFO("Send '%s'", payload->c_str());
104 sg4::Mailbox::by_name("mbox")->put(payload, 6e8);
107 int main(int argc, char* argv[])
109 sg4::Engine e(&argc, argv);
111 e.load_platform(argv[1]);
113 sg4::Actor::create("bob", e.host_by_name("bob"), bob);
114 sg4::Actor::create("alice", e.host_by_name("alice"), alice);