Browse Source

1x speedup

master
mortie 7 years ago
parent
commit
6f796a9117
3 changed files with 30 additions and 20 deletions
  1. 1
    0
      inf2440/hw3/Main.java
  2. 7
    1
      inf2440/hw3/MultiRadix.java
  3. 22
    19
      inf2440/hw3/MultiRadixPar.java

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

@@ -35,6 +35,7 @@ class Main {
a = s.sort(a);
t.end();
new MultiRadix().testSort(a);

return t;
} // end doIt
}

+ 7
- 1
inf2440/hw3/MultiRadix.java View File

@@ -17,9 +17,11 @@ class MultiRadix{
int [] bit ;
// a) finn max verdi i a[]
//Timer ta = new Timer().start();
for (int i = 1 ; i < n ; i++)
if (a[i] > max) max = a[i];
while (max >= (1L<<numBit) )numBit++; // antall binaere siffer i max
//System.out.println("A: "+ta.end().prettyTime());
// bestem antall bit i numBits sifre
numDigits = Math.max(1, numBit/NUM_BIT);
@@ -57,16 +59,20 @@ class MultiRadix{
int [] count = new int [mask+1];
// b) count=the frequency of each radix value in a
//Timer tb = new Timer().start();
for (int i = 0; i < n; i++) {
count[(a[i]>>> shift) & mask]++;
}
//System.out.println("B: "+tb.end().prettyTime());
// c) Add up in 'count' - accumulated values
//Timer tc = new Timer().start();
for (int i = 0; i <= mask; i++) {
j = count[i];
count[i] = acumVal;
acumVal += j;
}
//System.out.println("C: "+tc.end().prettyTime());
// d) move numbers in sorted order a to b
Timer t = new Timer().start();
@@ -74,7 +80,7 @@ class MultiRadix{
b[count[(a[i]>>>shift) & mask]++] = a[i];
}
t.end();
System.out.println("seq D from "+0+" to "+n+": "+t.prettyTime());
System.out.println("seq D: "+t.prettyTime());
}// end radixSort

+ 22
- 19
inf2440/hw3/MultiRadixPar.java View File

@@ -15,9 +15,11 @@ class MultiRadixPar{
int [] bit ;
// a) finn max verdi i a[]
//Timer ta = new Timer().start();
for (int i = 1 ; i < n ; i++)
if (a[i] > max) max = a[i];
while (max >= (1L<<numBit) )numBit++; // antall binaere siffer i max
//System.out.println("A: "+ta.end().prettyTime());
// bestem antall bit i numBits sifre
numDigits = Math.max(1, numBit/NUM_BIT);
@@ -40,34 +42,30 @@ class MultiRadixPar{
a = b;
b = t;
}
if (bit.length%2 != 0 ) {
// et odde antall sifre, kopier innhold tilbake til original a[] (nå b)
System.arraycopy (a,0,b,0,a.length);
}
return a;
} // end radixMulti
class Worker implements Runnable {
int[] a, b, count;
int start, end, shift, mask;
int id;
int[] a, b, count;
Worker(int[] a, int[] b, int[] count, int start, int end, int shift, int mask) {
this.a = a;
this.b = b;
this.count = count; //Arrays.copyOf(count, count.length);
Worker(int start, int end, int[] a, int[] b, int[] count, int shift, int mask) {
this.start = start;
this.end = end;
this.a = a;
this.b = b;
this.count = count;
this.shift = shift;
this.mask = mask;
}
public void run() {
// d) move numbers in sorted order a to b
for (int i = start; i < end - 1; i++) {
b[count[(a[i]>>>shift) & mask]++] = a[i];
for (int i = 0; i < a.length; i++) {
int var = (a[i] >>> shift) & mask;
if (var >= start && var < end) {
b[count[var]++] = a[i];
}
}
}
}
@@ -79,11 +77,15 @@ class MultiRadixPar{
int [] count = new int [mask+1];
// b) count=the frequency of each radix value in a
//Timer tb = new Timer().start();
for (int i = 0; i < n; i++) {
count[(a[i]>>> shift) & mask]++;
int var = (a[i]>>>shift) & mask;
count[var]++;
}
//System.out.println("B: "+tb.end().prettyTime());
// c) Add up in 'count' - accumulated values
//Timer tc = new Timer().start();
int j = 0;
int acumVal = 0;
for (int i = 0; i <= mask; i++) {
@@ -91,25 +93,26 @@ class MultiRadixPar{
count[i] = acumVal;
acumVal += j;
}
//System.out.println("C: "+tc.end().prettyTime());
// Parallel
Timer td = new Timer().start();
int d = n / nThreads;
Thread[] threads = new Thread[nThreads];
Worker[] workers = new Worker[nThreads];
for (int i = 0; i < nThreads; ++i) {
int start = d * i;
int end = i == nThreads - 1 ? n : d * (i + 1);
int end = i == nThreads - 1 ? n + 1 : d * (i + 1);
Worker w = workers[i] = new Worker(a, b, count, start, end, shift, mask);
Worker w = new Worker(start, end, a, b, count, shift, mask);
Thread t = threads[i] = new Thread(w);
t.start();
}
for (Thread t: threads) {
try { t.join(); } catch (InterruptedException ex) {}
}
System.out.println("par D: "+td.end().prettyTime());
//System.out.println("");
}// end radixSort
void testSort(int [] a){

Loading…
Cancel
Save