changes
[IRC.git] / Robust / src / Benchmarks / Prefetch / SOR / dsm / SORRunner.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  *      adapted from SciMark 2.0, author Roldan Pozo (pozo@cam.nist.gov)   *
16  *                                                                         *
17  *      This version copyright (c) The University of Edinburgh, 2001.      *
18  *                         All rights reserved.                            *
19  *                                                                         *
20  **************************************************************************/
21
22 class SORRunner extends Thread {
23
24   int id,num_iterations;
25   double G[][],omega;
26   long sync[][];
27   int nthreads;
28
29   public SORRunner(int id, double omega, double G[][], int num_iterations,long[][] sync, int nthreads) {
30     this.id = id;
31     this.omega=omega;
32     this.G=G;
33     this.num_iterations=num_iterations;
34     this.sync=sync;
35     this.nthreads = nthreads;
36   }
37
38   public void run() {
39     int tmpid, M, N, numthreads;
40     double omega_over_four, one_minus_omega;
41     int numiterations;
42     atomic {
43       M = G.length;
44       N = G[0].length;
45       omega_over_four = omega * 0.25;
46       one_minus_omega = 1.0 - omega;
47       numthreads = nthreads;
48       tmpid = id;
49       numiterations = num_iterations;
50     }
51
52     // update interior points
53     //
54     int Mm1 = M-1;
55     int Nm1 = N-1;
56
57
58     int ilow, iupper, slice, tslice, ttslice;
59
60     tslice = (Mm1) / 2;
61     ttslice = (tslice + numthreads-1)/numthreads;
62     slice = ttslice*2;
63     ilow=tmpid*slice+1;
64     iupper = ((tmpid+1)*slice)+1;
65     if (iupper > Mm1) iupper =  Mm1+1;
66     if (tmpid == (numthreads-1)) iupper = Mm1+1;
67
68       for (int p=0; p<2*numiterations; p++) {
69         atomic {
70           for (int i=ilow+(p%2); i<iupper; i=i+2) {
71
72             double [] Gi = G[i];
73             double [] Gim1 = G[i-1];
74
75             if(i == 1) { 
76               double [] Gip1 = G[i+1];
77
78               for (int j=1; j<Nm1; j=j+2){
79                 Gi[j] = omega_over_four * (Gim1[j] + Gip1[j] + Gi[j-1]
80                     + Gi[j+1]) + one_minus_omega * Gi[j];
81
82               }
83             } else if (i == Mm1) {
84
85               double [] Gim2 = G[i-2];
86
87               for (int j=1; j<Nm1; j=j+2){
88                 if((j+1) != Nm1) {
89                   Gim1[j+1]=omega_over_four * (Gim2[j+1] + Gi[j+1] + Gim1[j]
90                       + Gim1[j+2]) + one_minus_omega * Gim1[j+1];
91                 }
92               }
93
94             } else {
95
96               double [] Gip1 = G[i+1];
97               double [] Gim2 = G[i-2];
98
99               for (int j=1; j<Nm1; j=j+2){
100                 Gi[j] = omega_over_four * (Gim1[j] + Gip1[j] + Gi[j-1]
101                     + Gi[j+1]) + one_minus_omega * Gi[j];
102
103                 if((j+1) != Nm1) {
104                   Gim1[j+1]=omega_over_four * (Gim2[j+1] + Gi[j+1] + Gim1[j]
105                       + Gim1[j+2]) + one_minus_omega * Gim1[j+1];
106                 }
107               }
108             }
109           }
110         } //close atomic
111         // Signal this thread has done iteration
112
113         // Wait for neighbours;
114         long ourcount;
115         boolean done=true;
116
117         atomic {
118           sync[tmpid][0]++;
119           ourcount=sync[tmpid][0];
120         }
121
122         while(done) {
123             atomic {
124                 if ((tmpid==0 || ourcount <= sync[tmpid-1][0])
125                     &&((tmpid==(numthreads-1))||ourcount<=sync[tmpid+1][0]))
126                     done=false;
127             }
128         }
129
130       }//end of for
131   } //end of run()
132 }