GPUEASystemFactory.java

1
package net.bmahe.genetics4j.gpu;
2
3
import java.util.concurrent.ExecutorService;
4
import java.util.concurrent.ForkJoinPool;
5
6
import net.bmahe.genetics4j.core.EASystem;
7
import net.bmahe.genetics4j.core.EASystemFactory;
8
import net.bmahe.genetics4j.gpu.spec.GPUEAConfiguration;
9
import net.bmahe.genetics4j.gpu.spec.GPUEAExecutionContext;
10
11
/**
12
 * Factory class for creating GPU-accelerated evolutionary algorithm systems using OpenCL.
13
 * 
14
 * <p>GPUEASystemFactory provides convenient factory methods for creating {@link EASystem} instances
15
 * that leverage GPU acceleration through OpenCL for fitness evaluation. This factory extends the
16
 * capabilities of the core EA framework to support high-performance computing on graphics processors
17
 * and other OpenCL-compatible devices.
18
 * 
19
 * <p>The factory handles the integration between GPU-specific configurations and the core EA framework:
20
 * <ul>
21
 * <li><strong>GPU Configuration</strong>: Uses {@link GPUEAConfiguration} with OpenCL program specifications</li>
22
 * <li><strong>Device Selection</strong>: Leverages {@link GPUEAExecutionContext} for platform and device filtering</li>
23
 * <li><strong>GPU Evaluator</strong>: Creates specialized {@link GPUFitnessEvaluator} for OpenCL fitness computation</li>
24
 * <li><strong>Resource Management</strong>: Coordinates executor services with OpenCL resource lifecycle</li>
25
 * </ul>
26
 * 
27
 * <p>GPU acceleration benefits:
28
 * <ul>
29
 * <li><strong>Massive parallelism</strong>: Evaluate hundreds or thousands of individuals simultaneously</li>
30
 * <li><strong>Memory bandwidth</strong>: High-throughput data processing for population-based algorithms</li>
31
 * <li><strong>Specialized hardware</strong>: Leverage dedicated compute units optimized for parallel operations</li>
32
 * <li><strong>Energy efficiency</strong>: Often better performance-per-watt compared to CPU-only execution</li>
33
 * </ul>
34
 * 
35
 * <p>Common usage patterns:
36
 * <pre>{@code
37
 * // Define OpenCL kernel for fitness evaluation
38
 * Program fitnessProgram = Program.ofResource("/kernels/fitness.cl")
39
 *     .withBuildOption("-DPROBLEM_SIZE=256");
40
 * 
41
 * // Configure GPU-specific EA settings
42
 * GPUEAConfiguration<Double> gpuConfig = GPUEAConfigurationBuilder.<Double>builder()
43
 *     .chromosomeSpecs(chromosomeSpec)
44
 *     .parentSelectionPolicy(Tournament.of(3))
45
 *     .combinationPolicy(SinglePointCrossover.build())
46
 *     .mutationPolicies(List.of(RandomMutation.of(0.1)))
47
 *     .replacementStrategy(Elitism.builder().offspringRatio(0.8).build())
48
 *     .program(fitnessProgram)
49
 *     .fitness(myGPUFitness)
50
 *     .build();
51
 * 
52
 * // Configure execution context with device preferences
53
 * GPUEAExecutionContext<Double> gpuContext = GPUEAExecutionContextBuilder.<Double>builder()
54
 *     .populationSize(1000)
55
 *     .termination(Generations.of(100))
56
 *     .platformFilter(platform -> platform.profile() == PlatformProfile.FULL_PROFILE)
57
 *     .deviceFilter(device -> device.type() == DeviceType.GPU)
58
 *     .build();
59
 * 
60
 * // Create GPU-accelerated EA system
61
 * EASystem<Double> gpuSystem = GPUEASystemFactory.from(gpuConfig, gpuContext);
62
 * 
63
 * // Run evolution on GPU
64
 * EvolutionResult<Double> result = gpuSystem.evolve();
65
 * }</pre>
66
 * 
67
 * <p>OpenCL integration considerations:
68
 * <ul>
69
 * <li><strong>Device compatibility</strong>: Ensure target devices support required OpenCL features</li>
70
 * <li><strong>Memory management</strong>: GPU memory is typically limited compared to system RAM</li>
71
 * <li><strong>Kernel optimization</strong>: GPU performance depends heavily on kernel implementation</li>
72
 * <li><strong>Transfer overhead</strong>: Consider data transfer costs between CPU and GPU memory</li>
73
 * </ul>
74
 * 
75
 * <p>Performance optimization tips:
76
 * <ul>
77
 * <li><strong>Large populations</strong>: GPU acceleration benefits increase with population size</li>
78
 * <li><strong>Complex fitness functions</strong>: More computation per individual improves GPU utilization</li>
79
 * <li><strong>Minimize transfers</strong>: Keep data on GPU between generations when possible</li>
80
 * <li><strong>Coalesced memory access</strong>: Design kernels for optimal memory access patterns</li>
81
 * </ul>
82
 * 
83
 * @see GPUEAConfiguration
84
 * @see GPUEAExecutionContext
85
 * @see GPUFitnessEvaluator
86
 * @see net.bmahe.genetics4j.core.EASystemFactory
87
 * @see net.bmahe.genetics4j.gpu.opencl.model.Program
88
 */
