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) {
14 double localresults[][];
18 localresults=new double[1+x1-x0][1+y1-y0];
20 double lbtranspose[][]=mmul.b;
24 //Use b transpose for cache performance
25 for(int i = x0; i<= x1; i++){
27 for (int j = y0; j <= y1; j++) {
28 double innerProduct=0;
29 double b[] = lbtranspose[j];
30 for(int k = 0; k < M; k++) {
31 innerProduct += a[k] *b[k];
33 localresults[i-x0][j-y0]=innerProduct;
40 for(int i=x0;i<=x1;i++) {
42 for(int j=y0;j<=y1;j++) {
43 c[j]=localresults[i-x0][j-y0];
49 public static void main(String[] args) {
51 int[] mid = new int[NUM_THREADS];
52 mid[0] = (128<<24)|(195<<16)|(175<<8)|69;
53 mid[1] = (128<<24)|(195<<16)|(175<<8)|73;
54 mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
55 mid[3] = (128<<24)|(195<<16)|(175<<8)|79;
62 matrix = global new MMul(400, 400, 400);
68 mm = global new MatrixMultiply[NUM_THREADS];
72 mm[0] = global new MatrixMultiply(matrix,0,0,200,200);
73 mm[1] = global new MatrixMultiply(matrix,0,201,200,399);
74 mm[2] = global new MatrixMultiply(matrix,201,0,399,200);
75 mm[3] = global new MatrixMultiply(matrix,201,201,399,399);
84 // print out the matrices to be multiplied
85 System.printString("\n");
86 System.printString("MatrixMultiply: L=");
88 System.printString("\t");
89 System.printString("M=");
91 System.printString("\t");
92 System.printString("N=");
94 System.printString("\n");
96 // start a thread to compute each c[l,n]
97 for (int i = 0; i < NUM_THREADS; i++) {
104 // wait for them to finish
105 for (int i = 0; i < NUM_THREADS; i++) {
112 // print out the result of the matrix multiply
113 System.printString("Starting\n");
114 System.printString("Matrix Product c =\n");
117 for (int i = 0; i < p; i++) {
118 double c[]=matrix.c[i];
119 for (int j = 0; j < r; j++) {
124 System.printString("Finished\n");
134 public double[][] btranspose;
136 public MMul(int L, int M, int N) {
140 a = global new double[L][M];
141 b = global new double[M][N];
142 c = global new double[L][N];
143 btranspose = global new double[N][M];
146 public void setValues() {
147 for(int i = 0; i < L; i++) {
149 for(int j = 0; j < M; j++) {
154 for(int i = 0; i < M; i++) {
156 for(int j = 0; j < N; j++) {
161 for(int i = 0; i < L; i++) {
163 for(int j = 0; j < N; j++) {
167 for(int i = 0; i < N; i++) {
168 double btransposei[] = btranspose[i];
169 for(int j = 0; j < M; j++) {
175 public void transpose() {
176 for(int row = 0; row < M; row++) {
177 double brow[] = b[row];
178 for(int col = 0; col < N; col++) {
179 btranspose[col][row] = brow[col];