1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
5 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
15 double la[][][]=mmul.a;
16 double lc[][][]=mmul.c;
17 double lb[][][]=mmul.btranspose;
20 //Use btranspose for cache performance
21 for(int q=0;q<P;q++) {
25 for(int i = x0; i< x1; i++){
28 for (int j = y0; j < y1; j++) {
29 double innerProduct=0;
31 for(int k = 0; k < M; k++) {
32 innerProduct += a[k] *b[k];
41 public static void main(String[] args) {
46 NUM_THREADS=Integer.parseInt(args[0]);
48 SIZE=Integer.parseInt(args[1]);
50 NUM_MATRIX=Integer.parseInt(args[2]);
54 int[] mid = new int[8];
55 int[] mid = new int[8];
56 mid[0] = (128<<24)|(195<<16)|(136<<8)|162; //dw-10
57 mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dw-11
58 mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dw-12
59 mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dw-13
60 mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dw-14
61 mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dw-15
62 mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dw-16
63 mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dw-17
71 matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
74 mm = global new MatrixMultiply[NUM_THREADS];
75 int increment=SIZE/NUM_THREADS;
77 for(int i=0;i<NUM_THREADS;i++) {
78 if ((i+1)==NUM_THREADS)
79 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
81 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
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("Finished\n");
112 public int L, M, N, P;
113 public double[][][] a;
114 public double[][][] b;
115 public double[][][] c;
116 public double[][][] btranspose;
118 public MMul(int P, int L, int M, int N) {
123 a = global new double[P][L][M];
124 b = global new double[P][M][N];
125 c = global new double[P][L][N];
126 btranspose = global new double[P][N][M];
129 public void setValues() {
130 for(int q = 0; q < P; q++) {
131 for(int i = 0; i < L; i++) {
132 double ai[] = a[q][i];
133 for(int j = 0; j < M; j++) {
138 for(int i = 0; i < M; i++) {
139 double bi[] = b[q][i];
140 for(int j = 0; j < N; j++) {
147 public void transpose() {
148 for(int q=0;q<P;q++) {
150 double bt[][]=btranspose[q];
151 for(int row = 0; row < M; row++) {
152 double brow[] = br[row];
153 for(int col = 0; col < N; col++) {
154 bt[col][row] = brow[col];