Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert all xml files to the new DTD v4
[simgrid.git] / examples / java / async / Master.java
1 /* Master of a basic master/slave example in Java */
2
3 /* Copyright (c) 2006-2014. The SimGrid Team.
4  * 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 package async;
10 import java.util.ArrayList;
11
12 import org.simgrid.msg.Comm;
13 import org.simgrid.msg.Host;
14 import org.simgrid.msg.Msg;
15 import org.simgrid.msg.MsgException;
16 import org.simgrid.msg.Process;
17 import org.simgrid.msg.Task;
18
19 public class Master extends Process {
20         public Master(Host host, String name, String[]args) {
21                 super(host,name,args);
22         }
23         public void main(String[] args) throws MsgException {
24                 if (args.length < 4) {
25                         Msg.info("Master needs 4 arguments");
26                         System.exit(1);
27                 }
28
29                 int tasksCount = Integer.valueOf(args[0]).intValue();           
30                 double taskComputeSize = Double.valueOf(args[1]).doubleValue();         
31                 double taskCommunicateSize = Double.valueOf(args[2]).doubleValue();
32
33                 int slavesCount = Integer.valueOf(args[3]).intValue();
34
35                 Msg.info("Hello! Got "+  slavesCount + " slaves and "+tasksCount+" tasks to process");
36                 ArrayList<Comm> comms = new ArrayList<Comm>();
37                 
38                 for (int i = 0; i < tasksCount; i++) {
39                         Task task = new Task("Task_" + i, taskComputeSize, taskCommunicateSize); 
40                         Msg.info("Sending \"" + task.getName()+ "\" to \"slave_" + i % slavesCount + "\"");
41                         //task.send("slave_"+(i%slavesCount));
42                         Comm comm = task.isend("slave_"+(i%slavesCount));
43                         comms.add(comm);
44                 }
45                 
46                 while (comms.size() > 0) {
47                         for (int i = 0; i < comms.size(); i++) {
48                                 try {
49                                         if (comms.get(i).test()) {
50                                                 comms.remove(i);
51                                                 i--;
52                                         }
53                                 }
54                                 catch (Exception e) {
55                                         e.printStackTrace();
56                                 }
57                         }
58                         waitFor(1);
59                 }
60                 
61                 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.");
62
63                 for (int i = 0; i < slavesCount; i++) {
64                         FinalizeTask task = new FinalizeTask();
65                         task.dsend("slave_"+(i%slavesCount));
66                 }
67                 waitFor(20);
68
69                 Msg.info("Goodbye now!");
70         }
71 }