Class ThreadManager
Manages a global thread pool and utility methods for executing parallel operations in Flag4j.
Usage:
- This class provides a shared, fixed-size thread pool to perform parallel operations.
- The size of this thread pool (i.e. the "parallelism level") can be set via
setParallelismLevel(int)
and queried viagetParallelismLevel()
. - The pool uses daemon threads to avoid blocking JVM shutdown.
Thread Safety invalid input: '&' Design:
- This class is thread-safe in that calls to
setParallelismLevel
and concurrent operations do not corrupt internal state. However, changing the parallelism level while tasks are actively running may cause those tasks to be abruptly shut down. Use extreme caution if dynamic changes to parallelism are required. - The methods
concurrentOperation(int, TensorOperation)
andconcurrentBlockedOperation(int, int, TensorOperation)
assume the providedTensorOperation
is itself thread-safe. If there are blocks within the operation which are not thread-safe they should be wrapped in asynchronized
block.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
concurrentBlockedOperation
(int totalSize, int blockSize, TensorOperation blockedOperation) Computes a specified blocked tensor operation concurrently by evenly dividing work among available threads (specified byConfigurations.getParallelismLevel()
).static void
concurrentOperation
(int totalSize, TensorOperation operation) Computes a specified tensor operation concurrently by evenly dividing work among available threads (specified byConfigurations.getParallelismLevel()
).static int
Gets the current parallelism level for the ThreadManager.protected static void
setParallelismLevel
(int parallelismLevel) Sets the number of threads to use in the thread pool.
-
Method Details
-
setParallelismLevel
protected static void setParallelismLevel(int parallelismLevel) Sets the number of threads to use in the thread pool.- Parameters:
parallelismLevel
- The parallelism level to use in the thread pool.- If
parallelismLevel > 0
: The parallelism level is used as is. - If
parallelismLevel <= 0
: The parallelism level will be set toMath.max(Configurations.DEFAULT_PARALLELISM + parallelismLevel, 1)
. Such values may be interpreted as 'x' less than the number of available processors. To set the parallelism level to 2 less than the number of available processors, dosetParallelismLevel(-2)
.
- If
-
getParallelismLevel
public static int getParallelismLevel()Gets the current parallelism level for the ThreadManager. That is, the number of threads used in the thread pool.- Returns:
- The current parallelism level for the ThreadManager.
-
concurrentOperation
Computes a specified tensor operation concurrently by evenly dividing work among available threads (specified by
Configurations.getParallelismLevel()
).WARNING: This method provides no guarantees of thread safety. It is the responsibility of the caller to ensure that
operation
is thread safe.- Parameters:
totalSize
- Total size of the outer loop for the operation.operation
- Operation to be computed.
-
concurrentBlockedOperation
public static void concurrentBlockedOperation(int totalSize, int blockSize, TensorOperation blockedOperation) Computes a specified blocked tensor operation concurrently by evenly dividing work among available threads (specified by
Configurations.getParallelismLevel()
).Unlike
concurrentOperation(int, TensorOperation)
this method respects the block size of the blocked operation. This means tasks split across threads will be aligned to block borders if possible which allows for the improved cache performance benefits of blocked ops to be fully realized. For this reason, it is not recommended to useconcurrentOperation(int, TensorOperation)
to compute a blocked operation concurrently.WARNING: This method provides no guarantees of thread safety. It is the responsibility of the caller to ensure that
blockedOperation
is thread safe.- Parameters:
totalSize
- Total size of the outer loop for the operation.blockSize
- Size of the block used in theblockedOperation
.blockedOperation
- Operation to be computed.
-