Class CUDAStorage
java.lang.Object
org.episteme.nativ.technical.backend.gpu.cuda.CUDAStorage
- All Implemented Interfaces:
AutoCloseable, 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
rather than
direct instantiation to ensure proper backend initialization.
invalid reference
NativeCUDABackend
- Since:
- 1.0
- Author:
- Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
-
Constructor Summary
ConstructorsConstructorDescriptionCUDAStorage(int size) Allocates memory on the GPU.CUDAStorage(jcuda.Pointer devicePointer, int size) Wraps an existing device pointer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Frees the GPU memory.double[]download()Downloads data from device (GPU) memory to host (CPU) memory.Returns the native GPU memory handle.jcuda.PointerReturns the CUDA device pointer.intgetSize()Returns the size of the allocated GPU memory in elements.booleanisClosed()Checks if the GPU memory has been freed.voidupload(double[] hostData) Uploads data from host (CPU) memory to device (GPU) memory.
-
Constructor Details
-
CUDAStorage
public CUDAStorage(int size) Allocates memory on the GPU.Note: Prefer using
to ensure proper backend initialization.invalid reference
NativeCUDABackend#allocateStorage(int)- 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 memorysize- 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:GPUStorageReturns the size of the allocated GPU memory in elements.- Specified by:
getSizein interfaceGPUStorage- Returns:
- number of elements (typically doubles or floats)
-
upload
public void upload(double[] hostData) Description copied from interface:GPUStorageUploads data from host (CPU) memory to device (GPU) memory.- Specified by:
uploadin interfaceGPUStorage- Parameters:
hostData- array of data to upload
-
download
public double[] download()Description copied from interface:GPUStorageDownloads data from device (GPU) memory to host (CPU) memory.- Specified by:
downloadin interfaceGPUStorage- Returns:
- array of data from GPU
-
getNativeHandle
Description copied from interface:GPUStorageReturns 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:
getNativeHandlein interfaceGPUStorage- Returns:
- native GPU memory handle (type varies by backend)
- CUDA:
-
isClosed
public boolean isClosed()Description copied from interface:GPUStorageChecks if the GPU memory has been freed.- Specified by:
isClosedin interfaceGPUStorage- Returns:
- true if memory has been freed, false otherwise
-
close
public void close()Description copied from interface:GPUStorageFrees the GPU memory.This method is called automatically when using try-with-resources. Implementations should be idempotent (safe to call multiple times).
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceGPUStorage
-