Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix MC. Host names always bite me
[simgrid.git] / examples / s4u / cloud-simple / s4u-cloud-simple.cpp
1 /* Copyright (c) 2007-2017. 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 #include "simgrid/s4u/VirtualMachine.hpp"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
9
10 static void computation_fun()
11 {
12   double clock_sta = simgrid::s4u::Engine::getClock();
13   simgrid::s4u::this_actor::execute(1000000);
14   double clock_end = simgrid::s4u::Engine::getClock();
15
16   XBT_INFO("%s:%s task executed %g", simgrid::s4u::this_actor::getHost()->getCname(),
17            simgrid::s4u::this_actor::getCname(), clock_end - clock_sta);
18 }
19
20 static void launch_computation_worker(s4u_Host* host)
21 {
22   simgrid::s4u::Actor::createActor("compute", host, computation_fun);
23 }
24
25 struct s_payload {
26   s4u_Host* tx_host;
27   const char* tx_actor_name;
28   double clock_sta;
29 };
30
31 static void communication_tx_fun(std::vector<std::string> args)
32 {
33   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(args.at(0));
34   struct s_payload* payload     = xbt_new(struct s_payload, 1);
35   payload->tx_actor_name        = simgrid::s4u::Actor::self()->getCname();
36   payload->tx_host              = simgrid::s4u::this_actor::getHost();
37   payload->clock_sta            = simgrid::s4u::Engine::getClock();
38
39   mbox->put(payload, 1000000);
40 }
41
42 static void communication_rx_fun(std::vector<std::string> args)
43 {
44   const char* actor_name        = simgrid::s4u::Actor::self()->getCname();
45   const char* host_name         = simgrid::s4u::this_actor::getHost()->getCname();
46   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(args.at(0));
47
48   struct s_payload* payload = static_cast<struct s_payload*>(mbox->get());
49   double clock_end          = simgrid::s4u::Engine::getClock();
50
51   XBT_INFO("%s:%s to %s:%s => %g sec", payload->tx_host->getCname(), payload->tx_actor_name, host_name, actor_name,
52            clock_end - payload->clock_sta);
53
54   xbt_free(payload);
55 }
56
57 static void launch_communication_worker(s4u_Host* tx_host, s4u_Host* rx_host)
58 {
59   std::string mbox_name = std::string("MBOX:") + tx_host->getCname() + "-" + rx_host->getCname();
60   std::vector<std::string> args;
61   args.push_back(mbox_name);
62
63   simgrid::s4u::Actor::createActor("comm_tx", tx_host, communication_tx_fun, args);
64
65   simgrid::s4u::Actor::createActor("comm_rx", rx_host, communication_rx_fun, args);
66 }
67
68 static void master_main()
69 {
70   s4u_Host* pm0 = simgrid::s4u::Host::by_name("Fafard");
71   s4u_Host* pm1 = simgrid::s4u::Host::by_name("Tremblay");
72
73   XBT_INFO("## Test 1 (started): check computation on normal PMs");
74
75   XBT_INFO("### Put a task on a PM");
76   launch_computation_worker(pm0);
77   simgrid::s4u::this_actor::sleep_for(2);
78
79   XBT_INFO("### Put two tasks on a PM");
80   launch_computation_worker(pm0);
81   launch_computation_worker(pm0);
82   simgrid::s4u::this_actor::sleep_for(2);
83
84   XBT_INFO("### Put a task on each PM");
85   launch_computation_worker(pm0);
86   launch_computation_worker(pm1);
87   simgrid::s4u::this_actor::sleep_for(2);
88
89   XBT_INFO("## Test 1 (ended)");
90
91   XBT_INFO("## Test 2 (started): check impact of running a task inside a VM (there is no degradation for the moment)");
92
93   XBT_INFO("### Put a VM on a PM, and put a task to the VM");
94   simgrid::s4u::VirtualMachine* vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
95   vm0->start();
96   launch_computation_worker(vm0);
97   simgrid::s4u::this_actor::sleep_for(2);
98   vm0->destroy();
99
100   XBT_INFO("## Test 2 (ended)");
101
102   XBT_INFO(
103       "## Test 3 (started): check impact of running a task collocated with a VM (there is no VM noise for the moment)");
104
105   XBT_INFO("### Put a VM on a PM, and put a task to the PM");
106   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
107   vm0->start();
108   launch_computation_worker(pm0);
109   simgrid::s4u::this_actor::sleep_for(2);
110   vm0->destroy();
111   XBT_INFO("## Test 3 (ended)");
112
113   XBT_INFO("## Test 4 (started): compare the cost of running two tasks inside two different VMs collocated or not (for"
114            " the moment, there is no degradation for the VMs. Hence, the time should be equals to the time of test 1");
115
116   XBT_INFO("### Put two VMs on a PM, and put a task to each VM");
117   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
118   vm0->start();
119   simgrid::s4u::VirtualMachine* vm1 = new simgrid::s4u::VirtualMachine("VM1", pm0, 1);
120   launch_computation_worker(vm0);
121   launch_computation_worker(vm1);
122   simgrid::s4u::this_actor::sleep_for(2);
123   vm0->destroy();
124   vm1->destroy();
125
126   XBT_INFO("### Put a VM on each PM, and put a task to each VM");
127   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
128   vm1 = new simgrid::s4u::VirtualMachine("VM1", pm1, 1);
129   vm0->start();
130   vm1->start();
131   launch_computation_worker(vm0);
132   launch_computation_worker(vm1);
133   simgrid::s4u::this_actor::sleep_for(2);
134   vm0->destroy();
135   vm1->destroy();
136   XBT_INFO("## Test 4 (ended)");
137
138   XBT_INFO("## Test 5  (started): Analyse network impact");
139   XBT_INFO("### Make a connection between PM0 and PM1");
140   launch_communication_worker(pm0, pm1);
141   simgrid::s4u::this_actor::sleep_for(5);
142
143   XBT_INFO("### Make two connection between PM0 and PM1");
144   launch_communication_worker(pm0, pm1);
145   launch_communication_worker(pm0, pm1);
146   simgrid::s4u::this_actor::sleep_for(5);
147
148   XBT_INFO("### Make a connection between PM0 and VM0@PM0");
149   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
150   vm0->start();
151   launch_communication_worker(pm0, vm0);
152   simgrid::s4u::this_actor::sleep_for(5);
153   vm0->destroy();
154
155   XBT_INFO("### Make a connection between PM0 and VM0@PM1");
156   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm1, 1);
157   launch_communication_worker(pm0, vm0);
158   simgrid::s4u::this_actor::sleep_for(5);
159   vm0->destroy();
160
161   XBT_INFO("### Make two connections between PM0 and VM0@PM1");
162   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm1, 1);
163   vm0->start();
164   launch_communication_worker(pm0, vm0);
165   launch_communication_worker(pm0, vm0);
166   simgrid::s4u::this_actor::sleep_for(5);
167   vm0->destroy();
168
169   XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
170   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm1, 1);
171   vm0->start();
172   launch_communication_worker(pm0, vm0);
173   launch_communication_worker(pm0, pm1);
174   simgrid::s4u::this_actor::sleep_for(5);
175   vm0->destroy();
176
177   XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
178   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
179   vm1 = new simgrid::s4u::VirtualMachine("VM1", pm1, 1);
180   vm0->start();
181   vm1->start();
182   launch_communication_worker(vm0, vm1);
183   launch_communication_worker(vm0, vm1);
184   simgrid::s4u::this_actor::sleep_for(5);
185   vm0->destroy();
186   vm1->destroy();
187
188   XBT_INFO("## Test 5 (ended)");
189 }
190
191 static void launch_master(s4u_Host* host)
192 {
193   simgrid::s4u::Actor::createActor("master_", host, master_main);
194 }
195
196 int main(int argc, char* argv[])
197 {
198   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
199   e->loadPlatform(argv[1]); /* - Load the platform description */
200
201   launch_master(simgrid::s4u::Host::by_name("Fafard"));
202
203   e->run();
204
205   XBT_INFO("Simulation time %g", e->getClock());
206
207   return 0;
208 }