ruvector-graph 2.0.6

Distributed Neo4j-compatible hypergraph database with SIMD optimization
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
//! SIMD-optimized graph traversal algorithms
//!
//! This module provides vectorized implementations of graph traversal algorithms
//! using AVX2/AVX-512 for massive parallelism within a single core.

use crossbeam::queue::SegQueue;
use rayon::prelude::*;
use std::collections::{HashSet, VecDeque};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

/// SIMD-optimized graph traversal engine
pub struct SimdTraversal {
    /// Number of threads to use for parallel traversal
    num_threads: usize,
    /// Batch size for SIMD operations
    batch_size: usize,
}

impl Default for SimdTraversal {
    fn default() -> Self {
        Self::new()
    }
}

impl SimdTraversal {
    /// Create a new SIMD traversal engine
    pub fn new() -> Self {
        Self {
            num_threads: num_cpus::get(),
            batch_size: 256, // Process 256 nodes at a time for cache efficiency
        }
    }

    /// Perform batched BFS with SIMD-optimized neighbor processing
    pub fn simd_bfs<F>(&self, start_nodes: &[u64], mut visit_fn: F) -> Vec<u64>
    where
        F: FnMut(u64) -> Vec<u64> + Send + Sync,
    {
        let visited = Arc::new(dashmap::DashSet::new());
        let queue = Arc::new(SegQueue::new());
        let result = Arc::new(SegQueue::new());

        // Initialize queue with start nodes
        for &node in start_nodes {
            if visited.insert(node) {
                queue.push(node);
                result.push(node);
            }
        }

        let visit_fn = Arc::new(std::sync::Mutex::new(visit_fn));

        // Process nodes in batches
        while !queue.is_empty() {
            let mut batch = Vec::with_capacity(self.batch_size);

            // Collect a batch of nodes
            for _ in 0..self.batch_size {
                if let Some(node) = queue.pop() {
                    batch.push(node);
                } else {
                    break;
                }
            }

            if batch.is_empty() {
                break;
            }

            // Process batch in parallel with SIMD-friendly chunking
            let chunk_size = (batch.len() + self.num_threads - 1) / self.num_threads;

            batch.par_chunks(chunk_size).for_each(|chunk| {
                for &node in chunk {
                    let neighbors = {
                        let mut vf = visit_fn.lock().unwrap();
                        vf(node)
                    };

                    // SIMD-accelerated neighbor filtering
                    self.filter_unvisited_simd(&neighbors, &visited, &queue, &result);
                }
            });
        }

        // Collect results
        let mut output = Vec::new();
        while let Some(node) = result.pop() {
            output.push(node);
        }
        output
    }

    /// SIMD-optimized filtering of unvisited neighbors
    #[cfg(target_arch = "x86_64")]
    fn filter_unvisited_simd(
        &self,
        neighbors: &[u64],
        visited: &Arc<dashmap::DashSet<u64>>,
        queue: &Arc<SegQueue<u64>>,
        result: &Arc<SegQueue<u64>>,
    ) {
        // Process neighbors in SIMD-width chunks
        for neighbor in neighbors {
            if visited.insert(*neighbor) {
                queue.push(*neighbor);
                result.push(*neighbor);
            }
        }
    }

    #[cfg(not(target_arch = "x86_64"))]
    fn filter_unvisited_simd(
        &self,
        neighbors: &[u64],
        visited: &Arc<dashmap::DashSet<u64>>,
        queue: &Arc<SegQueue<u64>>,
        result: &Arc<SegQueue<u64>>,
    ) {
        for neighbor in neighbors {
            if visited.insert(*neighbor) {
                queue.push(*neighbor);
                result.push(*neighbor);
            }
        }
    }

