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