gov.sandia.cognition.algorithm
Class ParallelUtil

java.lang.Object
  extended by gov.sandia.cognition.algorithm.ParallelUtil

public class ParallelUtil
extends Object

Utility methods for creating parallel algorithms.

Since:
3.0
Author:
Kevin R. Dixon

Field Summary
static int OPTIMAL_THREADS
          Indicates to the createThreadPool() method to estimate the "optimal" number of threads for the computer currently executing the codes.
 
Constructor Summary
protected ParallelUtil()
          Protected constructor since this is a utility class.
 
Method Summary
static
<ResultType>
NamedValue<Double>
compareTimes(Collection<? extends Callable<ResultType>> tasks)
          Compares the times needed by running the tasks sequentially versus parallel.
static
<ResultType>
NamedValue<Double>
compareTimes(Collection<? extends Callable<ResultType>> tasks, ThreadPoolExecutor threadPool)
          Compares the times needed by running the tasks sequentially versus parallel.
static ThreadPoolExecutor createThreadPool()
          Creates a thread pool with the "optimal" number of threads.
static ThreadPoolExecutor createThreadPool(int numRequestedThreads)
          Creates a thread pool with the given number of threads.
static ThreadFactory defaultDaemonThreadFactory()
          Creates a version of the default thread factory from Executors.defaultThreadFactory() that creates daemon threads.
static
<ResultType>
ArrayList<ResultType>
executeInParallel(Collection<? extends Callable<ResultType>> tasks)
          Executes the given Callable tasks in parallel using a default thread pool
static
<ResultType>
ArrayList<ResultType>
executeInParallel(Collection<? extends Callable<ResultType>> tasks, ParallelAlgorithm algorithm)
          Executes the given Callable tasks in parallel using a given thread pool
static
<ResultType>
ArrayList<ResultType>
executeInParallel(Collection<? extends Callable<ResultType>> tasks, ThreadPoolExecutor threadPool)
          Executes the given Callable tasks in parallel using a given thread pool
static
<ResultType>
ArrayList<ResultType>
executeInSequence(Collection<? extends Callable<ResultType>> tasks)
          Executes the given Callable tasks sequentially in series.
static int getDefaultNumThreads()
          Gets the current default number of threads to use when called using the default createThreadPool() method
static int getNumThreads(ParallelAlgorithm algorithm)
          Gets the number of threads in a ParallelAlgorithm by querying the thread pool
static int getNumThreads(ThreadPoolExecutor threadPool)
          Gets the number of threads in a ThreadPoolExecutor
static void setDefaultNumThreads(int defaultNumThreads)
          Sets the current default number of threads to use when calling the default createThreadPool() method
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPTIMAL_THREADS

public static final int OPTIMAL_THREADS
Indicates to the createThreadPool() method to estimate the "optimal" number of threads for the computer currently executing the codes.

See Also:
Constant Field Values
Constructor Detail

ParallelUtil

protected ParallelUtil()
Protected constructor since this is a utility class.

Method Detail

getDefaultNumThreads

public static int getDefaultNumThreads()
Gets the current default number of threads to use when called using the default createThreadPool() method

Returns:
Number of threads to use during a default call to createThreadPool()

setDefaultNumThreads

public static void setDefaultNumThreads(int defaultNumThreads)
Sets the current default number of threads to use when calling the default createThreadPool() method

Parameters:
defaultNumThreads - Number of threads to use during a default call to createThreadPool()

createThreadPool

public static ThreadPoolExecutor createThreadPool()
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.

Returns:
Thread pool with the "optimal" number of threads

createThreadPool

public static ThreadPoolExecutor createThreadPool(int numRequestedThreads)
Creates a thread pool with the given 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.

Parameters:
numRequestedThreads - Number of threads to create, or OPTIMAL_THREADS an "optimal" number of threads for the current computer.
Returns:
Thread pool with the desired size that creates daemon threads.

