Class Arrays.Copier

Enclosing class:
Arrays

public static class Arrays.Copier extends Arrays.ParallelExecutor

Implementation of Arrays.ParallelExecutor performing simple copying of the source array. This class is used by Arrays.copy(ArrayContext, UpdatableArray, Array) and Arrays.copy(ArrayContext, UpdatableArray, Array, int) methods.

Please note: this class does not provide functionality of the methods Arrays.copy(ArrayContext, UpdatableArray, Array, int, boolean) and Arrays.compareAndCopy(ArrayContext, UpdatableArray, Array).

  • Constructor Details

  • Method Details

    • process

      public void process()
      Description copied from class: Arrays.ParallelExecutor
      Performs full processing the source AlgART array, passed to the constructor.

      This method uses a thread pool for performing calculations: java.util.concurrent.ExecutorService. The full processing task is split into M=Arrays.ParallelExecutor.numberOfTasks tasks, and the source array is split into n=Arrays.ParallelExecutor.numberOfRanges ranges (n>=M, n%M=0). It is possible to use M=1 (recommended settings for 1 CPU kernel), but even in this case we recommend to choice n big enough to split the array into not too large regions: it can help this method to optimize preloading and flushing external disk resources. The lengths of ranges are chosen equal or almost equal, with the only condition that the first index of every range (splitting position) is multiple of Arrays.ParallelExecutor.granularity(). Each task #k, 0<=k<M, processes a set of regions of the src array with step M regions, i.e. the regions #k, #k+M, #k+2M, ... Processing each region means just a call of Arrays.ParallelExecutor.processRange(long, long, int, long) method for the corresponding region of the array.

      All tasks are submitted to the thread pool, and then this method waits until all tasks will be completed. If some task throws an exception, this exception is stored and the internal flag "interruptionRequested" is set, that should lead to interruption of all Arrays.ParallelExecutor.processRange(long, long, int, long) methods (due to calling Arrays.ParallelExecutor.checkInterruption() by them). This exception will be re-thrown by this method before finishing.

      In addition to calling Arrays.ParallelExecutor.checkInterruption(), this method also interrupts all running threads, if the current thread, that calls this method, is interrupted by the standard Thread.interrupt() call. In this (and only this) case this method throws java.io.IOError. Usually, you should avoid interrupting the threads, processing AlgART arrays, via Thread.interrupt() technique: see the package description about runtime exceptions issue.

      The number of tasks and regions and the sizes of regions may be specified by arguments of the constructor or may be chosen automatically (if that arguments are 0).

      The thread pool, performing the multithread processing, is returned and (before finishing the processing) released by the methods of context.getThreadPoolFactory() object, where context is the argument of the constructor. If context argument is null, the DefaultThreadPoolFactory instance is created and used instead context.getThreadPoolFactory().

      Note: if the number of parallel tasks is 1, this method performs processing in the current thread and does not use the thread pool at all.

      At the end, in finally section, this method calls Arrays.ParallelExecutor.finish() method. Please note: if some RuntimeException B is thrown by Arrays.ParallelExecutor.finish() and there were some other exception A while executing the main body of this Arrays.ParallelExecutor.process() method, than the finishing exception B is ignored, but the main (usually more important) exception A is thrown.

      This method does nothing if the length the source array is 0.

      Overrides:
      process in class Arrays.ParallelExecutor
    • processSubArr

      protected void processSubArr(long position, int count, int threadIndex)
      This implementation copies src.subArr(position,count) to dest.subArr(position,count) via UpdatableArray.copy(Array) method.
      Specified by:
      processSubArr in class Arrays.ParallelExecutor
      Parameters:
      position - the start index of processed region, inclusive.
      count - the number of the processed elements.
      threadIndex - the index of the processed region (thread), from 0 to (number of tasks)-1.