1 package net.bmahe.genetics4j.gpu.opencl; 2 3 import java.util.Map; 4 5 import org.immutables.value.Value; 6 import org.jocl.cl_command_queue; 7 import org.jocl.cl_context; 8 import org.jocl.cl_kernel; 9 import org.jocl.cl_program; 10 11 import net.bmahe.genetics4j.gpu.opencl.model.Device; 12 import net.bmahe.genetics4j.gpu.opencl.model.KernelInfo; 13 import net.bmahe.genetics4j.gpu.opencl.model.Platform; 14 15 /** 16 * Encapsulates a complete OpenCL execution environment for a specific device with compiled kernels and runtime context. 17 * 18 * <p>OpenCLExecutionContext represents a fully initialized OpenCL execution environment tied to a specific device and 19 * containing all the resources needed for kernel execution. This includes the OpenCL context, command queue, compiled 20 * program, and kernel objects, along with associated metadata about platform and device capabilities. 21 * 22 * <p>The execution context serves as the primary interface between high-level fitness evaluation code and low-level 23 * OpenCL operations. It provides access to: 24 * <ul> 25 * <li><strong>Device information</strong>: Platform and device metadata for optimization decisions</li> 26 * <li><strong>OpenCL runtime</strong>: Context and command queue for memory and execution management</li> 27 * <li><strong>Compiled kernels</strong>: Ready-to-execute kernel objects with associated metadata</li> 28 * <li><strong>Execution parameters</strong>: Kernel work group information for optimal kernel launch</li> 29 * </ul> 30 * 31 * <p>Context lifecycle and management: 32 * <ol> 33 * <li><strong>Creation</strong>: Built by {@link net.bmahe.genetics4j.gpu.GPUFitnessEvaluator} during 34 * initialization</li> 35 * <li><strong>Usage</strong>: Passed to fitness functions for kernel execution and memory operations</li> 36 * <li><strong>Cleanup</strong>: Resources automatically released by the fitness evaluator</li> 37 * </ol> 38 * 39 * <p>Key usage patterns in fitness evaluation: 40 * 41 * <pre>{@code 42 * public CompletableFuture<List<Double>> compute(OpenCLExecutionContext context, ExecutorService executor, 43 * long generation, List<Genotype> genotypes) { 44 * 45 * return CompletableFuture.supplyAsync(() -> { 46 * // Access device capabilities for optimization 47 * Device device = context.device(); 48 * int maxWorkGroupSize = device.maxWorkGroupSize(); 49 * 50 * // Get compiled kernel for execution 51 * cl_kernel fitnessKernel = context.kernels() 52 * .get("fitness_evaluation"); 53 * 54 * // Get kernel execution parameters 55 * KernelInfo kernelInfo = context.kernelInfo("fitness_evaluation"); 56 * int preferredWorkGroupSize = kernelInfo.preferredWorkGroupSizeMultiple(); 57 * 58 * // Execute kernel with optimal work group configuration 59 * executeKernel(context, fitnessKernel, genotypes.size(), preferredWorkGroupSize); 60 * 61 * // Extract results using the execution context 62 * return extractResults(context, genotypes.size()); 63 * }, executor); 64 * } 65 * }</pre> 66 * 67 * <p>Memory management considerations: 68 * <ul> 69 * <li><strong>Context ownership</strong>: The execution context owns OpenCL resources</li> 70 * <li><strong>Thread safety</strong>: OpenCL contexts are not thread-safe; use appropriate synchronization</li> 71 * <li><strong>Resource lifecycle</strong>: Resources are managed by the parent fitness evaluator</li> 72 * <li><strong>Command queue usage</strong>: Use the provided command queue for all operations</li> 73 * </ul> 74 * 75 * <p>Performance optimization capabilities: 76 * <ul> 77 * <li><strong>Device-specific tuning</strong>: Access device capabilities for optimal kernel configuration</li> 78 * <li><strong>Kernel information</strong>: Use kernel metadata for work group size optimization</li> 79 * <li><strong>Memory hierarchy</strong>: Leverage device memory characteristics for data layout</li> 80 * <li><strong>Compute capabilities</strong>: Adapt algorithms based on device compute units and features</li> 81 * </ul> 82 * 83 * <p>Error handling and robustness: 84 * <ul> 85 * <li><strong>Resource validation</strong>: All OpenCL objects are validated during context creation</li> 86 * <li><strong>Device compatibility</strong>: Context ensures device supports required kernels</li> 87 * <li><strong>Kernel availability</strong>: All specified kernels are guaranteed to be compiled and available</li> 88 * <li><strong>Exception safety</strong>: Context provides consistent state even if operations fail</li> 89 * </ul> 90 * 91 * @see net.bmahe.genetics4j.gpu.GPUFitnessEvaluator 92 * @see net.bmahe.genetics4j.gpu.spec.fitness.OpenCLFitness 93 * @see Platform 94 * @see Device 95 * @see KernelInfo 96 */ 97 @Value.Immutable 98 public interface OpenCLExecutionContext { 99 100 /** 101 * Returns the OpenCL platform associated with this execution context. 102 * 103 * @return the platform containing the device for this context 104 */ 105 @Value.Parameter 106 Platform platform(); 107 108 /** 109 * Returns the OpenCL device associated with this execution context. 110 * 111 * @return the device on which kernels will be executed 112 */ 113 @Value.Parameter 114 Device device(); 115 116 /** 117 * Returns the OpenCL context for this execution environment. 118 * 119 * @return the OpenCL context for memory and resource management 120 */ 121 @Value.Parameter 122 cl_context clContext(); 123 124 /** 125 * Returns the OpenCL command queue for kernel execution and memory operations. 126 * 127 * @return the command queue for submitting OpenCL operations 128 */ 129 @Value.Parameter 130 cl_command_queue clCommandQueue(); 131 132 /** 133 * Returns the compiled OpenCL program containing all kernels. 134 * 135 * @return the compiled OpenCL program object 136 */ 137 @Value.Parameter 138 cl_program clProgram(); 139 140 /** 141 * Returns a map of kernel names to compiled kernel objects. 142 * 143 * @return map from kernel names to executable kernel objects 144 */ 145 @Value.Parameter 146 Map<String, cl_kernel> kernels(); 147 148 /** 149 * Returns a map of kernel names to kernel execution information. 150 * 151 * @return map from kernel names to kernel metadata and execution parameters 152 */ 153 @Value.Parameter 154 Map<String, KernelInfo> kernelInfos(); 155 156 /** 157 * Convenience method to retrieve kernel execution information by name. 158 * 159 * @param kernelName the name of the kernel to get information for 160 * @return the kernel execution information, or null if not found 161 */ 162 default KernelInfo kernelInfo(final String kernelName) { 163 return kernelInfos().get(kernelName); 164 } 165 166 public static class Builder extends ImmutableOpenCLExecutionContext.Builder { 167 } 168 169 /** 170 * Creates a new builder for constructing OpenCL execution contexts. 171 * 172 * @return a new builder instance 173 */ 174 public static Builder builder() { 175 return new Builder(); 176 } 177 }