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) {
50 <<<<<<< MatrixMultiply.java
51 int mid1 = (128<<24)|(195<<16)|(175<<8)|69;
52 int mid2 = (128<<24)|(195<<16)|(175<<8)|69;
53 int mid3 = (128<<24)|(195<<16)|(175<<8)|71;
57 int[] mid = new int[NUM_THREADS];
58 mid[0] = (128<<24)|(195<<16)|(175<<8)|69;
59 mid[1] = (128<<24)|(195<<16)|(175<<8)|73;
60 mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
61 mid[3] = (128<<24)|(195<<16)|(175<<8)|79;
62 //int mid1 = (128<<24)|(195<<16)|(175<<8)|69;
63 //int mid2 = (128<<24)|(195<<16)|(175<<8)|73;
71 matrix = global new MMul(400, 400, 400);
77 mm = global new MatrixMultiply[NUM_THREADS];
81 mm[0] = global new MatrixMultiply(matrix,0,0,200,200);
82 mm[1] = global new MatrixMultiply(matrix,0,201,200,399);
83 mm[2] = global new MatrixMultiply(matrix,201,0,399,200);
84 mm[3] = global new MatrixMultiply(matrix,201,201,399,399);
93 // print out the matrices to be multiplied
94 System.printString("\n");
95 System.printString("MatrixMultiply: L=");
97 System.printString("\t");
98 System.printString("M=");
100 System.printString("\t");
101 System.printString("N=");
103 System.printString("\n");
105 // start a thread to compute each c[l,n]
106 for (int i = 0; i < NUM_THREADS; i++) {
113 // wait for them to finish
114 for (int i = 0; i < NUM_THREADS; i++) {
121 // print out the result of the matrix multiply
122 System.printString("Starting\n");
123 System.printString("Matrix Product c =\n");
126 for (int i = 0; i < p; i++) {
127 double c[]=matrix.c[i];
128 for (int j = 0; j < r; j++) {
133 System.printString("Finished\n");
143 public double[][] btranspose;
145 public MMul(int L, int M, int N) {
149 a = global new double[L][M];
150 b = global new double[M][N];
151 c = global new double[L][N];
152 btranspose = global new double[N][M];
155 public void setValues() {
156 for(int i = 0; i < L; i++) {
158 for(int j = 0; j < M; j++) {
163 for(int i = 0; i < M; i++) {
165 for(int j = 0; j < N; j++) {
170 for(int i = 0; i < L; i++) {
172 for(int j = 0; j < N; j++) {
176 for(int i = 0; i < N; i++) {
177 double btransposei[] = btranspose[i];
178 for(int j = 0; j < M; j++) {
184 public void transpose() {
185 for(int row = 0; row < M; row++) {
186 double brow[] = b[row];
187 for(int col = 0; col < N; col++) {
188 btranspose[col][row] = brow[col];