start of new file
[IRC.git] / Robust / src / Benchmarks / Prefetch / Moldyn / dsm / 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 public class TournamentBarrier {
26   // Array of flags indicating whether the given process and all those
27   // for which it is responsible have finished. The "sense" of this
28   // array alternates with each barrier, to prevent having to
29   // reinitialise.
30   boolean[] IsDone;
31   public int maxBusyIter;
32   int numThreads;
33
34   public TournamentBarrier(int n) {
35     numThreads = n;
36     maxBusyIter = 1;
37     // Superclass constructor should record the number of threads
38     // and thread manager.
39     //super(n);
40
41     // Initialise the IsDone array. The choice of initial value is
42     // arbitrary, but must be consistent!
43     IsDone = global new boolean[numThreads];
44     for(int i = 0; i < n; i++) {
45       IsDone[i] = false;
46     }
47   }
48
49   // Uses the manager's debug function, so this can only be used after
50   // construction!
51   public void debug(String s) {
52     // System.err.println("Debug message");
53   }
54
55   public void setMaxBusyIter(int b) {
56     maxBusyIter = b;
57   }
58
59   public void DoBarrier(int myid) {
60     int b;
61     // debug("Thread " + myid + " checking in");
62
63     int roundmask = 3;
64     boolean donevalue = !IsDone[myid];
65
66     while(((myid & roundmask) == 0) && (roundmask<(numThreads<<2))) {
67       int spacing = (roundmask+1) >> 2;
68       for(int i=1; i<=3 && myid+i*spacing < numThreads; i++) {
69         // debug("Thread " + myid + " waiting for thread " + (myid+i*spacing));
70         b = maxBusyIter;
71         while(IsDone[myid+i*spacing] != donevalue) {
72           b--;
73           if(b==0) {
74            //Thread.yield();
75             b = maxBusyIter;
76           }
77         }
78       }
79       roundmask = (roundmask << 2) + 3;
80     }
81     // debug("Thread " + myid + " reporting done");
82     IsDone[myid] = donevalue;
83     b = maxBusyIter;
84     while(IsDone[0] != donevalue) {
85       b--;
86       if(b==0) {
87         //Thread.yield();
88         b = maxBusyIter;
89       }
90     }
91     //debug("Thread " + myid + " checking out");
92
93   }
94 }