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