File: RelayRunner.java

package info (click to toggle)
simgrid 3.21%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,972 kB
  • sloc: cpp: 88,193; ansic: 69,244; fortran: 6,089; f90: 5,162; xml: 4,861; java: 4,250; perl: 2,056; python: 1,193; sh: 1,159; makefile: 57; sed: 6
file content (57 lines) | stat: -rw-r--r-- 2,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Copyright (c) 2016-2018. The SimGrid Team. All rights reserved.          */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

package app.tokenring;
import org.simgrid.msg.Host;
import org.simgrid.msg.Msg;
import org.simgrid.msg.MsgException;
import org.simgrid.msg.Process;
import org.simgrid.msg.Task;

public class RelayRunner extends Process {

	private static final int TASK_COMM_SIZE = 1000000; /* The token is 1MB long*/

	public RelayRunner(Host host, String name, String[]args) {
		super(host,name,args);
	}

	/* This is the function executed by this kind of processes */
	@Override
	public void main(String[] args) throws MsgException {
		// In this example, the processes are given numerical names: "0", "1", "2", and so on 
		int rank = Integer.parseInt(this.getName());

		if (rank == 0) {
			/* The root (rank 0) first sends the token then waits to receive it back */
			
			String mailbox = "1"; 
			Task token = new Task("Token", 0/* no computation associated*/ , TASK_COMM_SIZE );

			Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
			token.send(mailbox);
			
			token = Task.receive(this.getName()); // Get a message from the mailbox having the same name as the current processor
			
			Msg.info("Host '"+rank+"' received '"+token.getName()+"'");

		} else {
		    /* The others processes receive on their name (coming from their left neighbor -- rank-1)
		     * and send to their right neighbor (rank+1) */
			Task token = Task.receive(this.getName());
			
		    Msg.info("Host '"+rank+"' received '"+token.getName()+"'");

		    String mailbox = Integer.toString(rank+1);
		    if (rank+1 == Host.getCount()) {
		    	/* The last process has no right neighbor, so it sends the token back to rank 0 */
		    	mailbox = "0";
		    }
		    
		    Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
		    token.send(mailbox);
		}
	}
}