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
//! Array with SIMD alignment

use error::*;
use ffi;
use types::*;

use ndarray::*;
use num_traits::Zero;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::os::raw::c_void;
use std::slice::{from_raw_parts, from_raw_parts_mut};

/// Multi-dimensional Array using [AlignedVec]
///
/// [AlignedVec]: struct.AlignedVec.html
#[derive(Debug, Clone)]
pub struct AlignedArray<A, D>
where
    A: AlignedAllocable,
    D: Dimension,
{
    data: AlignedVec<A>,
    shape: Shape<D>,
}

impl<A: AlignedAllocable> AlignedArray<A, Ix1> {
    pub fn from_vec(data: AlignedVec<A>) -> Self {
        let shape = data.len().into_shape();
        Self { data, shape }
    }
}

impl<A: AlignedAllocable, D: Dimension> AlignedArray<A, D> {
    pub fn new<Sh: ShapeBuilder<Dim = D>>(shape: Sh) -> Self
    where
        A: Zero + AlignedAllocable,
    {
        let shape = shape.into_shape();
        let data = AlignedVec::new(shape.size());
        Self { data, shape }
    }

    pub fn dim(&self) -> D::Pattern {
        self.as_view().dim()
    }

    pub fn shape(&self) -> &Shape<D> {
        &self.shape
    }

    pub fn copy_from_slice(&mut self, input: &[A])
    where
        A: Copy,
    {
        self.data.copy_from_slice(input)
    }

    pub fn as_slice(&self) -> &[A] {
        self.data.as_slice()
    }

    pub fn as_slice_mut(&mut self) -> &mut [A] {
        self.data.as_slice_mut()
    }

    pub fn as_view<'a>(&'a self) -> ArrayView<'a, A, D> {
        self.data.as_view(self.shape.clone()).unwrap()
    }

    pub fn as_view_mut<'a>(&'a mut self) -> ArrayViewMut<'a, A, D> {
        self.data.as_view_mut(self.shape.clone()).unwrap()
    }
}

/// SIMD-aligned Vector
///
/// A RAII-wrapper of `fftw_alloc` and `fftw_free` with the [SIMD alignment].
///
/// [SIMD alignment]: http://www.fftw.org/fftw3_doc/SIMD-alignment-and-fftw_005fmalloc.html
#[derive(Debug)]
pub struct AlignedVec<T> {
    n: usize,
    data: *mut T,
}

/// Allocate SIMD-aligned memory of Real/Complex type
pub trait AlignedAllocable: Zero {
    /// Allocate SIMD-aligned memory
    unsafe fn alloc(n: usize) -> *mut Self;
}

impl AlignedAllocable for f64 {
    unsafe fn alloc(n: usize) -> *mut Self {
        ffi::fftw_alloc_real(n)
    }
}

impl AlignedAllocable for f32 {
    unsafe fn alloc(n: usize) -> *mut Self {
        ffi::fftwf_alloc_real(n)
    }
}

impl AlignedAllocable for c64 {
    unsafe fn alloc(n: usize) -> *mut Self {
        ffi::fftw_alloc_complex(n)
    }
}

impl AlignedAllocable for c32 {
    unsafe fn alloc(n: usize) -> *mut Self {
        ffi::fftwf_alloc_complex(n)
    }
}

impl<T> AlignedVec<T> {
    /// Recast to Rust's immutable slice
    pub fn as_slice(&self) -> &[T] {
        unsafe { from_raw_parts(self.data, self.n) }
    }
    /// Recast to Rust's mutable slice
    pub fn as_slice_mut(&mut self) -> &mut [T] {
        unsafe { from_raw_parts_mut(self.data, self.n) }
    }

    pub fn as_view<'a, Sh, D>(&'a self, shape: Sh) -> Result<ArrayView<'a, T, D>>
    where
        D: Dimension,
        Sh: ShapeBuilder<Dim = D>,
    {
        Ok(ArrayView::from_shape(shape, self)?)
    }

    pub fn as_view_mut<'a, Sh, D>(&'a mut self, shape: Sh) -> Result<ArrayViewMut<'a, T, D>>
    where
        D: Dimension,
        Sh: ShapeBuilder<Dim = D>,
    {
        Ok(ArrayViewMut::from_shape(shape, self)?)
    }
}

impl<T> Deref for AlignedVec<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        self.as_slice()
    }
}

impl<T> DerefMut for AlignedVec<T> {
    fn deref_mut(&mut self) -> &mut [T] {
        self.as_slice_mut()
    }
}

impl<T> Drop for AlignedVec<T> {
    fn drop(&mut self) {
        excall! { ffi::fftw_free(self.data as *mut c_void) };
    }
}

impl<T> AlignedVec<T>
where
    T: AlignedAllocable,
{
    /// Create array with `fftw_malloc` (`fftw_free` is automatically called when the arrya is `Drop`-ed)
    pub fn new(n: usize) -> Self {
        let ptr = excall! { T::alloc(n) };
        let mut vec = AlignedVec { n: n, data: ptr };
        for v in vec.iter_mut() {
            *v = T::zero();
        }
        vec
    }
}

impl<T> Clone for AlignedVec<T>
where
    T: AlignedAllocable,
{
    fn clone(&self) -> Self {
        Self::new(self.n)
    }
}

impl<T> Index<usize> for AlignedVec<T> {
    type Output = T;
    fn index(&self, index: usize) -> &Self::Output {
        unsafe { &*self.data.offset(index as isize) }
    }
}

impl<T> IndexMut<usize> for AlignedVec<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        unsafe { &mut *self.data.offset(index as isize) }
    }
}