Class CUDAStorage

java.lang.Object
org.episteme.nativ.technical.backend.gpu.cuda.CUDAStorage
All Implemented Interfaces:
AutoCloseable, GPUStorage

public class CUDAStorage extends Object implements GPUStorage
CUDA implementation of GPU memory storage.

Manages GPU memory for CUDA operations using JCublas. This class should only be used when the CUDA backend is available.

Usage: Obtain instances through

invalid reference
NativeCUDABackend
rather than direct instantiation to ensure proper backend initialization.

Since:
1.0
Author:
Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
  • Constructor Summary

    Constructors
    Constructor
    Description
    CUDAStorage(int size)
    Allocates memory on the GPU.
    CUDAStorage(jcuda.Pointer devicePointer, int size)
    Wraps an existing device pointer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Frees the GPU memory.
    double[]
    Downloads data from device (GPU) memory to host (CPU) memory.
    Returns the native GPU memory handle.
    jcuda.Pointer
    Returns the CUDA device pointer.
    int
    Returns the size of the allocated GPU memory in elements.
    boolean
    Checks if the GPU memory has been freed.
    void
    upload(double[] hostData)
    Uploads data from host (CPU) memory to device (GPU) memory.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CUDAStorage

      public CUDAStorage(int size)
      Allocates memory on the GPU.

      Note: Prefer using

      invalid reference
      NativeCUDABackend#allocateStorage(int)
      to ensure proper backend initialization.

      Parameters:
      size - number of double elements to allocate
      Throws:
      RuntimeException - if CUDA allocation fails
    • CUDAStorage

      public CUDAStorage(jcuda.Pointer devicePointer, int size)
      Wraps an existing device pointer.

      Takes ownership of the pointer and will free it on close.

      Parameters:
      devicePointer - the pointer to device memory
      size - number of elements
  • Method Details

    • getPointer

      public jcuda.Pointer getPointer()
      Returns the CUDA device pointer.
      Returns:
      CUDA Pointer to device memory
    • getSize

      public int getSize()
      Description copied from interface: GPUStorage
      Returns the size of the allocated GPU memory in elements.
      Specified by:
      getSize in interface GPUStorage
      Returns:
      number of elements (typically doubles or floats)
    • upload

      public void upload(double[] hostData)
      Description copied from interface: GPUStorage
      Uploads data from host (CPU) memory to device (GPU) memory.
      Specified by:
      upload in interface GPUStorage
      Parameters:
      hostData - array of data to upload
    • download

      public double[] download()
      Description copied from interface: GPUStorage
      Downloads data from device (GPU) memory to host (CPU) memory.
      Specified by:
      download in interface GPUStorage
      Returns:
      array of data from GPU
    • getNativeHandle

      public Object getNativeHandle()
      Description copied from interface: GPUStorage
      Returns the native GPU memory handle.

      The type of this handle is backend-specific:

      • CUDA: jcuda.Pointer
      • OpenCL: org.jocl.cl_mem
      • Vulkan: long (VkDeviceMemory handle)

      Specified by:
      getNativeHandle in interface GPUStorage
      Returns:
      native GPU memory handle (type varies by backend)
    • isClosed

      public boolean isClosed()
      Description copied from interface: GPUStorage
      Checks if the GPU memory has been freed.
      Specified by:
      isClosed in interface GPUStorage
      Returns:
      true if memory has been freed, false otherwise
    • close

      public void close()
      Description copied from interface: GPUStorage
      Frees the GPU memory.

      This method is called automatically when using try-with-resources. Implementations should be idempotent (safe to call multiple times).

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface GPUStorage