2 * Master of a basic master/slave example in Java
4 * Copyright 2006-2012 The SimGrid Team. All rights reserved.
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.
11 import java.util.ArrayList;
13 import org.simgrid.msg.Comm;
14 import org.simgrid.msg.Host;
15 import org.simgrid.msg.Msg;
16 import org.simgrid.msg.MsgException;
17 import org.simgrid.msg.Process;
18 import org.simgrid.msg.Task;
20 public class Master extends Process {
21 public Master(Host host, String name, String[]args) {
22 super(host,name,args);
24 public void main(String[] args) throws MsgException {
25 if (args.length < 4) {
26 Msg.info("Master needs 4 arguments");
30 int tasksCount = Integer.valueOf(args[0]).intValue();
31 double taskComputeSize = Double.valueOf(args[1]).doubleValue();
32 double taskCommunicateSize = Double.valueOf(args[2]).doubleValue();
34 int slavesCount = Integer.valueOf(args[3]).intValue();
36 Msg.info("Hello! Got "+ slavesCount + " slaves and "+tasksCount+" tasks to process");
37 ArrayList<Comm> comms = new ArrayList<Comm>();
39 for (int i = 0; i < tasksCount; i++) {
40 Task task = new Task("Task_" + i, taskComputeSize, taskCommunicateSize);
41 Msg.info("Sending \"" + task.getName()+ "\" to \"slave_" + i % slavesCount + "\"");
42 //task.send("slave_"+(i%slavesCount));
43 Comm comm = task.isend("slave_"+(i%slavesCount));
47 while (comms.size() > 0) {
48 for (int i = 0; i < comms.size(); i++) {
50 if (comms.get(i).test()) {
62 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 for (int i = 0; i < slavesCount; i++) {
65 FinalizeTask task = new FinalizeTask();
66 task.dsend("slave_"+(i%slavesCount));
70 Msg.info("Goodbye now!");