Class ResultExtractor

java.lang.Object
net.bmahe.genetics4j.gpu.spec.fitness.ResultExtractor

public class ResultExtractor extends Object
Utility class for extracting computation results from OpenCL device memory after GPU kernel execution.

ResultExtractor provides type-safe methods for retrieving different data types from OpenCL memory buffers that contain the results of GPU-accelerated fitness evaluation. This class handles the device-to-host data transfer and type conversion necessary to make GPU computation results available to the evolutionary algorithm.

Key functionality includes:

  • Type-safe extraction: Methods for extracting float, int, long arrays with type validation
  • Image data support: Specialized extraction for OpenCL image objects
  • Device management: Tracks result data across multiple devices
  • Argument indexing: Maps kernel arguments to their corresponding result data

Common usage patterns:


 // Extract fitness values as float array
 float[] fitnessValues = resultExtractor.extractFloatArray(context, 0);
 
 // Extract integer results (e.g., classification results)
 int[] classifications = resultExtractor.extractIntArray(context, 1);
 
 // Extract long results (e.g., counters or large indices)
 long[] counters = resultExtractor.extractLongArray(context, 2);
 
 // Extract image data for visualization
 byte[] imageData = resultExtractor.extractImageAsByteArray(context, 3, width, height, channels, channelSize);
 
 // Use extracted results in fitness evaluation
 List<Double> fitness = IntStream.range(0, fitnessValues.length)
     .mapToDouble(i -> (double) fitnessValues[i])
     .boxed()
     .collect(Collectors.toList());
 

Data extraction workflow:

  1. Kernel execution: GPU kernels compute results and store them in device memory
  2. Result mapping: Results are mapped by device and kernel argument index
  3. Type validation: Data types are validated before extraction
  4. Data transfer: Results are transferred from device to host memory
  5. Type conversion: Data is converted to appropriate Java types

Error handling and validation:

  • Device validation: Ensures requested device has result data
  • Argument validation: Validates argument indices exist in result mapping
  • Type checking: Ensures extracted data matches expected OpenCL types
  • Transfer validation: Validates successful device-to-host data transfer

Performance considerations:

  • Synchronous transfers: Uses blocking transfers to ensure data availability
  • Memory efficiency: Allocates host memory based on actual data sizes
  • Transfer optimization: Minimizes number of device-to-host transfers
  • Type safety: Validates types at runtime to prevent data corruption
See Also:
  • Field Details

    • logger

      public static final org.apache.logging.log4j.Logger logger
    • resultData

      private final Map<Device,Map<Integer,CLData>> resultData
  • Constructor Details

    • ResultExtractor

      public ResultExtractor(Map<Device,Map<Integer,CLData>> _resultData)
      Constructs a ResultExtractor with the specified result data mapping.
      Parameters:
      _resultData - mapping from devices to their kernel argument results
  • Method Details

    • extractClData

      protected CLData extractClData(Device device, int argumentIndex)
      Extracts CLData for the specified device and kernel argument index.
      Parameters:
      device - the OpenCL device to extract data from
      argumentIndex - the kernel argument index for the data
      Returns:
      the CLData object containing the result data
      Throws:
      IllegalArgumentException - if device is null, argumentIndex is negative, device not found, or argument index not found
    • extractImageAsByteArray

      public byte[] extractImageAsByteArray(OpenCLExecutionContext openCLExecutionContext, int argumentIndex, int width, int height, int numChannels, int channelSize)
      Extracts image data from OpenCL device memory as a byte array.

      This method reads an OpenCL image object from device memory and converts it to a byte array suitable for host processing. The image dimensions and channel information must be provided to properly interpret the image data.

      Parameters:
      openCLExecutionContext - the OpenCL execution context
      argumentIndex - the kernel argument index containing the image data
      width - the image width in pixels
      height - the image height in pixels
      numChannels - the number of color channels (e.g., 3 for RGB, 4 for RGBA)
      channelSize - the size of each channel in bytes
      Returns:
      byte array containing the image data
      Throws:
      IllegalArgumentException - if any parameter is invalid
    • extractFloatArray

      public float[] extractFloatArray(OpenCLExecutionContext openCLExecutionContext, int argumentIndex)
      Extracts floating-point data from OpenCL device memory as a float array.

      This method reads floating-point data from device memory and transfers it to host memory. The data type is validated to ensure it contains floating-point values before extraction.

      Parameters:
      openCLExecutionContext - the OpenCL execution context
      argumentIndex - the kernel argument index containing the float data
      Returns:
      float array containing the extracted data
      Throws:
      IllegalArgumentException - if the data is not of type float
    • extractIntArray

      public int[] extractIntArray(OpenCLExecutionContext openCLExecutionContext, int argumentIndex)
      Extracts integer data from OpenCL device memory as an int array.

      This method reads integer data from device memory and transfers it to host memory. The data type is validated to ensure it contains integer values before extraction.

      Parameters:
      openCLExecutionContext - the OpenCL execution context
      argumentIndex - the kernel argument index containing the integer data
      Returns:
      int array containing the extracted data
      Throws:
      IllegalArgumentException - if the data is not of type int
    • extractLongArray

      public long[] extractLongArray(OpenCLExecutionContext openCLExecutionContext, int argumentIndex)
      Extracts long integer data from OpenCL device memory as a long array.

      This method reads long integer data from device memory and transfers it to host memory. The data type is validated to ensure it contains long integer values before extraction.

      Parameters:
      openCLExecutionContext - the OpenCL execution context
      argumentIndex - the kernel argument index containing the long integer data
      Returns:
      long array containing the extracted data
      Throws:
      IllegalArgumentException - if the data is not of type long