Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
57806fdc894c5f98bb4c31fc8403264223aa2ce9
[simgrid.git] / examples / java / cloud / Master.java
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package cloud;
8
9 import java.util.ArrayList;
10
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Msg;
13 import org.simgrid.msg.MsgException;
14 import org.simgrid.msg.Process;
15 import org.simgrid.msg.Task;
16 import org.simgrid.msg.VM;
17
18 public class Master extends Process {
19         private Host[] hosts;
20         
21         public Master(Host host, String name, Host[] hosts) {
22                 super(host,name,null);
23                 this.hosts = hosts;
24         }
25         public void main(String[] args) throws MsgException {
26                 int slavesCount = Cloud.hostNB;
27                 
28                 ArrayList<VM> vms = new ArrayList<VM>();
29                 
30                 // Create one VM per host and bind a process inside each one. 
31                 for (int i = 0; i < slavesCount; i++) {
32                         Msg.info("create VM0"+i);       
33                         VM vm = new VM(hosts[i+1],"VM0"+i);
34                         vm.start();
35                         vms.add(vm);
36                         Slave slave = new Slave(vm,i);
37                         Msg.info("Put Worker "+slave.msgName()+ " on "+vm.getName());
38                         slave.start();
39         
40                 }
41                 Msg.info("Launched " + vms.size() + " VMs");
42                 
43                 Msg.info("Send a first batch of work to everyone");
44                 workBatch(slavesCount);
45                 
46                 Msg.info("Suspend all VMs");
47                 for (int i = 0; i < vms.size(); i++) {
48                         Msg.info("Suspend "+vms.get(i).getName());
49                         vms.get(i).suspend();
50                 }
51                 
52                 Msg.info("Wait a while");
53                 waitFor(2);
54                 
55                 Msg.info("Resume all VMs.");
56                 for (int i = 0; i < vms.size(); i++) {
57                         vms.get(i).resume();
58                 }
59                 
60                 Msg.info("Sleep long enough for everyone to be done with previous batch of work");
61                 waitFor(1000 - Msg.getClock());
62                 
63 /*              Msg.info("Add one more process per VM.");
64                 for (int i = 0; i < vms.size(); i++) {
65                         VM vm = vms.get(i);
66                         Slave slave = new Slave(vm,i + vms.size());
67                         slave.start();
68                 }
69         
70                 workBatch(slavesCount * 2);
71 */
72
73                 Msg.info("Migrate everyone to "+hosts[3].getName());
74                 for (int i = 0; i < vms.size(); i++) {
75                         Msg.info("Migrate "+vms.get(i).getName()+"from"+hosts[i+1].getName()+"to "+hosts[3].getName());
76                         vms.get(i).migrate(hosts[2]);
77                 }
78                 
79                 
80                 Msg.info("Let's shut down the simulation and kill everyone.");
81                 
82                 for (int i = 0; i < vms.size(); i++) {
83                         vms.get(i).shutdown();
84                         vms.get(i).destroy();
85                 }                               
86                 Msg.info("Master done.");
87         }
88         
89         public void workBatch(int slavesCount) throws MsgException {
90                 for (int i = 0; i < slavesCount; i++) {
91                         Task task = new Task("Task0" + i, Cloud.task_comp_size, Cloud.task_comm_size);
92                         Msg.info("Sending to WRK0" + i);
93                         task.send("MBOX:WRK0" + i);
94                 }
95         }
96 }