Search in sources :

Example 1 with WorkerRegion

use of edu.rit.pj.WorkerRegion in project ffx by mjschnie.

the class Runner method main.

// Main program.
/**
 * Main program.
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        usage();
    }
    // Initialize world communicator.
    Comm.init(args);
    world = Comm.world();
    rank = world.rank();
    // Set up worker team.
    team = new WorkerTeam();
    // Master process sets up job generator.
    if (rank == team.masterRank()) {
        omitted = new HashSet<Integer>();
        // Assume argument is a checkpoint file name and try to read it.
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File(args[0]));
        } catch (IOException exc) {
        }
        // Read checkpoint file.
        if (scanner != null) {
            while (scanner.hasNextLine()) {
                Scanner linescanner = new Scanner(scanner.nextLine());
                String word;
                int jobnum;
                if (!linescanner.hasNext()) {
                    continue;
                }
                word = linescanner.next();
                if (!word.equals("***")) {
                    continue;
                }
                if (!linescanner.hasNext()) {
                    continue;
                }
                word = linescanner.next();
                if (word.equals("Generator")) {
                    if (!linescanner.hasNext()) {
                        continue;
                    }
                    if (generatorExpression == null) {
                        generatorExpression = linescanner.next();
                    }
                } else if (word.equals("Job")) {
                    if (!linescanner.hasNextInt()) {
                        continue;
                    }
                    jobnum = linescanner.nextInt();
                    if (!linescanner.hasNext()) {
                        continue;
                    }
                    word = linescanner.next();
                    if (word.equals("finished")) {
                        omitted.add(jobnum);
                    }
                }
            }
            scanner.close();
        } else // Assume argument is a job generator constructor expression.
        {
            generatorExpression = args[0];
        }
        // Create job generator.
        if (generatorExpression == null) {
            stderr.printf("Runner: No job generator in checkpoint file %s%n", args[0]);
        } else {
            try {
                generator = (JobGenerator) Instance.newInstance(generatorExpression);
                stdout.printf("*** Generator %s%n", generatorExpression);
                generator.omit(omitted);
            } catch (Throwable exc) {
                stderr.printf("Runner: Could not create job generator %s%n", generatorExpression);
                exc.printStackTrace(stderr);
            }
        }
    }
    // Abort every process if job generator was not created.
    BooleanItemBuf buf = BooleanBuf.buffer(generator != null);
    world.broadcast(team.masterRank(), buf);
    if (!buf.item) {
        System.exit(1);
    }
    // Generate and run jobs.
    team.execute(new WorkerRegion() {

        public void run() throws Exception {
            execute(generator, new WorkerIteration<Job>() {

                public void sendTaskInput(Job job, Comm comm, int wRank, int tag) {
                    stdout.printf("*** Job %d started %s %s%n", job.getJobNumber(), new Date(), job.getDescription());
                }

                public void run(Job job) {
                    job.run();
                }

                public void receiveTaskOutput(Job job, Comm comm, int wRank, int tag) {
                    stdout.printf("*** Job %d finished %s%n", job.getJobNumber(), new Date());
                }
            });
        }
    });
    if (rank == team.masterRank()) {
        stdout.printf("*** All jobs finished%n");
    }
}
Also used : Scanner(java.util.Scanner) WorkerIteration(edu.rit.pj.WorkerIteration) IOException(java.io.IOException) Comm(edu.rit.pj.Comm) IOException(java.io.IOException) Date(java.util.Date) WorkerTeam(edu.rit.pj.WorkerTeam) WorkerRegion(edu.rit.pj.WorkerRegion) BooleanItemBuf(edu.rit.mp.buf.BooleanItemBuf) File(java.io.File)

Aggregations

BooleanItemBuf (edu.rit.mp.buf.BooleanItemBuf)1 Comm (edu.rit.pj.Comm)1 WorkerIteration (edu.rit.pj.WorkerIteration)1 WorkerRegion (edu.rit.pj.WorkerRegion)1 WorkerTeam (edu.rit.pj.WorkerTeam)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Scanner (java.util.Scanner)1