jsat.utils.concurrent
Class ParallelUtils
- java.lang.Object
-
- jsat.utils.concurrent.ParallelUtils
-
public class ParallelUtils extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field and Description static java.util.concurrent.ExecutorServiceCACHED_THREAD_POOLThis object provides a re-usable source of threads for use without having to create a new thread pool.
-
Constructor Summary
Constructors Constructor and Description ParallelUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static intgetEndBlock(int N, int ID)Gets the ending index (exclusive) for splitting up a list of items intoSystemInfo.LogicalCoresevenly sized blocks.static intgetEndBlock(int N, int ID, int P)Gets the ending index (exclusive) for splitting up a list of items intoPevenly sized blocks.static java.util.concurrent.ExecutorServicegetNewExecutor(boolean parallel)static intgetStartBlock(int N, int ID)Gets the starting index (inclusive) for splitting up a list of items intoSystemInfo.LogicalCoresevenly sized blocks.static intgetStartBlock(int N, int ID, int P)Gets the starting index (inclusive) for splitting up a list of items intoPevenly sized blocks.static java.util.stream.IntStreamrange(int end, boolean parallel)static java.util.stream.IntStreamrange(int start, int end, boolean parallel)static <T> Trun(boolean parallel, int N, IndexReducer<T> ir, java.util.function.BinaryOperator<T> reducer)static voidrun(boolean parallel, int N, IndexRunnable ir)static voidrun(boolean parallel, int N, IndexRunnable ir, java.util.concurrent.ExecutorService threadPool)This helper method provides a convenient way to break up a computation across N items into individual indices to be processed.static <T> Trun(boolean parallel, int N, LoopChunkReducer<T> lcr, java.util.function.BinaryOperator<T> reducer)static <T> Trun(boolean parallel, int N, LoopChunkReducer<T> lcr, java.util.function.BinaryOperator<T> reducer, java.util.concurrent.ExecutorService threadPool)static voidrun(boolean parallel, int N, LoopChunkRunner lcr)This helper method provides a convenient way to break up a computation across N items into contiguous ranges that can be processed independently in parallel.static voidrun(boolean parallel, int N, LoopChunkRunner lcr, java.util.concurrent.ExecutorService threadPool)This helper method provides a convenient way to break up a computation across N items into contiguous ranges that can be processed independently in parallel.static java.util.stream.DoubleStreamstreamP(java.util.stream.DoubleStream source, boolean parallel)static java.util.stream.IntStreamstreamP(java.util.stream.IntStream source, boolean parallel)static <T> java.util.stream.Stream<T>streamP(java.util.stream.Stream<T> source, boolean parallel)
-
-
-
Field Detail
-
CACHED_THREAD_POOL
public static final java.util.concurrent.ExecutorService CACHED_THREAD_POOL
This object provides a re-usable source of threads for use without having to create a new thread pool. The cached executor service is used because no threads are created until needed. If the pool is unused for a long enough time, the threads will be destroyed. This avoids the user needing to do anything. This pool is filled with daemon threads, and so will not prevent program termination.
-
-
Method Detail
-
run
public static void run(boolean parallel, int N, LoopChunkRunner lcr)This helper method provides a convenient way to break up a computation across N items into contiguous ranges that can be processed independently in parallel.- Parameters:
parallel- a boolean indicating if the work should be done in parallel. If false, it will run single-threaded. This is for code convenience so that only one set of code is needed to handle both cases.N- the total number of items to processlcr- the runnable over a contiguous range
-
run
public static void run(boolean parallel, int N, LoopChunkRunner lcr, java.util.concurrent.ExecutorService threadPool)This helper method provides a convenient way to break up a computation across N items into contiguous ranges that can be processed independently in parallel.- Parameters:
parallel- a boolean indicating if the work should be done in parallel. If false, it will run single-threaded. This is for code convenience so that only one set of code is needed to handle both cases.N- the total number of items to process.lcr- the runnable over a contiguous rangethreadPool- the source of threads for the computation
-
run
public static <T> T run(boolean parallel, int N, LoopChunkReducer<T> lcr, java.util.function.BinaryOperator<T> reducer, java.util.concurrent.ExecutorService threadPool)
-
run
public static <T> T run(boolean parallel, int N, LoopChunkReducer<T> lcr, java.util.function.BinaryOperator<T> reducer)
-
run
public static <T> T run(boolean parallel, int N, IndexReducer<T> ir, java.util.function.BinaryOperator<T> reducer)
-
run
public static void run(boolean parallel, int N, IndexRunnable ir)
-
run
public static void run(boolean parallel, int N, IndexRunnable ir, java.util.concurrent.ExecutorService threadPool)This helper method provides a convenient way to break up a computation across N items into individual indices to be processed. This method is meant for when the execution time of any given index is highly variable, and so for load balancing purposes, should be treated as individual jobs. If runtime is consistent, look atrun(boolean, int, jsat.utils.concurrent.LoopChunkRunner, java.util.concurrent.ExecutorService).- Parameters:
parallel- a boolean indicating if the work should be done in parallel. If false, it will run single-threaded. This is for code convenience so that only one set of code is needed to handle both cases.N- the total number of items to process.ir- the runnable over a contiguous rangethreadPool- the source of threads for the computation
-
getNewExecutor
public static java.util.concurrent.ExecutorService getNewExecutor(boolean parallel)
-
streamP
public static <T> java.util.stream.Stream<T> streamP(java.util.stream.Stream<T> source, boolean parallel)
-
streamP
public static java.util.stream.IntStream streamP(java.util.stream.IntStream source, boolean parallel)
-
streamP
public static java.util.stream.DoubleStream streamP(java.util.stream.DoubleStream source, boolean parallel)
-
range
public static java.util.stream.IntStream range(int end, boolean parallel)
-
range
public static java.util.stream.IntStream range(int start, int end, boolean parallel)
-
getStartBlock
public static int getStartBlock(int N, int ID, int P)Gets the starting index (inclusive) for splitting up a list of items intoPevenly sized blocks. In the event thatNis not evenly divisible byP, the size of ranges will differ by at most 1.- Parameters:
N- the number of items to split upID- the block number in [0,P)P- the number of blocks to break up the items into- Returns:
- the starting index (inclusive) of the blocks owned by the
ID'th process.
-
getStartBlock
public static int getStartBlock(int N, int ID)Gets the starting index (inclusive) for splitting up a list of items intoSystemInfo.LogicalCoresevenly sized blocks. In the event thatNis not evenly divisible bySystemInfo.LogicalCores, the size of ranges will differ by at most 1.- Parameters:
N- the number of items to split upID- the block number in [0,SystemInfo.LogicalCores)- Returns:
- the starting index (inclusive) of the blocks owned by the
ID'th process.
-
getEndBlock
public static int getEndBlock(int N, int ID, int P)Gets the ending index (exclusive) for splitting up a list of items intoPevenly sized blocks. In the event thatNis not evenly divisible byP, the size of ranges will differ by at most 1.- Parameters:
N- the number of items to split upID- the block number in [0,P)P- the number of blocks to break up the items into- Returns:
- the ending index (exclusive) of the blocks owned by the
ID'th process.
-
getEndBlock
public static int getEndBlock(int N, int ID)Gets the ending index (exclusive) for splitting up a list of items intoSystemInfo.LogicalCoresevenly sized blocks. In the event thatSystemInfo.LogicalCoresis not evenly divisible bySystemInfo.LogicalCores, the size of ranges will differ by at most 1.- Parameters:
N- the number of items to split upID- the block number in [0,SystemInfo.LogicalCores)- Returns:
- the ending index (exclusive) of the blocks owned by the
ID'th process.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG