Class Parallelization
The algorithm needs to use the TaskExecutor returned by
getTaskExecutor() to implement the parallelization.
Alternatively it can use getExecutorService().
But TaskExecutor is simpler and better suited for image precessing algorithms.
The algorithm can be executed singleThreaded, multiThreaded or by
using a specified ExecutorService or TaskExecutor:
// Single-threaded call
Parallelization.runSingleThreaded( () -> myAlgorithm( image ) );
// Multi-threaded call
Parallelization.runMultiThreaded( () -> myAlgorithm( image ) );
// ExecutorService
Parallelization.withExecutor( executorService, () -> myAlgorithm( image ) );
// Multi-threaded is the default.
// A normal function call, that's not somehow wrapped by
// Parallelization.runSingleThreaded( ... ) runs multi-threaded.
myAlgorithm( image );
// Example Algorithm, that fills an image with ones.
public void myAlgorithm( RandomAccessibleInterval< IntType > image )
{
TaskExecutor taskExecutor = Parallelization.getTaskExecutor();
int numTasks = taskExecutor.suggestNumberOfTasks();
List< Interval > chunks = IntervalChunks.chunkInterval( image, numTasks );
// The TaskExecutor executes the forEach method in multiple threads, if requested.
taskExecutor.forEach( chunks, chunk -> {
for ( IntType pixel : Views.interval( image, chunk ) )
pixel.setOne();
} );
}
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorServicestatic TaskExecutorReturns theTaskExecutorthat was set for this thread.static voidrunMultiThreaded(Runnable action) To run an algorithm multi-threaded use:static <T> TrunMultiThreaded(Callable<T> action) To run an algorithm multi-threaded use:static voidrunSingleThreaded(Runnable action) To run an algorithm single-threaded use:static <T> TrunSingleThreaded(Callable<T> action) To run an algorithm single-threaded use:static voidrunWithExecutor(ExecutorService executorService, Runnable action) Executes the givenRunnablewith the givenExecutorService, and waits for the execution to finish.static <R> RrunWithExecutor(ExecutorService executorService, Callable<R> action) Executes the givenCallablewith the givenExecutorService, waits for the execution to finish and returns the result.static voidrunWithExecutor(TaskExecutor taskExecutor, Runnable action) Executes the givenRunnablewith the givenTaskExecutor, and waits for the execution to finish.static <T> TrunWithExecutor(TaskExecutor taskExecutor, Callable<T> action) Executes the givenCallablewith the givenTaskExecutor, waits for the execution to finish and returns the result.static voidrunWithNumThreads(int numThreads, Runnable action) To run an algorithm a given number of threads use:static <R> RrunWithNumThreads(int numThreads, Callable<R> action) To run an algorithm a given number of threads use:(package private) static Parallelization.FramesetExecutorRequiresReset(TaskExecutor taskExecutor) This method can be used to execute an algorithm with a givenTaskExecutor.
-
Field Details
-
executor
-
-
Constructor Details
-
Parallelization
private Parallelization()
-
-
Method Details
-
getTaskExecutor
Returns theTaskExecutorthat was set for this thread. -
getExecutorService
-
runSingleThreaded
To run an algorithm single-threaded use:Parallelization.runSingleThreaded( () -> myAlgorithm( input ) ); -
runSingleThreaded
To run an algorithm single-threaded use:output = Parallelization.runSingleThreaded( () -> myAlgorithm( input ) ); -
runMultiThreaded
To run an algorithm multi-threaded use:Parallelization.runMultiThreaded( () -> myAlgorithm( input ) ); -
runMultiThreaded
To run an algorithm multi-threaded use:output = Parallelization.runMultiThreaded( () -> myAlgorithm( input ) ); -
runWithNumThreads
To run an algorithm a given number of threads use:Parallelization.runWithNumThreads( numThreads, () -> myAlgorithm( input ) ); -
runWithNumThreads
To run an algorithm a given number of threads use:output = Parallelization.runWithNumThreads( numThreads, () -> myAlgorithm( input ) ); -
runWithExecutor
Executes the givenRunnablewith the givenExecutorService, and waits for the execution to finish. -
runWithExecutor
Executes the givenCallablewith the givenExecutorService, waits for the execution to finish and returns the result. -
runWithExecutor
Executes the givenRunnablewith the givenTaskExecutor, and waits for the execution to finish. -
runWithExecutor
Executes the givenCallablewith the givenTaskExecutor, waits for the execution to finish and returns the result. -
setExecutorRequiresReset
This method can be used to execute an algorithm with a givenTaskExecutor. But it's easier to userunWithExecutor(ExecutorService, Runnable).This method sets the
TaskExecutorfor the current thread. This can be used to execute a certain part of your code with the givenTaskExecutor. It's mandatory to call theclose()of the returnframeafterwards. This could be done using an try-with-resources statement.
Or by explicitly callingtry ( Parallelization.Frame frame = Parallelization.setExecutorRequiresReset( taskExecutor ) ) { myAlgorithm(input); }frame.close()in the finally block.Parallelization.Frame frame = Parallelization.setExecutorRequiresReset( taskExecutor ); try { myAlgorithm(input); } finally { frame.close(); }
-