Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7c88907c9b2ae4f1521d1040eba7c4203678dd87
[simgrid.git] / examples / cpp / comm-dependent / s4u-comm-dependent.cpp
1 /* Copyright (c) 2007-2021. 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 namespace sg4 = simgrid::s4u;
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_dependent, "Messages specific for this s4u example");
10
11 static void sender(sg4::Mailbox* mailbox)
12 {
13   auto* computation_amount = new double(sg4::this_actor::get_host()->get_speed());
14   sg4::ExecPtr exec        = sg4::this_actor::exec_init(2 * (*computation_amount));
15   sg4::CommPtr comm        = mailbox->put_init(computation_amount, 7e6);
16
17   exec->set_name("exec on sender")->add_successor(comm)->start();
18   comm->set_name("comm to receiver")->vetoable_start();
19   exec->wait();
20   comm->wait();
21 }
22
23 static void receiver(sg4::Mailbox* mailbox)
24 {
25   double* received           = nullptr;
26   double computation_amount  = sg4::this_actor::get_host()->get_speed();
27   sg4::ExecPtr exec          = sg4::this_actor::exec_init(2 * computation_amount);
28   sg4::CommPtr comm          = mailbox->get_init()->set_dst_data((void**)&received, sizeof(double));
29
30   comm->set_name("comm from sender")->add_successor(exec)->start();
31   exec->set_name("exec on receiver")->vetoable_start();
32
33   comm->wait();
34   exec->wait();
35   XBT_INFO("Received: %.0f flops were computed on sender", *received);
36   delete received;
37 }
38
39 int main(int argc, char* argv[])
40 {
41   sg4::Engine e(&argc, argv);
42   e.load_platform(argv[1]);
43
44   sg4::Mailbox* mbox = sg4::Mailbox::by_name("Mailbox");
45
46   sg4::Actor::create("sender", sg4::Host::by_name("Tremblay"), sender, mbox);
47   sg4::Actor::create("receiver", sg4::Host::by_name("Jupiter"), receiver, mbox);
48
49   e.run();
50
51   XBT_INFO("Simulation time: %.3f", e.get_clock());
52
53   return 0;
54 }