oxirs-cluster 0.2.4

Raft-backed distributed dataset for high availability and horizontal scaling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# GPU Acceleration Setup Guide for OxiRS Cluster

**Version:** 0.2.3
**Last Updated:** 2026-03-05

## Overview

This guide covers setup and configuration of GPU acceleration for OxiRS Cluster, enabling 10-100x performance improvements for machine learning operations, load balancing, and data compression.

## Table of Contents

1. [Hardware Requirements]#hardware-requirements
2. [NVIDIA CUDA Setup]#nvidia-cuda-setup
3. [Apple Metal Setup]#apple-metal-setup
4. [Configuration]#configuration
5. [Performance Tuning]#performance-tuning
6. [Troubleshooting]#troubleshooting

---

## Hardware Requirements

### NVIDIA GPUs (CUDA)

**Minimum Requirements:**
- NVIDIA GPU with Compute Capability 6.0+
- CUDA Toolkit 11.0+
- 2GB VRAM minimum (8GB+ recommended)
- CUDA-compatible driver

**Recommended:**
- NVIDIA A100, V100, or RTX 4090
- CUDA Toolkit 12.0+
- 16GB+ VRAM
- NVLink for multi-GPU setups

**Compatibility Matrix:**

| GPU Model | Compute Capability | Recommended Use Case |
|-----------|-------------------|----------------------|
| RTX 4090 | 8.9 | Development, small-scale production |
| A100 | 8.0 | Large-scale production |
| V100 | 7.0 | Production workloads |
| T4 | 7.5 | Cloud deployments |
| GTX 1080 Ti | 6.1 | Development only |

### Apple Silicon (Metal)

**Supported:**
- M1, M1 Pro, M1 Max, M1 Ultra
- M2, M2 Pro, M2 Max, M2 Ultra
- M3, M3 Pro, M3 Max
- macOS 12.0 (Monterey) or later

**Recommended:**
- M2 Ultra or M3 Max for production
- 32GB+ unified memory
- macOS 13.0 (Ventura) or later

---

## NVIDIA CUDA Setup

### Step 1: Install CUDA Toolkit

**Ubuntu/Debian:**

```bash
# Add NVIDIA repository
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update

# Install CUDA Toolkit
sudo apt-get install -y cuda-toolkit-12-3

# Install cuDNN (optional, for neural networks)
sudo apt-get install -y libcudnn8 libcudnn8-dev
```

**RHEL/CentOS:**

```bash
# Add NVIDIA repository
sudo dnf config-manager --add-repo \
    https://developer.download.nvidia.com/compute/cuda/repos/rhel8/x86_64/cuda-rhel8.repo

# Install CUDA Toolkit
sudo dnf install cuda-toolkit-12-3

# Install cuDNN
sudo dnf install libcudnn8 libcudnn8-devel
```

**Verify Installation:**

```bash
nvidia-smi
nvcc --version
```

Expected output:
```
CUDA Version: 12.3
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 535.129.03   Driver Version: 535.129.03   CUDA Version: 12.3   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  NVIDIA A100-SXM...  On   | 00000000:00:1E.0 Off |                    0 |
| N/A   30C    P0    46W / 400W |      0MiB / 40960MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
```

### Step 2: Configure Environment

Add to `~/.bashrc` or `~/.zshrc`:

```bash
export PATH=/usr/local/cuda-12.3/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.3/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda-12.3
```

### Step 3: Build OxiRS with CUDA Support

```bash
cd /path/to/oxirs/storage/oxirs-cluster

# Build with CUDA feature
cargo build --release --features cuda

# Or for development
cargo build --features cuda
```

### Step 4: Verify GPU Acceleration

```rust
use oxirs_cluster::gpu_acceleration::GpuAcceleratedCluster;

#[tokio::main]
async fn main() -> Result<()> {
    let cluster = GpuAcceleratedCluster::new().await?;

    if let Some(backend) = cluster.gpu_backend() {
        println!("GPU Backend: {:?}", backend);
        println!("GPU Available: true");
    } else {
        println!("GPU not available, using CPU fallback");
    }

    Ok(())
}
```

---

## Apple Metal Setup

### Step 1: System Requirements

Ensure you have:
- macOS 12.0 or later
- Xcode Command Line Tools
- Rust toolchain with aarch64-apple-darwin target

### Step 2: Install Xcode Command Line Tools

```bash
xcode-select --install
```

### Step 3: Build OxiRS with Metal Support

```bash
cd /path/to/oxirs/storage/oxirs-cluster

# Build with Metal feature
cargo build --release --features metal

# For development
cargo build --features metal
```

### Step 4: Verify Metal Acceleration

```bash
# Check Metal support
system_profiler SPDisplaysDataType | grep Metal

# Expected output:
# Metal: Supported, feature set macOS GPUFamily2 v1
```

Test in code:

```rust
use oxirs_cluster::gpu_acceleration::{GpuAcceleratedCluster, GpuBackend};

#[tokio::main]
async fn main() -> Result<()> {
    let cluster = GpuAcceleratedCluster::new().await?;

    match cluster.gpu_backend() {
        Some(GpuBackend::Metal) => {
            println!("✅ Metal GPU acceleration enabled");
        }
        Some(GpuBackend::ParallelCpu) => {
            println!("⚠️  GPU not available, using parallel CPU");
        }
        _ => {
            println!("❌ No acceleration available");
        }
    }

    Ok(())
}
```

