start of new file
[IRC.git] / Robust / src / Benchmarks / Prefetch / LUFact / java / TournamentBarrier.java
1 /**************************************************************************
2  *                                                                         *
3  *         Java Grande Forum Benchmark Suite - Thread Version 1.0          *
4  *                                                                         *
5  *                            produced by                                  *
6  *                                                                         *
7  *                  Java Grande Benchmarking Project                       *
8  *                                                                         *
9  *                                at                                       *
10  *                                                                         *
11  *                Edinburgh Parallel Computing Centre                      *
12  *                                                                         *
13  *                email: epcc-javagrande@epcc.ed.ac.uk                     *
14  *                                                                         *
15  *                                                                         *
16  *      This version copyright (c) The University of Edinburgh, 2001.      *
17  *                         All rights reserved.                            *
18  *                                                                         *
19  **************************************************************************/
20
21 // This implements a simple tournament-based barrier, using entirely its
22 // own synchronisation. At present Yield() is called to stop busy-waiting
23 // processes hogging the processor(s)!
24
25 import java.util.*;
26
27 public class TournamentBarrier {
28   // Array of flags indicating whether the given process and all those
29   // for which it is responsible have finished. The "sense" of this
30   // array alternates with each barrier, to prevent having to
31   // reinitialise.
32   boolean[] IsDone;
33   public int maxBusyIter;
34   int numThreads;
35
36   public TournamentBarrier(int n) {
37     numThreads = n;
38     maxBusyIter = 1;
39     // Superclass constructor should record the number of threads
40     // and thread manager.
41     //super(n);
42
43     // Initialise the IsDone array. The choice of initial value is
44     // arbitrary, but must be consistent!
45     IsDone = new boolean[numThreads];
46     for(int i = 0; i < n; i++) {
47       IsDone[i] = false;
48     }
49   }
50
51   // Uses the manager's debug function, so this can only be used after
52   // construction!
53   public void debug(String s) {
54     // System.err.println("Debug message");
55   }
56
57   public void setMaxBusyIter(int b) {
58     maxBusyIter = b;
59   }
60
61   public void DoBarrier(int myid) {
62     int b;
63     // debug("Thread " + myid + " checking in");
64
65     int roundmask = 3;
66     boolean donevalue = !IsDone[myid];
67
68     while(((myid & roundmask) == 0) && (roundmask<(numThreads<<2))) {
69       int spacing = (roundmask+1) >> 2;
70       for(int i=1; i<=3 && myid+i*spacing < numThreads; i++) {
71         // debug("Thread " + myid + " waiting for thread " + (myid+i*spacing));
72         b = maxBusyIter;
73         while(IsDone[myid+i*spacing] != donevalue) {
74           b--;
75           if(b==0) {
76            //Thread.yield();
77             b = maxBusyIter;
78           }
79         }
80       }
81       roundmask = (roundmask << 2) + 3;
82     }
83     // debug("Thread " + myid + " reporting done");
84     IsDone[myid] = donevalue;
85     b = maxBusyIter;
86     while(IsDone[0] != donevalue) {
87       b--;
88       if(b==0) {
89         //Thread.yield();
90         b = maxBusyIter;
91       }
92     }
93     //debug("Thread " + myid + " checking out");
94
95   }
96 }