1 | package net.bmahe.genetics4j.gpu.spec.fitness.cldata; | |
2 | ||
3 | import org.apache.commons.lang3.Validate; | |
4 | import org.immutables.value.Value; | |
5 | import org.jocl.cl_mem; | |
6 | ||
7 | /** | |
8 | * Container representing data stored in OpenCL device memory for GPU-accelerated evolutionary algorithm processing. | |
9 | * | |
10 | * <p>CLData encapsulates the information needed to reference and manage data that has been allocated and stored | |
11 | * on an OpenCL device (GPU). This includes the OpenCL memory object, the data type, and the number of elements, | |
12 | * providing a type-safe way to work with GPU memory buffers in evolutionary algorithm fitness evaluation. | |
13 | * | |
14 | * <p>Key characteristics: | |
15 | * <ul> | |
16 | * <li><strong>Memory reference</strong>: OpenCL memory object (cl_mem) pointing to device memory</li> | |
17 | * <li><strong>Type information</strong>: OpenCL data type for proper kernel parameter binding</li> | |
18 | * <li><strong>Size tracking</strong>: Number of elements for bounds checking and memory management</li> | |
19 | * <li><strong>Resource management</strong>: Facilitates proper cleanup of OpenCL memory objects</li> | |
20 | * </ul> | |
21 | * | |
22 | * <p>Common usage patterns: | |
23 | * <pre>{@code | |
24 | * // Create CLData for population chromosomes | |
25 | * CLData populationData = CLData.of(chromosomeBuffer, CL.CL_FLOAT, populationSize * chromosomeLength); | |
26 | * | |
27 | * // Create CLData for fitness results | |
28 | * CLData fitnessData = CLData.of(fitnessBuffer, CL.CL_DOUBLE, populationSize); | |
29 | * | |
30 | * // Create CLData for algorithm parameters | |
31 | * CLData parameterData = CLData.of(paramBuffer, CL.CL_INT, parameterCount); | |
32 | * | |
33 | * // Use in kernel execution | |
34 | * clSetKernelArg(kernel, 0, Sizeof.cl_mem, Pointer.to(populationData.clMem())); | |
35 | * clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(fitnessData.clMem())); | |
36 | * }</pre> | |
37 | * | |
38 | * <p>Memory lifecycle management: | |
39 | * <ol> | |
40 | * <li><strong>Allocation</strong>: Memory is allocated using clCreateBuffer or similar OpenCL functions</li> | |
41 | * <li><strong>Wrapping</strong>: CLData object is created to wrap the allocated memory</li> | |
42 | * <li><strong>Usage</strong>: Memory is used in kernel execution through CLData references</li> | |
43 | * <li><strong>Cleanup</strong>: Memory is released using clReleaseMemObject when no longer needed</li> | |
44 | * </ol> | |
45 | * | |
46 | * <p>Data type mapping: | |
47 | * <ul> | |
48 | * <li><strong>CL_FLOAT</strong>: Single-precision floating-point data</li> | |
49 | * <li><strong>CL_DOUBLE</strong>: Double-precision floating-point data</li> | |
50 | * <li><strong>CL_INT</strong>: 32-bit integer data</li> | |
51 | * <li><strong>CL_CHAR</strong>: 8-bit character data</li> | |
52 | * </ul> | |
53 | * | |
54 | * <p>Error handling considerations: | |
55 | * <ul> | |
56 | * <li><strong>Memory validation</strong>: Ensures cl_mem objects are valid before use</li> | |
57 | * <li><strong>Type safety</strong>: Validates OpenCL data types are positive</li> | |
58 | * <li><strong>Size validation</strong>: Ensures element counts are positive</li> | |
59 | * <li><strong>Resource tracking</strong>: Facilitates proper memory cleanup</li> | |
60 | * </ul> | |
61 | * | |
62 | * @see DataLoader | |
63 | * @see ResultExtractor | |
64 | * @see net.bmahe.genetics4j.gpu.spec.fitness.OpenCLFitness | |
65 | */ | |
66 | @Value.Immutable | |
67 | public interface CLData { | |
68 | ||
69 | /** | |
70 | * Returns the OpenCL memory object that references the data stored on the device. | |
71 | * | |
72 | * @return the OpenCL memory object (cl_mem) containing the device data | |
73 | */ | |
74 | @Value.Parameter | |
75 | cl_mem clMem(); | |
76 | ||
77 | /** | |
78 | * Returns the OpenCL data type of the elements stored in the memory buffer. | |
79 | * | |
80 | * <p>Common OpenCL types include CL_FLOAT, CL_DOUBLE, CL_INT, and CL_CHAR. | |
81 | * This information is used for proper kernel parameter binding and type checking. | |
82 | * | |
83 | * @return the OpenCL data type constant (e.g., CL_FLOAT, CL_DOUBLE) | |
84 | */ | |
85 | @Value.Parameter | |
86 | int clType(); | |
87 | ||
88 | /** | |
89 | * Returns the number of elements stored in the OpenCL memory buffer. | |
90 | * | |
91 | * <p>This represents the count of individual data elements (not bytes) contained | |
92 | * in the memory object. For example, a buffer containing 1000 floating-point values | |
93 | * would have a size of 1000, regardless of the actual byte size. | |
94 | * | |
95 | * @return the number of elements in the memory buffer | |
96 | */ | |
97 | @Value.Parameter | |
98 | int size(); | |
99 | ||
100 | /** | |
101 | * Creates a new CLData instance with the specified OpenCL memory object, data type, and size. | |
102 | * | |
103 | * @param clMem the OpenCL memory object containing the device data | |
104 | * @param clType the OpenCL data type constant (e.g., CL_FLOAT, CL_DOUBLE) | |
105 | * @param size the number of elements in the memory buffer | |
106 | * @return a new CLData instance | |
107 | * @throws IllegalArgumentException if clMem is null, clType is not positive, or size is not positive | |
108 | */ | |
109 | static CLData of(final cl_mem clMem, final int clType, final int size) { | |
110 | Validate.notNull(clMem); | |
111 | Validate.isTrue(clType > 0); | |
112 | Validate.isTrue(size > 0); | |
113 | ||
114 |
2
1. of : removed call to net/bmahe/genetics4j/gpu/spec/fitness/cldata/ImmutableCLData::of → NO_COVERAGE 2. of : replaced return value with null for net/bmahe/genetics4j/gpu/spec/fitness/cldata/CLData::of → NO_COVERAGE |
return ImmutableCLData.of(clMem, clType, size); |
115 | } | |
116 | } | |
Mutations | ||
114 |
1.1 2.2 |