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 //Use b transpose for cache performance
21 for(int i = x0; i<= x1; i++){
24 for (int j = y0; j <= y1; j++) {
25 double innerProduct=0;
26 double 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)|69;
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,200);
68 mm[1] = global new MatrixMultiply(matrix,0,201,399,399);
77 // print out the matrices to be multiplied
78 System.printString("\n");
79 System.printString("MatrixMultiply: L=");
81 System.printString("\t");
82 System.printString("M=");
84 System.printString("\t");
85 System.printString("N=");
87 System.printString("\n");
89 // start a thread to compute each c[l,n]
90 for (int i = 0; i < NUM_THREADS; i++) {
97 // wait for them to finish
98 for (int i = 0; i < NUM_THREADS; i++) {
105 // print out the result of the matrix multiply
106 System.printString("Starting\n");
107 System.printString("Matrix Product c =\n");
110 for (int i = 0; i < p; i++) {
111 double c[]=matrix.c[i];
112 for (int j = 0; j < r; j++) {
117 System.printString("Finished\n");
127 public double[][] btranspose;
129 public MMul(int L, int M, int N) {
133 a = global new double[L][M];
134 b = global new double[M][N];
135 c = global new double[L][N];
136 btranspose = global new double[N][M];
139 public void setValues() {
140 for(int i = 0; i < L; i++) {
142 for(int j = 0; j < M; j++) {
147 for(int i = 0; i < M; i++) {
149 for(int j = 0; j < N; j++) {
154 for(int i = 0; i < L; i++) {
156 for(int j = 0; j < N; j++) {
160 for(int i = 0; i < N; i++) {
161 double btransposei[] = btranspose[i];
162 for(int j = 0; j < M; j++) {
168 public void transpose() {
169 for(int row = 0; row < M; row++) {
170 double brow[] = b[row];
171 for(int col = 0; col < N; col++) {
172 btranspose[col][row] = brow[col];