| 1 | package net.bmahe.genetics4j.gpu.spec; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | import java.util.Collection; | |
| 5 | import java.util.function.Predicate; | |
| 6 | ||
| 7 | import org.apache.commons.lang3.Validate; | |
| 8 | ||
| 9 | import net.bmahe.genetics4j.gpu.opencl.model.Device; | |
| 10 | import net.bmahe.genetics4j.gpu.opencl.model.DeviceType; | |
| 11 | ||
| 12 | /** | |
| 13 | * Utility class providing predicate-based filters for selecting OpenCL devices in GPU-accelerated evolutionary | |
| 14 | * algorithms. | |
| 15 | * | |
| 16 | * <p>DeviceFilters offers a fluent API for creating device selection criteria based on device characteristics such as | |
| 17 | * type, capabilities, and performance metrics. These filters are used to automatically select appropriate OpenCL | |
| 18 | * devices for GPU-accelerated evolutionary algorithm execution. | |
| 19 | * | |
| 20 | * <p>Key functionality includes: | |
| 21 | * <ul> | |
| 22 | * <li><strong>Type-based filtering</strong>: Select devices by type (GPU, CPU, accelerator)</li> | |
| 23 | * <li><strong>Logical combinations</strong>: Combine filters using AND and OR operations</li> | |
| 24 | * <li><strong>Performance filtering</strong>: Filter devices based on computational capabilities</li> | |
| 25 | * <li><strong>Predicate composition</strong>: Build complex selection criteria from simple predicates</li> | |
| 26 | * </ul> | |
| 27 | * | |
| 28 | * <p>Common usage patterns: | |
| 29 | * | |
| 30 | * <pre>{@code | |
| 31 | * // Select GPU devices only | |
| 32 | * Predicate<Device> gpuFilter = DeviceFilters.ofGPU(); | |
| 33 | * | |
| 34 | * // Select CPU devices only | |
| 35 | * Predicate<Device> cpuFilter = DeviceFilters.ofCPU(); | |
| 36 | * | |
| 37 | * // Select GPU or accelerator devices | |
| 38 | * Predicate<Device> computeFilter = DeviceFilters | |
| 39 | * .or(DeviceFilters.ofGPU(), DeviceFilters.ofType(DeviceType.ACCELERATOR)); | |
| 40 | * | |
| 41 | * // Select high-performance GPU devices | |
| 42 | * Predicate<Device> highPerformanceGPU = DeviceFilters.and( | |
| 43 | * DeviceFilters.ofGPU(), | |
| 44 | * device -> device.maxComputeUnits() >= 8, | |
| 45 | * device -> device.maxWorkGroupSize() >= 256); | |
| 46 | * | |
| 47 | * // Apply filter to device selection | |
| 48 | * GPUEAExecutionContext context = GPUEAExecutionContext.builder().deviceFilters(gpuFilter).build(); | |
| 49 | * }</pre> | |
| 50 | * | |
| 51 | * <p>Device selection workflow: | |
| 52 | * <ol> | |
| 53 | * <li><strong>Platform discovery</strong>: Enumerate available OpenCL platforms</li> | |
| 54 | * <li><strong>Device enumeration</strong>: Discover devices on selected platforms</li> | |
| 55 | * <li><strong>Filter application</strong>: Apply device filters to candidate devices</li> | |
| 56 | * <li><strong>Device selection</strong>: Select filtered devices for EA execution</li> | |
| 57 | * </ol> | |
| 58 | * | |
| 59 | * <p>Filter composition patterns: | |
| 60 | * <ul> | |
| 61 | * <li><strong>Type filtering</strong>: Select devices by computational type</li> | |
| 62 | * <li><strong>Capability filtering</strong>: Filter by device computational capabilities</li> | |
| 63 | * <li><strong>Performance filtering</strong>: Select devices meeting performance criteria</li> | |
| 64 | * <li><strong>Logical combinations</strong>: Combine multiple criteria using boolean logic</li> | |
| 65 | * </ul> | |
| 66 | * | |
| 67 | * <p>Performance considerations: | |
| 68 | * <ul> | |
| 69 | * <li><strong>Device enumeration overhead</strong>: Filters are applied during device discovery</li> | |
| 70 | * <li><strong>Capability validation</strong>: Ensure selected devices meet algorithm requirements</li> | |
| 71 | * <li><strong>Load balancing</strong>: Consider device performance when selecting multiple devices</li> | |
| 72 | * <li><strong>Fallback strategies</strong>: Implement fallback filters for systems with limited devices</li> | |
| 73 | * </ul> | |
| 74 | * | |
| 75 | * @see Device | |
| 76 | * @see DeviceType | |
| 77 | * @see GPUEAExecutionContext | |
| 78 | */ | |
| 79 | public class DeviceFilters { | |
| 80 | ||
| 81 | private DeviceFilters() { | |
| 82 | ||
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Creates a predicate that filters devices by the specified device type. | |
| 87 | * | |
| 88 | * @param deviceType the OpenCL device type to filter for | |
| 89 | * @return predicate that returns true for devices of the specified type | |
| 90 | * @throws IllegalArgumentException if deviceType is null | |
| 91 | */ | |
| 92 | public static Predicate<Device> ofType(final DeviceType deviceType) { | |
| 93 | Validate.notNull(deviceType); | |
| 94 | ||
| 95 |
5
1. lambda$ofType$0 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$ofType$0 → NO_COVERAGE 2. ofType : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::ofType → NO_COVERAGE 3. lambda$ofType$0 : removed call to net/bmahe/genetics4j/gpu/opencl/model/Device::deviceType → NO_COVERAGE 4. lambda$ofType$0 : removed call to java/util/Set::contains → NO_COVERAGE 5. lambda$ofType$0 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$ofType$0 → NO_COVERAGE |
return (device) -> device.deviceType().contains(deviceType); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Creates a predicate that filters for GPU devices only. | |
| 100 | * | |
| 101 | * <p>This is a convenience method equivalent to {@code ofType(DeviceType.GPU)}. | |
| 102 | * | |
| 103 | * @return predicate that returns true for GPU devices | |
| 104 | */ | |
| 105 | public static Predicate<Device> ofGPU() { | |
| 106 |
2
1. ofGPU : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::ofGPU → NO_COVERAGE 2. ofGPU : removed call to net/bmahe/genetics4j/gpu/spec/DeviceFilters::ofType → NO_COVERAGE |
return ofType(DeviceType.GPU); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Creates a predicate that filters for CPU devices only. | |
| 111 | * | |
| 112 | * <p>This is a convenience method equivalent to {@code ofType(DeviceType.CPU)}. | |
| 113 | * | |
| 114 | * @return predicate that returns true for CPU devices | |
| 115 | */ | |
| 116 | public static Predicate<Device> ofCPU() { | |
| 117 |
2
1. ofCPU : removed call to net/bmahe/genetics4j/gpu/spec/DeviceFilters::ofType → NO_COVERAGE 2. ofCPU : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::ofCPU → NO_COVERAGE |
return ofType(DeviceType.CPU); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Creates a predicate that returns true if any of the provided predicates return true (logical OR). | |
| 122 | * | |
| 123 | * @param predicates array of device predicates to combine with OR logic | |
| 124 | * @return predicate that returns true if any input predicate returns true | |
| 125 | * @throws IllegalArgumentException if predicates is null or empty | |
| 126 | */ | |
| 127 | public static Predicate<Device> or(@SuppressWarnings("unchecked") final Predicate<Device>... predicates) { | |
| 128 | Validate.notNull(predicates); | |
| 129 | Validate.isTrue(predicates.length > 0); | |
| 130 | ||
| 131 |
8
1. lambda$or$1 : removed call to java/util/function/Predicate::test → NO_COVERAGE 2. lambda$or$1 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$1 → NO_COVERAGE 3. lambda$or$1 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$1 → NO_COVERAGE 4. lambda$or$2 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$2 → NO_COVERAGE 5. lambda$or$2 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$2 → NO_COVERAGE 6. lambda$or$2 : removed call to java/util/Arrays::stream → NO_COVERAGE 7. or : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::or → NO_COVERAGE 8. lambda$or$2 : removed call to java/util/stream/Stream::anyMatch → NO_COVERAGE |
return device -> Arrays.stream(predicates).anyMatch(predicate -> predicate.test(device)); |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Creates a predicate that returns true if any of the provided predicates return true (logical OR). | |
| 136 | * | |
| 137 | * @param predicates collection of device predicates to combine with OR logic | |
| 138 | * @return predicate that returns true if any input predicate returns true | |
| 139 | * @throws IllegalArgumentException if predicates is null or empty | |
| 140 | */ | |
| 141 | public static Predicate<Device> or(final Collection<Predicate<Device>> predicates) { | |
| 142 | Validate.notNull(predicates); | |
| 143 | Validate.isTrue(predicates.size() > 0); | |
| 144 | ||
| 145 |
8
1. lambda$or$3 : removed call to java/util/function/Predicate::test → NO_COVERAGE 2. lambda$or$3 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$3 → NO_COVERAGE 3. lambda$or$3 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$3 → NO_COVERAGE 4. lambda$or$4 : removed call to java/util/Collection::stream → NO_COVERAGE 5. lambda$or$4 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$4 → NO_COVERAGE 6. lambda$or$4 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$or$4 → NO_COVERAGE 7. or : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::or → NO_COVERAGE 8. lambda$or$4 : removed call to java/util/stream/Stream::anyMatch → NO_COVERAGE |
return device -> predicates.stream().anyMatch(predicate -> predicate.test(device)); |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Creates a predicate that returns true only if all provided predicates return true (logical AND). | |
| 150 | * | |
| 151 | * @param predicates array of device predicates to combine with AND logic | |
| 152 | * @return predicate that returns true only if all input predicates return true | |
| 153 | * @throws IllegalArgumentException if predicates is null or empty | |
| 154 | */ | |
| 155 | @SafeVarargs | |
| 156 | public static Predicate<Device> and(final Predicate<Device>... predicates) { | |
| 157 | Validate.notNull(predicates); | |
| 158 | Validate.isTrue(predicates.length > 0); | |
| 159 | ||
| 160 |
8
1. lambda$and$6 : removed call to java/util/Arrays::stream → NO_COVERAGE 2. and : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::and → NO_COVERAGE 3. lambda$and$5 : removed call to java/util/function/Predicate::test → NO_COVERAGE 4. lambda$and$5 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$5 → NO_COVERAGE 5. lambda$and$5 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$5 → NO_COVERAGE 6. lambda$and$6 : removed call to java/util/stream/Stream::allMatch → NO_COVERAGE 7. lambda$and$6 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$6 → NO_COVERAGE 8. lambda$and$6 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$6 → NO_COVERAGE |
return device -> Arrays.stream(predicates).allMatch(predicate -> predicate.test(device)); |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * Creates a predicate that returns true only if all provided predicates return true (logical AND). | |
| 165 | * | |
| 166 | * @param predicates collection of device predicates to combine with AND logic | |
| 167 | * @return predicate that returns true only if all input predicates return true | |
| 168 | * @throws IllegalArgumentException if predicates is null or empty | |
| 169 | */ | |
| 170 | public static Predicate<Device> and(final Collection<Predicate<Device>> predicates) { | |
| 171 | Validate.notNull(predicates); | |
| 172 | Validate.isTrue(predicates.size() > 0); | |
| 173 | ||
| 174 |
8
1. lambda$and$8 : removed call to java/util/stream/Stream::allMatch → NO_COVERAGE 2. and : replaced return value with null for net/bmahe/genetics4j/gpu/spec/DeviceFilters::and → NO_COVERAGE 3. lambda$and$7 : removed call to java/util/function/Predicate::test → NO_COVERAGE 4. lambda$and$7 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$7 → NO_COVERAGE 5. lambda$and$8 : removed call to java/util/Collection::stream → NO_COVERAGE 6. lambda$and$8 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$8 → NO_COVERAGE 7. lambda$and$8 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$8 → NO_COVERAGE 8. lambda$and$7 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/DeviceFilters::lambda$and$7 → NO_COVERAGE |
return device -> predicates.stream().allMatch(predicate -> predicate.test(device)); |
| 175 | } | |
| 176 | ||
| 177 | } | |
Mutations | ||
| 95 |
1.1 2.2 3.3 4.4 5.5 |
|
| 106 |
1.1 2.2 |
|
| 117 |
1.1 2.2 |
|
| 131 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 145 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 160 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 174 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |