Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More java approach with a specific task type for finalization instead of magic value...
[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       Msg.info("hello!");
16         
17       int slaveCount = 0;
18       Host[] slaves = null;
19       
20       Msg.info("argc="+args.length);
21       for (int i = 0; i<args.length; i++)           
22         Msg.info("argv:"+args[i]);
23       
24       if (args.length < 3) {
25          Msg.info("Master needs 3 arguments");
26          System.exit(1);
27       }
28       
29       int numberOfTasks = Integer.valueOf(args[0]).intValue();          
30       double taskComputeSize = Double.valueOf(args[1]).doubleValue();           
31       double taskCommunicateSize = Double.valueOf(args[2]).doubleValue();
32       BasicTask[] todo = new BasicTask[numberOfTasks];
33       
34       String taskName = null;
35       
36       for (int i = 0; i < numberOfTasks; i++) {
37          todo[i] = new BasicTask("Task_" + i, taskComputeSize, taskCommunicateSize); 
38       }
39       
40       slaveCount = args.length - 3;
41       slaves = new Host[slaveCount];
42       
43       for(int i = 3; i < args.length ; i++)  {
44          try {
45             slaves[i-3] = Host.getByName(args[i]);
46          }
47          catch(HostNotFoundException e) {
48             Msg.info("Unknown host " + args[i] + ". Stopping Now!");
49             e.printStackTrace();
50             System.exit(1);
51          }
52       }
53       
54       Msg.info("Got "+  slaveCount + " slave(s) :");
55       
56       for (int i = 0; i < slaveCount; i++)
57         Msg.info("\t"+slaves[i].getName());
58       
59       Msg.info("Got "+ numberOfTasks + " task to process.");
60       
61       Channel channel = new Channel(0);
62       
63       for (int i = 0; i < numberOfTasks; i++) {
64          Msg.info("Sending \"" + todo[i].getName()+ "\" to \"" + slaves[i % slaveCount].getName() + "\"");
65          
66          if((Host.currentHost()).equals(slaves[i % slaveCount])) 
67            Msg.info("Hey ! It's me ! ");
68          
69          channel.put(todo[i], slaves[i % slaveCount]);
70       }
71       
72       Msg.info("Send completed");
73       
74       Msg.info("All tasks have been dispatched. Let's tell everybody the computation is over.");
75       
76       for (int i = 0; i < slaveCount; i++) {
77          channel.put(new FinalizeTask(),slaves[i]);
78       }
79       
80       Msg.info("Goodbye now!");
81     }
82 }