Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastkmeans-rs"
version = "1.0.6"
version = "1.0.7"
edition = "2021"
description = "A fast and efficient k-means clustering implementation in Rust, compatible with ndarray"
license = "Apache-2.0"
Expand Down Expand Up @@ -30,7 +30,11 @@ intel-mkl-src = { version = "0.8", features = ["mkl-static-lp64-seq"], optional
accelerate-src = { version = "0.3", optional = true }

# CUDA dependencies (optional)
cudarc = { version = "0.19", default-features = false, features = ["cuda-11080", "cublas", "dynamic-loading", "nvrtc"], optional = true }
# NOTE: the CUDA major-version binding (`cuda-11080` / `cuda-13000` / ...) is
# intentionally NOT pinned here. cudarc binds exactly one CUDA major version at
# compile time, so the choice is exposed through the `cuda` / `cuda-13` features
# below and lets a parent crate select it via feature unification.
cudarc = { version = "0.19", default-features = false, features = ["cublas", "dynamic-loading", "nvrtc"], optional = true }

# Metal GPU dependencies (macOS only, optional)
metal = { version = "0.31", optional = true }
Expand All @@ -52,9 +56,20 @@ mkl = ["blas-src", "intel-mkl-src", "ndarray/blas"]
# Use Apple Accelerate framework (macOS only, recommended for macOS)
accelerate = ["blas-src", "accelerate-src", "ndarray/blas"]

# CUDA GPU acceleration
# Requires CUDA toolkit installed (nvcc in PATH)
cuda = ["cudarc"]
# CUDA GPU acceleration. Requires the CUDA toolkit / driver at runtime.
#
# cudarc binds exactly ONE CUDA major version at compile time (the cuBLAS/NVRTC
# SONAMEs and bindings differ between majors), so pick the one matching the
# target host and DO NOT enable more than one:
# * `cuda` -> CUDA 11.8 / 12.x (default, unchanged behaviour)
# * `cuda-13` -> CUDA 13.x (hosts where only .so.13 libraries exist)
# Both turn on the internal `_cuda` gate that actually compiles the CUDA code.
cuda = ["_cuda", "cudarc/cuda-11080"]
cuda-13 = ["_cuda", "cudarc/cuda-13000"]

# Internal: pulls in cudarc and gates all CUDA code. Not meant to be enabled
# directly — always go through `cuda` or `cuda-13` so a CUDA version is chosen.
_cuda = ["dep:cudarc"]

# Metal GPU acceleration (macOS only, Apple Silicon recommended)
# Uses Metal compute shaders for GPU-accelerated k-means
Expand All @@ -68,7 +83,7 @@ required-features = ["npy"]
[[bin]]
name = "compare-kmeans-cuda"
path = "src/bin/compare_kmeans_cuda.rs"
required-features = ["npy", "cuda"]
required-features = ["npy", "_cuda"]

[[bench]]
name = "kmeans_benchmark"
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ fastkmeans-rs = "0.1"

| Feature | Platform | Description |
|---|---|---|
| `cuda` | NVIDIA GPU | Flash-accelerated CUDA with cuBLAS GEMM and warp-cooperative kernels |
| `cuda` | NVIDIA GPU (CUDA 11.8 / 12.x) | Flash-accelerated CUDA with cuBLAS GEMM and warp-cooperative kernels |
| `cuda-13` | NVIDIA GPU (CUDA 13.x) | Same as `cuda`, built against the CUDA 13 SONAMEs (use on hosts where only `.so.13` libraries exist) |
| `metal_gpu` | macOS (Apple Silicon) | Metal Performance Shaders GPU acceleration |
| `accelerate` | macOS | Apple Accelerate BLAS for CPU |
| `mkl` | Linux (Intel/AMD) | Intel MKL for CPU (recommended for Linux, fastest) |
| `openblas` | Linux / Windows | OpenBLAS for CPU (requires `libopenblas-dev`) |

```toml
# NVIDIA GPU (recommended for Linux)
fastkmeans-rs = { version = "0.1", features = ["cuda"] }
# NVIDIA GPU (recommended for Linux). Pick the CUDA major matching the host;
# do NOT enable both (cudarc binds one CUDA major at compile time).
fastkmeans-rs = { version = "1", features = ["cuda"] } # CUDA 11.8 / 12.x
fastkmeans-rs = { version = "1", features = ["cuda-13"] } # CUDA 13.x

# Apple Silicon GPU
fastkmeans-rs = { version = "0.1", features = ["metal_gpu", "accelerate"] }
Expand Down
6 changes: 3 additions & 3 deletions examples/bench_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::RandomExt;
use std::time::Instant;

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
use fastkmeans_rs::cuda::FastKMeansCuda;

fn main() {
println!("=== Flash K-Means CUDA Benchmark (H100) ===");
println!("25 iterations, seed=42, no subsampling\n");

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
{
// Warmup GPU
let warmup_data = Array2::random((1000, 128), Uniform::new(-1.0f32, 1.0));
Expand Down Expand Up @@ -98,7 +98,7 @@ fn main() {
}
}

#[cfg(not(feature = "cuda"))]
#[cfg(not(feature = "_cuda"))]
println!("CUDA feature not enabled");

println!("\n=== Done ===");
Expand Down
6 changes: 3 additions & 3 deletions examples/bench_flash_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ndarray::Array2;
use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::RandomExt;

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
use fastkmeans_rs::cuda::FastKMeansCuda;

fn main() {
Expand Down Expand Up @@ -68,7 +68,7 @@ fn main() {

let data = Array2::random((n_samples, n_features), Uniform::new(-1.0f32, 1.0));

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
{
let config = KMeansConfig::new(k)
.with_seed(42)
Expand All @@ -86,6 +86,6 @@ fn main() {
);
}

#[cfg(not(feature = "cuda"))]
#[cfg(not(feature = "_cuda"))]
println!("CUDA not enabled");
}
6 changes: 3 additions & 3 deletions examples/bench_train_predict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ fn main() {
let n = 100_000;
let d = 128;

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
let backend = "CUDA";
#[cfg(not(feature = "cuda"))]
#[cfg(not(feature = "_cuda"))]
let backend = "CPU";

println!("=== fastkmeans-rs {} ===", backend);
println!("100K vectors x 128d, 25 iters (train on 100K, predict on 100K)\n");

// Warmup
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
{
let w = Array2::random((1000, d), Uniform::new(-1.0f32, 1.0));
let mut wk = FastKMeans::with_config(
Expand Down
6 changes: 3 additions & 3 deletions examples/cuda_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::RandomExt;
use std::time::Instant;

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
use fastkmeans_rs::cuda::FastKMeansCuda;

fn main() {
Expand Down Expand Up @@ -40,7 +40,7 @@ fn main() {
println!(" CPU time: {:>8.3}s", cpu_time.as_secs_f64());

// CUDA benchmark
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
{
let cuda_config = KMeansConfig::new(k)
.with_seed(42)
Expand Down Expand Up @@ -77,7 +77,7 @@ fn main() {
}
}

#[cfg(not(feature = "cuda"))]
#[cfg(not(feature = "_cuda"))]
{
println!(" CUDA: (feature not enabled)");
}
Expand Down
10 changes: 5 additions & 5 deletions src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct FastKMeans {
centroids: Option<Array2<f32>>,

/// CUDA GPU backend (lazily initialized when cuda feature is enabled)
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
cuda: Option<crate::cuda::FastKMeansCuda>,

/// Metal GPU backend (lazily initialized when metal_gpu feature is enabled)
Expand All @@ -70,7 +70,7 @@ impl FastKMeans {
config: KMeansConfig::new(k),
d,
centroids: None,
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
cuda: None,
#[cfg(feature = "metal_gpu")]
metal: None,
Expand All @@ -93,7 +93,7 @@ impl FastKMeans {
d: 0, // Will be set on first train call
config,
centroids: None,
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
cuda: None,
#[cfg(feature = "metal_gpu")]
metal: None,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl FastKMeans {
}

// CUDA GPU (flash-accelerated) — error if init fails
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
{
if self.cuda.is_none() {
self.cuda = Some(crate::cuda::FastKMeansCuda::with_config(
Expand Down Expand Up @@ -226,7 +226,7 @@ impl FastKMeans {
}

// Use CUDA GPU if it was initialized during training
#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
if let Some(ref cuda) = self.cuda {
return cuda.predict(data);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod distance;
mod error;
mod kmeans;

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
pub mod cuda;

#[cfg(feature = "metal_gpu")]
Expand All @@ -125,7 +125,7 @@ pub use config::KMeansConfig;
pub use error::KMeansError;
pub use kmeans::FastKMeans;

#[cfg(feature = "cuda")]
#[cfg(feature = "_cuda")]
pub use cuda::FastKMeansCuda;

#[cfg(feature = "metal_gpu")]
Expand Down
2 changes: 1 addition & 1 deletion tests/cuda_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! CUDA-specific integration tests

#![cfg(feature = "cuda")]
#![cfg(feature = "_cuda")]

use fastkmeans_rs::cuda::FastKMeansCuda;
use fastkmeans_rs::{FastKMeans, KMeansConfig};
Expand Down
Loading