Skip to content
This repository was archived by the owner on Oct 31, 2026. It is now read-only.
axiomanticPublic archive

About

Rust bindings for the Kompute GPU compute framework.

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

115 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Komputers: GPU Computing for Rust

Crates.io Documentation CI Security Audit License: Apache 2.0

Rust bindings for the Kompute GPU compute framework.

Komputers provides safety-first GPU computing with Vulkan through a Rust interface. It prioritizes correctness, reliability, and cross-platform compatibility for GPU tensor operations, SPIR-V algorithm support, and memory management.

Status

Komputers is under active development. It supports Kompute's master branch. Official Kompute releases (v0.8.1, v0.9.0) are not currently supported due to compatibility requirements.

Features

  • Multi-Type Tensors: Support for f32, f64, i32, u32 data types with GPU operations
  • SPIR-V Integration: Load and execute compute shaders with tensor parameter binding
  • Multi-Queue Support: Parallel execution across multiple GPU command queues
  • Error Handling: Structured error types with recovery strategies
  • Memory Management: GPU memory allocation and cleanup through RAII patterns
  • Cross-Platform: Linux, Windows, and macOS support (macOS via MoltenVK)
  • Async Operations: Optional async/await API for non-blocking GPU work
  • Testing Suite: Unit, integration, and GPU hardware tests

Implementation Details

  • Memory Safety: GPU resource management through RAII patterns with automatic cleanup
  • Type Safety: Generic tensor types with compile-time and runtime type checking
  • Handle-Based Architecture: Resource management with lifecycle tracking
  • SPIR-V Support: Compute shader loading and execution with parameter binding
  • Vulkan Backend: Uses Vulkan API through the Kompute C++ framework

Platform Support

  • Linux: Native Vulkan drivers (NVIDIA, AMD, Intel)
  • Windows: Native Vulkan drivers
  • macOS: MoltenVK translation layer for Metal backend compatibility
  • GPU Vendors: NVIDIA, AMD, Intel, and Apple Silicon support

Quick Start

Add this to your Cargo.toml:

[dependencies]
komputers = "0.1"

# Optional: Enable async GPU operations
# komputers = { version = "0.1", features = ["async"] }

Basic GPU Computing

use komputers::{KomputeError, Manager, Tensor, Algorithm, VulkanConfig};

fn main() -> komputers::Result<()> {
    // Create GPU manager with auto-detected Vulkan configuration
    let config = VulkanConfig::auto_detect()
        .map_err(|e| KomputeError::vulkan(&e))?;
    let mut manager = Manager::with_config(config)?;

    // Create multi-type tensors
    let input_a = Tensor::new(&mut manager, &[1.0f32, 2.0, 3.0, 4.0])?;
    let input_b = Tensor::new(&mut manager, &[5.0f32, 6.0, 7.0, 8.0])?;
    let mut output: Tensor<f32> = Tensor::uninit(&mut manager, 4)?;

    // Load SPIR-V compute shader and bind tensors
    let spirv_data = include_bytes!("shaders/vector_add.spv");
    let algorithm = Algorithm::with_tensors(
        &mut manager,
        spirv_data,
        &[&input_a, &input_b, &output]
    )?;

    // Execute GPU computation
    let mut sequence = manager.sequence()?;
    sequence.record_algorithm(&algorithm)?;
    sequence.eval()?;

    // Retrieve results from GPU
    let results = output.to_vec()?;
    println!("GPU Results: {:?}", results); // [6.0, 8.0, 10.0, 12.0]

    Ok(())
}

Multi-Type Tensor Operations

use komputers::{KomputeError, Manager, Tensor, DataType, VulkanConfig};