---

## Configuration

### Basic GPU Configuration

**Cargo.toml:**

```toml
[dependencies]
oxirs-cluster = { version = "0.2.3", features = ["cuda"] }  # Or "metal"

[features]
default = []
cuda = ["oxirs-cluster/cuda"]
metal = ["oxirs-cluster/metal"]
```

### Runtime Configuration

**oxirs.toml:**

```toml
[gpu]
# Enable GPU acceleration
enabled = true

# Backend selection (auto, cuda, metal, cpu)
backend = "auto"

# Memory limit per GPU (MB)
memory_limit_mb = 8192

# Batch size for GPU operations
batch_size = 1000

# Enable mixed precision (FP16/FP32)
mixed_precision = true

# Number of GPUs to use (0 = all available)
gpu_count = 0

[gpu.replica_selection]
# Enable GPU-accelerated replica selection
enabled = true

# Feature weights for scoring
latency_weight = 0.3
connection_weight = 0.2
lag_weight = 0.2
cpu_weight = 0.1
memory_weight = 0.1
success_rate_weight = 0.1

[gpu.load_forecasting]
# Enable GPU-accelerated load forecasting
enabled = true

# History size for time series
history_size = 100

# Forecast horizon
forecast_steps = 10
```

### Programmatic Configuration

```rust
use oxirs_cluster::gpu_acceleration::{
    GpuAcceleratedCluster,
    GpuConfig,
    GpuBackend,
};

let config = GpuConfig {
    backend: GpuBackend::CUDA,
    memory_limit_mb: 8192,
    batch_size: 1000,
    mixed_precision: true,
    device_id: 0,
};

let cluster = GpuAcceleratedCluster::with_config(config).await?;
```

---

## Performance Tuning

### Optimal Batch Sizes

GPU operations have overhead. Use these guidelines for batch sizing:

| Operation | Recommended Batch Size | GPU Threshold |
|-----------|----------------------|---------------|
| Replica Selection | 50-500 | 10+ replicas |
| Load Forecasting | 100-1000 | 24+ datapoints |
| Compression | 1MB-10MB | 1MB+ data |
| Hash Computation | 1000-10000 | 100+ items |

```rust
// Adaptive batching based on data size
let batch_size = if data.len() < 1000 {
    // Use CPU for small batches
    data.len()
} else {
    // Use GPU with optimal batch size
    1000.min(data.len())
};
```

### Memory Management

**Prevent Out-of-Memory Errors:**

```rust
use scirs2_core::gpu::GpuContext;

let ctx = GpuContext::new()?;

// Check available memory before allocation
let available_mb = ctx.available_memory_mb()?;
if available_mb < required_mb {
    return Err(anyhow!("Insufficient GPU memory"));
}

// Allocate with explicit size limits
let buffer = GpuBuffer::with_capacity(&ctx, max_size)?;
```

**Memory Pooling:**

```rust
use scirs2_core::memory::BufferPool;

// Reuse GPU buffers
let pool = BufferPool::new();
let buffer = pool.acquire(size)?;

// ... use buffer ...

// Automatically returned to pool when dropped
```

### Multi-GPU Setup

**Distribute workload across GPUs:**

```rust
use rayon::prelude::*;

let gpu_count = get_gpu_count()?;
let chunks = data.chunks(data.len() / gpu_count);

let results: Vec<_> = chunks
    .enumerate()
    .par_bridge()
    .map(|(gpu_id, chunk)| {
        // Process on specific GPU
        process_on_gpu(gpu_id, chunk)
    })
    .collect();
```

### Benchmark Your Setup

```bash
# Run GPU benchmarks
cargo bench --features cuda --bench cluster_benchmarks

# Specific benchmark
cargo bench --features cuda gpu_replica_selection
```

Expected performance (NVIDIA A100):

```
GPU replica selection/10 replicas   time: [45.2 µs 46.1 µs 47.3 µs]
GPU replica selection/100 replicas  time: [82.5 µs 84.2 µs 86.1 µs]
GPU replica selection/500 replicas  time: [201 µs 205 µs 210 µs]

CPU replica selection/10 replicas   time: [156 µs 159 µs 163 µs]
CPU replica selection/100 replicas  time: [1.85 ms 1.89 ms 1.94 ms]
CPU replica selection/500 replicas  time: [9.12 ms 9.34 ms 9.58 ms]
```

Speedup: **10-46x** for replica selection

---

## Troubleshooting

### Issue: "CUDA driver version is insufficient"

**Cause:** Outdated NVIDIA driver

**Solution:**
```bash
# Check required version
cat /usr/local/cuda/version.txt

# Update driver (Ubuntu)
sudo apt-get install --reinstall nvidia-driver-535

# Reboot
sudo reboot
```

### Issue: "No CUDA-capable device detected"

**Diagnostic:**
```bash
nvidia-smi
lspci | grep -i nvidia
```