getNumThreads

public static int getNumThreads(ParallelAlgorithm algorithm)
Gets the number of threads in a ParallelAlgorithm by querying the thread pool

Parameters:
algorithm - ParallelAlgorithm to consider
Returns:
Number of threads used by the ParallelAlgorithm's thread pool

getNumThreads

public static int getNumThreads(ThreadPoolExecutor threadPool)
Gets the number of threads in a ThreadPoolExecutor

Parameters:
threadPool - ThreadPoolExecutor to consider
Returns:
Number of threads used by the thread pool

executeInParallel

public static <ResultType> ArrayList<ResultType> executeInParallel(Collection<? extends Callable<ResultType>> tasks)
                                               throws InterruptedException,
                                                      ExecutionException
Executes the given Callable tasks in parallel using a default thread pool

Type Parameters:
ResultType - Type of results returned by the Callable tasks.
Parameters:
tasks - Callable tasks to be split across multiple cores
Returns:
Collection of results from the Callables
Throws:
InterruptedException - If interrupted
ExecutionException - If the Callable task can't execute its method

executeInParallel

public static <ResultType> ArrayList<ResultType> executeInParallel(Collection<? extends Callable<ResultType>> tasks,
                                                                   ThreadPoolExecutor threadPool)
                                               throws InterruptedException,
                                                      ExecutionException
Executes the given Callable tasks in parallel using a given thread pool

Type Parameters:
ResultType - Type of results returned by the Callable tasks.
Parameters:
tasks - Callable tasks to be split across multiple cores
threadPool - Thread pool to use for the parallelization
Returns:
Collection of results from the Callables
Throws:
InterruptedException - If interrupted
ExecutionException - If the Callable task can't execute its method

executeInParallel

public static <ResultType> ArrayList<ResultType> executeInParallel(Collection<? extends Callable<ResultType>> tasks,
                                                                   ParallelAlgorithm algorithm)
                                               throws InterruptedException,
                                                      ExecutionException
Executes the given Callable tasks in parallel using a given thread pool

Type Parameters:
ResultType - Type of results returned by the Callable tasks.
Parameters:
tasks - Callable tasks to be split across multiple cores
algorithm - Uses the embedded thread pool in the ParallelAlgorithm
Returns:
Collection of results from the Callables
Throws:
InterruptedException - If interrupted
ExecutionException - If the Callable task can't execute its method

executeInSequence

public static <ResultType> ArrayList<ResultType> executeInSequence(Collection<? extends Callable<ResultType>> tasks)
                                               throws Exception
Executes the given Callable tasks sequentially in series.

Type Parameters:
ResultType - Type of results returned by the Callable tasks.
Parameters:
tasks - Callable tasks to execute sequentially
Returns:
Results from the sequential execution
Throws:
Exception - If one of the Callables can't execute its method.

compareTimes

public static <ResultType> NamedValue<Double> compareTimes(Collection<? extends Callable<ResultType>> tasks)
Compares the times needed by running the tasks sequentially versus parallel.

Type Parameters:
ResultType - Result types of the Callable tasks
Parameters:
tasks - Callable tasks to execute in parallel or sequentially
Returns:
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.

compareTimes

public static <ResultType> NamedValue<Double> compareTimes(Collection<? extends Callable<ResultType>> tasks,
                                                           ThreadPoolExecutor threadPool)
Compares the times needed by running the tasks sequentially versus parallel.

Type Parameters:
ResultType - Result types of the Callable tasks
Parameters:
tasks - Callable tasks to execute in parallel or sequentially
threadPool - Thread pool to use for the parallelization
Returns:
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.

defaultDaemonThreadFactory

public static ThreadFactory defaultDaemonThreadFactory()
Creates a version of the default thread factory from Executors.defaultThreadFactory() that creates daemon threads. Daemon threads mean that the application can exit even if the threads are still alive.

Returns:
A default thread factory that creates daemon threads.