hive-gpu 0.2.0

High-performance GPU acceleration for vector operations with Device Info API (Metal, CUDA, ROCm)
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# hive-gpu - Integration Guide


## Overview


This guide demonstrates how to integrate hive-gpu into various applications and workflows, including integration with vector databases, embedding pipelines, and custom applications.

---

## Table of Contents


1. [Basic Integration]#basic-integration
2. [Vectorizer Integration]#vectorizer-integration
3. [Custom Vector Database]#custom-vector-database
4. [Embedding Pipeline]#embedding-pipeline
5. [Web Service Integration]#web-service-integration
6. [Real-time Search Application]#real-time-search-application
7. [Best Practices]#best-practices

---

## Basic Integration


### Quick Start


```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::{GpuVector, GpuDistanceMetric};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize GPU context
    let context = MetalNativeContext::new()?;
    
    // Create vector storage
    let mut storage = context.create_storage(384, GpuDistanceMetric::Cosine)?;
    
    // Add vectors
    let vectors = vec![
        GpuVector::new("doc_1".into(), vec![0.1; 384]),
        GpuVector::new("doc_2".into(), vec![0.2; 384]),
    ];
    storage.add_vectors(&vectors)?;
    
    // Search
    let query = vec![0.15; 384];
    let results = storage.search(&query, 10)?;
    
    for result in results {
        println!("{}: {:.4}", result.id, result.score);
    }
    
    Ok(())
}
```

---

## Vectorizer Integration


### Using with Hive-Vectorizer


