Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[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.Msg;
12 import org.simgrid.msg.VM;
13 import org.simgrid.msg.Host;
14 import org.simgrid.msg.Task;
15 import org.simgrid.msg.Process;
16 import org.simgrid.msg.MsgException;
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
26   public void main(String[] args) throws MsgException {
27     int slavesCount = Cloud.hostNB;
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.getName()+ " 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[3]);
77     }
78
79     Msg.info("Let's shut down the simulation and kill everyone.");
80
81     for (int i = 0; i < vms.size(); i++) {
82       vms.get(i).shutdown();
83     }
84     Msg.info("Master done.");
85   }
86
87   public void workBatch(int slavesCount) throws MsgException {
88     for (int i = 0; i < slavesCount; i++) {
89       Task task = new Task("Task0" + i, Cloud.task_comp_size, Cloud.task_comm_size);
90       Msg.info("Sending to WRK0" + i);
91       task.send("MBOX:WRK0" + i);
92     }
93   }
94 }