Logo AND Algorithmique Numérique Distribuée

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