hive-gpu is designed to integrate seamlessly with the [hive-vectorizer](https://github.com/hivellm/vectorizer) project.

#### Installation


```toml
[dependencies]
vectorizer = { git = "https://github.com/hivellm/vectorizer.git" }
hive-gpu = "0.1"
```

#### Integration Example


```rust
use vectorizer::{VectorStore, models::*};
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use std::sync::Arc;

struct GpuVectorStore {
    store: VectorStore,
    gpu_storage: Box<dyn GpuVectorStorage>,
}

impl GpuVectorStore {
    async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        // Initialize vectorizer store
        let store = VectorStore::new();
        
        // Initialize GPU context
        let context = MetalNativeContext::new()?;
        let gpu_storage = context.create_storage(512, GpuDistanceMetric::Cosine)?;
        
        Ok(Self {
            store,
            gpu_storage,
        })
    }
    
    async fn add_documents(&mut self, documents: Vec<Document>) -> Result<(), Box<dyn std::error::Error>> {
        // Generate embeddings using vectorizer
        let vectors: Vec<GpuVector> = documents
            .iter()
            .map(|doc| {
                let embedding = self.store.generate_embedding(&doc.content)?;
                Ok(GpuVector {
                    id: doc.id.clone(),
                    data: embedding,
                    metadata: doc.metadata.clone(),
                })
            })
            .collect::<Result<_, Box<dyn std::error::Error>>>()?;
        
        // Add to GPU storage
        self.gpu_storage.add_vectors(&vectors)?;
        
        Ok(())
    }
    
    async fn search(&self, query: &str, limit: usize) -> Result<Vec<Document>, Box<dyn std::error::Error>> {
        // Generate query embedding
        let query_embedding = self.store.generate_embedding(query)?;
        
        // GPU-accelerated search
        let results = self.gpu_storage.search(&query_embedding, limit)?;
        
        // Fetch full documents
        let documents = results
            .into_iter()
            .map(|result| self.store.get_document(&result.id))
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(documents)
    }
}

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut store = GpuVectorStore::new().await?;
    
    // Add documents
    let documents = vec![
        Document {
            id: "doc1".into(),
            content: "Machine learning with GPUs".into(),
            metadata: HashMap::new(),
        },
        Document {
            id: "doc2".into(),
            content: "Vector similarity search".into(),
            metadata: HashMap::new(),
        },
    ];
    store.add_documents(documents).await?;
    
    // Search
    let results = store.search("GPU acceleration", 5).await?;
    for doc in results {
        println!("Found: {}", doc.content);
    }
    
    Ok(())
}
```

---

## Custom Vector Database


### Building a Custom Vector DB


```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::*;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use serde::{Serialize, Deserialize};

#[derive(Clone, Serialize, Deserialize)]

pub struct Document {
    pub id: String,
    pub content: String,
    pub metadata: HashMap<String, String>,
}

pub struct VectorDatabase {
    gpu_storage: Arc<RwLock<Box<dyn GpuVectorStorage>>>,
    documents: Arc<RwLock<HashMap<String, Document>>>,
}

impl VectorDatabase {
    pub fn new(dimension: usize, metric: GpuDistanceMetric) -> Result<Self, Box<dyn std::error::Error>> {
        let context = MetalNativeContext::new()?;
        let storage = context.create_storage(dimension, metric)?;
        
        Ok(Self {
            gpu_storage: Arc::new(RwLock::new(storage)),
            documents: Arc::new(RwLock::new(HashMap::new())),
        })
    }
    
    pub fn add_document(&self, document: Document, embedding: Vec<f32>) -> Result<(), Box<dyn std::error::Error>> {
        let vector = GpuVector {
            id: document.id.clone(),
            data: embedding,
            metadata: document.metadata.clone(),
        };
        
        // Add to GPU storage
        let mut storage = self.gpu_storage.write().unwrap();
        storage.add_vectors(&[vector])?;
        
        // Store document
        let mut documents = self.documents.write().unwrap();
        documents.insert(document.id.clone(), document);
        
        Ok(())
    }
    
    pub fn batch_add_documents(&self, docs_with_embeddings: Vec<(Document, Vec<f32>)>) -> Result<(), Box<dyn std::error::Error>> {
        let vectors: Vec<GpuVector> = docs_with_embeddings
            .iter()
            .map(|(doc, embedding)| GpuVector {
                id: doc.id.clone(),
                data: embedding.clone(),
                metadata: doc.metadata.clone(),
            })
            .collect();
        
        // Batch add to GPU
        let mut storage = self.gpu_storage.write().unwrap();
        storage.add_vectors(&vectors)?;
        
        // Store documents
        let mut documents = self.documents.write().unwrap();
        for (doc, _) in docs_with_embeddings {
            documents.insert(doc.id.clone(), doc);
        }
        
        Ok(())
    }
    
    pub fn search(&self, query_embedding: &[f32], limit: usize) -> Result<Vec<(Document, f32)>, Box<dyn std::error::Error>> {
        // GPU search
        let storage = self.gpu_storage.read().unwrap();
        let results = storage.search(query_embedding, limit)?;
        
        // Fetch documents
        let documents = self.documents.read().unwrap();
        let docs_with_scores: Vec<(Document, f32)> = results
            .into_iter()
            .filter_map(|result| {
                documents.get(&result.id).map(|doc| (doc.clone(), result.score))
            })
            .collect();
        
        Ok(docs_with_scores)
    }
    
    pub fn delete_document(&self, id: &str) -> Result<(), Box<dyn std::error::Error>> {
        // Remove from GPU storage
        let mut storage = self.gpu_storage.write().unwrap();
        storage.remove_vectors(&[id.to_string()])?;
        
        // Remove document
        let mut documents = self.documents.write().unwrap();
        documents.remove(id);
        
        Ok(())
    }
    
    pub fn count(&self) -> usize {
        let documents = self.documents.read().unwrap();
        documents.len()
    }
}

// Usage example
#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = VectorDatabase::new(384, GpuDistanceMetric::Cosine)?;
    
    // Add documents
    let docs_with_embeddings = vec![
        (
            Document {
                id: "1".into(),
                content: "GPU acceleration".into(),
                metadata: HashMap::new(),
            },
            vec![0.1; 384],
        ),
        (
            Document {
                id: "2".into(),
                content: "Vector search".into(),
                metadata: HashMap::new(),
            },
            vec![0.2; 384],
        ),
    ];
    
    db.batch_add_documents(docs_with_embeddings)?;
    
    // Search
    let query = vec![0.15; 384];
    let results = db.search(&query, 5)?;
    
    for (doc, score) in results {
        println!("{}: {} (score: {:.4})", doc.id, doc.content, score);
    }
    
    println!("Total documents: {}", db.count());
    
    Ok(())
}
```

---

## Embedding Pipeline


### Integration with Embedding Models


```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::*;

// Mock embedding model (replace with actual model)
struct EmbeddingModel {
    dimension: usize,
}

impl EmbeddingModel {
    fn new() -> Self {
        Self { dimension: 384 }
    }
    
    async fn encode(&self, text: &str) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
        // In practice, use sentence-transformers, OpenAI API, etc.
        // This is a mock implementation
        Ok((0..self.dimension).map(|i| (text.len() as f32 + i as f32) * 0.01).collect())
    }
    
    async fn encode_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, Box<dyn std::error::Error>> {
        let mut embeddings = Vec::new();
        for text in texts {
            embeddings.push(self.encode(text).await?);
        }
        Ok(embeddings)
    }
}

pub struct EmbeddingPipeline {
    model: EmbeddingModel,
    gpu_storage: Box<dyn GpuVectorStorage>,
}

impl EmbeddingPipeline {
    pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let model = EmbeddingModel::new();
        let context = MetalNativeContext::new()?;
        let gpu_storage = context.create_storage(model.dimension, GpuDistanceMetric::Cosine)?;
        
        Ok(Self {
            model,
            gpu_storage,
        })
    }
    
    pub async fn index_documents(&mut self, documents: Vec<(String, String)>) -> Result<(), Box<dyn std::error::Error>> {
        // Extract texts
        let texts: Vec<String> = documents.iter().map(|(_, text)| text.clone()).collect();
        
        // Generate embeddings in batch
        let embeddings = self.model.encode_batch(&texts).await?;
        
        // Create GPU vectors
        let vectors: Vec<GpuVector> = documents
            .into_iter()
            .zip(embeddings)
            .map(|((id, text), embedding)| {
                let mut metadata = HashMap::new();
                metadata.insert("text".to_string(), text);
                GpuVector::with_metadata(id, embedding, metadata)
            })
            .collect();
        
        // Add to GPU storage
        self.gpu_storage.add_vectors(&vectors)?;
        
        Ok(())
    }
    
    pub async fn search(&self, query: &str, limit: usize) -> Result<Vec<SearchResult>, Box<dyn std::error::Error>> {
        // Generate query embedding
        let query_embedding = self.model.encode(query).await?;
        
        // GPU search
        let results = self.gpu_storage.search(&query_embedding, limit)?;
        
        // Return results
        Ok(results.into_iter().map(|r| SearchResult {
            id: r.id,
            score: r.score,
        }).collect())
    }
}

#[derive(Debug)]

pub struct SearchResult {
    pub id: String,
    pub score: f32,
}

// Usage
#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut pipeline = EmbeddingPipeline::new().await?;
    
    // Index documents
    let documents = vec![
        ("1".into(), "GPU-accelerated vector search".into()),
        ("2".into(), "Machine learning on Apple Silicon".into()),
        ("3".into(), "HNSW graph algorithms".into()),
    ];
    
    pipeline.index_documents(documents).await?;
    
    // Search
    let results = pipeline.search("GPU vector search", 5).await?;
    for result in results {
        println!("{}: {:.4}", result.id, result.score);
    }
    
    Ok(())
}
```

---

## Web Service Integration


### REST API with Axum


```rust
use axum::{
    routing::{get, post},
    Json, Router, extract::State,
};
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::*;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
use tokio::net::TcpListener;

#[derive(Clone)]

struct AppState {
    storage: Arc<RwLock<Box<dyn GpuVectorStorage>>>,
}

#[derive(Deserialize)]

struct AddVectorRequest {
    id: String,
    data: Vec<f32>,
}

#[derive(Deserialize)]

struct SearchRequest {
    query: Vec<f32>,
    limit: usize,
}

#[derive(Serialize)]

struct SearchResponse {
    results: Vec<SearchResultItem>,
}

#[derive(Serialize)]

struct SearchResultItem {
    id: String,
    score: f32,
}

async fn add_vector(
    State(state): State<AppState>,
    Json(request): Json<AddVectorRequest>,
) -> Json<serde_json::Value> {
    let vector = GpuVector::new(request.id, request.data);
    
    let mut storage = state.storage.write().unwrap();
    match storage.add_vectors(&[vector]) {
        Ok(_) => Json(serde_json::json!({ "success": true })),
        Err(e) => Json(serde_json::json!({ "error": e.to_string() })),
    }
}

async fn search(
    State(state): State<AppState>,
    Json(request): Json<SearchRequest>,
) -> Json<SearchResponse> {
    let storage = state.storage.read().unwrap();
    
    let results = storage.search(&request.query, request.limit)
        .unwrap_or_default()
        .into_iter()
        .map(|r| SearchResultItem {
            id: r.id,
            score: r.score,
        })
        .collect();
    
    Json(SearchResponse { results })
}

async fn health() -> Json<serde_json::Value> {
    Json(serde_json::json!({ "status": "healthy" }))
}

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize GPU storage
    let context = MetalNativeContext::new()?;
    let storage = context.create_storage(384, GpuDistanceMetric::Cosine)?;
    
    let state = AppState {
        storage: Arc::new(RwLock::new(storage)),
    };
    
    // Build router
    let app = Router::new()
        .route("/health", get(health))
        .route("/add", post(add_vector))
        .route("/search", post(search))
        .with_state(state);
    
    // Run server
    let listener = TcpListener::bind("0.0.0.0:3000").await?;
    println!("Server running on http://0.0.0.0:3000");
    axum::serve(listener, app).await?;
    
    Ok(())
}
```

#### Testing the API


```bash
# Add vectors

curl -X POST http://localhost:3000/add \
  -H "Content-Type: application/json" \
  -d '{"id": "vec1", "data": [0.1, 0.2, 0.3]}'

# Search

curl -X POST http://localhost:3000/search \
  -H "Content-Type: application/json" \
  -d '{"query": [0.15, 0.25, 0.35], "limit": 10}'

# Health check

curl http://localhost:3000/health
```

---

## Real-time Search Application


### Live Document Search


```rust
use hive_gpu::metal::context::MetalNativeContext;
use hive_gpu::traits::{GpuContext, GpuVectorStorage};
use hive_gpu::types::*;
use std::sync::{Arc, RwLock};
use tokio::sync::mpsc;

pub enum IndexCommand {
    AddDocument { id: String, embedding: Vec<f32> },
    RemoveDocument { id: String },
    Search { query: Vec<f32>, limit: usize, response_tx: mpsc::Sender<Vec<GpuSearchResult>> },
}

pub struct RealtimeSearch {
    command_tx: mpsc::Sender<IndexCommand>,
}

impl RealtimeSearch {
    pub async fn new(dimension: usize) -> Result<Self, Box<dyn std::error::Error>> {
        let context = MetalNativeContext::new()?;
        let storage = context.create_storage(dimension, GpuDistanceMetric::Cosine)?;
        let storage = Arc::new(RwLock::new(storage));
        
        let (command_tx, mut command_rx) = mpsc::channel::<IndexCommand>(1000);
        
        // Spawn background worker
        tokio::spawn(async move {
            while let Some(command) = command_rx.recv().await {
                match command {
                    IndexCommand::AddDocument { id, embedding } => {
                        let vector = GpuVector::new(id, embedding);
                        let mut storage = storage.write().unwrap();
                        if let Err(e) = storage.add_vectors(&[vector]) {
                            eprintln!("Error adding vector: {}", e);
                        }
                    }
                    IndexCommand::RemoveDocument { id } => {
                        let mut storage = storage.write().unwrap();
                        if let Err(e) = storage.remove_vectors(&[id]) {
                            eprintln!("Error removing vector: {}", e);
                        }
                    }
                    IndexCommand::Search { query, limit, response_tx } => {
                        let storage = storage.read().unwrap();
                        if let Ok(results) = storage.search(&query, limit) {
                            let _ = response_tx.send(results).await;
                        }
                    }
                }
            }
        });
        
        Ok(Self { command_tx })
    }
    
    pub async fn add_document(&self, id: String, embedding: Vec<f32>) -> Result<(), Box<dyn std::error::Error>> {
        self.command_tx.send(IndexCommand::AddDocument { id, embedding }).await?;
        Ok(())
    }
    
    pub async fn remove_document(&self, id: String) -> Result<(), Box<dyn std::error::Error>> {
        self.command_tx.send(IndexCommand::RemoveDocument { id }).await?;
        Ok(())
    }
    
    pub async fn search(&self, query: Vec<f32>, limit: usize) -> Result<Vec<GpuSearchResult>, Box<dyn std::error::Error>> {
        let (response_tx, mut response_rx) = mpsc::channel(1);
        self.command_tx.send(IndexCommand::Search { query, limit, response_tx }).await?;
        
        response_rx.recv().await.ok_or_else(|| "No response".into())
    }
}

// Usage
#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let search = RealtimeSearch::new(384).await?;
    
    // Add documents continuously
    let search_clone = search.clone();
    tokio::spawn(async move {
        for i in 0..1000 {
            let embedding = vec![i as f32 * 0.001; 384];
            search_clone.add_document(format!("doc_{}", i), embedding).await.unwrap();
            tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        }
    });
    
    // Concurrent searches
    for _ in 0..10 {
        let query = vec![0.5; 384];
        let results = search.search(query, 10).await?;
        println!("Found {} results", results.len());
    }
    
    Ok(())
}
```

---

## Best Practices


### 1. Resource Management


```rust
// ✅ GOOD: Single context, multiple storages
let context = Arc::new(MetalNativeContext::new()?);

let storage1 = context.create_storage(128, GpuDistanceMetric::Cosine)?;
let storage2 = context.create_storage(384, GpuDistanceMetric::Cosine)?;

// ❌ BAD: Multiple contexts
let ctx1 = MetalNativeContext::new()?;
let ctx2 = MetalNativeContext::new()?;  // Wasteful!
```

### 2. Error Handling


```rust
// ✅ GOOD: Proper error propagation
pub async fn add_document(&self, doc: Document) -> Result<(), AppError> {
    let vector = self.create_vector(&doc)?;
    self.storage.add_vectors(&[vector])
        .map_err(|e| AppError::GpuError(e))?;
    Ok(())
}

// ❌ BAD: Swallowing errors
pub async fn add_document(&self, doc: Document) {
    let _ = self.storage.add_vectors(&[vector]);  // Ignores errors!
}
```

### 3. Batch Processing


```rust
// ✅ GOOD: Batch operations
async fn index_documents(&mut self, docs: Vec<Document>) -> Result<()> {
    let vectors: Vec<GpuVector> = docs.into_iter()
        .map(|doc| self.create_vector(&doc))
        .collect::<Result<_>>()?;
    
    self.storage.add_vectors(&vectors)?;
    Ok(())
}

// ❌ BAD: Individual operations
async fn index_documents(&mut self, docs: Vec<Document>) -> Result<()> {
    for doc in docs {
        let vector = self.create_vector(&doc)?;
        self.storage.add_vectors(&[vector])?;  // Inefficient!
    }
    Ok(())
}
```

### 4. Thread Safety


```rust
// ✅ GOOD: Thread-safe access
use std::sync::{Arc, RwLock};

struct ThreadSafeStorage {
    storage: Arc<RwLock<Box<dyn GpuVectorStorage>>>,
}

impl ThreadSafeStorage {
    pub fn search(&self, query: &[f32], limit: usize) -> Result<Vec<GpuSearchResult>> {
        let storage = self.storage.read().unwrap();
        storage.search(query, limit)
    }
}
```

---

## Troubleshooting


### Common Integration Issues


#### Issue: Out of Memory


**Solution:**
```rust
// Check VRAM before adding
let stats = context.memory_stats();
if stats.utilization > 0.9 {
    return Err("VRAM nearly full".into());
}

// Or batch with memory checks
for chunk in vectors.chunks(1000) {
    storage.add_vectors(chunk)?;
    
    let stats = context.memory_stats();
    if stats.utilization > 0.95 {
        eprintln!("Warning: High VRAM usage");
        break;
    }
}
```

#### Issue: Dimension Mismatch


**Solution:**
```rust
// Validate dimensions before adding
fn validate_vector(vector: &GpuVector, expected_dim: usize) -> Result<()> {
    if vector.dimension() != expected_dim {
        return Err(format!("Expected dimension {}, got {}", 
                          expected_dim, vector.dimension()).into());
    }
    Ok(())
}
```

---

*Last Updated: 2025-01-03*