Package org.apache.sysds.runtime.util
Class CommonThreadPool
java.lang.Object
org.apache.sysds.runtime.util.CommonThreadPool
- All Implemented Interfaces:
Executor,ExecutorService
This common thread pool provides an abstraction to obtain a shared thread pool.
If the number of logical cores is specified a ForkJoinPool.commonPool is returned on all requests.
If pools of different size are requested, we create new pool instances of FixedThreadPool, Unless we currently are on
the main thread, Then we return a shared instance of the first requested number of cores.
Alternatively the class also contain a dynamic threadPool, that is intended for asynchronous long running tasks with
low compute overhead, such as broadcast and collect from federated workers.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic booleanLocal variable indicating if there was a thread that was not main, and requested a thread pool -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitTermination(long timeout, TimeUnit unit) voidstatic booleanGet if the current threads that calls have a cached thread pool available.static ExecutorServiceget()Get the shared Executor thread pool, that have the number of threads of the host systemstatic ExecutorServiceget(int k) Get a Executor thread pool, that have the number of threads specified in k.static ExecutorServiceGet a dynamic thread pool that allocate threads as the requests are made.invokeAll(Collection<? extends Callable<T>> tasks) invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) static <T> voidinvokeAndShutdown(ExecutorService pool, Collection<? extends Callable<T>> tasks) Invoke the collection of tasks and shutdown the pool upon job termination.<T> TinvokeAny(Collection<? extends Callable<T>> tasks) <T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) final booleanisCached()If there is a thread pool cached for this thread.booleanbooleanvoidshutdown()static voidShutdown the cached thread pools.static voidshutdownAsyncPools(Thread thread) Shutdown the asynchronous pool associated with the given thread.Future<?><T> Future<T><T> Future<T>static booleanThis method returns true or false depending on if the current thread have an overhead of having to allocate it's own thread pool if parallelization is requested.
-
Field Details
-
incorrectPoolUse
public static boolean incorrectPoolUseLocal variable indicating if there was a thread that was not main, and requested a thread pool
-
-
Constructor Details
-
CommonThreadPool
Constructor of the threadPool. This is intended not to be used except for tests. Please use the static constructors.- Parameters:
pool- The thread pool instance to use.
-
-
Method Details
-
get
Get the shared Executor thread pool, that have the number of threads of the host system- Returns:
- An ExecutorService
-
get
Get a Executor thread pool, that have the number of threads specified in k. The thread pool can be reused by other processes in the same host thread requesting another pool of the same number of threads. The executor that is guaranteed ThreadLocal except if it is number of host logical cores.- Parameters:
k- The number of threads wanted- Returns:
- The executor with specified parallelism
-
invokeAndShutdown
public static <T> void invokeAndShutdown(ExecutorService pool, Collection<? extends Callable<T>> tasks) Invoke the collection of tasks and shutdown the pool upon job termination.- Type Parameters:
T- The type of class to return from the job- Parameters:
pool- The pool to execute intasks- The tasks to execute
-
getDynamicPool
Get a dynamic thread pool that allocate threads as the requests are made. This pool is intended for async remote calls that does not depend on local compute.- Returns:
- A dynamic thread pool.
-
shutdownAsyncPools
public static void shutdownAsyncPools()Shutdown the cached thread pools. -
shutdownAsyncPools
Shutdown the asynchronous pool associated with the given thread.- Parameters:
thread- The thread given that could or could not have allocated a thread pool itself.
-
generalCached
public static boolean generalCached()Get if the current threads that calls have a cached thread pool available. This method is synchronized to analyze the general cache not the common thread pool for the main thread.- Returns:
- If there is a thread pool allocated for this thread.
-
isCached
public final boolean isCached()If there is a thread pool cached for this thread.- Returns:
- if there is a cached thread pool.
-
shutdown
public void shutdown()- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
- Specified by:
shutdownNowin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
execute
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
useParallelismOnThread
public static boolean useParallelismOnThread()This method returns true or false depending on if the current thread have an overhead of having to allocate it's own thread pool if parallelization is requested. Currently if it is the Main thread or a PARFOR thread, then we suggest using parallelism since we can reuse the allocated threads.- Returns:
- If parallelism is suggested.
-