Interface CLData
- All Known Implementing Classes:
ImmutableCLData
CLData encapsulates the information needed to reference and manage data that has been allocated and stored on an OpenCL device (GPU). This includes the OpenCL memory object, the data type, and the number of elements, providing a type-safe way to work with GPU memory buffers in evolutionary algorithm fitness evaluation.
Key characteristics:
- Memory reference: OpenCL memory object (cl_mem) pointing to device memory
- Type information: OpenCL data type for proper kernel parameter binding
- Size tracking: Number of elements for bounds checking and memory management
- Resource management: Facilitates proper cleanup of OpenCL memory objects
Common usage patterns:
// Create CLData for population chromosomes
CLData populationData = CLData.of(chromosomeBuffer, CL.CL_FLOAT, populationSize * chromosomeLength);
// Create CLData for fitness results
CLData fitnessData = CLData.of(fitnessBuffer, CL.CL_DOUBLE, populationSize);
// Create CLData for algorithm parameters
CLData parameterData = CLData.of(paramBuffer, CL.CL_INT, parameterCount);
// Use in kernel execution
clSetKernelArg(kernel, 0, Sizeof.cl_mem, Pointer.to(populationData.clMem()));
clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(fitnessData.clMem()));
Memory lifecycle management:
- Allocation: Memory is allocated using clCreateBuffer or similar OpenCL functions
- Wrapping: CLData object is created to wrap the allocated memory
- Usage: Memory is used in kernel execution through CLData references
- Cleanup: Memory is released using clReleaseMemObject when no longer needed
Data type mapping:
- CL_FLOAT: Single-precision floating-point data
- CL_DOUBLE: Double-precision floating-point data
- CL_INT: 32-bit integer data
- CL_CHAR: 8-bit character data
Error handling considerations:
- Memory validation: Ensures cl_mem objects are valid before use
- Type safety: Validates OpenCL data types are positive
- Size validation: Ensures element counts are positive
- Resource tracking: Facilitates proper memory cleanup
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionorg.jocl.cl_mem
clMem()
Returns the OpenCL memory object that references the data stored on the device.int
clType()
Returns the OpenCL data type of the elements stored in the memory buffer.static CLData
of
(org.jocl.cl_mem clMem, int clType, int size) Creates a new CLData instance with the specified OpenCL memory object, data type, and size.int
size()
Returns the number of elements stored in the OpenCL memory buffer.
-
Method Details
-
clMem
@Parameter org.jocl.cl_mem clMem()Returns the OpenCL memory object that references the data stored on the device.- Returns:
- the OpenCL memory object (cl_mem) containing the device data
-
clType
@Parameter int clType()Returns the OpenCL data type of the elements stored in the memory buffer.Common OpenCL types include CL_FLOAT, CL_DOUBLE, CL_INT, and CL_CHAR. This information is used for proper kernel parameter binding and type checking.
- Returns:
- the OpenCL data type constant (e.g., CL_FLOAT, CL_DOUBLE)
-
size
@Parameter int size()Returns the number of elements stored in the OpenCL memory buffer.This represents the count of individual data elements (not bytes) contained in the memory object. For example, a buffer containing 1000 floating-point values would have a size of 1000, regardless of the actual byte size.
- Returns:
- the number of elements in the memory buffer
-
of
Creates a new CLData instance with the specified OpenCL memory object, data type, and size.- Parameters:
clMem
- the OpenCL memory object containing the device dataclType
- the OpenCL data type constant (e.g., CL_FLOAT, CL_DOUBLE)size
- the number of elements in the memory buffer- Returns:
- a new CLData instance
- Throws:
IllegalArgumentException
- if clMem is null, clType is not positive, or size is not positive
-