2e1118f53a74a960994507e3ba5f27b869276724
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / 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         mid[0] = (128<<24)|(195<<16)|(175<<8)|84;//dw-10
56         mid[1] = (128<<24)|(195<<16)|(175<<8)|85;//dw-11
57         mid[2] = (128<<24)|(195<<16)|(175<<8)|86;//dw-12
58         mid[3] = (128<<24)|(195<<16)|(175<<8)|87;//dw-13
59         mid[4] = (128<<24)|(195<<16)|(175<<8)|88;//dw-14
60         mid[5] = (128<<24)|(195<<16)|(175<<8)|89;//dw-15
61         mid[6] = (128<<24)|(195<<16)|(175<<8)|90;//dw-16
62         mid[7] = (128<<24)|(195<<16)|(175<<8)|91;//dw-17
63
64         int p, q, r;
65         MatrixMultiply[] mm;
66         MatrixMultiply tmp;
67         MMul matrix;
68
69         atomic {
70             matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
71             matrix.setValues();
72             matrix.transpose();
73             mm = global new MatrixMultiply[NUM_THREADS];
74             int increment=SIZE/NUM_THREADS;
75             int base=0;
76             for(int i=0;i<NUM_THREADS;i++) {
77                 if ((i+1)==NUM_THREADS)
78                     mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
79                 else
80                     mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
81                 base+=increment;
82             }
83             p = matrix.L;
84             q = matrix.M;
85             r = matrix.N;
86         }
87         
88       // start a thread to compute each c[l,n]
89       for (int i = 0; i < NUM_THREADS; i++) {
90         atomic {
91           tmp = mm[i];
92         }
93         tmp.start(mid[i]);
94       }
95
96       // wait for them to finish
97       for (int i = 0; i < NUM_THREADS; i++) {
98         atomic {
99           tmp = mm[i];
100         }
101         tmp.join();
102       }
103     
104         // print out the result of the matrix multiply
105         System.printString("Finished\n");
106     }
107 }
108
109 public class MMul{
110
111     public int L, M, N, P;
112     public double[][][] a;
113     public double[][][] b;
114     public double[][][] c;
115     public double[][][] btranspose;
116     
117     public MMul(int P, int L, int M, int N) {
118         this.L = L;
119         this.M = M;
120         this.N = N;
121         this.P = P;
122         a = global new double[P][L][M];  
123         b = global new double[P][M][N]; 
124         c = global new double[P][L][N]; 
125         btranspose = global new double[P][N][M];
126     }
127
128     public void setValues() {
129         for(int q = 0; q < P; q++) {
130             for(int i = 0; i < L; i++) {
131                 double ai[] = a[q][i];
132                 for(int j = 0; j < M; j++) {
133                     ai[j] = j+1;
134                 }
135             }
136             
137             for(int i = 0; i < M; i++) {
138                 double bi[] = b[q][i];
139                 for(int j = 0; j < N; j++) {
140                     bi[j] = j+1;
141                 }
142             }
143         }
144     }
145     
146     public void transpose() {
147         for(int q=0;q<P;q++) {
148             double br[][]=b[q];
149             double bt[][]=btranspose[q];
150             for(int row = 0; row < M; row++) {
151                 double brow[] = br[row];
152                 for(int col = 0; col < N; col++) {
153                     bt[col][row] = brow[col];
154                 }
155             }
156         }
157     }
158 }