standardize the benchmarks so we script them easily
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / dsm / MatrixMultiplyNrun.java
1 public class MatrixMultiply extends Thread{
2     MMul mmul;
3     public int x0, y0, x1, y1;
4     
5     public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
6         this.mmul = mmul;
7         this.x0 = x0;
8         this.y0 = y0;
9         this.x1 = x1;
10         this.y1 = y1;
11     }
12     
13     public void run() {
14       atomic {
15         double la[][][]=mmul.a;
16         double lc[][][]=mmul.c;
17         double lb[][][]=mmul.btranspose;
18         int M=mmul.M;
19         int P=mmul.P;
20         //Use btranspose for cache performance
21         for(int q=0;q<P;q++) {
22             double ra[][]=la[q];
23             double rb[][]=lb[q];
24             double rc[][]=lc[q];
25             for(int i = x0; i< x1; i++){
26                 double a[]=ra[i];
27                 double c[]=rc[i];
28                 for (int j = y0; j < y1; j++) {
29                     double innerProduct=0;
30                     double b[] = rb[j];
31                     for(int k = 0; k < M; k++) {
32                         innerProduct += a[k] *b[k];
33                     }
34                     c[j]=innerProduct;
35                 }
36             }
37         }
38       }
39     }
40     
41     public static void main(String[] args) {
42         int NUM_THREADS = 4;
43         int SIZE=150;
44         int NUM_MATRIX = 1;
45         if (args.length>0) {
46             NUM_THREADS=Integer.parseInt(args[0]);
47             if (args.length>1) {
48                 SIZE=Integer.parseInt(args[1]);
49                 if (args.length>2)
50                     NUM_MATRIX=Integer.parseInt(args[2]);
51             }
52         }
53         
54         int[] mid = new int[8];
55         int[] mid = new int[8];
56         mid[0] = (128<<24)|(195<<16)|(136<<8)|162; //dw-10
57         mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dw-11
58         mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dw-12
59         mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dw-13
60         mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dw-14
61         mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dw-15
62         mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dw-16
63         mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dw-17
64
65         int p, q, r;
66         MatrixMultiply[] mm;
67         MatrixMultiply tmp;
68         MMul matrix;
69
70         atomic {
71             matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
72             matrix.setValues();
73             matrix.transpose();
74             mm = global new MatrixMultiply[NUM_THREADS];
75             int increment=SIZE/NUM_THREADS;
76             int base=0;
77             for(int i=0;i<NUM_THREADS;i++) {
78                 if ((i+1)==NUM_THREADS)
79                     mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
80                 else
81                     mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
82                 base+=increment;
83             }
84             p = matrix.L;
85             q = matrix.M;
86             r = matrix.N;
87         }
88         
89       // start a thread to compute each c[l,n]
90       for (int i = 0; i < NUM_THREADS; i++) {
91         atomic {
92           tmp = mm[i];
93         }
94         tmp.start(mid[i]);
95       }
96
97       // wait for them to finish
98       for (int i = 0; i < NUM_THREADS; i++) {
99         atomic {
100           tmp = mm[i];
101         }
102         tmp.join();
103       }
104     
105         // print out the result of the matrix multiply
106         System.printString("Finished\n");
107     }
108 }
109
110 public class MMul{
111
112     public int L, M, N, P;
113     public double[][][] a;
114     public double[][][] b;
115     public double[][][] c;
116     public double[][][] btranspose;
117     
118     public MMul(int P, int L, int M, int N) {
119         this.L = L;
120         this.M = M;
121         this.N = N;
122         this.P = P;
123         a = global new double[P][L][M];  
124         b = global new double[P][M][N]; 
125         c = global new double[P][L][N]; 
126         btranspose = global new double[P][N][M];
127     }
128
129     public void setValues() {
130         for(int q = 0; q < P; q++) {
131             for(int i = 0; i < L; i++) {
132                 double ai[] = a[q][i];
133                 for(int j = 0; j < M; j++) {
134                     ai[j] = j+1;
135                 }
136             }
137             
138             for(int i = 0; i < M; i++) {
139                 double bi[] = b[q][i];
140                 for(int j = 0; j < N; j++) {
141                     bi[j] = j+1;
142                 }
143             }
144         }
145     }
146     
147     public void transpose() {
148         for(int q=0;q<P;q++) {
149             double br[][]=b[q];
150             double bt[][]=btranspose[q];
151             for(int row = 0; row < M; row++) {
152                 double brow[] = br[row];
153                 for(int col = 0; col < N; col++) {
154                     bt[col][row] = brow[col];
155                 }
156             }
157         }
158     }
159 }