start of new file
[IRC.git] / Robust / src / Benchmarks / Prefetch / SOR / java / 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     try {
90       thobjects[0].join();
91     }
92     catch (InterruptedException e) {}
93
94
95     for(int i=1;i<numthreads;i++) {
96       try {
97         thobjects[i].join();
98       }
99       catch (InterruptedException e) {}
100     }
101
102     JGFInstrumentor.stopTimer("Section2:SOR:Kernel", instr.timers);
103
104     for (int i=1; i<Nm1; i++) {
105       for (int j=1; j<Nm1; j++) {
106         sor.Gtotal += G[i][j];
107       }
108     }               
109
110   }
111
112   private long[][] init_sync(int nthreads, int cachelinesize) {
113     long sync[][] = new long [nthreads][cachelinesize];
114     for (int i = 0; i<nthreads; i++)
115       sync[i][0] = 0;
116     return sync;
117   }
118
119   public void JGFvalidate(){
120
121     double refval[] = {0.498574406322512,1.1234778980135105,1.9954895063582696};
122     double dev = Math.abs(Gtotal - refval[size]);
123     if (dev > 1.0e-12 ){
124       System.out.println("Validation failed");
125       System.out.println("Gtotal = " + Gtotal + "  " + dev + "  " + size);
126     }
127   }
128
129   /*
130      public void JGFtidyup(){
131      System.gc();
132      }  
133
134      public void JGFrun(int size){
135
136
137      JGFInstrumentor.addTimer("Section2:SOR:Kernel", "Iterations",size);
138
139      JGFsetsize(size); 
140      JGFinitialise(); 
141      JGFkernel(); 
142      JGFvalidate(); 
143      JGFtidyup(); 
144
145
146      JGFInstrumentor.addOpsToTimer("Section2:SOR:Kernel", (double) (JACOBI_NUM_ITER));
147
148      JGFInstrumentor.printTimer("Section2:SOR:Kernel"); 
149      }
150      */
151
152   public double[][] RandomMatrix(int M, int N, Random R)
153   {
154     double A[][] = new double[M][N];
155
156     for (int i=0; i<N; i++)
157       for (int j=0; j<N; j++)
158       {
159         A[i][j] = R.nextDouble() * 1e-6;
160       }      
161     return A;
162   }
163
164
165 }