sklears-manifold 0.1.2

Manifold learning algorithms (t-SNE, Isomap, etc.)
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
use scirs2_core::ndarray::{Array1, Array2};
use std::alloc::{alloc_zeroed, dealloc, Layout};

pub struct CacheFriendlyMatrix<T> {
    data: *mut T,
    layout: Layout,
    rows: usize,
    cols: usize,
    row_stride: usize,
}

impl<T> CacheFriendlyMatrix<T>
where
    T: Copy + Clone + Default,
{
    pub fn new(rows: usize, cols: usize) -> Result<Self, &'static str> {
        if rows == 0 || cols == 0 {
            return Err("Matrix dimensions must be positive");
        }

        let cache_line_size = 64; // bytes
        let element_size = std::mem::size_of::<T>();
        let elements_per_cache_line = cache_line_size / element_size;

        // Pad row stride to cache line boundary
        let row_stride = cols.div_ceil(elements_per_cache_line) * elements_per_cache_line;

        let total_elements = rows * row_stride;
        let layout = Layout::array::<T>(total_elements).map_err(|_| "Layout error")?;

        let data = unsafe { alloc_zeroed(layout) as *mut T };
        if data.is_null() {
            return Err("Allocation failed");
        }

        Ok(Self {
            data,
            layout,
            rows,
            cols,
            row_stride,
        })
    }

    /// # Safety
    /// Caller must ensure `row < self.rows` and `col < self.cols`.
    pub unsafe fn get_unchecked(&self, row: usize, col: usize) -> &T {
        let ptr = self.data.add(row * self.row_stride + col);
        &*ptr
    }

    /// # Safety
    /// Caller must ensure `row < self.rows` and `col < self.cols`.
    pub unsafe fn get_unchecked_mut(&mut self, row: usize, col: usize) -> &mut T {
        let ptr = self.data.add(row * self.row_stride + col);
        &mut *ptr
    }

    pub fn get(&self, row: usize, col: usize) -> Option<&T> {
        if row < self.rows && col < self.cols {
            Some(unsafe { self.get_unchecked(row, col) })
        } else {
            None
        }
    }

    pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
        if row < self.rows && col < self.cols {
            Some(unsafe { self.get_unchecked_mut(row, col) })
        } else {
            None
        }
    }

    pub fn set(&mut self, row: usize, col: usize, value: T) -> Result<(), &'static str> {
        if row >= self.rows || col >= self.cols {
            return Err("Index out of bounds");
        }
        unsafe {
            *self.get_unchecked_mut(row, col) = value;
        }
        Ok(())
    }

    pub fn rows(&self) -> usize {
        self.rows
    }

    pub fn cols(&self) -> usize {
        self.cols
    }

    pub fn to_ndarray(&self) -> Array2<T> {
        let mut result = Array2::default((self.rows, self.cols));
        for i in 0..self.rows {
            for j in 0..self.cols {
                result[[i, j]] = *unsafe { self.get_unchecked(i, j) };
            }
        }
        result
    }

    pub fn from_ndarray(array: &Array2<T>) -> Result<Self, &'static str> {
        let mut result = Self::new(array.nrows(), array.ncols())?;
        for i in 0..array.nrows() {
            for j in 0..array.ncols() {
                result.set(i, j, array[[i, j]])?;
            }
        }
        Ok(result)
    }
}

impl<T> Drop for CacheFriendlyMatrix<T> {
    fn drop(&mut self) {
        unsafe {
            dealloc(self.data as *mut u8, self.layout);
        }
    }
}

unsafe impl<T: Send> Send for CacheFriendlyMatrix<T> {}
unsafe impl<T: Sync> Sync for CacheFriendlyMatrix<T> {}

pub struct UnsafeDistanceComputer;

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

impl UnsafeDistanceComputer {
    pub fn new() -> Self {
        Self
    }

    /// # Safety
    /// Caller must ensure `a` and `b` are valid pointers to at least `len` elements.
    pub unsafe fn euclidean_distance_unchecked(a: *const f64, b: *const f64, len: usize) -> f64 {
        let mut sum = 0.0;
        let mut i = 0;

        // Process 4 elements at a time for better performance
        while i + 4 <= len {
            let diff1 = *a.add(i) - *b.add(i);
            let diff2 = *a.add(i + 1) - *b.add(i + 1);
            let diff3 = *a.add(i + 2) - *b.add(i + 2);
            let diff4 = *a.add(i + 3) - *b.add(i + 3);

            sum += diff1 * diff1 + diff2 * diff2 + diff3 * diff3 + diff4 * diff4;
            i += 4;
        }

        // Process remaining elements
        while i < len {
            let diff = *a.add(i) - *b.add(i);
            sum += diff * diff;
            i += 1;
        }

        sum.sqrt()
    }