fn multi_type_example() -> komputers::Result<()> {
    let config = VulkanConfig::auto_detect()
        .map_err(|e| KomputeError::vulkan(&e))?;
    let mut manager = Manager::with_config(config)?;

    // Create different data type tensors
    let f64_tensor = Tensor::new(&mut manager, &[1.0f64, 2.0, 3.0])?;
    let i32_tensor = Tensor::new(&mut manager, &[10i32, 20, 30])?;
    let u32_tensor = Tensor::new(&mut manager, &[100u32, 200, 300])?;

    // Runtime type detection
    assert_eq!(f64_tensor.data_type()?, DataType::Float64);
    assert_eq!(i32_tensor.data_type()?, DataType::Int32);
    assert_eq!(u32_tensor.data_type()?, DataType::UInt32);

    // Create filled tensors for initialization
    let zeros: Tensor<f32> = manager.tensor_filled(1000, 0.0)?;
    let ones: Tensor<i32> = manager.tensor_filled(500, 1)?;

    println!("Multi-type tensors created successfully!");
    Ok(())
}

High-Level GPU Operations

use komputers::{GpuOperations, WorkgroupConfig};

fn high_level_example() -> komputers::Result<()> {
    // High-level operations manager with safety-first validation
    let mut ops = GpuOperations::new()?;

    let source = Tensor::new(ops.manager(), &[1.0f32; 1000])?;
    let mut dest = Tensor::uninit::<f32>(ops.manager(), 1000)?;

    // Safe GPU copy with validated workgroup sizing
    ops.copy(&source, &mut dest)?;

    // Custom workgroup configuration with safety validation
    let workgroup = WorkgroupConfig::validated_1d(1000);
    println!("Safe workgroup: {}x{}x{}", workgroup.x, workgroup.y, workgroup.z);

    Ok(())
}

Async GPU Operations

#[cfg(feature = "async")]
use komputers::{AsyncGpuExecutor, AsyncSequence};

#[cfg(feature = "async")]
async fn async_gpu_example() -> komputers::Result<()> {
    let mut executor = AsyncGpuExecutor::new().await?;

    let input = Tensor::new(executor.manager(), &[1.0f32; 100])?;
    let mut output = Tensor::uninit::<f32>(executor.manager(), 100)?;

    // Non-blocking GPU operations
    let async_sequence = AsyncSequence::new(&mut executor)?;
    async_sequence.record_copy(&input, &mut output).await?;

    // Parallel execution across multiple GPU queues
    executor.execute_parallel(vec![async_sequence], Duration::from_secs(5)).await?;

    println!("Async GPU operations completed!");
    Ok(())
}

Error Recovery and Memory Optimization

use komputers::{MemoryOptimizer, ErrorRecoveryManager};

fn advanced_features_example() -> komputers::Result<()> {
    // Advanced memory optimization
    let memory_optimizer = MemoryOptimizer::new()?;
    let stats = memory_optimizer.memory_statistics()?;
    println!("GPU Memory: {}MB allocated, {:.1}% fragmentation",
             stats.total_allocated / 1024 / 1024,
             stats.average_fragmentation * 100.0);

    // Error recovery and diagnostics
    let error_recovery = ErrorRecoveryManager::new();
    let diagnostic = error_recovery.generate_diagnostic_report()?;
    println!("System Status: {:?}", diagnostic.overall_health);

    Ok(())
}

Architecture

Komputers provides a layered architecture for GPU computing:

Core Components

  • [Manager]: GPU device management and resource allocation
  • [Tensor<T>]: Type-safe GPU memory with host-device synchronization
  • [Algorithm]: SPIR-V compute shader loading and execution
  • [Sequence]: Command buffer for batched GPU operations

Additional Components

  • [GpuOperations]: High-level GPU operation patterns
  • [MemoryOptimizer]: GPU memory management utilities
  • [ErrorRecoveryManager]: Error handling and recovery
  • [MultiQueueManager]: Multi-queue execution support
  • [HandleTracker]: Resource lifecycle tracking

Platform Support

Vulkan Drivers

  • Linux: Native Vulkan drivers (NVIDIA, AMD, Intel)
  • Windows: Native Vulkan drivers with full DirectX integration
  • macOS: MoltenVK with Metal backend integration

MoltenVK Compatibility (macOS)