**Solutions:**
1. Verify GPU is properly seated
2. Check BIOS settings (PCIe config)
3. Install/reinstall NVIDIA driver
4. Verify GPU is not in compute-prohibited mode

### Issue: "Out of memory" errors

**Solutions:**

1. **Reduce batch size:**
```rust
let config = GpuConfig {
    batch_size: 500,  // Reduce from 1000
    ..Default::default()
};
```

2. **Enable memory pooling:**
```rust
use scirs2_core::memory::GlobalBufferPool;

let pool = GlobalBufferPool::new();
// Buffers automatically reused
```

3. **Clear GPU memory explicitly:**
```rust
cluster.clear_gpu_cache().await?;
```

### Issue: "Kernel launch failed"

**Cause:** Invalid grid/block dimensions or corrupted kernel

**Solutions:**
1. Verify CUDA installation: `nvcc --version`
2. Rebuild with clean: `cargo clean && cargo build --features cuda`
3. Check GPU compute capability matches code
4. Verify GPU is not overheating: `nvidia-smi -q -d TEMPERATURE`

### Issue: Metal acceleration not working on macOS

**Diagnostic:**
```bash
# Check Metal support
system_profiler SPDisplaysDataType | grep Metal

# Check for errors
log show --predicate 'process == "oxirs-cluster"' --last 5m | grep -i metal
```

**Solutions:**
1. Update to latest macOS: `softwareupdate -l`
2. Update Xcode Command Line Tools: `xcode-select --install`
3. Rebuild: `cargo clean && cargo build --features metal`

### Issue: Performance is worse with GPU

**Causes:**
- Batch size too small (overhead dominates)
- Data transfer bottleneck
- CPU-bound operations mixed with GPU

**Solutions:**

1. **Increase batch size:**
```rust
// Only use GPU for large batches
if replicas.len() >= 50 {
    select_replica_gpu(replicas)
} else {
    select_replica_cpu(replicas)
}
```

2. **Profile to find bottleneck:**
```rust
use scirs2_core::profiling::Profiler;

let mut profiler = Profiler::new();
profiler.start("gpu_transfer");
// ... transfer data to GPU ...
profiler.stop("gpu_transfer");

profiler.start("gpu_compute");
// ... GPU computation ...
profiler.stop("gpu_compute");

println!("{:#?}", profiler.generate_report());
```

3. **Use pinned memory for transfers:**
```rust
use scirs2_core::memory_efficient::PinnedMemory;

let pinned = PinnedMemory::new(size)?;
// Faster host-to-device transfers
```

---

## Performance Comparison

### Replica Selection (500 replicas)

| Hardware | Time | Speedup |
|----------|------|---------|
| CPU (Intel Xeon) | 9.34 ms | 1x |
| CPU (Apple M2 Max) | 6.12 ms | 1.5x |
| GPU (NVIDIA T4) | 1.2 ms | 7.8x |
| GPU (NVIDIA A100) | 205 µs | 45.6x |

### Load Forecasting (500 datapoints)

| Hardware | Time | Speedup |
|----------|------|---------|
| CPU | 45.2 ms | 1x |
| GPU (T4) | 8.5 ms | 5.3x |
| GPU (A100) | 3.2 ms | 14.1x |

### Compression (10MB data)

| Hardware | Time | Speedup |
|----------|------|---------|
| CPU (single-threaded) | 125 ms | 1x |
| CPU (8 threads) | 45 ms | 2.8x |
| GPU (T4) | 18 ms | 6.9x |
| GPU (A100) | 9 ms | 13.9x |

---

## Best Practices

### 1. Automatic Fallback

```rust
let cluster = GpuAcceleratedCluster::new().await?;

// Automatically falls back to CPU if GPU unavailable
let result = cluster.select_best_replica(replicas).await?;
```

### 2. Batch Operations

```rust
// ❌ Bad: Individual operations
for replica in replicas {
    process_single(replica).await?;
}

// ✅ Good: Batch processing
process_batch(replicas).await?;
```

### 3. Profile Before Optimizing

```bash
# Benchmark CPU vs GPU
cargo bench --features cuda

# Profile with perf (Linux)
perf record -g cargo run --release --features cuda
perf report
```

### 4. Monitor GPU Utilization

```bash
# Real-time monitoring
watch -n 1 nvidia-smi

# Log to file
nvidia-smi dmon -s mu -o T -f gpu_usage.log
```

### 5. Graceful Degradation

```rust
let gpu_available = check_gpu_availability();

let config = if gpu_available {
    ClusterConfig::with_gpu()
} else {
    ClusterConfig::with_parallel_cpu()
};
```

---

## Additional Resources

- [NVIDIA CUDA Toolkit Documentation]https://docs.nvidia.com/cuda/
- [Apple Metal Programming Guide]https://developer.apple.com/metal/
- [SciRS2 GPU Integration Guide]./SCIRS2_INTEGRATION_GUIDE.md
- [OxiRS Performance Tuning]./PERFORMANCE_TUNING.md

---

**Last Updated:** 2026-01-06
**Maintainer:** OxiRS Team