1 | package net.bmahe.genetics4j.gpu.spec; | |
2 | ||
3 | import java.util.function.Predicate; | |
4 | ||
5 | import org.immutables.value.Value; | |
6 | ||
7 | import net.bmahe.genetics4j.core.spec.AbstractEAExecutionContext; | |
8 | import net.bmahe.genetics4j.gpu.opencl.model.Device; | |
9 | import net.bmahe.genetics4j.gpu.opencl.model.Platform; | |
10 | ||
11 | /** | |
12 | * GPU-specific execution context that extends the core EA framework with OpenCL device selection capabilities. | |
13 | * | |
14 | * <p>GPUEAExecutionContext extends {@link AbstractEAExecutionContext} to include GPU-specific execution | |
15 | * parameters required for OpenCL device discovery and selection. This context combines traditional EA | |
16 | * execution settings (population size, termination criteria) with GPU-specific device filtering capabilities. | |
17 | * | |
18 | * <p>Key GPU-specific additions: | |
19 | * <ul> | |
20 | * <li><strong>Platform filtering</strong>: Predicates for selecting appropriate OpenCL platforms</li> | |
21 | * <li><strong>Device filtering</strong>: Predicates for selecting compatible OpenCL devices</li> | |
22 | * <li><strong>Multi-device support</strong>: Automatic discovery and utilization of multiple GPU devices</li> | |
23 | * <li><strong>Hardware abstraction</strong>: Device-agnostic configuration with runtime device selection</li> | |
24 | * </ul> | |
25 | * | |
26 | * <p>Device selection workflow: | |
27 | * <ol> | |
28 | * <li><strong>Platform discovery</strong>: Enumerate all available OpenCL platforms</li> | |
29 | * <li><strong>Platform filtering</strong>: Apply platform predicates to select compatible platforms</li> | |
30 | * <li><strong>Device enumeration</strong>: Discover devices for each selected platform</li> | |
31 | * <li><strong>Device filtering</strong>: Apply device predicates to select suitable devices</li> | |
32 | * <li><strong>Context creation</strong>: Create OpenCL contexts for selected devices</li> | |
33 | * </ol> | |
34 | * | |
35 | * <p>Common filtering patterns: | |
36 | * <pre>{@code | |
37 | * // Select only GPU devices with sufficient memory | |
38 | * GPUEAExecutionContext<Double> context = GPUEAExecutionContext.<Double>builder() | |
39 | * .populationSize(2000) | |
40 | * .termination(Generations.of(100)) | |
41 | * | |
42 | * // Platform filtering - prefer full OpenCL profiles | |
43 | * .platformFilter(platform -> platform.profile() == PlatformProfile.FULL_PROFILE) | |
44 | * | |
45 | * // Device filtering - GPU devices with at least 2GB memory | |
46 | * .deviceFilter(device -> | |
47 | * device.type() == DeviceType.GPU && | |
48 | * device.globalMemSize() >= 2L * 1024 * 1024 * 1024) | |
49 | * | |
50 | * .build(); | |
51 | * | |
52 | * // Select any available compute device (GPUs or CPUs) | |
53 | * GPUEAExecutionContext<Double> flexibleContext = GPUEAExecutionContext.<Double>builder() | |
54 | * .populationSize(1000) | |
55 | * .termination(FitnessTarget.of(0.95)) | |
56 | * | |
57 | * // Accept any platform | |
58 | * .platformFilter(platform -> true) | |
59 | * | |
60 | * // Prefer GPUs but accept CPUs as fallback | |
61 | * .deviceFilter(device -> | |
62 | * device.type() == DeviceType.GPU || | |
63 | * device.type() == DeviceType.CPU) | |
64 | * | |
65 | * .build(); | |
66 | * }</pre> | |
67 | * | |
68 | * <p>Performance optimization through device selection: | |
69 | * <ul> | |
70 | * <li><strong>Compute capability</strong>: Filter devices by OpenCL version and feature support</li> | |
71 | * <li><strong>Memory capacity</strong>: Ensure devices have sufficient memory for population size</li> | |
72 | * <li><strong>Compute units</strong>: Prefer devices with more parallel processing units</li> | |
73 | * <li><strong>Memory bandwidth</strong>: Select devices optimized for data-intensive operations</li> | |
74 | * </ul> | |
75 | * | |
76 | * <p>Multi-device strategies: | |
77 | * <ul> | |
78 | * <li><strong>Load balancing</strong>: Automatic population distribution across selected devices</li> | |
79 | * <li><strong>Heterogeneous computing</strong>: Utilize both GPU and CPU devices simultaneously</li> | |
80 | * <li><strong>Fault tolerance</strong>: Graceful degradation when devices become unavailable</li> | |
81 | * <li><strong>Resource optimization</strong>: Efficient utilization of available compute resources</li> | |
82 | * </ul> | |
83 | * | |
84 | * <p>Default behavior: | |
85 | * <ul> | |
86 | * <li><strong>Platform acceptance</strong>: All platforms accepted by default</li> | |
87 | * <li><strong>Device acceptance</strong>: All devices accepted by default</li> | |
88 | * <li><strong>Discovery process</strong>: Automatic enumeration of available hardware</li> | |
89 | * <li><strong>Validation</strong>: Runtime validation ensures at least one device is selected</li> | |
90 | * </ul> | |
91 | * | |
92 | * @param <T> the type of fitness values used in the evolutionary algorithm | |
93 | * @see AbstractEAExecutionContext | |
94 | * @see Platform | |
95 | * @see Device | |
96 | * @see net.bmahe.genetics4j.gpu.GPUFitnessEvaluator | |
97 | */ | |
98 | @Value.Immutable | |
99 | public abstract class GPUEAExecutionContext<T extends Comparable<T>> extends AbstractEAExecutionContext<T> { | |
100 | ||
101 | /** | |
102 | * Returns the predicate used to filter OpenCL platforms during device discovery. | |
103 | * | |
104 | * <p>Platform filtering allows selective use of OpenCL platforms based on vendor, | |
105 | * version, profile, or other platform characteristics. This enables optimization | |
106 | * for specific hardware configurations or requirements. | |
107 | * | |
108 | * <p>Common filtering criteria: | |
109 | * <ul> | |
110 | * <li><strong>Profile support</strong>: Filter by FULL_PROFILE vs EMBEDDED_PROFILE</li> | |
111 | * <li><strong>Vendor preference</strong>: Select platforms from specific vendors</li> | |
112 | * <li><strong>Version requirements</strong>: Ensure minimum OpenCL version support</li> | |
113 | * <li><strong>Extension support</strong>: Filter platforms with required extensions</li> | |
114 | * </ul> | |
115 | * | |
116 | * @return the platform filtering predicate (default accepts all platforms) | |
117 | */ | |
118 | @Value.Default | |
119 | public Predicate<Platform> platformFilters() { | |
120 |
3
1. lambda$platformFilters$0 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/GPUEAExecutionContext::lambda$platformFilters$0 → NO_COVERAGE 2. platformFilters : replaced return value with null for net/bmahe/genetics4j/gpu/spec/GPUEAExecutionContext::platformFilters → NO_COVERAGE 3. lambda$platformFilters$0 : Substituted 1 with 0 → NO_COVERAGE |
return (platform) -> true; |
121 | } | |
122 | ||
123 | /** | |
124 | * Returns the predicate used to filter OpenCL devices during device discovery. | |
125 | * | |
126 | * <p>Device filtering enables selection of appropriate compute devices based on | |
127 | * type, capabilities, memory, and performance characteristics. This allows | |
128 | * optimization for specific workload requirements and hardware constraints. | |
129 | * | |
130 | * <p>Common filtering criteria: | |
131 | * <ul> | |
132 | * <li><strong>Device type</strong>: GPU, CPU, ACCELERATOR, or combinations</li> | |
133 | * <li><strong>Memory capacity</strong>: Minimum global or local memory requirements</li> | |
134 | * <li><strong>Compute units</strong>: Minimum parallel processing capability</li> | |
135 | * <li><strong>OpenCL version</strong>: Required feature support level</li> | |
136 | * <li><strong>Extensions</strong>: Specific OpenCL extension requirements</li> | |
137 | * </ul> | |
138 | * | |
139 | * @return the device filtering predicate (default accepts all devices) | |
140 | */ | |
141 | @Value.Default | |
142 | public Predicate<Device> deviceFilters() { | |
143 |
3
1. lambda$deviceFilters$1 : replaced boolean return with false for net/bmahe/genetics4j/gpu/spec/GPUEAExecutionContext::lambda$deviceFilters$1 → NO_COVERAGE 2. deviceFilters : replaced return value with null for net/bmahe/genetics4j/gpu/spec/GPUEAExecutionContext::deviceFilters → NO_COVERAGE 3. lambda$deviceFilters$1 : Substituted 1 with 0 → NO_COVERAGE |
return (device) -> true; |
144 | } | |
145 | ||
146 | /** | |
147 | * Creates a new builder for constructing GPU EA execution contexts. | |
148 | * | |
149 | * <p>The builder provides a fluent interface for specifying both core EA execution | |
150 | * parameters and GPU-specific device selection criteria. Type safety is ensured | |
151 | * through generic parameterization. | |
152 | * | |
153 | * @param <U> the type of fitness values for the execution context | |
154 | * @return a new builder instance for creating GPU EA execution contexts | |
155 | */ | |
156 | public static <U extends Comparable<U>> ImmutableGPUEAExecutionContext.Builder<U> builder() { | |
157 |
2
1. builder : removed call to net/bmahe/genetics4j/gpu/spec/ImmutableGPUEAExecutionContext::builder → NO_COVERAGE 2. builder : replaced return value with null for net/bmahe/genetics4j/gpu/spec/GPUEAExecutionContext::builder → NO_COVERAGE |
return ImmutableGPUEAExecutionContext.builder(); |
158 | } | |
159 | } | |
Mutations | ||
120 |
1.1 2.2 3.3 |
|
143 |
1.1 2.2 3.3 |
|
157 |
1.1 2.2 |