89
public class GPUEASystemFactory {
90
91
	private GPUEASystemFactory() {
92
	}
93
94
	/**
95
	 * Creates a GPU-accelerated EA system with explicit thread pool management.
96
	 * 
97
	 * <p>This method provides full control over thread pool management while enabling GPU acceleration
98
	 * for fitness evaluation. The provided executor service is used for coordinating between CPU
99
	 * and GPU operations, managing asynchronous OpenCL operations, and handling concurrent access
100
	 * to OpenCL resources.
101
	 * 
102
	 * <p>The factory method performs the following operations:
103
	 * <ol>
104
	 * <li>Creates a specialized {@link GPUFitnessEvaluator} configured for OpenCL execution</li>
105
	 * <li>Integrates the GPU evaluator with the core EA framework</li>
106
	 * <li>Ensures proper resource management and cleanup for OpenCL contexts</li>
107
	 * </ol>
108
	 * 
109
	 * <p>Use this method when:
110
	 * <ul>
111
	 * <li>You need explicit control over thread pool configuration and lifecycle</li>
112
	 * <li>Integration with existing thread management systems is required</li>
113
	 * <li>Custom executor services are needed for performance tuning</li>
114
	 * <li>Resource-constrained environments require careful thread pool sizing</li>
115
	 * </ul>
116
	 * 
117
	 * @param <T> the type of fitness values, must be comparable for selection operations
118
	 * @param gpuEAConfiguration the GPU-specific EA configuration with OpenCL program and fitness function
119
	 * @param gpuEAExecutionContext the GPU execution context with device selection and population parameters
120
	 * @param executorService the thread pool for managing CPU-GPU coordination (caller responsible for shutdown)
121
	 * @return a fully configured {@link EASystem} with GPU acceleration capabilities
122
	 * @throws IllegalArgumentException if any parameter is null
123
	 * @throws RuntimeException if OpenCL initialization fails or no compatible devices are found
124
	 */
125
	public static <T extends Comparable<T>> EASystem<T> from(final GPUEAConfiguration<T> gpuEAConfiguration,
126
			final GPUEAExecutionContext<T> gpuEAExecutionContext, final ExecutorService executorService) {
127
128 1 1. from : removed call to net/bmahe/genetics4j/gpu/GPUFitnessEvaluator::<init> → NO_COVERAGE
		final var gpuFitnessEvaluator = new GPUFitnessEvaluator<T>(gpuEAExecutionContext,
129
				gpuEAConfiguration,
130
				executorService);
131 2 1. from : removed call to net/bmahe/genetics4j/core/EASystemFactory::from → NO_COVERAGE
2. from : replaced return value with null for net/bmahe/genetics4j/gpu/GPUEASystemFactory::from → NO_COVERAGE
		return EASystemFactory.from(gpuEAConfiguration, gpuEAExecutionContext, executorService, gpuFitnessEvaluator);
132
	}
133
134
	/**
135
	 * Creates a GPU-accelerated EA system using the common thread pool.
136
	 * 
137
	 * <p>This convenience method provides GPU acceleration without requiring explicit thread pool
138
	 * management. It automatically uses {@link ForkJoinPool#commonPool()} for CPU-GPU coordination,
139
	 * making it ideal for applications where thread pool management is not critical.
140
	 * 
141
	 * <p>This method is recommended for:
142
	 * <ul>
143
	 * <li>Rapid prototyping and experimentation with GPU acceleration</li>
144
	 * <li>Applications where default thread pool behavior is sufficient</li>
145
	 * <li>Educational purposes and demonstration code</li>
146
	 * <li>Simple GPU-accelerated applications without complex threading requirements</li>
147
	 * </ul>
148
	 * 
149
	 * <p>The common thread pool provides automatic parallelization and reasonable default
150
	 * behavior for most GPU acceleration scenarios. However, for production systems with
151
	 * specific performance requirements, consider using {@link #from(GPUEAConfiguration, GPUEAExecutionContext, ExecutorService)}
152
	 * with a custom thread pool.
153
	 * 
154
	 * @param <T> the type of fitness values, must be comparable for selection operations
155
	 * @param gpuEAConfiguration the GPU-specific EA configuration with OpenCL program and fitness function
156
	 * @param gpuEAExecutionContext the GPU execution context with device selection and population parameters
157
	 * @return a fully configured {@link EASystem} with GPU acceleration using the common thread pool
158
	 * @throws IllegalArgumentException if any parameter is null
159
	 * @throws RuntimeException if OpenCL initialization fails or no compatible devices are found
160
	 */
161
	public static <T extends Comparable<T>> EASystem<T> from(final GPUEAConfiguration<T> gpuEAConfiguration,
162
			final GPUEAExecutionContext<T> gpuEAExecutionContext) {
163 1 1. from : removed call to java/util/concurrent/ForkJoinPool::commonPool → NO_COVERAGE
		final ExecutorService executorService = ForkJoinPool.commonPool();
164
165 2 1. from : replaced return value with null for net/bmahe/genetics4j/gpu/GPUEASystemFactory::from → NO_COVERAGE
2. from : removed call to net/bmahe/genetics4j/gpu/GPUEASystemFactory::from → NO_COVERAGE
		return from(gpuEAConfiguration, gpuEAExecutionContext, executorService);
166
	}
167
}

Mutations

128

1.1
Location : from
Killed by : none
removed call to net/bmahe/genetics4j/gpu/GPUFitnessEvaluator::<init> → NO_COVERAGE

131

1.1
Location : from
Killed by : none
removed call to net/bmahe/genetics4j/core/EASystemFactory::from → NO_COVERAGE

2.2
Location : from
Killed by : none
replaced return value with null for net/bmahe/genetics4j/gpu/GPUEASystemFactory::from → NO_COVERAGE

163

1.1
Location : from
Killed by : none
removed call to java/util/concurrent/ForkJoinPool::commonPool → NO_COVERAGE

165

1.1
Location : from
Killed by : none
replaced return value with null for net/bmahe/genetics4j/gpu/GPUEASystemFactory::from → NO_COVERAGE

2.2
Location : from
Killed by : none
removed call to net/bmahe/genetics4j/gpu/GPUEASystemFactory::from → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.6