    pub fn fast_pairwise_distances(&self, data: &Array2<f64>) -> Array2<f64> {
        let n_samples = data.nrows();
        let n_features = data.ncols();
        let mut distances: Array2<f64> = Array2::zeros((n_samples, n_samples));

        unsafe {
            for i in 0..n_samples {
                let row_i = data.as_ptr().add(i * n_features);
                for j in i + 1..n_samples {
                    let row_j = data.as_ptr().add(j * n_features);
                    let dist = Self::euclidean_distance_unchecked(row_i, row_j, n_features);

                    // Access matrix elements directly using raw pointers
                    let dist_ptr = distances.as_mut_ptr().add(i * n_samples + j);
                    *dist_ptr = dist;
                    let sym_dist_ptr = distances.as_mut_ptr().add(j * n_samples + i);
                    *sym_dist_ptr = dist;
                }
            }
        }

        distances
    }

    /// # Safety
    /// Caller must ensure `a` and `b` are valid pointers to at least `len` elements.
    pub unsafe fn dot_product_unchecked(a: *const f64, b: *const f64, len: usize) -> f64 {
        let mut sum = 0.0;
        let mut i = 0;

        // Unroll loop for better performance
        while i + 8 <= len {
            sum += *a.add(i) * *b.add(i)
                + *a.add(i + 1) * *b.add(i + 1)
                + *a.add(i + 2) * *b.add(i + 2)
                + *a.add(i + 3) * *b.add(i + 3)
                + *a.add(i + 4) * *b.add(i + 4)
                + *a.add(i + 5) * *b.add(i + 5)
                + *a.add(i + 6) * *b.add(i + 6)
                + *a.add(i + 7) * *b.add(i + 7);
            i += 8;
        }

        while i < len {
            sum += *a.add(i) * *b.add(i);
            i += 1;
        }

        sum
    }

    pub fn fast_matrix_multiply(
        &self,
        a: &Array2<f64>,
        b: &Array2<f64>,
    ) -> Result<Array2<f64>, &'static str> {
        if a.ncols() != b.nrows() {
            return Err("Matrix dimensions do not match for multiplication");
        }

        let m = a.nrows();
        let n = a.ncols();
        let p = b.ncols();

        let mut result: Array2<f64> = Array2::zeros((m, p));

        unsafe {
            for i in 0..m {
                let a_row = a.as_ptr().add(i * n);
                for j in 0..p {
                    let mut sum = 0.0;
                    for k in 0..n {
                        let b_elem = *b.as_ptr().add(k * p + j);
                        sum += *a_row.add(k) * b_elem;
                    }
                    *result.as_mut_ptr().add(i * p + j) = sum;
                }
            }
        }

        Ok(result)
    }
}

pub struct BlockedMatrixMultiply {
    block_size: usize,
}

impl BlockedMatrixMultiply {
    pub fn new(block_size: usize) -> Self {
        Self { block_size }
    }

    pub fn multiply(&self, a: &Array2<f64>, b: &Array2<f64>) -> Result<Array2<f64>, &'static str> {
        if a.ncols() != b.nrows() {
            return Err("Matrix dimensions do not match");
        }

        let m = a.nrows();
        let n = a.ncols();
        let p = b.ncols();

        let mut result: Array2<f64> = Array2::zeros((m, p));

        // Block-based multiplication for better cache locality
        for i_block in (0..m).step_by(self.block_size) {
            for j_block in (0..p).step_by(self.block_size) {
                for k_block in (0..n).step_by(self.block_size) {
                    let i_end = (i_block + self.block_size).min(m);
                    let j_end = (j_block + self.block_size).min(p);
                    let k_end = (k_block + self.block_size).min(n);

                    for i in i_block..i_end {
                        for j in j_block..j_end {
                            let mut sum = result[[i, j]];
                            for k in k_block..k_end {
                                sum += a[[i, k]] * b[[k, j]];
                            }
                            result[[i, j]] = sum;
                        }
                    }
                }
            }
        }

        Ok(result)
    }
}

pub struct PrefetchOptimizedOperations;

