-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiplyTables.java
150 lines (123 loc) · 5.75 KB
/
MultiplyTables.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package thema1;
import java.util.Random;
import java.util.Scanner;
/**
* @author Stamatis Chatzichristodoulou <[email protected]>
* The program creates two tables and multiply them.
* From Linear Algebra, we can multiply a matrix by a vector from the right, as long as the number of its columns is sufficient
* matrix to be equal to the number of rows of the vector. For example if we have a matrix A of dimensions n x m and a vector v of dimensions m x 1,
* then the product A * v equals an n x 1 vector by applying the well-known matrix multiplication method with vector.
* An example is given in MultiplyTables.png
*
* Assuming we have k threads, where k is a power of 2 and the array has dimensions n x m where n is also a power of 2 and n > k,
* design a solution that calculates the product A * v using the k threads in the best possible way.
* The program should "fill" the array A and the vector v with random numbers between 0 and 10.
*
* Q: What the time needed for 1,2,4 and 8 Threads?
*/
public class MultiplyTables extends Thread {
private int[][] arr1;
private int[] arr2,arr3;
private int numberM,arxi,plithos;
public MultiplyTables(int[][] arr1, int[] arr2, int[] arr3, int arxi, int plithos, int numberM) {
this.arr1 = arr1;
this.arr2 = arr2;
this.arr3 = arr3;
this.arxi = arxi;
this.plithos = plithos;
this.numberM = numberM;
}
@Override
public void run() {
for (int n=arxi; n<arxi+plithos; n++)
for (int m=0; m<numberM; m++)
arr3[n] += arr1[n][m]*arr2[m];
}
public static int[] muplip(int[][] arr1, int[] arr2, int numbN, int numbM, int nimata ) throws InterruptedException {
// Δημιουργία νημάτων και εκκίνηση
int[] arr3 = new int[numbN];
int k=nimata;
MultiplyTables[] nima = new MultiplyTables[k];
for (int i = 0; i < k; i++) {
nima[i] = new MultiplyTables(arr1,arr2,arr3,i*numbN/k, numbN/k, numbM);
nima[i].start();
}
// Wait for the threads to finish and sum their results.
for (int i = 0; i < k; i++) {
nima[i].join();
arr3[i] = (int) nima[i].arr3[i];
}
return arr3;
}
//έλεγχος για δύναμη του 2
public static boolean isPerfectSquare(int n) {
if (n <= 0) {
return false;
}
int result = (int)(Math.log(n) / Math.log(2));
return Math.pow(2,result) == n;
}
public static void main(String[] args) throws InterruptedException {
//Αμυντικός έλεγοχς για δυνάμεις του 2 μόνο
int numberN=3;
while (!isPerfectSquare(numberN)) {
Scanner input=new Scanner(System.in);
System.out.print("Rows n table (power of ^2: ");
numberN=input.nextInt();
}
//Δεν γίνεται αμυντικός έλεγχος για τις δυνάμεις το 2, string, double
System.out.print("Columns m table: ");
Scanner input=new Scanner(System.in);
int numberM=input.nextInt();
//Δημιουργία πινάκων
int[][] pinakas = new int[numberN][numberM];
int[] dianisma = new int[numberM];
int[] apotelesma = new int[numberN];
//Γέμισμα πινάκων με τυχαίους αριθμούς 0-10
Random rand = new Random();
for (int n=0; n<numberN; n++){
System.out.print("| " );
for (int m=0; m<numberM; m++) {
pinakas[n][m]= rand.nextInt(10);
System.out.print(pinakas[n][m]+" ");
}
System.out.print("| " );
//προτιμώ να εμφανιστεί το διάνυσμα δίπλα στον πίνακα
if (n<numberM) {
dianisma[n]= rand.nextInt(10);
System.out.print("|"+dianisma[n]+"|");
}
System.out.println("");
}
//και στην περίπτωση που το διάνυσμά έχει περισσότερες γραμμές από τον πίνακα
for (int n=numberN; n<numberM; n++) {
dianisma[n]= rand.nextInt(10);
for (int i=0; i<numberM; i++) System.out.print(" ");
System.out.println(" |"+dianisma[n]+"|");
}
/* Τεστ πριν το setup των νημάτων
System.out.println("--Αποτέλεσμα χωρίς νήματα--");
for (int n=0; n<numberN; n++){
for (int m=0; m<numberM; m++) {
apotelesma[n] += pinakas[n][m]*dianisma[m];
}
System.out.println("|"+apotelesma[n]+"|" );
}
*/
System.out.println("--Result--");
apotelesma=muplip(pinakas, dianisma, numberN, numberM, 4 );
for (int n=0; n<numberN; n++)
System.out.println("|"+apotelesma[n]+"|" );
if (numberN>=8) { //Θέμα δύο: Μετρήσεις χρόνου για 1, 2, 4 και 8 νήματα.
for (int i=0; i<4; i++) {
double start=System.nanoTime();
int nimata = (int) Math.pow(2, i);
apotelesma=muplip(pinakas, dianisma, numberN, numberM, nimata );
// Εμφάνιση αποτελεσμάτων για κάθε νήμα ξεχωριστά
// for (int n=0; n<numberN; n++) System.out.println(nimata+" |"+apotelesma[n]+"|" );
double finish=System.nanoTime()-start;
System.out.println("Result with "+nimata+" Threads: "+finish/1000);
}
}
}
}