Class DeviceFilters

java.lang.Object
net.bmahe.genetics4j.gpu.spec.DeviceFilters

public class DeviceFilters extends Object
Utility class providing predicate-based filters for selecting OpenCL devices in GPU-accelerated evolutionary algorithms.

DeviceFilters offers a fluent API for creating device selection criteria based on device characteristics such as type, capabilities, and performance metrics. These filters are used to automatically select appropriate OpenCL devices for GPU-accelerated evolutionary algorithm execution.

Key functionality includes:

  • Type-based filtering: Select devices by type (GPU, CPU, accelerator)
  • Logical combinations: Combine filters using AND and OR operations
  • Performance filtering: Filter devices based on computational capabilities
  • Predicate composition: Build complex selection criteria from simple predicates

Common usage patterns:


 // Select GPU devices only
 Predicate<Device> gpuFilter = DeviceFilters.ofGPU();
 
 // Select CPU devices only
 Predicate<Device> cpuFilter = DeviceFilters.ofCPU();
 
 // Select GPU or accelerator devices
 Predicate<Device> computeFilter = DeviceFilters.or(
     DeviceFilters.ofGPU(),
     DeviceFilters.ofType(DeviceType.ACCELERATOR)
 );
 
 // Select high-performance GPU devices
 Predicate<Device> highPerformanceGPU = DeviceFilters.and(
     DeviceFilters.ofGPU(),
     device -> device.maxComputeUnits() >= 8,
     device -> device.maxWorkGroupSize() >= 256
 );
 
 // Apply filter to device selection
 GPUEAExecutionContext context = GPUEAExecutionContext.builder()
     .deviceFilters(gpuFilter)
     .build();
 

Device selection workflow:

  1. Platform discovery: Enumerate available OpenCL platforms
  2. Device enumeration: Discover devices on selected platforms
  3. Filter application: Apply device filters to candidate devices
  4. Device selection: Select filtered devices for EA execution

Filter composition patterns:

  • Type filtering: Select devices by computational type
  • Capability filtering: Filter by device computational capabilities
  • Performance filtering: Select devices meeting performance criteria
  • Logical combinations: Combine multiple criteria using boolean logic

Performance considerations:

  • Device enumeration overhead: Filters are applied during device discovery
  • Capability validation: Ensure selected devices meet algorithm requirements
  • Load balancing: Consider device performance when selecting multiple devices
  • Fallback strategies: Implement fallback filters for systems with limited devices
See Also:
  • Constructor Details

    • DeviceFilters

      private DeviceFilters()
  • Method Details

    • ofType

      public static Predicate<Device> ofType(DeviceType deviceType)
      Creates a predicate that filters devices by the specified device type.
      Parameters:
      deviceType - the OpenCL device type to filter for
      Returns:
      predicate that returns true for devices of the specified type
      Throws:
      IllegalArgumentException - if deviceType is null
    • ofGPU

      public static Predicate<Device> ofGPU()
      Creates a predicate that filters for GPU devices only.

      This is a convenience method equivalent to ofType(DeviceType.GPU).

      Returns:
      predicate that returns true for GPU devices
    • ofCPU

      public static Predicate<Device> ofCPU()
      Creates a predicate that filters for CPU devices only.

      This is a convenience method equivalent to ofType(DeviceType.CPU).

      Returns:
      predicate that returns true for CPU devices
    • or

      public static Predicate<Device> or(Predicate<Device>... predicates)
      Creates a predicate that returns true if any of the provided predicates return true (logical OR).
      Parameters:
      predicates - array of device predicates to combine with OR logic
      Returns:
      predicate that returns true if any input predicate returns true
      Throws:
      IllegalArgumentException - if predicates is null or empty
    • or

      public static Predicate<Device> or(Collection<Predicate<Device>> predicates)
      Creates a predicate that returns true if any of the provided predicates return true (logical OR).
      Parameters:
      predicates - collection of device predicates to combine with OR logic
      Returns:
      predicate that returns true if any input predicate returns true
      Throws:
      IllegalArgumentException - if predicates is null or empty
    • and

      @SafeVarargs public static Predicate<Device> and(Predicate<Device>... predicates)
      Creates a predicate that returns true only if all provided predicates return true (logical AND).
      Parameters:
      predicates - array of device predicates to combine with AND logic
      Returns:
      predicate that returns true only if all input predicates return true
      Throws:
      IllegalArgumentException - if predicates is null or empty
    • and

      public static Predicate<Device> and(Collection<Predicate<Device>> predicates)
      Creates a predicate that returns true only if all provided predicates return true (logical AND).
      Parameters:
      predicates - collection of device predicates to combine with AND logic
      Returns:
      predicate that returns true only if all input predicates return true
      Throws:
      IllegalArgumentException - if predicates is null or empty