impl PrefetchOptimizedOperations {
    pub fn optimized_vector_sum(data: &Array2<f64>) -> Array1<f64> {
        let n_rows = data.nrows();
        let n_cols = data.ncols();
        let mut result: Array1<f64> = Array1::zeros(n_cols);

        unsafe {
            for j in 0..n_cols {
                let mut sum = 0.0;
                let mut i = 0;

                while i + 4 < n_rows {
                    // Prefetch next cache line on x86_64
                    #[cfg(target_arch = "x86_64")]
                    if i + 8 < n_rows {
                        let prefetch_addr = data.as_ptr().add((i + 8) * n_cols + j);
                        std::arch::x86_64::_mm_prefetch(
                            prefetch_addr as *const i8,
                            std::arch::x86_64::_MM_HINT_T0,
                        );
                    }

                    sum += *data.as_ptr().add(i * n_cols + j)
                        + *data.as_ptr().add((i + 1) * n_cols + j)
                        + *data.as_ptr().add((i + 2) * n_cols + j)
                        + *data.as_ptr().add((i + 3) * n_cols + j);
                    i += 4;
                }

                while i < n_rows {
                    sum += *data.as_ptr().add(i * n_cols + j);
                    i += 1;
                }

                *result.as_mut_ptr().add(j) = sum;
            }
        }

        result
    }
}

pub fn profile_guided_optimization_hint() {
    // This function would normally contain profile-guided optimization hints
    // For now, it's a placeholder for future PGO implementation
    // Note: `likely`/`unlikely` hints are not yet stable target_feature values
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_cache_friendly_matrix_creation() {
        let matrix = CacheFriendlyMatrix::<f64>::new(10, 20);
        assert!(matrix.is_ok());

        let matrix = matrix.expect("operation should succeed");
        assert_eq!(matrix.rows(), 10);
        assert_eq!(matrix.cols(), 20);
    }

    #[test]
    fn test_cache_friendly_matrix_access() {
        let mut matrix = CacheFriendlyMatrix::<f64>::new(5, 5).expect("operation should succeed");

        matrix.set(2, 3, 42.0).expect("operation should succeed");
        let value = matrix.get(2, 3).expect("operation should succeed");
        assert_eq!(*value, 42.0);
    }

    #[test]
    fn test_cache_friendly_matrix_conversion() {
        let ndarray = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("operation should succeed");
        let matrix = CacheFriendlyMatrix::from_ndarray(&ndarray).expect("operation should succeed");
        let converted_back = matrix.to_ndarray();

        assert_eq!(ndarray, converted_back);
    }

    #[test]
    fn test_unsafe_distance_computer() {
        let computer = UnsafeDistanceComputer::new();
        let data = Array2::from_shape_vec((3, 2), vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0])
            .expect("operation should succeed");

        let distances = computer.fast_pairwise_distances(&data);

        assert_eq!(distances.shape(), &[3, 3]);
        assert!((distances[[0, 1]] - 1.0).abs() < 1e-10);
        assert!((distances[[0, 2]] - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_blocked_matrix_multiply() {
        let multiplier = BlockedMatrixMultiply::new(2);
        let a = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("operation should succeed");
        let b = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("operation should succeed");

        let result = multiplier
            .multiply(&a, &b)
            .expect("operation should succeed");

        assert_eq!(result.shape(), &[2, 2]);
        assert_eq!(result[[0, 0]], 22.0);
        assert_eq!(result[[0, 1]], 28.0);
        assert_eq!(result[[1, 0]], 49.0);
        assert_eq!(result[[1, 1]], 64.0);
    }

    #[test]
    fn test_optimized_vector_sum() {
        let data = Array2::from_shape_vec(
            (3, 4),
            vec![
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
            ],
        )
        .expect("operation should succeed");

        let result = PrefetchOptimizedOperations::optimized_vector_sum(&data);

        assert_eq!(result.len(), 4);
        assert_eq!(result[0], 15.0); // 1 + 5 + 9
        assert_eq!(result[1], 18.0); // 2 + 6 + 10
        assert_eq!(result[2], 21.0); // 3 + 7 + 11
        assert_eq!(result[3], 24.0); // 4 + 8 + 12
    }

    #[test]
    fn test_fast_matrix_multiply() {
        let computer = UnsafeDistanceComputer::new();
        let a = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 3.0, 4.0])
            .expect("operation should succeed");
        let b = Array2::from_shape_vec((2, 2), vec![5.0, 6.0, 7.0, 8.0])
            .expect("operation should succeed");

        let result = computer
            .fast_matrix_multiply(&a, &b)
            .expect("operation should succeed");

        assert_eq!(result.shape(), &[2, 2]);
        assert_eq!(result[[0, 0]], 19.0); // 1*5 + 2*7
        assert_eq!(result[[0, 1]], 22.0); // 1*6 + 2*8
        assert_eq!(result[[1, 0]], 43.0); // 3*5 + 4*7
        assert_eq!(result[[1, 1]], 50.0); // 3*6 + 4*8
    }
}