Komputers includes MoltenVK compatibility for macOS:

  • Auto-Configuration: MoltenVK detection and environment setup
  • Version Compatibility: Handling of MoltenVK API version differences
  • Metal GPU Detection: Detection of Metal-capable GPUs with fallback
  • Build Flags: Pre-configured with KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK=ON

Compatibility Notes: This library targets the Kompute master branch due to compatibility issues with official releases. For details, see MoltenVK Issue #2292 and Kompute Issue #378.

User Override: Set KOMPUTE_DISABLE_MOLTENVK_AUTO=1 to disable automatic MoltenVK configuration.

GPU Vendors

  • NVIDIA: GeForce, Quadro, Tesla series
  • AMD: RDNA, RDNA2, RDNA3 architectures
  • Intel: UHD, Iris, Arc graphics
  • Apple: M1, M2, M3 processors

Environment Setup

use komputers::vulkan_setup::{configure_vulkan_environment, diagnose_vulkan_environment};

// Vulkan configuration
configure_vulkan_environment()?;

// Environment diagnostics
let report = diagnose_vulkan_environment()?;
println!("Vulkan Status: {}", report.status);

Safety-First Design

Komputers prioritizes safety, reliability, and correctness:

  • Memory Safety: RAII-based GPU resource management with automatic cleanup
  • Type Safety: Compile-time and runtime validation for all GPU operations
  • Cross-Platform Reliability: Consistent behavior across Linux, Windows, and macOS
  • Error Recovery: Comprehensive error handling with recovery strategies
  • Validation System: Runtime checks to prevent GPU errors and resource leaks
  • Multi-Queue Safety: Thread-safe concurrent GPU execution

Safety

Memory Safety

  • GPU Memory Management: RAII-based automatic resource cleanup
  • Lifetime Checking: Compile-time lifetime validation with Rust borrow checker
  • Bounds Checking: Bounds validation on tensor operations
  • Thread Safety: Safe concurrent access patterns

Error Handling

  • Error Recovery: Detection and recovery from GPU errors
  • Error Context: Error messages with contextual information
  • Hardware Compatibility: Fallback strategies for compatibility issues
  • Resource Cleanup: Resource cleanup in error conditions

Testing

Komputers includes testing suites:

Test Coverage

  • Test Suites: Unit, integration, and end-to-end testing
  • Memory Safety: AddressSanitizer and LeakSanitizer integration
  • Correctness Validation: Functional testing with comprehensive error detection
  • Cross-Platform: Testing across Linux, Windows, macOS
  • GPU Hardware: Testing across NVIDIA, AMD, Intel, Apple GPUs

Test Commands

# Run comprehensive test suite (excludes benchmarks for faster builds)
cargo test --release --features "async,glsl,debug,validation,test-utils"

# Memory safety validation (if asan feature is available)
# cargo test --features asan

# Safety and correctness validation
cargo test --test safety_validation_suite -- --nocapture

# Cross-platform compatibility
cargo test --test gpu_vendor_compatibility_matrix

# Run benchmarks (heavier compilation with criterion)
cargo test --features "benchmarks" --benches

# Run property tests (heavier compilation with proptest)
cargo test --features "property-testing" --test property_tests

Documentation

API Documentation

  • docs.rs: Official API documentation with examples, tutorials, and version selector

Examples and Tutorials

Complete Examples

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup

# Clone repository
git clone https://github.com/axiomantic/komputers.git
cd komputers

# Install dependencies (Ubuntu/Debian)
sudo apt install vulkan-dev cmake build-essential

# Install dependencies (macOS)
brew install vulkan-headers vulkan-loader molten-vk cmake

# Run tests (excludes benchmarks for faster builds)
cargo test --features "async,glsl,debug,validation,test-utils"

License

Licensed under the Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)

Acknowledgments

  • Kompute Project for the excellent C++ GPU computing framework
  • Vulkan for the cross-platform GPU API
  • MoltenVK for Metal-to-Vulkan translation on macOS

About

Rust bindings for the Kompute GPU compute framework.

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages