1 public class MatrixMultiply extends Task {
6 public MatrixMultiply(MMul mmul, int num_threads, int size) {
16 todoList = global new Queue();
17 doneList = global new Queue();
22 // fill up the Work Pool
23 public void fillTodoList() {
27 for(i = 0; i < SIZE; i +=increment) {
29 if(i+increment > SIZE) {
30 seg = global new Segment(i,SIZE);
33 seg = global new Segment(i, i + increment);
39 public void execute() {
55 seg = (Segment)myWork;
56 x0 = seg.x0; // x start row
57 x1 = seg.x1; // x end row
58 la = mmul.a; // first mat
59 lb = mmul.btranspose; // second mat
60 // lc = mmul.c; // destination mat
64 lc = new double[size][size];
65 System.out.println("Seg x0 = " + x0 + " - x1 = " + x1);
67 for(i = x0; i < x1 ; i++) {
68 System.printString("i = " + i + "\n");
70 rowA = la[i]; // grab first mat's row
72 for(j = 0; j < size ; j++) {
73 colB = lb[j]; // grab second mat's col
75 innerproduct = computeProduct(rowA,colB, size); // computes the value
77 lc[i][j] = innerproduct; // store in dest mat
82 System.out.println("Finished comutation");
85 for (i = x0; i < x1; i++) {
86 for (j = 0; j < size; j++) {
87 mmul.c[i][j] = lc[i][j];
93 public double computeProduct(double[] rowA,double[] colB, int size)
98 for(i = 0 ;i < size; i++) {
100 sum += rowA[i] * colB[i];
107 public void done(Object work) {
109 ((Queue)doneList).push(work);
113 public static void main(String[] args) {
120 Segment[] currentWorkList;
122 if (args.length > 0) {
123 NUM_THREADS = Integer.parseInt(args[0]);
126 int[] mid = new int[NUM_THREADS];
127 mid[0] = (128<<24)|(195<<16)|(180<<8)|21; //dc1
128 mid[1] = (128<<24)|(195<<16)|(180<<8)|24; //dc2
129 mid[2] = (128<<24)|(195<<16)|(180<<8)|26; //dc3
132 matrix = global new MMul(SIZE, SIZE, SIZE);
135 mm = global new MatrixMultiply(matrix, NUM_THREADS, SIZE);
137 works = global new Work[NUM_THREADS];
138 currentWorkList = global new Segment[NUM_THREADS];
140 for(i = 0; i < NUM_THREADS; i++) {
141 works[i] = global new Work(mm, NUM_THREADS, i,currentWorkList);
144 System.out.println("Finished to createObjects");
147 for (i = 0; i < NUM_THREADS; i++) {
151 Thread.myStart(tmp,mid[i]);
154 for (i = 0; i < NUM_THREADS; i++) {
161 System.printString("Finished\n");
170 public double[][] btranspose;
172 public MMul(int L, int M, int N) {
176 a = global new double[L][M];
177 b = global new double[M][N];
178 c = global new double[L][N];
179 btranspose = global new double[N][M];
182 public void setValues() {
183 for(int i = 0; i < L; i++) {
185 for(int j = 0; j < M; j++) {
190 for(int i = 0; i < M; i++) {
192 for(int j = 0; j < N; j++) {
197 for(int i = 0; i < L; i++) {
199 for(int j = 0; j < N; j++) {
203 for(int i = 0; i < N; i++) {
204 double btransposei[] = btranspose[i];
205 for(int j = 0; j < M; j++) {
211 public void transpose() {
212 for(int row = 0; row < M; row++) {
213 double brow[] = b[row];
214 for(int col = 0; col < N; col++) {
215 btranspose[col][row] = brow[col];
221 public class Segment {
225 Segment (int x0, int x1) {