mortie пре 7 година
родитељ
комит
6f796a9117
3 измењених фајлова са 30 додато и 20 уклоњено
  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 Прегледај датотеку

a = s.sort(a); a = s.sort(a);
t.end(); t.end();
new MultiRadix().testSort(a); new MultiRadix().testSort(a);

return t; return t;
} // end doIt } // end doIt
} }

+ 7
- 1
inf2440/hw3/MultiRadix.java Прегледај датотеку

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

+ 22
- 19
inf2440/hw3/MultiRadixPar.java Прегледај датотеку

int [] bit ; int [] bit ;
// a) finn max verdi i a[] // a) finn max verdi i a[]
//Timer ta = new Timer().start();
for (int i = 1 ; i < n ; i++) for (int i = 1 ; i < n ; i++)
if (a[i] > max) max = a[i]; if (a[i] > max) max = a[i];
while (max >= (1L<<numBit) )numBit++; // antall binaere siffer i max while (max >= (1L<<numBit) )numBit++; // antall binaere siffer i max
//System.out.println("A: "+ta.end().prettyTime());
// bestem antall bit i numBits sifre // bestem antall bit i numBits sifre
numDigits = Math.max(1, numBit/NUM_BIT); numDigits = Math.max(1, numBit/NUM_BIT);
a = b; a = b;
b = t; 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; return a;
} // end radixMulti } // end radixMulti
class Worker implements Runnable { class Worker implements Runnable {
int[] a, b, count;
int start, end, shift, mask; 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.start = start;
this.end = end; this.end = end;
this.a = a;
this.b = b;
this.count = count;
this.shift = shift; this.shift = shift;
this.mask = mask; this.mask = mask;
} }
public void run() { 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];
}
} }
} }
} }
int [] count = new int [mask+1]; int [] count = new int [mask+1];
// b) count=the frequency of each radix value in a // b) count=the frequency of each radix value in a
//Timer tb = new Timer().start();
for (int i = 0; i < n; i++) { 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 // c) Add up in 'count' - accumulated values
//Timer tc = new Timer().start();
int j = 0; int j = 0;
int acumVal = 0; int acumVal = 0;
for (int i = 0; i <= mask; i++) { for (int i = 0; i <= mask; i++) {
count[i] = acumVal; count[i] = acumVal;
acumVal += j; acumVal += j;
} }
//System.out.println("C: "+tc.end().prettyTime());
// Parallel
Timer td = new Timer().start(); Timer td = new Timer().start();
int d = n / nThreads; int d = n / nThreads;
Thread[] threads = new Thread[nThreads]; Thread[] threads = new Thread[nThreads];
Worker[] workers = new Worker[nThreads];
for (int i = 0; i < nThreads; ++i) { for (int i = 0; i < nThreads; ++i) {
int start = d * 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); Thread t = threads[i] = new Thread(w);
t.start(); t.start();
} }
for (Thread t: threads) { for (Thread t: threads) {
try { t.join(); } catch (InterruptedException ex) {} try { t.join(); } catch (InterruptedException ex) {}
} }
System.out.println("par D: "+td.end().prettyTime()); System.out.println("par D: "+td.end().prettyTime());
//System.out.println("");
}// end radixSort }// end radixSort
void testSort(int [] a){ void testSort(int [] a){

Loading…
Откажи
Сачувај