Class Parallelization


  • public final class Parallelization
    extends java.lang.Object
    This class allows to configure an algorithm for 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 
      Modifier and Type Class Description
      (package private) static interface  Parallelization.Frame  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.ThreadLocal<TaskExecutor> executor  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Parallelization()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.concurrent.ExecutorService getExecutorService()  
      static TaskExecutor getTaskExecutor()
      Returns the TaskExecutor that was set for this thread.
      static void runMultiThreaded​(java.lang.Runnable action)
      To run an algorithm multi-threaded use:
      static <T> T runMultiThreaded​(java.util.concurrent.Callable<T> action)
      To run an algorithm multi-threaded use:
      static void runSingleThreaded​(java.lang.Runnable action)
      To run an algorithm single-threaded use:
      static <T> T runSingleThreaded​(java.util.concurrent.Callable<T> action)
      To run an algorithm single-threaded use:
      static void runWithExecutor​(java.util.concurrent.ExecutorService executorService, java.lang.Runnable action)
      Executes the given Runnable with the given ExecutorService, and waits for the execution to finish.
      static <R> R runWithExecutor​(java.util.concurrent.ExecutorService executorService, java.util.concurrent.Callable<R> action)
      Executes the given Callable with the given ExecutorService, waits for the execution to finish and returns the result.
      static void runWithExecutor​(TaskExecutor taskExecutor, java.lang.Runnable action)
      Executes the given Runnable with the given TaskExecutor, and waits for the execution to finish.
      static <T> T runWithExecutor​(TaskExecutor taskExecutor, java.util.concurrent.Callable<T> action)
      Executes the given Callable with the given TaskExecutor, waits for the execution to finish and returns the result.
      static void runWithNumThreads​(int numThreads, java.lang.Runnable action)
      To run an algorithm a given number of threads use:
      static <R> R runWithNumThreads​(int numThreads, java.util.concurrent.Callable<R> action)
      To run an algorithm a given number of threads use:
      (package private) static Parallelization.Frame setExecutorRequiresReset​(TaskExecutor taskExecutor)
      This method can be used to execute an algorithm with a given TaskExecutor.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • executor

        private static final java.lang.ThreadLocal<TaskExecutor> executor
    • Constructor Detail

      • Parallelization

        private Parallelization()
    • Method Detail

      • getExecutorService

        public static java.util.concurrent.ExecutorService getExecutorService()
      • runSingleThreaded

        public static void runSingleThreaded​(java.lang.Runnable action)
        To run an algorithm single-threaded use:

        Parallelization.runSingleThreaded( () -> myAlgorithm( input ) );

      • runSingleThreaded

        public static <T> T runSingleThreaded​(java.util.concurrent.Callable<T> action)
        To run an algorithm single-threaded use:

        output = Parallelization.runSingleThreaded( () -> myAlgorithm( input ) );

      • runMultiThreaded

        public static void runMultiThreaded​(java.lang.Runnable action)
        To run an algorithm multi-threaded use:

        Parallelization.runMultiThreaded( () -> myAlgorithm( input ) );

      • runMultiThreaded

        public static <T> T runMultiThreaded​(java.util.concurrent.Callable<T> action)
        To run an algorithm multi-threaded use:

        output = Parallelization.runMultiThreaded( () -> myAlgorithm( input ) );

      • runWithNumThreads

        public static void runWithNumThreads​(int numThreads,
                                             java.lang.Runnable action)
        To run an algorithm a given number of threads use:

        Parallelization.runWithNumThreads( numThreads, () -> myAlgorithm( input ) );

      • runWithNumThreads

        public static <R> R runWithNumThreads​(int numThreads,
                                              java.util.concurrent.Callable<R> action)
        To run an algorithm a given number of threads use:

        output = Parallelization.runWithNumThreads( numThreads, () -> myAlgorithm( input ) );

      • runWithExecutor

        public static void runWithExecutor​(java.util.concurrent.ExecutorService executorService,
                                           java.lang.Runnable action)
        Executes the given Runnable with the given ExecutorService, and waits for the execution to finish.
      • runWithExecutor

        public static <R> R runWithExecutor​(java.util.concurrent.ExecutorService executorService,
                                            java.util.concurrent.Callable<R> action)
        Executes the given Callable with the given ExecutorService, waits for the execution to finish and returns the result.
      • runWithExecutor

        public static void runWithExecutor​(TaskExecutor taskExecutor,
                                           java.lang.Runnable action)
        Executes the given Runnable with the given TaskExecutor, and waits for the execution to finish.
      • runWithExecutor

        public static <T> T runWithExecutor​(TaskExecutor taskExecutor,
                                            java.util.concurrent.Callable<T> action)
        Executes the given Callable with the given TaskExecutor, waits for the execution to finish and returns the result.
      • setExecutorRequiresReset

        static Parallelization.Frame setExecutorRequiresReset​(TaskExecutor taskExecutor)
        This method can be used to execute an algorithm with a given TaskExecutor. But it's easier to use runWithExecutor(java.util.concurrent.ExecutorService, java.lang.Runnable).

        This method sets the TaskExecutor for the current thread. This can be used to execute a certain part of your code with the given TaskExecutor. It's mandatory to call the close() of the return frame afterwards. This could be done using an try-with-resources statement.

         
        
         try ( Parallelization.Frame frame = Parallelization.setExecutorRequiresReset( taskExecutor ) )
         {
             myAlgorithm(input);
         }
         
         
        Or by explicitly calling frame.close() in the finally block.
         
        
         Parallelization.Frame frame = Parallelization.setExecutorRequiresReset( taskExecutor );
         try
         {
             myAlgorithm(input);
         }
         finally
         {
             frame.close();
         }