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)|(136<<8)|162; //dw-10
46 mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dw-11
47 mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dw-12
48 mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dw-13
49 mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dw-14
50 mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dw-15
51 mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dw-16
52 mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dw-17
60 matrix = global new MMul(SIZE, SIZE, SIZE);
63 mm = global new MatrixMultiply[NUM_THREADS];
64 int increment=SIZE/NUM_THREADS;
66 for(int i=0;i<NUM_THREADS;i++) {
67 if ((i+1)==NUM_THREADS)
68 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
70 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
78 // print out the matrices to be multiplied
79 System.printString("\n");
80 System.printString("MatrixMultiply: L=");
82 System.printString("\t");
83 System.printString("M=");
85 System.printString("\t");
86 System.printString("N=");
88 System.printString("\n");
90 // start a thread to compute each c[l,n]
91 for (int i = 0; i < NUM_THREADS; i++) {
99 // wait for them to finish
100 for (int i = 0; i < NUM_THREADS; i++) {
107 // print out the result of the matrix multiply
109 System.printString("Finished\n");
119 public double[][] btranspose;
121 public MMul(int L, int M, int N) {
125 a = global new double[L][M];
126 b = global new double[M][N];
127 c = global new double[L][N];
128 btranspose = global new double[N][M];
131 public void setValues() {
132 for(int i = 0; i < L; i++) {
134 for(int j = 0; j < M; j++) {
139 for(int i = 0; i < M; i++) {
141 for(int j = 0; j < N; j++) {
146 for(int i = 0; i < L; i++) {
148 for(int j = 0; j < N; j++) {
152 for(int i = 0; i < N; i++) {
153 double btransposei[] = btranspose[i];
154 for(int j = 0; j < M; j++) {
160 public void transpose() {
161 for(int row = 0; row < M; row++) {
162 double brow[] = b[row];
163 for(int col = 0; col < N; col++) {
164 btranspose[col][row] = brow[col];