Class Arrays.Copier
- Enclosing class:
Arrays
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)
.
-
Field Summary
Fields inherited from class net.algart.arrays.Arrays.ParallelExecutor
blockSize, dest, numberOfRanges, numberOfTasks, src
-
Constructor Summary
ConstructorDescriptionCopier
(ArrayContext context, UpdatableArray dest, Array src, int numberOfTasks, long numberOfRanges) Creates new instance of this class, performing copying of src array to dest array. -
Method Summary
Modifier and TypeMethodDescriptionvoid
process()
Performs full processing the source AlgART array, passed to the constructor.protected void
processSubArr
(long position, int count, int threadIndex) This implementation copies src.subArr
(position,count) to dest.subArr
(position,count) viaUpdatableArray.copy(Array)
method.Methods inherited from class net.algart.arrays.Arrays.ParallelExecutor
checkInterruption, context, correctNumberOfRanges, endGap, finish, granularity, increaseReadyCount, numberOfRanges, numberOfTasks, processRange, rangeFrom, rangeLength, rangeTo, readyCount, recommendedNumberOfRanges, startGap, toString, updateProgress
-
Constructor Details
-
Copier
public Copier(ArrayContext context, UpdatableArray dest, Array src, int numberOfTasks, long numberOfRanges) Creates new instance of this class, performing copying of src array to dest array. Theprocess()
method of the created instance is equivalent toArrays.copy(context, dest, src, numberOfTasks)
call, excepting that the last method sometimes uses more complex copying algorithms.- Parameters:
context
- the context of copying; may be null, then it will be ignored.dest
- the destination array.src
- the source array.numberOfTasks
- the desired number of parallel tasks or 0 if you want to determine this number automatically, from the passed context or (if it is null) fromDefaultThreadPoolFactory
instance.numberOfRanges
- the desired number of ranges for splitting the source array. If theArrays.ParallelExecutor.numberOfTasks
(calculated automatically or equal to the corresponding argument) is 1, then this argument is ignored and 1 range is used (no splitting). If this argument is positive and <Arrays.ParallelExecutor.numberOfTasks
, it is replaced withArrays.ParallelExecutor.numberOfTasks
. If this argument is 0 andArrays.ParallelExecutor.numberOfTasks
>1, then the number of ranges is chosen automatically as max(Arrays.ParallelExecutor.numberOfTasks
,recommendedNumberOfRanges(src, true)
)- Throws:
NullPointerException
- if the src or dest argument is null.IllegalArgumentException
- if blockSize <= 0, or if numberOfTasks < 0, or if numberOfRanges < 0.
-
-
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 ofArrays.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 ofArrays.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 callingArrays.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, theDefaultThreadPoolFactory
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 byArrays.ParallelExecutor.finish()
and there were some other exception A while executing the main body of thisArrays.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 classArrays.ParallelExecutor
-
processSubArr
protected void processSubArr(long position, int count, int threadIndex) This implementation copies src.subArr
(position,count) to dest.subArr
(position,count) viaUpdatableArray.copy(Array)
method.- Specified by:
processSubArr
in classArrays.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.
-