1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
5 public MatrixMultiply(MMul mmul, int x0, int y0, int x1, int y1) {
18 localresults=new int[1+x1-x0][1+y1-y0];
20 //Use b transpose for cache performance
21 for(int i = x0; i<= x1; i++){
24 for (int j = y0; j <= y1; j++) {
26 int b[] = mmul.btranspose[j];
27 for(int k = 0; k < M; k++) {
28 innerProduct += a[k] *b[k];
30 localresults[i-x0][j-y0]=innerProduct;
37 for(int i=x0;i<=x1;i++) {
39 for(int j=y0;j<=y1;j++) {
40 c[j]=localresults[i-x0][j-y0];
46 public static void main(String[] args) {
47 int mid1 = (128<<24)|(195<<16)|(175<<8)|70;
48 int mid2 = (128<<24)|(195<<16)|(175<<8)|69;
49 int mid3 = (128<<24)|(195<<16)|(175<<8)|71;
57 matrix = global new MMul(400, 400, 400);
63 mm = global new MatrixMultiply[NUM_THREADS];
67 mm[0] = global new MatrixMultiply(matrix,0,0,399,399);
76 // print out the matrices to be multiplied
77 System.printString("\n");
78 System.printString("MatrixMultiply: L=");
80 System.printString("\t");
81 System.printString("M=");
83 System.printString("\t");
84 System.printString("N=");
86 System.printString("\n");
88 // start a thread to compute each c[l,n]
89 for (i = 0; i < NUM_THREADS; i++) {
96 // wait for them to finish
97 for (i = 0; i < NUM_THREADS; i++) {
104 // print out the result of the matrix multiply
105 System.printString("Starting\n");
106 System.printString("Matrix Product c =\n");
109 for (i = 0; i < p; i++) {
111 for (j = 0; j < r; j++) {
116 System.printString("Finished\n");
126 public int[][] btranspose;
128 public MMul(int L, int M, int N) {
132 a = global new int[L][M];
133 b = global new int[M][N];
134 c = global new int[L][N];
135 btranspose = global new int[N][M];
138 public void setValues() {
141 for(i = 0; i < L; i++) {
142 for(j = 0; j < M; j++) {
147 for(i = 0; i < M; i++) {
148 for(j = 0; j < N; j++) {
153 for(i = 0; i < L; i++) {
154 for(j = 0; j < N; j++) {
158 for(i = 0; i < N; i++) {
159 for(j = 0; j < M; j++) {
160 btranspose[i][j] = 0;
165 public void transpose() {
168 for(row = 0; row < M; row++) {
169 for(col = 0; col < N; col++) {
170 btranspose[col][row] = b[row][col];