changes
[IRC.git] / Robust / src / Benchmarks / Prefetch / SOR / dsm / JGFSORBench.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 public class JGFSORBench { 
21
22   int size; 
23   int[] datasizes;
24   int JACOBI_NUM_ITER;
25   long RANDOM_SEED;
26   public int nthreads;
27   Random R;
28   public double Gtotal;
29   public int cachelinesize;
30   public long sync[][];
31
32   public JGFSORBench(int nthreads){
33     this.nthreads = nthreads;
34     datasizes = global new int[3];
35     datasizes[0] = 1000;
36     datasizes[1] = 1500;
37     datasizes[2] = 2000;
38     JACOBI_NUM_ITER = 100;
39     RANDOM_SEED = 10101010;
40     R = global new Random(RANDOM_SEED);
41     Gtotal = 0.0;
42     cachelinesize = 1;
43   }
44
45   public void JGFsetsize(int size){
46     this.size = size;
47   }
48
49   public static void JGFkernel(JGFSORBench sor) {
50     int numthreads, datasize;
51     Random rand;
52     atomic {
53       numthreads = sor.nthreads;
54       rand = sor.R;
55       datasize = sor.datasizes[sor.size];
56     }
57
58     double[][] G;
59     int M, N;
60     atomic {
61       G = sor.RandomMatrix(datasize, datasize, rand);
62       M = G.length;
63       N = G[0].length;
64     }
65     double omega = 1.25;
66     int num_iterations;
67     atomic {
68       num_iterations = sor.JACOBI_NUM_ITER;
69     }
70
71     double omega_over_four = omega * 0.25;
72     double one_minus_omega = 1.0 - omega;
73
74     // update interior points
75     //
76     int Mm1 = M-1;
77     int Nm1 = N-1;
78
79     //spawn threads
80     int tmpcachelinesize;
81     atomic {
82       tmpcachelinesize = sor.cachelinesize;
83     }
84
85     SORRunner[] thobjects;
86     atomic {
87       thobjects = global new SORRunner[numthreads];
88       sor.sync = sor.init_sync(numthreads, tmpcachelinesize);
89     }
90
91     //JGFInstrumentor.startTimer("Section2:SOR:Kernel", instr.timers); 
92
93     SORRunner tmp;
94     int[] mid = new int[4];
95     mid[0] = (128<<24)|(195<<16)|(175<<8)|79;
96     mid[1] = (128<<24)|(195<<16)|(175<<8)|80;
97     mid[2] = (128<<24)|(195<<16)|(175<<8)|73;
98     mid[3] = (128<<24)|(195<<16)|(175<<8)|78;
99     for(int i=1;i<numthreads;i++) {
100       atomic {
101         thobjects[i] =  global new SORRunner(i,omega,G,num_iterations,sor.sync,numthreads);
102         tmp = thobjects[i];
103       }
104       tmp.start(mid[i]);
105     }
106     atomic {
107       thobjects[0] =  global new SORRunner(0,omega,G,num_iterations,sor.sync,numthreads);
108       tmp = thobjects[0];
109     }
110     tmp.start(mid[0]);
111     tmp.join();
112
113     for(int i=1;i<numthreads;i++) {
114       atomic {
115         tmp = thobjects[i];
116       }
117       tmp.join();
118     }
119
120     //JGFInstrumentor.stopTimer("Section2:SOR:Kernel", instr.timers);
121     atomic {
122       for (int i=1; i<Nm1; i++) {
123         for (int j=1; j<Nm1; j++) {
124           sor.Gtotal += G[i][j];
125         }
126       }               
127     }
128   }
129
130   public long[][] init_sync(int nthreads, int cachelinesize) {
131     long[][] sync;
132     sync = global new long [nthreads][cachelinesize];
133     for (int i = 0; i<nthreads; i++)
134       sync[i][0] = 0;
135     return sync;
136   }
137
138   public double[][] RandomMatrix(int M, int N, Random R)
139   {
140     double A[][] = global new double[M][N];
141
142     for (int i=0; i<N; i++)
143       for (int j=0; j<N; j++)
144       {
145         A[i][j] = R.nextDouble() * 1e-6;
146       }      
147     return A;
148   }
149
150   public int JGFvalidate(){
151
152     double refval[];
153     refval = new double[3];
154     refval[0] = 0.498574406322512;
155     refval[1] = 1.1234778980135105;
156     refval[2] = 1.9954895063582696;
157     double dev = Math.fabs(Gtotal - refval[size]);
158     long l = (long) refval[size] * 1000000;
159     long r = (long) Gtotal * 1000000;
160     if (l != r ){
161       //System.printString("Validation failed");
162       //System.printString("Gtotal = " + (long) Gtotal * 1000000 + "  " +(long) dev * 1000000 + "  " + size);
163       return 1;
164     } else {
165       return 0;
166     }
167   }
168 }