import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import easyIO.*; class Oppgave5{ int [] a; int n, antT; CyclicBarrier sync ; public static void main (String [] args) { if ( args.length < 1 ) { System.out.println("use: >java Oppg4 "); } else { new Oppgave5().doTest(args); } } // end main void swap (int [] a, int left, int right ) { int temp,r = a.length-1; for (int j = left ; j < right; j++){ temp = a[j]; a[j] = a [r-j]; a [r-j]=temp; } } // end swap void doTest (String [] args) { // initiate n = Integer.parseInt(args[0]); antT = Runtime.getRuntime().availableProcessors(); sync = new CyclicBarrier(antT +1); a = new int[n]; Random r = new Random(1337); long tid ; int k; // ***************** Sekvensiell RadSort ****************** int sum =0; // -- init a[][] ----- for (int i =0; i< a.length;i++){ a[i] = i;// random fill } // end initarray tid = System.nanoTime(); // start timer swap ( a,0,n/2); double tt = (System.nanoTime()-tid)/1000000.0; testRS(a, "Sekv "); System.out.println("n: "+n+", num cores:"+ antT ); System.out.println(" Sequental swaping in : "+ tt+ " ms. " ); // ***************** Parallell RadSort ****************** for (int i =0; i< a.length;i++){ a[i] = i; } // end initarray // a) regn ut radsum i parallell tid = System.nanoTime(); // start timer Thread [ ] tr = new Thread[antT]; for (int i =0; i< antT;i++){ (tr[i] = new Thread(new Arbeider(i))).start();; } // end initarray try{ for (int i =0; i< antT;i++) tr[i].join(); }catch(Exception e) {return;} /* try{ sync.await(); } catch (Exception e) { return;} */ double ttt = (System.nanoTime()-tid)/1000000.0; testRS(a, "Para "); System.out.println("n: "+n+", num cores:"+ antT ); System.out.println(" Parallel swapping in: "+ ttt+ " ms., speedup: " +Format.align (tt/ttt, 5,2) ); } // end doTest void testRS(int [] rs, String s) { for (int i =1; i < rs.length; i++) if (rs[i-1] < rs[i]) { System.out.println("ERROR:" + s+ ", at i ="+i); } } // end testRS class Arbeider implements Runnable { int ind; // lokale data og metoder B int part, left,right; Arbeider ( int in) { ind = in; part = n/(antT*2); left = ind*part; if (ind == antT-1) right = n/2; else right = left + part; } public void run( ) { // kalles n?r tr?den er startet swap(a,left,right); /* try{ sync.await(); } catch (Exception e) { return;} */ } // end run } // end indre klasse Arbeider } // end Oppg.4