Quellcode durchsuchen

snapshot

master
mortie vor 7 Jahren
Ursprung
Commit
0b985fe74d
3 geänderte Dateien mit 26 neuen und 11 gelöschten Zeilen
  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 Datei anzeigen

Random r = new Random(123); Random r = new Random(123);


int[] a = new int[len]; 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++) { for (int j = 0; j < len; j++) {
a[j] = r.nextInt(len); a[j] = r.nextInt(len);
} }

+ 4
- 0
inf2440/hw3/MultiRadix.java Datei anzeigen

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

+ 17
- 11
inf2440/hw3/MultiRadixPar.java Datei anzeigen

int n; int n;
int [] a; int [] a;
final static int NUM_BIT = 7; // alle tall 6-11 .. finn ut hvilken verdi som er best 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) { int [] radixMulti(int [] a) {
long tt = System.nanoTime(); long tt = System.nanoTime();
class Worker implements Runnable { class Worker implements Runnable {
int[] a, b, count; int[] a, b, count;
int start, end, shift, mask; int start, end, shift, mask;
int id;
Worker(int[] a, int[] b, int[] count, int start, int end, int shift, int mask) { Worker(int[] a, int[] b, int[] count, int start, int end, int shift, int mask) {
this.a = a; this.a = a;
this.b = b; this.b = b;
this.count = Arrays.copyOf(count, count.length);
this.count = count; //Arrays.copyOf(count, count.length);
this.start = start; this.start = start;
this.end = end; this.end = end;
this.shift = shift; this.shift = shift;
} }
public void run() { 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 // 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]; b[count[(a[i]>>>shift) & mask]++] = a[i];
} }
} }
count[(a[i]>>> shift) & mask]++; 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 // Parallel
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]; Worker[] workers = new Worker[nThreads];
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());
}// end radixSort }// end radixSort

Laden…
Abbrechen
Speichern