Interface DataLoader

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface DataLoader
Functional interface for loading genotype data into OpenCL device memory for GPU-accelerated fitness evaluation.

DataLoader defines the contract for converting evolutionary algorithm genotypes into OpenCL memory buffers that can be processed by GPU kernels. This abstraction allows different chromosome types and data formats to be efficiently transferred to GPU memory for parallel fitness evaluation.

Key responsibilities:

  • Data conversion: Transform genotypes into OpenCL-compatible data formats
  • Memory allocation: Allocate appropriate device memory for the data
  • Data transfer: Copy converted data to GPU memory buffers
  • Lifecycle management: Handle generation-specific data loading patterns

Common implementation patterns:


 // DataLoader for floating-point chromosomes
 DataLoader floatLoader = (context, generation, genotypes) -> {
     // Convert genotypes to float array
     float[] data = genotypes.stream()
         .flatMap(genotype -> genotype.getChromosome(FloatChromosome.class).stream()
             .mapToDouble(FloatChromosome::getValue)
             .mapToObj(d -> (float) d))
         .toArray(float[]::new);
     
     // Allocate and populate GPU memory
     cl_mem buffer = clCreateBuffer(context.context(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
         data.length * Sizeof.cl_float, Pointer.to(data), null);
     
     return CLData.of(buffer, CL_FLOAT, data.length);
 };
 
 // DataLoader for integer chromosomes with generation-specific logic
 DataLoader intLoader = (context, generation, genotypes) -> {
     int[] data = convertIntGenotypes(genotypes, generation);
     cl_mem buffer = allocateIntBuffer(context, data);
     return CLData.of(buffer, CL_INT, data.length);
 };
 
 // DataLoader using static data (parameters)
 DataLoader paramLoader = DataLoaders.staticFloatArray(new float[]{0.1f, 0.2f, 0.5f});
 

Memory management considerations:

  • Buffer allocation: Use appropriate OpenCL memory flags (READ_ONLY, READ_WRITE, etc.)
  • Data copying: Consider using CL_MEM_COPY_HOST_PTR for immediate data transfer
  • Memory alignment: Ensure data is properly aligned for GPU access patterns
  • Resource cleanup: Returned CLData objects should be properly released after use

Performance optimization strategies:

  • Batch processing: Process multiple genotypes efficiently in single operations
  • Memory reuse: Reuse memory buffers across generations when possible
  • Data layout: Optimize data layout for coalesced GPU memory access
  • Transfer minimization: Minimize host-to-device data transfers

Generation-aware loading:

  • Generation tracking: Use generation parameter for evolution-dependent data
  • Adaptive parameters: Modify algorithm parameters based on generation number
  • Dynamic sizing: Adjust data sizes based on population evolution
  • Caching strategies: Cache expensive computations across generations
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    load(OpenCLExecutionContext openCLExecutionContext, long generation, List<Genotype> genotypes)
    Loads genotype data into OpenCL device memory for GPU processing.
  • Method Details

    • load

      CLData load(OpenCLExecutionContext openCLExecutionContext, long generation, List<Genotype> genotypes)
      Loads genotype data into OpenCL device memory for GPU processing.

      This method converts the provided genotypes into an appropriate data format, allocates OpenCL device memory, and transfers the converted data to the GPU. The resulting CLData object can be used as input for OpenCL kernels.

      Parameters:
      openCLExecutionContext - the OpenCL execution context providing device access
      generation - the current generation number for generation-aware loading
      genotypes - the list of genotypes to convert and load into device memory
      Returns:
      CLData object containing the loaded data in device memory
      Throws:
      IllegalArgumentException - if any parameter is null
      RuntimeException - if OpenCL memory allocation or data transfer fails