silx_types/types/nalgebra/
mod.rs

1/// various implementations
2/// * implementrations of rkyv traits for `ConstSlx` and for `ArrayStorageSlx`
3/// * implementrations of `RawStorage`, `RawMutStorage`, `Storage` for `ArrayStorageSlx`
4mod hack;
5/// From implementations
6mod froms;
7/// Implement `Deref` (resp. `DerefMut`) from `ArrayStorageSlx` to Matrix given storage `ArchRefArrayStorageSlx` (resp. `ArchMutArrayStorageSlx`)
8mod derefs;
9/// implement `ArchToRef` and `ArchToMut` for archived silx array storages
10mod arch_tools;
11
12use crate::f64slx;
13
14use super::{ IntoSlx, SlxInto, SlxFrom, ArchToDerefMut, ArchToDeref, };
15use hashed_type_def::HashedTypeDef;
16use silx_core::utils::{ ArchData, ArchSized, };
17
18use std::{ pin::Pin, fmt::Debug, marker::PhantomData, };
19
20use rkyv::Archived;
21use nalgebra::{ matrix, Const, Dim, DimName, IsContiguous, Matrix, };
22
23/// Silx constant dimension
24#[derive(Debug, Clone, Copy, PartialEq,)]
25#[derive(HashedTypeDef)]
26pub struct ConstSlx<const R: usize>;
27
28/// Silx 1-dimension
29pub type U1Slx = ConstSlx<1>;
30
31/// Silx array storage
32#[derive(Debug, Clone, Copy, PartialEq,)]
33#[derive(HashedTypeDef)]
34#[repr(transparent)]
35pub struct ArrayStorageSlx<T, const R: usize, const C: usize>(pub [[T; R]; C]);
36
37/// array storage derived from archive reference of silx array storage; it is assumed that `T` is a simple structure which archive to itself
38#[repr(transparent)]
39pub struct ArchRefArrayStorageSlx<'a, T, const R: usize, const C: usize>(&'a Archived<ArrayStorageSlx<T,R,C>>) where T: 'a + rkyv::Archive;
40
41/// mutable array storage derived from archive mutable reference of silx array storage; it is assumed that `T` is a simple structure which archive to itself
42#[repr(transparent)]
43pub struct ArchMutArrayStorageSlx<'a, T, const R: usize, const C: usize>(&'a mut Archived<ArrayStorageSlx<T,R,C>>) where T: 'a + rkyv::Archive;
44
45/// Silx object for implementing Matrix deref
46#[repr(C)]
47#[derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, Debug, Clone, Copy,)]
48#[derive(HashedTypeDef)]
49pub struct DerefMatrixSlx<T,R,C,S> where R: Dim, C: Dim, {
50    data: S,
51    _phantoms: PhantomData<(T, R, C)>,
52}
53
54/// Silx marker for storage
55pub trait StorageSlx<R: Dim, C: Dim,> {}
56
57// ////////////////////////
58// Some implementations
59
60// implementation of `Dim` for `ConstSlx`
61unsafe impl<const T: usize> Dim for ConstSlx<T> {
62    fn try_to_usize() -> Option<usize> { Some(T) }
63    fn value(&self) -> usize { T }
64    fn from_usize(dim: usize) -> Self { assert_eq!(dim, T); Self }
65}
66// implementation of `DimName` for `ConstSlx`
67impl<const T: usize> DimName for ConstSlx<T> {
68    const USIZE: usize = T;
69    #[inline] fn name() -> Self { Self }
70    #[inline] fn dim() -> usize { T }
71}
72impl<'a, T, const R: usize, const C: usize> ArchRefArrayStorageSlx<'a, T,R,C> where T: rkyv::Archive {
73    /// Constructor for ArchRefArrayStorageSlx
74    fn new(archive: &'a Archived<ArrayStorageSlx<T,R,C>>) -> Self { Self(archive) }
75}
76impl<'a, T, const R: usize, const C: usize> ArchMutArrayStorageSlx<'a, T,R,C> where T: rkyv::Archive {
77    /// Constructor for ArchMutArrayStorageSlx
78    fn new(archive: &'a mut Archived<ArrayStorageSlx<T,R,C>>) -> Self  { Self(archive) }
79}
80
81impl<T: Clone,const R: usize, const C: usize> StorageSlx<Const<R>, Const<C>> for ArrayStorageSlx<T,R,C> {}
82unsafe impl<T: Clone, const R: usize, const C: usize> IsContiguous for ArrayStorageSlx<T, R, C> {}
83
84/// Experimentations on silx matrices archives
85pub fn exp_silx_matrix() {
86    use hashed_type_def::HashedTypeMethods;
87    // matrix to be processed
88    let matrix: Matrix<f64slx,_,_,_> = matrix![
89        1f64.slx(),     2f64.slx(),     3f64.slx(),     4f64.slx();
90        5f64.slx(),     6f64.slx(),     7f64.slx(),     8f64.slx();
91        10f64.slx(),    10f64.slx(),    11f64.slx(),    12f64.slx()
92    ];
93    println!("ArrayStorage:");
94    // build slx storage for the matrix: 
95    // * matrix storage is the only thing which is transmitted between processes
96    // * matrix storage entirely characterize the SMatrix
97    let arst: ArrayStorageSlx<_,3,4> = matrix.slx(); // the precision on the slx type is generally necessary
98    println!("arst -> {:?}",arst);
99    println!("arst::full_id() -> {}", arst.self_type_uuid());
100    // build the archived slx data
101    let mut arch = arst.arch_sized().expect("failed to serialize");
102    { // some test for accessing the marchived matrix data by reference
103      // * unlike method `unarchive()`, method `arch_ref()` does not need deserialization and is 'zero-copy'
104        // get the matrix reference (actually a value which deref to the matrix) from the archive (zero-copy)
105        let rmatrixslx: DerefMatrixSlx<_,_,_,_> = arch.arch_deref().expect("failure");
106        // get the reference by dereferencing
107        let rmatrix = & * rmatrixslx;
108        println!("rmatrix -> {}", rmatrix);
109        // compute matrice transpose  times matrice
110        let mtm = rmatrix.transpose() * rmatrix;
111        println!("mtm -> {:?}", mtm);
112        println!();
113    }
114    { // some test for accessing the marchived matrix data by mutable reference
115      // * unlike method `unarchive()`, method `arch_mut()` does not need deserialization and is 'zero-copy'
116        // get the matrix mutable reference (actually a value which derefmut to the matrix) from the archive (zero-copy)
117        // * rkyv makes necessary the use of pinned mutable reference
118        let mut rmatrixslx: Pin<DerefMatrixSlx<_,_,_,_>> = Pin::new(&mut arch).arch_deref_mut().expect("failure");
119        // get the mutable reference by dereferencing
120        let rmatrix: &mut Matrix<f64slx,_,_,_> = &mut * rmatrixslx;
121        // change left-top value of the matrix
122        rmatrix[(0,0)] = (-1f64).slx();
123        println!("rmatrix -> {}", rmatrix);
124        // compute matrice transpose  times matrice
125        let mtm = rmatrix.transpose() * &*rmatrix;
126        println!("mtm -> {}", mtm);
127        println!();
128    }
129}