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[8];
45 mid[0] = (128<<24)|(195<<16)|(175<<8)|84; //dw-10
46 mid[1] = (128<<24)|(195<<16)|(175<<8)|85; //dw-11
47 mid[2] = (128<<24)|(195<<16)|(175<<8)|86; //dw-12
48 mid[3] = (128<<24)|(195<<16)|(175<<8)|87; //dw-13
49 mid[4] = (128<<24)|(195<<16)|(175<<8)|88; //dw-14
50 mid[5] = (128<<24)|(195<<16)|(175<<8)|89; //dw-15
51 mid[6] = (128<<24)|(195<<16)|(175<<8)|90; //dw-16
52 mid[7] = (128<<24)|(195<<16)|(175<<8)|91; //dw-17
59 matrix = global new MMul(SIZE, SIZE, SIZE);
62 mm = global new MatrixMultiply[NUM_THREADS];
63 int increment=SIZE/NUM_THREADS;
65 for(int i=0;i<NUM_THREADS;i++) {
66 if ((i+1)==NUM_THREADS)
67 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
69 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
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++) {
98 // wait for them to finish
99 for (int i = 0; i < NUM_THREADS; i++) {
106 // print out the result of the matrix multiply
108 System.printString("Finished\n");
118 public double[][] btranspose;
120 public MMul(int L, int M, int N) {
124 a = global new double[L][M];
125 b = global new double[M][N];
126 c = global new double[L][N];
127 btranspose = global new double[N][M];
130 public void setValues() {
131 for(int i = 0; i < L; i++) {
133 for(int j = 0; j < M; j++) {
138 for(int i = 0; i < M; i++) {
140 for(int j = 0; j < N; j++) {
145 for(int i = 0; i < L; i++) {
147 for(int j = 0; j < N; j++) {
151 for(int i = 0; i < N; i++) {
152 double btransposei[] = btranspose[i];
153 for(int j = 0; j < M; j++) {
159 public void transpose() {
160 for(int row = 0; row < M; row++) {
161 double brow[] = b[row];
162 for(int col = 0; col < N; col++) {
163 btranspose[col][row] = brow[col];