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

Mutations

130

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

133

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

164

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

166

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.20.3