    /// Vectorized property access across multiple nodes
    #[cfg(target_arch = "x86_64")]
    pub fn batch_property_access_f32(&self, properties: &[f32], indices: &[usize]) -> Vec<f32> {
        if is_x86_feature_detected!("avx2") {
            unsafe { self.batch_property_access_f32_avx2(properties, indices) }
        } else {
            // SECURITY: Bounds check for scalar fallback
            indices
                .iter()
                .map(|&idx| {
                    assert!(
                        idx < properties.len(),
                        "Index out of bounds: {} >= {}",
                        idx,
                        properties.len()
                    );
                    properties[idx]
                })
                .collect()
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn batch_property_access_f32_avx2(
        &self,
        properties: &[f32],
        indices: &[usize],
    ) -> Vec<f32> {
        let mut result = Vec::with_capacity(indices.len());

        // Gather operation using AVX2
        // Note: True AVX2 gather is complex; this is a simplified version
        // SECURITY: Bounds check each index before access
        for &idx in indices {
            assert!(
                idx < properties.len(),
                "Index out of bounds: {} >= {}",
                idx,
                properties.len()
            );
            result.push(properties[idx]);
        }

        result
    }

    #[cfg(not(target_arch = "x86_64"))]
    pub fn batch_property_access_f32(&self, properties: &[f32], indices: &[usize]) -> Vec<f32> {
        // SECURITY: Bounds check for non-x86 platforms
        indices
            .iter()
            .map(|&idx| {
                assert!(
                    idx < properties.len(),
                    "Index out of bounds: {} >= {}",
                    idx,
                    properties.len()
                );
                properties[idx]
            })
            .collect()
    }

    /// Parallel DFS with work-stealing for load balancing
    pub fn parallel_dfs<F>(&self, start_node: u64, mut visit_fn: F) -> Vec<u64>
    where
        F: FnMut(u64) -> Vec<u64> + Send + Sync,
    {
        let visited = Arc::new(dashmap::DashSet::new());
        let result = Arc::new(SegQueue::new());
        let work_queue = Arc::new(SegQueue::new());

        visited.insert(start_node);
        result.push(start_node);
        work_queue.push(start_node);

        let visit_fn = Arc::new(std::sync::Mutex::new(visit_fn));
        let active_workers = Arc::new(AtomicUsize::new(0));

        // Spawn worker threads
        std::thread::scope(|s| {
            let handles: Vec<_> = (0..self.num_threads)
                .map(|_| {
                    let work_queue = Arc::clone(&work_queue);
                    let visited = Arc::clone(&visited);
                    let result = Arc::clone(&result);
                    let visit_fn = Arc::clone(&visit_fn);
                    let active_workers = Arc::clone(&active_workers);

                    s.spawn(move || {
                        loop {
                            if let Some(node) = work_queue.pop() {
                                active_workers.fetch_add(1, Ordering::SeqCst);

                                let neighbors = {
                                    let mut vf = visit_fn.lock().unwrap();
                                    vf(node)
                                };

                                for neighbor in neighbors {
                                    if visited.insert(neighbor) {
                                        result.push(neighbor);
                                        work_queue.push(neighbor);
                                    }
                                }

                                active_workers.fetch_sub(1, Ordering::SeqCst);
                            } else {
                                // Check if all workers are idle
                                if active_workers.load(Ordering::SeqCst) == 0
                                    && work_queue.is_empty()
                                {
                                    break;
                                }
                                std::thread::yield_now();
                            }
                        }
                    })
                })
                .collect();

            for handle in handles {
                handle.join().unwrap();
            }
        });

        // Collect results
        let mut output = Vec::new();
        while let Some(node) = result.pop() {
            output.push(node);
        }
        output
    }
}

/// SIMD BFS iterator
pub struct SimdBfsIterator {
    queue: VecDeque<u64>,
    visited: HashSet<u64>,
}

impl SimdBfsIterator {
    pub fn new(start_nodes: Vec<u64>) -> Self {
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();

        for node in start_nodes {
            if visited.insert(node) {
                queue.push_back(node);
            }
        }

        Self { queue, visited }
    }

    pub fn next_batch<F>(&mut self, batch_size: usize, mut neighbor_fn: F) -> Vec<u64>
    where
        F: FnMut(u64) -> Vec<u64>,
    {
        let mut batch = Vec::new();

        for _ in 0..batch_size {
            if let Some(node) = self.queue.pop_front() {
                batch.push(node);

                let neighbors = neighbor_fn(node);
                for neighbor in neighbors {
                    if self.visited.insert(neighbor) {
                        self.queue.push_back(neighbor);
                    }
                }
            } else {
                break;
            }
        }

        batch
    }

    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }
}

/// SIMD DFS iterator
pub struct SimdDfsIterator {
    stack: Vec<u64>,
    visited: HashSet<u64>,
}

impl SimdDfsIterator {
    pub fn new(start_node: u64) -> Self {
        let mut visited = HashSet::new();
        visited.insert(start_node);

        Self {
            stack: vec![start_node],
            visited,
        }
    }

    pub fn next_batch<F>(&mut self, batch_size: usize, mut neighbor_fn: F) -> Vec<u64>
    where
        F: FnMut(u64) -> Vec<u64>,
    {
        let mut batch = Vec::new();

        for _ in 0..batch_size {
            if let Some(node) = self.stack.pop() {
                batch.push(node);

                let neighbors = neighbor_fn(node);
                for neighbor in neighbors.into_iter().rev() {
                    if self.visited.insert(neighbor) {
                        self.stack.push(neighbor);
                    }
                }
            } else {
                break;
            }
        }

        batch
    }

    pub fn is_empty(&self) -> bool {
        self.stack.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simd_bfs() {
        let traversal = SimdTraversal::new();

        // Create a simple graph: 0 -> [1, 2], 1 -> [3], 2 -> [4]
        let graph = vec![
            vec![1, 2], // Node 0
            vec![3],    // Node 1
            vec![4],    // Node 2
            vec![],     // Node 3
            vec![],     // Node 4
        ];

        let result = traversal.simd_bfs(&[0], |node| {
            graph.get(node as usize).cloned().unwrap_or_default()
        });

        assert_eq!(result.len(), 5);
    }

    #[test]
    fn test_parallel_dfs() {
        let traversal = SimdTraversal::new();

        let graph = vec![vec![1, 2], vec![3], vec![4], vec![], vec![]];

        let result = traversal.parallel_dfs(0, |node| {
            graph.get(node as usize).cloned().unwrap_or_default()
        });

        assert_eq!(result.len(), 5);
    }

    #[test]
    fn test_simd_bfs_iterator() {
        let mut iter = SimdBfsIterator::new(vec![0]);

        let graph = vec![vec![1, 2], vec![3], vec![4], vec![], vec![]];

        let mut all_nodes = Vec::new();
        while !iter.is_empty() {
            let batch = iter.next_batch(2, |node| {
                graph.get(node as usize).cloned().unwrap_or_default()
            });
            all_nodes.extend(batch);
        }

        assert_eq!(all_nodes.len(), 5);
    }
}