Class NativeOpenCLNBodyBackend
java.lang.Object
org.episteme.nativ.physics.classical.mechanics.nbody.backends.NativeOpenCLNBodyBackend
- All Implemented Interfaces:
AlgorithmProvider, Backend, ComputeBackend, GPUBackend, NativeBackend, NBodyProvider
@AutoService({ComputeBackend.class,GPUBackend.class,Backend.class,NativeBackend.class,NBodyProvider.class})
public class NativeOpenCLNBodyBackend
extends Object
implements NBodyProvider, GPUBackend, NativeBackend
GPU-accelerated N-Body simulation backend using OpenCL.
Uses O(N²) brute-force for small particle counts (n < GPU_NBODY_THRESHOLD)
and O(N log N) Barnes-Hut for large counts. The octree is built on the CPU;
force traversal runs on the GPU via a dedicated OpenCL kernel.
- Since:
- 1.2
- Author:
- Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
-
Nested Class Summary
Nested classes/interfaces inherited from interface GPUBackend
GPUBackend.DeviceInfo -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongallocateGPUMemory(long sizeBytes) Allocates GPU memory and returns a handle.voidcomputeForces(double[] positions, double[] masses, double[] forces, double G, double softening) voidcomputeForces(float[] positions, float[] masses, float[] forces, float G, float softening) voidvoidcopyFromGPU(long gpuHandle, DoubleBuffer hostBuffer, long sizeBytes) Copies data from GPU to host.voidcopyToGPU(long gpuHandle, DoubleBuffer hostBuffer, long sizeBytes) Copies data from host to GPU.Creates an execution context for running operations.voidfreeGPUMemory(long gpuHandle) Frees GPU memory.Returns the type of hardware accelerator used by this backend.Returns the unique category of the algorithm.Returns information about available GPU devices.getId()Returns the unique identifier for this backend (e.g., "javafx", "jmol", "cuda").getName()Returns the display name for UI presentation.Returns the name of the native library this backend depends on.intReturns the execution priority (higher is better).voidbooleanChecks if the provider is available in the current environment.booleanChecks if this backend is specifically disabled via a system property:"episteme.backend.disable." + getId().booleanisLoaded()Checks if the native library is loaded and available.voidmatrixMultiply(DoubleBuffer A, DoubleBuffer B, DoubleBuffer C, int m, int n, int k) Performs matrix multiplication C = A * B on the GPU.doublescore(OperationContext context) Context-aware scoring that accounts for GPU data transfer overhead.voidselectDevice(int deviceId) Selects a GPU device for computation.voidshutdown()Called when the provider is no longer needed (e.g., application shutdown).voidstep(double[] positions, double[] velocities, double[] masses, int numBodies, double G, double dt, double softening) voidstepReal(Real[] positions, Real[] velocities, Real[] masses, int numBodies, Real G, Real dt, Real softening) voidSynchronizes GPU execution (waits for all kernels to complete).Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AlgorithmProvider
description, getMetadataMethods inherited from interface Backend
getAlgorithmProviders, getStatusMessageMethods inherited from interface ComputeBackend
createBackend, getDescription, getType, supportsComplexNumbers, supportsFloatingPoint, supportsParallelOpsMethods inherited from interface NBodyProvider
stepFloat
-
Constructor Details
-
NativeOpenCLNBodyBackend
public NativeOpenCLNBodyBackend()
-
-
Method Details
-
isLoaded
public boolean isLoaded()Description copied from interface:NativeBackendChecks if the native library is loaded and available.- Specified by:
isLoadedin interfaceNativeBackend- Returns:
- true if the native library has been successfully loaded
-
getNativeLibraryName
Description copied from interface:NativeBackendReturns the name of the native library this backend depends on.- Specified by:
getNativeLibraryNamein interfaceNativeBackend- Returns:
- library name (e.g., "openblas", "cuda", "fftw3")
-
getAcceleratorType
Description copied from interface:ComputeBackendReturns the type of hardware accelerator used by this backend.- Specified by:
getAcceleratorTypein interfaceComputeBackend- Returns:
- the accelerator type
-
getAlgorithmType
Description copied from interface:AlgorithmProviderReturns the unique category of the algorithm.- Specified by:
getAlgorithmTypein interfaceAlgorithmProvider- Specified by:
getAlgorithmTypein interfaceNBodyProvider
-
isAvailable
public boolean isAvailable()Description copied from interface:AlgorithmProviderChecks if the provider is available in the current environment.- Specified by:
isAvailablein interfaceAlgorithmProvider- Specified by:
isAvailablein interfaceBackend
-
isExplicitlyDisabled
public boolean isExplicitlyDisabled()Description copied from interface:BackendChecks if this backend is specifically disabled via a system property:"episteme.backend.disable." + getId().It also checks specialized properties like
"episteme.linearalgebra.disable." + getId().- Specified by:
isExplicitlyDisabledin interfaceBackend- Returns:
- true if explicitly disabled
-
getId
-
shutdown
public void shutdown()Description copied from interface:AlgorithmProviderCalled when the provider is no longer needed (e.g., application shutdown). Use this to release local resources or close native segments.- Specified by:
shutdownin interfaceAlgorithmProvider- Specified by:
shutdownin interfaceBackend
-
initialize
public void initialize() -
getName
Description copied from interface:BackendReturns the display name for UI presentation.- Specified by:
getNamein interfaceAlgorithmProvider- Specified by:
getNamein interfaceBackend- Specified by:
getNamein interfaceNBodyProvider
-
getDevices
Description copied from interface:GPUBackendReturns information about available GPU devices.- Specified by:
getDevicesin interfaceGPUBackend
-
selectDevice
public void selectDevice(int deviceId) Description copied from interface:GPUBackendSelects a GPU device for computation.- Specified by:
selectDevicein interfaceGPUBackend- Parameters:
deviceId- Device index (0-based)
-
allocateGPUMemory
public long allocateGPUMemory(long sizeBytes) Description copied from interface:GPUBackendAllocates GPU memory and returns a handle.- Specified by:
allocateGPUMemoryin interfaceGPUBackend- Parameters:
sizeBytes- Size in bytes- Returns:
- GPU memory handle (opaque pointer)
-
copyToGPU
Description copied from interface:GPUBackendCopies data from host to GPU.- Specified by:
copyToGPUin interfaceGPUBackend- Parameters:
gpuHandle- GPU memory handlehostBuffer- Host buffersizeBytes- Number of bytes to copy
-
copyFromGPU
Description copied from interface:GPUBackendCopies data from GPU to host.- Specified by:
copyFromGPUin interfaceGPUBackend- Parameters:
gpuHandle- GPU memory handlehostBuffer- Host buffersizeBytes- Number of bytes to copy
-
freeGPUMemory
public void freeGPUMemory(long gpuHandle) Description copied from interface:GPUBackendFrees GPU memory.- Specified by:
freeGPUMemoryin interfaceGPUBackend- Parameters:
gpuHandle- GPU memory handle
-
synchronize
public void synchronize()Description copied from interface:GPUBackendSynchronizes GPU execution (waits for all kernels to complete).- Specified by:
synchronizein interfaceGPUBackend
-
matrixMultiply
Description copied from interface:GPUBackendPerforms matrix multiplication C = A * B on the GPU. Dimensions: A(m x k), B(k x n), C(m x n).- Specified by:
matrixMultiplyin interfaceGPUBackend
-
createContext
Description copied from interface:ComputeBackendCreates 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:
createContextin interfaceComputeBackend- Returns:
- new execution context
-
getPriority
public int getPriority()Description copied from interface:AlgorithmProviderReturns the execution priority (higher is better).- Specified by:
getPriorityin interfaceAlgorithmProvider- Specified by:
getPriorityin interfaceBackend
-
score
Context-aware scoring that accounts for GPU data transfer overhead.GPU is only beneficial for large data sizes where the compute gain outweighs the host→device transfer cost.
- Specified by:
scorein interfaceAlgorithmProvider- Parameters:
context- the operation context (data size, hints, etc.)- Returns:
- suitability score (higher = better)
-
computeForces
public void computeForces(float[] positions, float[] masses, float[] forces, float G, float softening) - Specified by:
computeForcesin interfaceNBodyProvider
-
computeForces
- Specified by:
computeForcesin interfaceNBodyProvider
-
computeForces
public void computeForces(double[] positions, double[] masses, double[] forces, double G, double softening) - Specified by:
computeForcesin interfaceNBodyProvider
-
step
public void step(double[] positions, double[] velocities, double[] masses, int numBodies, double G, double dt, double softening) - Specified by:
stepin interfaceNBodyProvider
-
stepReal
-