Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Further cleanups of the Java bindings:
[simgrid.git] / examples / java / basic / Master.java
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier         
5  * All rights reserved. 
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. 
9  */
10
11 import simgrid.msg.*;
12
13 public class Master extends simgrid.msg.Process {
14    public void main(String[] args) throws JniException, NativeException {
15       int channel = 0;
16       Msg.info("hello!");
17         
18       int slaveCount = 0;
19       Host[] slaves = null;
20       
21       Msg.info("argc="+args.length);
22       for (int i = 0; i<args.length; i++)           
23         Msg.info("argv:"+args[i]);
24       
25       if (args.length < 3) {
26          Msg.info("Master needs 3 arguments");
27          System.exit(1);
28       }
29       
30       int numberOfTasks = Integer.valueOf(args[0]).intValue();          
31       double taskComputeSize = Double.valueOf(args[1]).doubleValue();           
32       double taskCommunicateSize = Double.valueOf(args[2]).doubleValue();
33       BasicTask[] todo = new BasicTask[numberOfTasks];
34       
35       for (int i = 0; i < numberOfTasks; i++) {
36          todo[i] = new BasicTask("Task_" + i, taskComputeSize, taskCommunicateSize); 
37       }
38       
39       slaveCount = args.length - 3;
40       slaves = new Host[slaveCount];
41       
42       for(int i = 3; i < args.length ; i++)  {
43          try {
44             slaves[i-3] = Host.getByName(args[i]);
45          }
46          catch(HostNotFoundException e) {
47             Msg.info("Unknown host " + args[i] + ". Stopping Now!");
48             e.printStackTrace();
49             System.exit(1);
50          }
51       }
52       
53       Msg.info("Got "+  slaveCount + " slave(s) :");
54       
55       for (int i = 0; i < slaveCount; i++)
56         Msg.info("\t"+slaves[i].getName());
57       
58       Msg.info("Got "+ numberOfTasks + " task to process.");
59       
60       for (int i = 0; i < numberOfTasks; i++) {
61          Msg.info("Sending \"" + todo[i].getName()+ "\" to \"" + slaves[i % slaveCount].getName() + "\"");
62          
63          if((Host.currentHost()).equals(slaves[i % slaveCount])) 
64            Msg.info("Hey ! It's me ! ");
65          
66          slaves[i % slaveCount].put(channel, todo[i]);
67       }
68       
69       Msg.info("Send completed");
70       
71       Msg.info("All tasks have been dispatched. Let's tell everybody the computation is over.");
72       
73       for (int i = 0; i < slaveCount; i++) {
74          slaves[i].put(channel, new FinalizeTask());
75       }
76       
77       Msg.info("Goodbye now!");
78     }
79 }