Class ThreadManager

java.lang.Object
org.flag4j.concurrency.ThreadManager

public final class ThreadManager extends Object

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 via getParallelismLevel().
  • 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) and concurrentBlockedOperation(int, int, TensorOperation) assume the provided TensorOperation is itself thread-safe. If there are blocks within the operation which are not thread-safe they should be wrapped in a synchronized block.
  • 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 to Math.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, do setParallelismLevel(-2).
    • 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

      public static void concurrentOperation(int totalSize, TensorOperation operation)

      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 use concurrentOperation(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 the blockedOperation.
      blockedOperation - Operation to be computed.