Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add example
[simgrid.git] / teshsuite / s4u / io-stream / io-stream.cpp
1 /* Copyright (c) 2017-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 #include <simgrid/s4u.hpp>
7
8 namespace sg4 = simgrid::s4u;
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(io_stream, "Messages specific for this simulation");
11
12 static void streamer(size_t size)
13 {
14   auto* bob        = sg4::Host::by_name("bob");
15   auto* bob_disk   = bob->get_disks().front();
16   auto* alice      = sg4::Host::by_name("alice");
17   auto* alice_disk = alice->get_disks().front();
18   double clock = sg4::Engine::get_clock();
19
20   XBT_INFO("[Bob -> Alice] Store and Forward (1 block)");
21   bob_disk->read(size);
22   XBT_INFO("    Read  : %.6f seconds", sg4::Engine::get_clock() - clock);
23   clock = sg4::Engine::get_clock();
24   sg4::Comm::sendto(bob, alice, size);
25   XBT_INFO("    Send  : %.6f seconds", sg4::Engine::get_clock() - clock);
26   clock = sg4::Engine::get_clock();
27   alice_disk->write(size);
28   XBT_INFO("    Write : %.6f seconds", sg4::Engine::get_clock() - clock);
29   XBT_INFO("    Total : %.6f seconds", sg4::Engine::get_clock());
30
31   XBT_INFO("[Bob -> Alice] Store and Forward (100 blocks)");
32   sg4::IoPtr read = bob_disk->read_async(size/100.0);
33   sg4::CommPtr transfer = sg4::Comm::sendto_async(bob, alice, size/100.0);
34   sg4::IoPtr write = alice_disk->write_async(size/100.);
35
36   clock = sg4::Engine::get_clock();
37
38   for (int i = 0; i < 99; i++){
39     read->wait();
40     read = bob_disk->read_async(size/100.0);
41     transfer->wait();
42     transfer = sg4::Comm::sendto_async(bob, alice, size/100.0);
43     write->wait();
44     write = alice_disk->write_async(size/100.);
45   }
46
47   read->wait();
48   transfer->wait();
49   write->wait();
50   XBT_INFO("    Total : %.6f seconds", sg4::Engine::get_clock() - clock);
51
52
53   XBT_INFO("[Bob -> Alice] Streaming (Read bottleneck)");
54   clock = sg4::Engine::get_clock();
55   sg4::Io::streamto(bob, bob_disk, alice, alice_disk, size);
56   XBT_INFO("    Total : %.6f seconds", sg4::Engine::get_clock() - clock);
57
58   XBT_INFO("[Bob -> Alice] Streaming (Write bottleneck)");
59   clock = sg4::Engine::get_clock();
60   sg4::Io::streamto(alice, alice_disk, bob, bob_disk, size);
61   XBT_INFO("    Total : %.6f seconds", sg4::Engine::get_clock() - clock);
62 }
63
64 int main(int argc, char** argv)
65 {
66   sg4::Engine e(&argc, argv);
67
68   /* simple platform containing 2 hosts and 2 disks */
69   auto* zone = sg4::create_full_zone("");
70   auto* bob  = zone->create_host("bob", 1e6);
71   auto* alice  = zone->create_host("alice", 1e6);
72
73   sg4::LinkInRoute link(zone->create_link("link", "2MBps")->set_latency("50us")->seal());
74   zone->add_route(bob->get_netpoint(), alice->get_netpoint(), nullptr, nullptr, {link}, true);
75
76   bob->create_disk("bob_disk", 1e6, 5e5);
77   alice->create_disk("alice_disk", 4e6, 4e6);
78
79   zone->seal();
80
81   sg4::Actor::create("streamer", bob, streamer, 4e6);
82
83   e.run();
84
85   return 0;
86 }