start of new file
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / java / MatrixMultiply.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 y0, int x1, 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         int localresults[][];
15
16         //compute the results
17         localresults=new int[1+x1-x0][1+y1-y0];
18
19         //Use b transpose for cache performance
20         for(int i = x0; i<= x1; i++){
21             int a[]=mmul.a[i];
22             int M=mmul.M;
23             for (int j = y0; j <= y1; j++) {
24                 int innerProduct=0;
25                 int b[] = mmul.btranspose[j];
26                 for(int k = 0; k < M; k++) {
27                     innerProduct += a[k] *b[k];
28                 }
29                 localresults[i-x0][j-y0]=innerProduct;
30             }
31         }
32
33         //write the results
34         for(int i=x0;i<=x1;i++) {
35             int c[]=mmul.c[i];
36             for(int j=y0;j<=y1;j++) {
37                 c[j]=localresults[i-x0][j-y0];
38             }
39         }
40     }
41
42     public static void main(String[] args) {
43         int NUM_THREADS = 1;
44         int p, q, r;
45         MatrixMultiply[] mm;
46         MatrixMultiply tmp;
47         MMul matrix;
48
49         matrix = new MMul(400, 400, 400);
50         matrix.setValues();
51         matrix.transpose();
52
53         mm = new MatrixMultiply[NUM_THREADS];
54         mm[0] = new MatrixMultiply(matrix,0,0,399,399);
55
56         p = matrix.L;
57         q = matrix.M;
58         r = matrix.N;
59
60         // print out the matrices to be multiplied
61         System.out.print("MatrixMultiply: L=");
62         System.out.print(p);
63         System.out.print("\t");
64         System.out.print("M=");
65         System.out.print(q);
66         System.out.print("\t");
67         System.out.print("N=");
68         System.out.print(r);
69         System.out.print("\n");
70
71         // start a thread to compute each c[l,n]
72         for (int i = 0; i < NUM_THREADS; i++) {
73             tmp = mm[i];
74             tmp.start();
75         }
76
77         // wait for them to finish
78         for (int i = 0; i < NUM_THREADS; i++) {
79             try {
80                 mm[i].join();
81             } catch (InterruptedException e) {
82                 System.out.println("Join Error");
83             } 
84
85         }
86
87         // print out the result of the matrix multiply
88         System.out.println("Starting\n");
89         System.out.println("Matrix Product c =\n");
90         int val;
91         for (int i = 0; i < p; i++) {
92             int c[]=matrix.c[i];
93             for (int j = 0; j < r; j++) {
94                 val = c[j];
95             }
96         }
97         System.out.println("Finished\n");
98     }
99 }
100
101 class MMul{
102
103     public int L, M, N;
104     public int[][] a;
105     public int[][] b;
106     public int[][] c;
107     public int[][] btranspose;
108
109     public MMul(int L, int M, int N) {
110         this.L = L;
111         this.M = M;
112         this.N = N;
113         a = new int[L][M];  
114         b = new int[M][N]; 
115         c = new int[L][N]; 
116         btranspose = new int[N][M];
117     }
118
119     public void setValues() {
120         for(int i = 0; i < L; i++) {
121             int ai[] = a[i];
122             for(int j = 0; j < M; j++) {
123                 ai[j] = j+1;
124             }
125         }
126
127         for(int i = 0; i < M; i++) {
128             int bi[] = b[i];
129             for(int j = 0; j < N; j++) {
130                 bi[j] = j+1;
131             }
132         }
133
134         for(int i = 0; i < L; i++) {
135             int ci[] = c[i];
136             for(int j = 0; j < N; j++) {
137                 ci[j] = 0;
138             }
139         }
140         for(int i = 0; i < N; i++) {
141             int btransposei[] = btranspose[i];
142             for(int j = 0; j < M; j++) {
143                 btransposei[j] = 0;
144             }
145         }
146     }
147
148     public void transpose() {
149         for(int row = 0; row < M; row++) {
150             int brow[] = b[row];
151             for(int col = 0; col < N; col++) {
152                 btranspose[col][row] = brow[col];
153             }
154         }
155     }
156 }