| 1 | package net.bmahe.genetics4j.gpu.spec; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | import java.util.Collection; | |
| 5 | import java.util.Set; | |
| 6 | import java.util.function.Predicate; | |
| 7 | ||
| 8 | import org.apache.commons.collections4.CollectionUtils; | |
| 9 | import org.apache.commons.lang3.Validate; | |
| 10 | ||
| 11 | import net.bmahe.genetics4j.gpu.opencl.model.Platform; | |
| 12 | import net.bmahe.genetics4j.gpu.opencl.model.PlatformProfile; | |
| 13 | ||
| 14 | /** | |
| 15 | * Utility class providing predicate-based filters for selecting OpenCL platforms in GPU-accelerated evolutionary | |
| 16 | * algorithms. | |
| 17 | * | |
| 18 | * <p>PlatformFilters offers a fluent API for creating platform selection criteria based on platform characteristics | |
| 19 | * such as profile type, supported extensions, and vendor capabilities. These filters are used to automatically select | |
| 20 | * appropriate OpenCL platforms before device enumeration and selection. | |
| 21 | * | |
| 22 | * <p>Key functionality includes: | |
| 23 | * <ul> | |
| 24 | * <li><strong>Profile-based filtering</strong>: Select platforms by profile (FULL_PROFILE or EMBEDDED_PROFILE)</li> | |
| 25 | * <li><strong>Extension filtering</strong>: Filter platforms based on supported OpenCL extensions</li> | |
| 26 | * <li><strong>Logical combinations</strong>: Combine filters using AND and OR operations</li> | |
| 27 | * <li><strong>Predicate composition</strong>: Build complex selection criteria from simple predicates</li> | |
| 28 | * </ul> | |
| 29 | * | |
| 30 | * <p>Common usage patterns: | |
| 31 | * | |
| 32 | * <pre>{@code | |
| 33 | * // Select platforms with full OpenCL profile | |
| 34 | * Predicate<Platform> fullProfileFilter = PlatformFilters.ofProfile(PlatformProfile.FULL_PROFILE); | |
| 35 | * | |
| 36 | * // Select platforms supporting double precision arithmetic | |
| 37 | * Predicate<Platform> fp64Filter = PlatformFilters.ofExtension("cl_khr_fp64"); | |
| 38 | * | |
| 39 | * // Select platforms with multiple required extensions | |
| 40 | * Set<String> requiredExtensions = Set.of("cl_khr_fp64", "cl_khr_global_int32_base_atomics"); | |
| 41 | * Predicate<Platform> extensionFilter = PlatformFilters.ofExtensions(requiredExtensions); | |
| 42 | * | |
| 43 | * // Combine multiple criteria | |
| 44 | * Predicate<Platform> advancedFilter = PlatformFilters.and(PlatformFilters.ofProfile(PlatformProfile.FULL_PROFILE), | |
| 45 | * PlatformFilters.ofExtension("cl_khr_fp64")); | |
| 46 | * | |
| 47 | * // Apply filter to platform selection | |
| 48 | * GPUEAExecutionContext context = GPUEAExecutionContext.builder() | |
| 49 | * .platformFilters(fullProfileFilter) | |
| 50 | * .build(); | |
| 51 | * }</pre> | |
| 52 | * | |
| 53 | * <p>Platform selection workflow: | |
| 54 | * <ol> | |
| 55 | * <li><strong>Platform enumeration</strong>: Discover all available OpenCL platforms</li> | |
| 56 | * <li><strong>Filter application</strong>: Apply platform filters to candidate platforms</li> | |
| 57 | * <li><strong>Platform validation</strong>: Validate filtered platforms meet requirements</li> | |
| 58 | * <li><strong>Device discovery</strong>: Enumerate devices on selected platforms</li> | |
| 59 | * </ol> | |
| 60 | * | |
| 61 | * <p>Filter criteria considerations: | |
| 62 | * <ul> | |
| 63 | * <li><strong>Profile compatibility</strong>: FULL_PROFILE platforms typically offer more features</li> | |
| 64 | * <li><strong>Extension requirements</strong>: Filter for extensions required by algorithms</li> | |
| 65 | * <li><strong>Vendor optimization</strong>: Consider vendor-specific optimizations and extensions</li> | |
| 66 | * <li><strong>Version requirements</strong>: Ensure platforms support required OpenCL versions</li> | |
| 67 | * </ul> | |
| 68 | * | |
| 69 | * <p>Performance considerations: | |
| 70 | * <ul> | |
| 71 | * <li><strong>Platform enumeration overhead</strong>: Filters are applied during platform discovery</li> | |
| 72 | * <li><strong>Extension validation</strong>: Extension checks may have runtime overhead</li> | |
| 73 | * <li><strong>Fallback strategies</strong>: Implement fallback filters for limited platform availability</li> | |
| 74 | * <li><strong>Caching</strong>: Platform characteristics are typically static during execution</li> | |
| 75 | * </ul> | |
| 76 | * | |
| 77 | * @see Platform | |
| 78 | * @see PlatformProfile | |
| 79 | * @see GPUEAExecutionContext | |
| 80 | */ | |
| 81 | public class PlatformFilters { | |
| 82 | ||
| 83 | private PlatformFilters() { | |
| 84 | ||
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Creates a predicate that filters platforms by the specified profile type. | |
| 89 | * | |
| 90 | * @param platformProfile the OpenCL profile type to filter for (FULL_PROFILE or EMBEDDED_PROFILE) | |
| 91 | * @return predicate that returns true for platforms with the specified profile | |
| 92 | * @throws IllegalArgumentException if platformProfile is null | |
| 93 | */ | |
| 94 | public static Predicate<Platform> ofProfile(final PlatformProfile platformProfile) { | |
| 95 | Validate.notNull(platformProfile); | |
| 96 | ||
| 97 |
5
1. lambda$ofProfile$0 : removed call to net/bmahe/genetics4j/gpu/opencl/model/PlatformProfile::equals → NO_COVERAGE 2. lambda$ofProfile$0 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$ofProfile$0 → NO_COVERAGE 3. lambda$ofProfile$0 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$ofProfile$0 → NO_COVERAGE 4. ofProfile : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::ofProfile → NO_COVERAGE 5. lambda$ofProfile$0 : removed call to net/bmahe/genetics4j/gpu/opencl/model/Platform::profile → NO_COVERAGE |
return (platform) -> platformProfile.equals(platform.profile()); |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Creates a predicate that filters platforms supporting the specified OpenCL extension. | |
| 102 | * | |
| 103 | * @param extension the OpenCL extension name to filter for (e.g., "cl_khr_fp64") | |
| 104 | * @return predicate that returns true for platforms supporting the extension | |
| 105 | * @throws IllegalArgumentException if extension is null or blank | |
| 106 | */ | |
| 107 | public static Predicate<Platform> ofExtension(final String extension) { | |
| 108 | Validate.notBlank(extension); | |
| 109 | ||
| 110 |
4
1. lambda$ofExtension$1 : removed call to net/bmahe/genetics4j/gpu/opencl/model/Platform::extensions → NO_COVERAGE 2. lambda$ofExtension$1 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$ofExtension$1 → NO_COVERAGE 3. lambda$ofExtension$1 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$ofExtension$1 → NO_COVERAGE 4. ofExtension : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::ofExtension → NO_COVERAGE |
return (platform) -> platform.extensions() |
| 111 |
1
1. lambda$ofExtension$1 : removed call to java/util/Set::contains → NO_COVERAGE |
.contains(extension); |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Creates a predicate that filters platforms supporting all specified OpenCL extensions. | |
| 116 | * | |
| 117 | * @param extensions set of OpenCL extension names that must all be supported | |
| 118 | * @return predicate that returns true for platforms supporting all specified extensions | |
| 119 | * @throws IllegalArgumentException if extensions is null | |
| 120 | */ | |
| 121 | public static Predicate<Platform> ofExtensions(final Set<String> extensions) { | |
| 122 | Validate.notNull(extensions); | |
| 123 | ||
| 124 |
5
1. lambda$ofExtensions$2 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$ofExtensions$2 → NO_COVERAGE 2. lambda$ofExtensions$2 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$ofExtensions$2 → NO_COVERAGE 3. ofExtensions : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::ofExtensions → NO_COVERAGE 4. lambda$ofExtensions$2 : removed call to org/apache/commons/collections4/CollectionUtils::containsAll → NO_COVERAGE 5. lambda$ofExtensions$2 : removed call to net/bmahe/genetics4j/gpu/opencl/model/Platform::extensions → NO_COVERAGE |
return (platform) -> CollectionUtils.containsAll(platform.extensions(), extensions); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Creates a predicate that returns true if any of the provided predicates return true (logical OR). | |
| 129 | * | |
| 130 | * @param predicates array of platform predicates to combine with OR logic | |
| 131 | * @return predicate that returns true if any input predicate returns true | |
| 132 | * @throws IllegalArgumentException if predicates is null or empty | |
| 133 | */ | |
| 134 | public static Predicate<Platform> or(@SuppressWarnings("unchecked") final Predicate<Platform>... predicates) { | |
| 135 | Validate.notNull(predicates); | |
| 136 | Validate.isTrue(predicates.length > 0); | |
| 137 | ||
| 138 |
4
1. lambda$or$4 : removed call to java/util/Arrays::stream → NO_COVERAGE 2. lambda$or$4 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$4 → NO_COVERAGE 3. or : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::or → NO_COVERAGE 4. lambda$or$4 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$4 → NO_COVERAGE |
return platform -> Arrays.stream(predicates) |
| 139 |
4
1. lambda$or$3 : removed call to java/util/function/Predicate::test → NO_COVERAGE 2. lambda$or$3 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$3 → NO_COVERAGE 3. lambda$or$4 : removed call to java/util/stream/Stream::anyMatch → NO_COVERAGE 4. lambda$or$3 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$3 → NO_COVERAGE |
.anyMatch(predicate -> predicate.test(platform)); |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * Creates a predicate that returns true if any of the provided predicates return true (logical OR). | |
| 144 | * | |
| 145 | * @param predicates collection of platform predicates to combine with OR logic | |
| 146 | * @return predicate that returns true if any input predicate returns true | |
| 147 | * @throws IllegalArgumentException if predicates is null or empty | |
| 148 | */ | |
| 149 | public static Predicate<Platform> or(final Collection<Predicate<Platform>> predicates) { | |
| 150 | Validate.notNull(predicates); | |
| 151 | Validate.isTrue(predicates.size() > 0); | |
| 152 | ||
| 153 |
4
1. lambda$or$6 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$6 → NO_COVERAGE 2. lambda$or$6 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$6 → NO_COVERAGE 3. lambda$or$6 : removed call to java/util/Collection::stream → NO_COVERAGE 4. or : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::or → NO_COVERAGE |
return platform -> predicates.stream() |
| 154 |
4
1. lambda$or$5 : removed call to java/util/function/Predicate::test → NO_COVERAGE 2. lambda$or$5 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$5 → NO_COVERAGE 3. lambda$or$6 : removed call to java/util/stream/Stream::anyMatch → NO_COVERAGE 4. lambda$or$5 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$or$5 → NO_COVERAGE |
.anyMatch(predicate -> predicate.test(platform)); |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Creates a predicate that returns true only if all provided predicates return true (logical AND). | |
| 159 | * | |
| 160 | * @param predicates array of platform predicates to combine with AND logic | |
| 161 | * @return predicate that returns true only if all input predicates return true | |
| 162 | * @throws IllegalArgumentException if predicates is null or empty | |
| 163 | */ | |
| 164 | public static Predicate<Platform> and(@SuppressWarnings("unchecked") final Predicate<Platform>... predicates) { | |
| 165 | Validate.notNull(predicates); | |
| 166 | Validate.isTrue(predicates.length > 0); | |
| 167 | ||
| 168 |
4
1. and : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::and → NO_COVERAGE 2. lambda$and$8 : removed call to java/util/Arrays::stream → NO_COVERAGE 3. lambda$and$8 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$8 → NO_COVERAGE 4. lambda$and$8 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$8 → NO_COVERAGE |
return platform -> Arrays.stream(predicates) |
| 169 |
4
1. lambda$and$7 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$7 → NO_COVERAGE 2. lambda$and$7 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$7 → NO_COVERAGE 3. lambda$and$8 : removed call to java/util/stream/Stream::allMatch → NO_COVERAGE 4. lambda$and$7 : removed call to java/util/function/Predicate::test → NO_COVERAGE |
.allMatch(predicate -> predicate.test(platform)); |
| 170 | } | |
| 171 | ||
| 172 | /** | |
| 173 | * Creates a predicate that returns true only if all provided predicates return true (logical AND). | |
| 174 | * | |
| 175 | * @param predicates collection of platform predicates to combine with AND logic | |
| 176 | * @return predicate that returns true only if all input predicates return true | |
| 177 | * @throws IllegalArgumentException if predicates is null or empty | |
| 178 | */ | |
| 179 | public static Predicate<Platform> and(final Collection<Predicate<Platform>> predicates) { | |
| 180 | Validate.notNull(predicates); | |
| 181 | Validate.isTrue(predicates.size() > 0); | |
| 182 | ||
| 183 |
4
1. lambda$and$10 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$10 → NO_COVERAGE 2. lambda$and$10 : removed call to java/util/Collection::stream → NO_COVERAGE 3. lambda$and$10 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$10 → NO_COVERAGE 4. and : replaced return value with null for net/bmahe/genetics4j/gpu/spec/PlatformFilters::and → NO_COVERAGE |
return platform -> predicates.stream() |
| 184 |
4
1. lambda$and$9 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$9 → NO_COVERAGE 2. lambda$and$9 : replaced boolean return with true for net/bmahe/genetics4j/gpu/spec/PlatformFilters::lambda$and$9 → NO_COVERAGE 3. lambda$and$10 : removed call to java/util/stream/Stream::allMatch → NO_COVERAGE 4. lambda$and$9 : removed call to java/util/function/Predicate::test → NO_COVERAGE |
.allMatch(predicate -> predicate.test(platform)); |
| 185 | } | |
| 186 | ||
| 187 | } | |
Mutations | ||
| 97 |
1.1 2.2 3.3 4.4 5.5 |
|
| 110 |
1.1 2.2 3.3 4.4 |
|
| 111 |
1.1 |
|
| 124 |
1.1 2.2 3.3 4.4 5.5 |
|
| 138 |
1.1 2.2 3.3 4.4 |
|
| 139 |
1.1 2.2 3.3 4.4 |
|
| 153 |
1.1 2.2 3.3 4.4 |
|
| 154 |
1.1 2.2 3.3 4.4 |
|
| 168 |
1.1 2.2 3.3 4.4 |
|
| 169 |
1.1 2.2 3.3 4.4 |
|
| 183 |
1.1 2.2 3.3 4.4 |
|
| 184 |
1.1 2.2 3.3 4.4 |