Browse Source

snapshot

master
mortie 7 years ago
parent
commit
0b985fe74d
3 changed files with 26 additions and 11 deletions
  1. 5
    0
      inf2440/hw3/Main.java
  2. 4
    0
      inf2440/hw3/MultiRadix.java
  3. 17
    11
      inf2440/hw3/MultiRadixPar.java

+ 5
- 0
inf2440/hw3/Main.java View File

@@ -22,6 +22,11 @@ class Main {
Random r = new Random(123);

int[] a = new int[len];
for (int j = 0; j < len; j++) {
a[j] = r.nextInt(len);
}
s.sort(a);

for (int j = 0; j < len; j++) {
a[j] = r.nextInt(len);
}

+ 4
- 0
inf2440/hw3/MultiRadix.java View File

@@ -67,10 +67,14 @@ class MultiRadix{
count[i] = acumVal;
acumVal += j;
}
// d) move numbers in sorted order a to b
Timer t = new Timer().start();
for (int i = 0; i < n; i++) {
b[count[(a[i]>>>shift) & mask]++] = a[i];
}
t.end();
System.out.println("seq D from "+0+" to "+n+": "+t.prettyTime());
}// end radixSort

+ 17
- 11
inf2440/hw3/MultiRadixPar.java View File

@@ -4,7 +4,9 @@ class MultiRadixPar{
int n;
int [] a;
final static int NUM_BIT = 7; // alle tall 6-11 .. finn ut hvilken verdi som er best
int nThreads = Runtime.getRuntime().availableProcessors();
int nThreads = Math.min(
Runtime.getRuntime().availableProcessors(),
8);
int [] radixMulti(int [] a) {
long tt = System.nanoTime();
@@ -49,11 +51,12 @@ class MultiRadixPar{
class Worker implements Runnable {
int[] a, b, count;
int start, end, shift, mask;
int id;
Worker(int[] a, int[] b, int[] count, int start, int end, int shift, int mask) {
this.a = a;
this.b = b;
this.count = Arrays.copyOf(count, count.length);
this.count = count; //Arrays.copyOf(count, count.length);
this.start = start;
this.end = end;
this.shift = shift;
@@ -61,17 +64,9 @@ class MultiRadixPar{
}
public void run() {
// c) Add up in 'count' - accumulated values
int j = 0;
int acumVal = 0;
for (int i = 0; i <= mask; i++) {
j = count[i];
count[i] = acumVal;
acumVal += j;
}
// d) move numbers in sorted order a to b
for (int i = 0; i < n; i++) {
for (int i = start; i < end - 1; i++) {
b[count[(a[i]>>>shift) & mask]++] = a[i];
}
}
@@ -88,7 +83,17 @@ class MultiRadixPar{
count[(a[i]>>> shift) & mask]++;
}
// c) Add up in 'count' - accumulated values
int j = 0;
int acumVal = 0;
for (int i = 0; i <= mask; i++) {
j = count[i];
count[i] = acumVal;
acumVal += j;
}
// Parallel
Timer td = new Timer().start();
int d = n / nThreads;
Thread[] threads = new Thread[nThreads];
Worker[] workers = new Worker[nThreads];
@@ -103,6 +108,7 @@ class MultiRadixPar{
for (Thread t: threads) {
try { t.join(); } catch (InterruptedException ex) {}
}
System.out.println("par D: "+td.end().prettyTime());
}// end radixSort

Loading…
Cancel
Save