Class NativeCUDATensorBackend

java.lang.Object
org.episteme.nativ.mathematics.tensors.backends.NativeCUDATensorBackend
All Implemented Interfaces:
TensorProvider, AlgorithmProvider, Backend, ComputeBackend, GPUBackend, NativeBackend

@AutoService({Backend.class,ComputeBackend.class,GPUBackend.class,NativeBackend.class,TensorProvider.class}) public class NativeCUDATensorBackend extends Object implements TensorProvider, GPUBackend, NativeBackend
CUDA-accelerated Tensor Backend.

Implements Tensor operations using JCuda, producing CUDATensor instances backed by GPU device memory. Implements GPUBackend and NativeBackend.

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

    • NativeCUDATensorBackend

      public NativeCUDATensorBackend()
  • Method Details

    • isLoaded

      public boolean isLoaded()
      Description copied from interface: NativeBackend
      Checks if the native library is loaded and available.
      Specified by:
      isLoaded in interface NativeBackend
      Returns:
      true if the native library has been successfully loaded
    • getNativeLibraryName

      public String getNativeLibraryName()
      Description copied from interface: NativeBackend
      Returns the name of the native library this backend depends on.
      Specified by:
      getNativeLibraryName in interface NativeBackend
      Returns:
      library name (e.g., "openblas", "cuda", "fftw3")
    • getId

      public String getId()
      Description copied from interface: Backend
      Returns the unique identifier for this backend (e.g., "javafx", "jmol", "cuda").
      Specified by:
      getId in interface Backend
      Specified by:
      getId in interface ComputeBackend
    • getName

      public String getName()
      Description copied from interface: Backend
      Returns the display name for UI presentation.
      Specified by:
      getName in interface AlgorithmProvider
      Specified by:
      getName in interface Backend
      Specified by:
      getName in interface TensorProvider
    • getDescription

      public String getDescription()
      Description copied from interface: Backend
      Returns a description of the backend.
      Specified by:
      getDescription in interface Backend
      Specified by:
      getDescription in interface ComputeBackend
    • isAvailable

      public boolean isAvailable()
      Description copied from interface: AlgorithmProvider
      Checks if the provider is available in the current environment.
      Specified by:
      isAvailable in interface AlgorithmProvider
      Specified by:
      isAvailable in interface Backend
    • shutdown

      public void shutdown()
      Description copied from interface: AlgorithmProvider
      Called when the provider is no longer needed (e.g., application shutdown). Use this to release local resources or close native segments.
      Specified by:
      shutdown in interface AlgorithmProvider
      Specified by:
      shutdown in interface Backend
    • zeros

      public <T> Tensor<T> zeros(Class<T> elementType, int... shape)
      Specified by:
      zeros in interface TensorProvider
    • ones

      public <T> Tensor<T> ones(Class<T> elementType, int... shape)
      Specified by:
      ones in interface TensorProvider
    • create

      public <T> Tensor<T> create(T[] data, int... shape)
      Specified by:
      create in interface TensorProvider
    • getPriority

      public int getPriority()
      Description copied from interface: AlgorithmProvider
      Returns the execution priority (higher is better).
      Specified by:
      getPriority in interface AlgorithmProvider
      Specified by:
      getPriority in interface Backend
    • score

      public double score(OperationContext context)
      Description copied from interface: AlgorithmProvider
      Scores this provider for a specific operation context.

      Higher scores indicate better suitability. Used by ProviderSelector for context-aware selection.

      Default implementation returns AlgorithmProvider.getPriority(), so existing providers behave identically without changes.

      Specified by:
      score in interface AlgorithmProvider
      Parameters:
      context - the operation context (data size, hints, etc.)
      Returns:
      suitability score (higher = better)
    • getAcceleratorType

      public HardwareAccelerator getAcceleratorType()
      Description copied from interface: ComputeBackend
      Returns the type of hardware accelerator used by this backend.
      Specified by:
      getAcceleratorType in interface ComputeBackend
      Returns:
      the accelerator type
    • createContext

      public ExecutionContext createContext()
      Description copied from interface: ComputeBackend
      Creates an execution context for running operations.

      The context should be used within a try-with-resources block to ensure proper cleanup of resources.

      Specified by:
      createContext in interface ComputeBackend
      Returns:
      new execution context
    • getDevices

      public GPUBackend.DeviceInfo[] getDevices()
      Description copied from interface: GPUBackend
      Returns information about available GPU devices.
      Specified by:
      getDevices in interface GPUBackend
    • selectDevice

      public void selectDevice(int deviceId)
      Description copied from interface: GPUBackend
      Selects a GPU device for computation.
      Specified by:
      selectDevice in interface GPUBackend
      Parameters:
      deviceId - Device index (0-based)
    • allocateGPUMemory

      public long allocateGPUMemory(long sizeBytes)
      Description copied from interface: GPUBackend
      Allocates GPU memory and returns a handle.
      Specified by:
      allocateGPUMemory in interface GPUBackend
      Parameters:
      sizeBytes - Size in bytes
      Returns:
      GPU memory handle (opaque pointer)
    • copyToGPU

      public void copyToGPU(long gpuHandle, DoubleBuffer hostBuffer, long sizeBytes)
      Description copied from interface: GPUBackend
      Copies data from host to GPU.
      Specified by:
      copyToGPU in interface GPUBackend
      Parameters:
      gpuHandle - GPU memory handle
      hostBuffer - Host buffer
      sizeBytes - Number of bytes to copy
    • copyFromGPU

      public void copyFromGPU(long gpuHandle, DoubleBuffer hostBuffer, long sizeBytes)
      Description copied from interface: GPUBackend
      Copies data from GPU to host.
      Specified by:
      copyFromGPU in interface GPUBackend
      Parameters:
      gpuHandle - GPU memory handle
      hostBuffer - Host buffer
      sizeBytes - Number of bytes to copy
    • freeGPUMemory

      public void freeGPUMemory(long gpuHandle)
      Description copied from interface: GPUBackend
      Frees GPU memory.
      Specified by:
      freeGPUMemory in interface GPUBackend
      Parameters:
      gpuHandle - GPU memory handle
    • synchronize

      public void synchronize()
      Description copied from interface: GPUBackend
      Synchronizes GPU execution (waits for all kernels to complete).
      Specified by:
      synchronize in interface GPUBackend
    • matrixMultiply

      public void matrixMultiply(DoubleBuffer A, DoubleBuffer B, DoubleBuffer C, int m, int n, int k)
      Description copied from interface: GPUBackend
      Performs matrix multiplication C = A * B on the GPU. Dimensions: A(m x k), B(k x n), C(m x n).
      Specified by:
      matrixMultiply in interface GPUBackend