Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add isend in Task
[simgrid.git] / examples / async / Master.java
1 /*
2  * Master of a basic master/slave example in Java
3  *
4  * Copyright 2006,2007,2010 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.Msg;
15 import org.simgrid.msg.MsgException;
16 import org.simgrid.msg.Task;
17 import org.simgrid.msg.Process;;
18
19 public class Master extends Process {
20         public void main(String[] args) throws MsgException {
21                 if (args.length < 4) {
22                         Msg.info("Master needs 4 arguments");
23                         System.exit(1);
24                 }
25
26                 int tasksCount = Integer.valueOf(args[0]).intValue();           
27                 double taskComputeSize = Double.valueOf(args[1]).doubleValue();         
28                 double taskCommunicateSize = Double.valueOf(args[2]).doubleValue();
29
30                 int slavesCount = Integer.valueOf(args[3]).intValue();
31
32                 Msg.info("Hello! Got "+  slavesCount + " slaves and "+tasksCount+" tasks to process");
33                 
34                 ArrayList<Comm> comms = new ArrayList<Comm>();
35                 
36                 for (int i = 0; i < tasksCount; i++) {
37                         Task task = new Task("Task_" + i, taskComputeSize, taskCommunicateSize); 
38                         Msg.info("Sending \"" + task.getName()+ "\" to \"slave_" + i % slavesCount + "\"");
39                         //task.send("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                         simulatedSleep(1);
57                 }
58                 
59                 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.");
60
61                 for (int i = 0; i < slavesCount; i++) {
62                         FinalizeTask task = new FinalizeTask();
63                         task.dsend("slave_"+(i%slavesCount));
64                 }
65                 simulatedSleep(20);
66
67                 Msg.info("Goodbye now!");
68         }
69 }