/*--------------------------------------------------------------- * Eksempelprogram p? bruk av Excecutor Service * by Arne Maus, 2013 med indre klasse *-----------------------------------------------------------------*/ import java.util.*; import java.util.concurrent.*; class ExcecutorFinn { int size =0, runs =1, traadmult=1; int [] a; int [] localMax; long startParaTimer = 0, paraTime = 0, seqTime = 0, overhTid =0; // create the Excecutor pool and a list of futures (now empty) remove hyperthreading int antTr?der =Runtime.getRuntime().availableProcessors()/2; ExecutorService pool ; List futures = new Vector (); public static void main(String[] args) { if (args.length <3) { System.out.println("use: >java ExcecutorFinn size(n) numIter traadmult"); } else { ExcecutorFinn pt = new ExcecutorFinn(); pt.size = Integer.parseInt(args[0]); pt.runs = Integer.parseInt(args[1]); pt.traadmult= Integer.parseInt(args[2]); pt.antTr?der *= pt.traadmult; pt.pool = Executors.newFixedThreadPool(pt.antTr?der); pt.a = new int[pt.size]; pt.localMax = new int[pt.antTr?der]; pt.startPara(); } } // end main void startPara() { int gMax= -1; // fill a[] with random Uniform numbers fillArrayWithUniformNumbers (a); //The run method of the target is called when this thread is started. // If the target argument is null, this thread's run method is called when this thread is started. startParaTimer = System.nanoTime(); for (int i =0; i< runs; i++) { gMax =-1; // start 'cores' threads for (int j =0; j < antTr?der; j++) { Thread QParExec = new Thread(new FindExec(j)); futures.add(pool.submit(QParExec)); // submit starts the Thread } // wait for all futures ( threads) to complete while (!futures.isEmpty()) { Future top = futures.remove(0); try { if (top != null) top.get(); } catch(Exception e) { System.out.println("Error Futures");} } // find global max for (int j = 0; j < antTr?der; j++){ if (localMax[j] > gMax) gMax=localMax[j]; } } pool.shutdown(); paraTime = (System.nanoTime() -startParaTimer)/runs; // fill a for new sort seqTime = System.nanoTime(); int seqRes =0; for (int i =0; i< runs; i++) { seqRes = FindMax(a); } seqTime = (System.nanoTime()- seqTime)/runs; System.out.println("ExcecutorFinn\n: " + " Parallell res="+ gMax+", paratid tid :"+ (paraTime*1.0/1000000) + " ms. \n Seqvential res="+ seqRes +", sekvensiell tid:" + (seqTime*1.0/1000000)+"ms."); System.out.println("\n Speedup (rel to sequential finnMax) :" + (seqTime*1.0/paraTime)); } // end startPara /** fill a[] with random Uniform(0:n-1) numbers */ void fillArrayWithUniformNumbers (int [] a) { Random r = new Random(13579); for (int i =0; i < a.length; i++) a[i] = r.nextInt(a.length); }// end fillArrayWithUniformNumbers int FindMax(int[] a){ int max = -1; for (int i = 0; i< a.length; i++) { if (a[i] > max) max = a[i]; } return max; // lev?r svar } // end FindLocalMax //----------------------------------------------------------------------------------- class FindExec implements Runnable { // ExecutorService implementation int index; // private static ExecutorService pool = // Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); public FindExec(int ind) { index = ind; } public void run() { FindLocalMax(index); // pool.shutdown(); } void FindLocalMax(int ind){ int minMax = -1; int ant= a.length/antTr?der, ant2=ant; // antall elementer per tr?d if (ind == antTr?der-1) ant2 += a.length%antTr?der; for (int i = 0; i< ant2; i++) { if (a[ant*ind+i] > minMax) minMax = a[ant*ind+i]; } localMax[ind] =minMax; // lev?r svar } // end FindLocalMax } // end FindExec } // end class ExcecutorFinn