arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/spqlios/src/module.rs
spqlios/src/module.rsRaw
 1use crate::bindings::*;
 2
 3pub fn create_module(N: u64, mtype: module_type_t) -> *mut MODULE {
 4    unsafe {
 5        let m = new_module_info(N, mtype);
 6        if m.is_null() {
 7            println!("Failed to create module.");
 8        }
 9        m
10    }
11}
12
13#[test]
14fn test_new_module_info() {
15    let N: u64 = 1024;
16    let module_ptr: *mut module_info_t = create_module(N, module_type_t_FFT64);
17    assert!(!module_ptr.is_null());
18    println!("{:?}", module_ptr);
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use std::ffi::c_void;
25    use std::time::Instant;
26    //use test::Bencher;
27
28    #[test]
29    fn test_fft() {
30        let log_bound: usize = 19;
31
32        let n: usize = 2048;
33        let m: usize = n >> 1;
34
35        let mut a: Vec<i64> = vec![i64::default(); n];
36        let mut b: Vec<i64> = vec![i64::default(); n];
37        let mut c: Vec<i64> = vec![i64::default(); n];
38
39        a.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
40        b[1] = 1;
41
42        println!("{:?}", b);
43
44        unsafe {
45            let reim_fft_precomp = new_reim_fft_precomp(m as u32, 2);
46            let reim_ifft_precomp = new_reim_ifft_precomp(m as u32, 1);
47
48            let buf_a = reim_fft_precomp_get_buffer(reim_fft_precomp, 0);
49            let buf_b = reim_fft_precomp_get_buffer(reim_fft_precomp, 1);
50            let buf_c = reim_ifft_precomp_get_buffer(reim_ifft_precomp, 0);
51
52            let now = Instant::now();
53            (0..1024).for_each(|i| {
54                reim_from_znx64_simple(
55                    m as u32,
56                    log_bound as u32,
57                    buf_a as *mut c_void,
58                    a.as_ptr(),
59                );
60                reim_fft(reim_fft_precomp, buf_a);
61
62                reim_from_znx64_simple(
63                    m as u32,
64                    log_bound as u32,
65                    buf_b as *mut c_void,
66                    b.as_ptr(),
67                );
68                reim_fft(reim_fft_precomp, buf_b);
69
70                reim_fftvec_mul_simple(
71                    m as u32,
72                    buf_c as *mut c_void,
73                    buf_a as *mut c_void,
74                    buf_b as *mut c_void,
75                );
76                reim_ifft(reim_ifft_precomp, buf_c);
77
78                reim_to_znx64_simple(
79                    m as u32,
80                    m as f64,
81                    log_bound as u32,
82                    c.as_mut_ptr(),
83                    buf_c as *mut c_void,
84                )
85            });
86
87            println!("time: {}us", now.elapsed().as_micros());
88            println!("{:?}", &c[..16]);
89        }
90    }
91}