1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
4 public int tid, numthreads;
6 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1, int tid, int numthreads) {
13 this.numthreads=numthreads;
17 Barrier barr=new Barrier("128.195.136.162");
19 mmul.setValues(tid, numthreads);
22 Barrier.enterBarrier(barr);
25 double la[][][]=mmul.a;
26 double lc[][][]=mmul.c;
27 double lb[][][]=mmul.btranspose;
30 //Use btranspose for cache performance
31 for(int q=0;q<P;q++) {
35 for(int i = x0; i< x1; i++){
38 for (int j = y0; j < y1; j++) {
39 double innerProduct=0;
41 for(int k = 0; k < M; k++) {
42 innerProduct += a[k] * b[k];
51 public static void main(String[] args) {
56 NUM_THREADS=Integer.parseInt(args[0]);
58 SIZE=Integer.parseInt(args[1]);
60 NUM_MATRIX=Integer.parseInt(args[2]);
64 int[] mid = new int[8];
65 mid[0] = (128<<24)|(195<<16)|(136<<8)|162;
66 mid[1] = (128<<24)|(195<<16)|(136<<8)|163;
67 mid[2] = (128<<24)|(195<<16)|(136<<8)|164;
68 mid[3] = (128<<24)|(195<<16)|(136<<8)|165;
69 mid[4] = (128<<24)|(195<<16)|(136<<8)|166;
70 mid[5] = (128<<24)|(195<<16)|(136<<8)|167;
71 mid[6] = (128<<24)|(195<<16)|(136<<8)|168;
72 mid[7] = (128<<24)|(195<<16)|(136<<8)|169;
81 mybarr = global new BarrierServer(NUM_THREADS);
87 matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
88 mm = global new MatrixMultiply[NUM_THREADS];
89 int increment=SIZE/NUM_THREADS;
91 for(int i=0;i<NUM_THREADS;i++) {
92 if ((i+1)==NUM_THREADS)
93 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE, i, NUM_THREADS);
95 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE, i, NUM_THREADS);
102 boolean waitfordone=true;
104 atomic { //Master aborts come from here
110 // start a thread to compute each c[l,n]
111 for (int i = 0; i < NUM_THREADS; i++) {
118 // wait for them to finish
119 for (int i = 0; i < NUM_THREADS; i++) {
126 // print out the result of the matrix multiply
127 System.printString("Finished\n");
132 public int L, M, N, P;
133 public double[][][] a;
134 public double[][][] c;
135 public double[][][] btranspose;
137 public MMul(int P, int L, int M, int N) {
142 // a = global new double[P][L][M];
143 // c = global new double[P][L][N];
144 // btranspose = global new double[P][N][M];
145 a = global new double[P][L][];
146 c = global new double[P][L][];
147 btranspose = global new double[P][N][];
150 public void setValues(int tid, int numthreads) {
152 for(int q = 0; q < P; q++) {
153 for(int i = 0; i < L; i++) {
154 double ai[] = global new double[M];
155 for(int j = 0; j < M; j++) {
161 for(int q = 0; q < P; q++) {
162 for(int i = 0; i < L; i++) {
163 c[q][i]=global new double[N];
167 if(tid>=0||numthreads==1) {
168 int delta=numthreads>1?numthreads-1:1;
169 int start=numthreads>1?tid-1:0;
171 for(int q = start; q < P; q+=delta) {
172 for(int i = 0; i < N; i++) {
173 double bi[] = global new double[M];
174 for(int j = 0; j < M; j++) {