Logo AND Algorithmique Numérique Distribuée

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