Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5f270cb9fb501a0fa75aee250200dcbbbb4aba00
[simgrid.git] / simgrid-java / examples / async / Master.java
1 /*
2  * Master of a basic master/slave example in Java
3  *
4  * Copyright 2006-2012 The SimGrid Team. All rights reserved. 
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. 
8  */
9
10 package async;
11 import java.util.ArrayList;
12
13 import org.simgrid.msg.Comm;
14 import org.simgrid.msg.Host;
15 import org.simgrid.msg.Process;
16 import org.simgrid.msg.Msg;
17 import org.simgrid.msg.MsgException;
18 import org.simgrid.msg.Task;
19 import org.simgrid.msg.Process;;
20
21 public class Master extends Process {
22         public Master(Host host, String name, String[]args) {
23                 super(host,name,args);
24         }
25         public void main(String[] args) throws MsgException {
26                 if (args.length < 4) {
27                         Msg.info("Master needs 4 arguments");
28                         System.exit(1);
29                 }
30
31                 int tasksCount = Integer.valueOf(args[0]).intValue();           
32                 double taskComputeSize = Double.valueOf(args[1]).doubleValue();         
33                 double taskCommunicateSize = Double.valueOf(args[2]).doubleValue();
34
35                 int slavesCount = Integer.valueOf(args[3]).intValue();
36
37                 Msg.info("Hello! Got "+  slavesCount + " slaves and "+tasksCount+" tasks to process");
38                 ArrayList<Comm> comms = new ArrayList<Comm>();
39                 
40                 for (int i = 0; i < tasksCount; i++) {
41                         Task task = new Task("Task_" + i, taskComputeSize, taskCommunicateSize); 
42                         Msg.info("Sending \"" + task.getName()+ "\" to \"slave_" + i % slavesCount + "\"");
43                         //task.send("slave_"+(i%slavesCount));
44                         Comm comm = task.isend("slave_"+(i%slavesCount));
45                         comms.add(comm);
46                 }
47                 
48                 while (comms.size() > 0) {
49                         for (int i = 0; i < comms.size(); i++) {
50                                 try {
51                                         if (comms.get(i).test()) {
52                                                 comms.remove(i);
53                                                 i--;
54                                         }
55                                 }
56                                 catch (Exception e) {
57                                         e.printStackTrace();
58                                 }
59                         }
60                         waitFor(1);
61                 }
62                 
63                 Msg.info("All tasks have been dispatched. Let's tell (asynchronously) everybody the computation is over, and sleep 20s so that nobody gets a message from a terminated process.");
64
65                 for (int i = 0; i < slavesCount; i++) {
66                         FinalizeTask task = new FinalizeTask();
67                         task.dsend("slave_"+(i%slavesCount));
68                 }
69                 waitFor(20);
70
71                 Msg.info("Goodbye now!");
72         }
73 }