1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
4 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
16 double lb[][]=mmul.btranspose;
19 //Use btranspose for cache performance
20 for(int i = x0; i< x1; i++){
23 for (int j = y0; j < y1; j++) {
24 double innerProduct=0;
26 for(int k = 0; k < M; k++) {
27 innerProduct += a[k] *b[k];
35 public static void main(String[] args) {
39 NUM_THREADS=Integer.parseInt(args[0]);
41 SIZE=Integer.parseInt(args[1]);
44 int[] mid = new int[4];
45 mid[0] = (128<<24)|(195<<16)|(175<<8)|79; //dw-8
46 mid[1] = (128<<24)|(195<<16)|(175<<8)|80; //dw-9
47 mid[2] = (128<<24)|(195<<16)|(175<<8)|78; //dw-7
48 mid[3] = (128<<24)|(195<<16)|(175<<8)|73; //dw-5
55 matrix = global new MMul(SIZE, SIZE, SIZE);
58 mm = global new MatrixMultiply[NUM_THREADS];
59 int increment=SIZE/NUM_THREADS;
61 for(int i=0;i<NUM_THREADS;i++) {
62 if ((i+1)==NUM_THREADS)
63 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
65 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
73 // print out the matrices to be multiplied
74 System.printString("\n");
75 System.printString("MatrixMultiply: L=");
77 System.printString("\t");
78 System.printString("M=");
80 System.printString("\t");
81 System.printString("N=");
83 System.printString("\n");
85 // start a thread to compute each c[l,n]
86 for (int i = 0; i < NUM_THREADS; i++) {
94 // wait for them to finish
95 for (int i = 0; i < NUM_THREADS; i++) {
102 // print out the result of the matrix multiply
104 System.printString("Finished\n");
114 public double[][] btranspose;
116 public MMul(int L, int M, int N) {
120 a = global new double[L][M];
121 b = global new double[M][N];
122 c = global new double[L][N];
123 btranspose = global new double[N][M];
126 public void setValues() {
127 for(int i = 0; i < L; i++) {
129 for(int j = 0; j < M; j++) {
134 for(int i = 0; i < M; i++) {
136 for(int j = 0; j < N; j++) {
141 for(int i = 0; i < L; i++) {
143 for(int j = 0; j < N; j++) {
147 for(int i = 0; i < N; i++) {
148 double btransposei[] = btranspose[i];
149 for(int j = 0; j < M; j++) {
155 public void transpose() {
156 for(int row = 0; row < M; row++) {
157 double brow[] = b[row];
158 for(int col = 0; col < N; col++) {
159 btranspose[col][row] = brow[col];