start of new file
[IRC.git] / Robust / src / Benchmarks / Prefetch / SOR / dsm / SORRunner2.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     double S[][];
27     int sync[][];
28   int nthreads;
29
30   public SORRunner(int id, double omega, double G[][], int num_iterations,int[][] sync, int nthreads) {
31     this.id = id;
32     this.omega=omega;
33     this.G=G;
34     this.num_iterations=num_iterations;
35     this.sync=sync;
36     this.nthreads = nthreads;
37   }
38
39   public void run() {
40     int tmpid, M, N, numthreads;
41     double omega_over_four, one_minus_omega;
42     int numiterations;
43     atomic {
44       M = G.length;
45       N = G[0].length;
46       omega_over_four = omega * 0.25;
47       one_minus_omega = 1.0 - omega;
48       numthreads = nthreads;
49       tmpid = id;
50       numiterations = num_iterations;
51     }
52
53     // update interior points
54     //
55     int Mm1 = M-1;
56     int Nm1 = N-1;
57
58
59     int ilow, iupper, slice, tslice, ttslice;
60
61     tslice = (Mm1) / 2;
62     ttslice = (tslice + numthreads-1)/numthreads;
63     slice = ttslice*2;
64     ilow=tmpid*slice+1;
65     iupper = ((tmpid+1)*slice)+1;
66     if (iupper > Mm1) iupper =  Mm1+1;
67     if (tmpid == (numthreads-1)) iupper = Mm1+1;
68
69     for (int p=-2; p<2*numiterations; p++) {
70         if (p<0) {
71             int l=ilow;
72             if (l==1)
73                 l=0;
74             if (p==-2) {
75                 atomic {
76                     S=global new double[iupper-l][N];
77                     for (int i=l; i<iupper; i++) {
78                         double q[]=S[i-l];
79                         double r[]=G[i];
80                         for(int j=0;j<N;j++) {
81                             q[j]=r[j];
82                         }
83                     }
84                 }
85             } else {
86                 atomic {
87                     for (int i=l; i<iupper; i++) {
88                         G[i]=S[i-l];
89                     }
90                 }
91             }
92         } else {
93       atomic {
94         for (int i=ilow+(p%2); i<iupper; i=i+2) {
95
96           double [] Gi = G[i];
97           double [] Gim1 = G[i-1];
98
99           if(i == 1) { 
100             double [] Gip1 = G[i+1];
101
102             for (int j=1; j<Nm1; j=j+2){
103               Gi[j] = omega_over_four * (Gim1[j] + Gip1[j] + Gi[j-1]
104                   + Gi[j+1]) + one_minus_omega * Gi[j];
105
106             }
107           } else if (i == Mm1) {
108
109             double [] Gim2 = G[i-2];
110
111             for (int j=1; j<Nm1; j=j+2){
112               if((j+1) != Nm1) {
113                 Gim1[j+1]=omega_over_four * (Gim2[j+1] + Gi[j+1] + Gim1[j]
114                     + Gim1[j+2]) + one_minus_omega * Gim1[j+1];
115               }
116             }
117
118           } else {
119
120             double [] Gip1 = G[i+1];
121             double [] Gim2 = G[i-2];
122
123             for (int j=1; j<Nm1; j=j+2){
124               Gi[j] = omega_over_four * (Gim1[j] + Gip1[j] + Gi[j-1]
125                   + Gi[j+1]) + one_minus_omega * Gi[j];
126
127               if((j+1) != Nm1) {
128                 Gim1[j+1]=omega_over_four * (Gim2[j+1] + Gi[j+1] + Gim1[j]
129                     + Gim1[j+2]) + one_minus_omega * Gim1[j+1];
130               }
131             }
132           }
133         }
134       } //close atomic
135         }
136
137       int ourcount;
138       boolean done=true;
139       atomic {
140         // Signal this thread has done iteration
141         sync[tmpid][0]++;
142         ourcount=sync[tmpid][0];
143       }
144
145       // Wait for neighbours;
146       while(done) {
147         atomic {
148           if ((tmpid==0 || ourcount <= sync[tmpid-1][0])
149               &&((tmpid==(numthreads-1))||ourcount<=sync[tmpid+1][0]))
150             done=false;
151         }
152       }
153
154       System.clearPrefetchCache();
155     }//end of for
156   } //end of run()
157 }