public ThreadPoolExecutor getThreadPool() { if( this.threadPool == null ) { this.threadPool = ParallelUtil.createThreadPool(); } return this.threadPool; }
/** * Executes the given Callable tasks in parallel using a default thread * pool * @param <ResultType> Type of results returned by the Callable tasks. * @param tasks * Callable tasks to be split across multiple cores * @return * Collection of results from the Callables * @throws java.lang.InterruptedException * If interrupted * @throws java.util.concurrent.ExecutionException * If the Callable task can't execute its method */ public static <ResultType> ArrayList<ResultType> executeInParallel( final Collection<? extends Callable<ResultType>> tasks) throws InterruptedException, ExecutionException { return executeInParallel(tasks, ParallelUtil.createThreadPool()); }
/** * Getter for #threads * @return * #threads */ public int getNumThreads() { return ParallelUtil.getNumThreads( this ); }
/** * Creates a thread pool with the "optimal" number of threads. The thread * pool creates daemon threads, so if all the non-daemon threads are * finished, then the application will not wait for the thread pool to be * cleaned up. * * @return * Thread pool with the "optimal" number of threads */ public static ThreadPoolExecutor createThreadPool() { return createThreadPool(getDefaultNumThreads()); }
/** * Compares the times needed by running the tasks sequentially versus * parallel. * @param <ResultType> Result types of the Callable tasks * @param tasks * Callable tasks to execute in parallel or sequentially * @return * Named value with the name being a report of the time taken for parallel * execution, sequential execution, the parallel to sequential ratio, and * the number of threads used in the parallel execution. The value * is the parallel to sequential ratio. That is, 1.0 means it takes just * as long to execute in parallel as sequentially. A value greater than * 1.0 means it takes longer in parallel than sequentially. A value less * than 1.0 means it takes longer sequentially than in parallel. */ public static <ResultType> NamedValue<Double> compareTimes( Collection<? extends Callable<ResultType>> tasks) { return compareTimes(tasks, createThreadPool()); }
/** * Executes the given Callable tasks in parallel using a given thread * pool * @param <ResultType> Type of results returned by the Callable tasks. * @param tasks * Callable tasks to be split across multiple cores * @param algorithm * Uses the embedded thread pool in the ParallelAlgorithm * @return * Collection of results from the Callables * @throws java.lang.InterruptedException * If interrupted * @throws java.util.concurrent.ExecutionException * If the Callable task can't execute its method */ public static <ResultType> ArrayList<ResultType> executeInParallel( Collection<? extends Callable<ResultType>> tasks, ParallelAlgorithm algorithm ) throws InterruptedException, ExecutionException { return executeInParallel( tasks, algorithm.getThreadPool() ); }
executeInSequence(tasks); long seriesStop = System.currentTimeMillis(); executeInParallel(tasks, threadPool); long parallelStop = System.currentTimeMillis(); String name = "Series time = " + seriesTime + ", Parallel time = " + parallelTime + ", Parallel/Series ratio = " + parallelToSeriesRatio + " using " + ParallelUtil.getNumThreads( threadPool) + " threads."; return new DefaultNamedValue<Double>(name, parallelToSeriesRatio);
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super Double> output) { ArrayList<SampleRange> tasks = new ArrayList<SampleRange>(sampleCount); SmoothUnivariateDistribution t; if( this.getDegreesOfFreedom() < 30.0 ) { t = new StudentTDistribution(this.getDegreesOfFreedom()); } else { t = new UnivariateGaussian(); } for( int n = 0; n < sampleCount; n++ ) { tasks.add( new SampleRange(random, this.getTreatmentCount(), t) ); } try { output.addAll(ParallelUtil.executeInSequence(tasks)); } catch (Exception ex) { throw new RuntimeException( ex ); } }
/** * Executes the given Callable tasks in parallel using a given thread * pool * @param <ResultType> Type of results returned by the Callable tasks. * @param tasks * Callable tasks to be split across multiple cores * @param algorithm * Uses the embedded thread pool in the ParallelAlgorithm * @return * Collection of results from the Callables * @throws java.lang.InterruptedException * If interrupted * @throws java.util.concurrent.ExecutionException * If the Callable task can't execute its method */ public static <ResultType> ArrayList<ResultType> executeInParallel( Collection<? extends Callable<ResultType>> tasks, ParallelAlgorithm algorithm ) throws InterruptedException, ExecutionException { return executeInParallel( tasks, algorithm.getThreadPool() ); }
executeInSequence(tasks); long seriesStop = System.currentTimeMillis(); executeInParallel(tasks, threadPool); long parallelStop = System.currentTimeMillis(); String name = "Series time = " + seriesTime + ", Parallel time = " + parallelTime + ", Parallel/Series ratio = " + parallelToSeriesRatio + " using " + ParallelUtil.getNumThreads( threadPool) + " threads."; return new DefaultNamedValue<Double>(name, parallelToSeriesRatio);
/** * Creates a thread pool with the "optimal" number of threads. The thread * pool creates daemon threads, so if all the non-daemon threads are * finished, then the application will not wait for the thread pool to be * cleaned up. * * @return * Thread pool with the "optimal" number of threads */ public static ThreadPoolExecutor createThreadPool() { return createThreadPool(getDefaultNumThreads()); }
/** * Compares the times needed by running the tasks sequentially versus * parallel. * @param <ResultType> Result types of the Callable tasks * @param tasks * Callable tasks to execute in parallel or sequentially * @return * Named value with the name being a report of the time taken for parallel * execution, sequential execution, the parallel to sequential ratio, and * the number of threads used in the parallel execution. The value * is the parallel to sequential ratio. That is, 1.0 means it takes just * as long to execute in parallel as sequentially. A value greater than * 1.0 means it takes longer in parallel than sequentially. A value less * than 1.0 means it takes longer sequentially than in parallel. */ public static <ResultType> NamedValue<Double> compareTimes( Collection<? extends Callable<ResultType>> tasks) { return compareTimes(tasks, createThreadPool()); }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super Double> output) { ArrayList<SampleRange> tasks = new ArrayList<SampleRange>(sampleCount); SmoothUnivariateDistribution t; if( this.getDegreesOfFreedom() < 30.0 ) { t = new StudentTDistribution(this.getDegreesOfFreedom()); } else { t = new UnivariateGaussian(); } for( int n = 0; n < sampleCount; n++ ) { tasks.add( new SampleRange(random, this.getTreatmentCount(), t) ); } try { output.addAll(ParallelUtil.executeInSequence(tasks)); } catch (Exception ex) { throw new RuntimeException( ex ); } }
public ThreadPoolExecutor getThreadPool() { if (this.threadPool == null) { this.threadPool = ParallelUtil.createThreadPool(); } return this.threadPool; }
/** * Getter for #threads * @return * #threads */ public int getNumThreads() { return ParallelUtil.getNumThreads( this ); }
/** * Executes the given Callable tasks in parallel using a given thread * pool * @param <ResultType> Type of results returned by the Callable tasks. * @param tasks * Callable tasks to be split across multiple cores * @param algorithm * Uses the embedded thread pool in the ParallelAlgorithm * @return * Collection of results from the Callables * @throws java.lang.InterruptedException * If interrupted * @throws java.util.concurrent.ExecutionException * If the Callable task can't execute its method */ public static <ResultType> ArrayList<ResultType> executeInParallel( Collection<? extends Callable<ResultType>> tasks, ParallelAlgorithm algorithm ) throws InterruptedException, ExecutionException { return executeInParallel( tasks, algorithm.getThreadPool() ); }
executeInSequence(tasks); long seriesStop = System.currentTimeMillis(); executeInParallel(tasks, threadPool); long parallelStop = System.currentTimeMillis(); String name = "Series time = " + seriesTime + ", Parallel time = " + parallelTime + ", Parallel/Series ratio = " + parallelToSeriesRatio + " using " + ParallelUtil.getNumThreads( threadPool) + " threads."; return new DefaultNamedValue<Double>(name, parallelToSeriesRatio);
/** * Executes the given Callable tasks in parallel using a default thread * pool * @param <ResultType> Type of results returned by the Callable tasks. * @param tasks * Callable tasks to be split across multiple cores * @return * Collection of results from the Callables * @throws java.lang.InterruptedException * If interrupted * @throws java.util.concurrent.ExecutionException * If the Callable task can't execute its method */ public static <ResultType> ArrayList<ResultType> executeInParallel( final Collection<? extends Callable<ResultType>> tasks) throws InterruptedException, ExecutionException { return executeInParallel(tasks, ParallelUtil.createThreadPool()); }