mdarray 0.7.2

Multidimensional array for Rust
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
#[cfg(feature = "nightly")]
use alloc::alloc::Allocator;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop};
use core::ptr;

#[cfg(not(feature = "nightly"))]
use crate::allocator::Allocator;
use crate::layout::Dense;
use crate::mapping::{DenseMapping, Mapping};
use crate::raw_slice::RawSlice;
use crate::shape::Shape;
use crate::slice::Slice;

#[cfg(not(feature = "nightly"))]
macro_rules! vec_t {
    ($type:ty, $alloc:ty) => {
        Vec<$type>
    };
}

#[cfg(feature = "nightly")]
macro_rules! vec_t {
    ($type:ty, $alloc:ty) => {
        Vec<$type, $alloc>
    };
}

pub(crate) struct RawTensor<T, S: Shape, A: Allocator> {
    slice: RawSlice<T, S, Dense>,
    capacity: usize,
    #[cfg(not(feature = "nightly"))]
    phantom: PhantomData<A>,
    #[cfg(feature = "nightly")]
    alloc: ManuallyDrop<A>,
}

struct DropGuard<'a, T, A: Allocator> {
    ptr: *mut T,
    len: usize,
    #[cfg(not(feature = "nightly"))]
    phantom: PhantomData<(&'a mut Vec<T>, &'a A)>,
    #[cfg(feature = "nightly")]
    phantom: PhantomData<&'a mut Vec<T, A>>,
}

impl<T, S: Shape, A: Allocator> RawTensor<T, S, A> {
    #[cfg(feature = "nightly")]
    #[inline]
    pub(crate) fn allocator(&self) -> &A {
        &self.alloc
    }

    #[inline]
    pub(crate) fn as_mut_slice(&mut self) -> &mut Slice<T, S> {
        self.slice.as_mut_slice()
    }

    #[inline]
    pub(crate) fn as_slice(&self) -> &Slice<T, S> {
        self.slice.as_slice()
    }

    #[inline]
    pub(crate) fn capacity(&self) -> usize {
        if mem::size_of::<T>() > 0 { self.capacity } else { usize::MAX }
    }

    #[cfg(not(feature = "nightly"))]
    #[inline]
    pub(crate) unsafe fn from_parts(vec: Vec<T>, mapping: DenseMapping<S>) -> Self {
        debug_assert!(Some(vec.len()) == mapping.shape().checked_len(), "length mismatch");

        let mut vec = ManuallyDrop::new(vec);

        Self {
            slice: unsafe { RawSlice::new_unchecked(vec.as_mut_ptr(), mapping) },
            capacity: vec.capacity(),
            phantom: PhantomData,
        }
    }

    #[cfg(feature = "nightly")]
    #[inline]
    pub(crate) unsafe fn from_parts(vec: Vec<T, A>, mapping: DenseMapping<S>) -> Self {
        debug_assert!(Some(vec.len()) == mapping.shape().checked_len(), "length mismatch");

        let (ptr, _, capacity, alloc) = vec.into_raw_parts_with_alloc();

        Self {
            slice: unsafe { RawSlice::new_unchecked(ptr, mapping) },
            capacity,
            alloc: ManuallyDrop::new(alloc),
        }
    }

    #[inline]
    pub(crate) fn into_parts(self) -> (vec_t!(T, A), DenseMapping<S>) {
        let mut me = ManuallyDrop::new(self);

        #[cfg(not(feature = "nightly"))]
        let vec = unsafe {
            Vec::from_raw_parts(me.slice.as_mut_ptr(), me.slice.mapping().len(), me.capacity)
        };
        #[cfg(feature = "nightly")]
        let vec = unsafe {
            Vec::from_raw_parts_in(
                me.slice.as_mut_ptr(),
                me.slice.mapping().len(),
                me.capacity,
                ManuallyDrop::take(&mut me.alloc),
            )
        };

        unsafe { (vec, ptr::read(me.slice.mapping())) }
    }

    #[inline]
    pub(crate) fn resize_with<F: FnMut() -> T>(&mut self, new_dims: &[usize], mut f: F)
    where
        A: Clone,
    {
        assert!(new_dims.len() == self.slice.mapping().rank(), "invalid rank");

        if !new_dims.is_empty() {
            let new_len = new_dims.iter().try_fold(1usize, |acc, &x| acc.checked_mul(x));
            let new_len = new_len.expect("invalid length");

            unsafe {
                self.with_mut_parts(|vec, old_mapping| {
                    old_mapping.shape().with_dims(|old_dims| {
                        if new_len == 0 {
                            vec.clear();
                        } else if new_dims[1..] == old_dims[1..] {
                            vec.resize_with(new_len, &mut f);
                        } else {
                            #[cfg(not(feature = "nightly"))]
                            let mut new_vec = Vec::with_capacity(new_len);
                            #[cfg(feature = "nightly")]
                            let mut new_vec =
                                Vec::with_capacity_in(new_len, vec.allocator().clone());

                            copy_dim::<T, S, A>(
                                &mut DropGuard::new(vec),
                                &mut new_vec,
                                old_dims,
                                new_dims,
                                &mut f,
                            );

                            *vec = new_vec;
                        }
                    });

                    old_mapping.shape_mut().with_mut_dims(|dims| dims.copy_from_slice(new_dims));
                });
            }
        }
    }

    #[inline]
    pub(crate) unsafe fn set_mapping(&mut self, new_mapping: DenseMapping<S>) {
        debug_assert!(new_mapping.shape().checked_len().is_some(), "invalid length");
        debug_assert!(new_mapping.len() <= self.capacity, "length exceeds capacity");

        unsafe {
            *self.slice.mapping_mut() = new_mapping;
        }
    }

    #[cfg(not(feature = "nightly"))]
    #[inline]
    pub(crate) unsafe fn with_mut_parts<U, F>(&mut self, f: F) -> U
    where
        F: FnOnce(&mut Vec<T>, &mut DenseMapping<S>) -> U,
    {
        struct DropGuard<'a, T, S: Shape, A: Allocator> {
            tensor: &'a mut RawTensor<T, S, A>,
            vec: ManuallyDrop<Vec<T>>,
        }

        impl<T, S: Shape, A: Allocator> Drop for DropGuard<'_, T, S, A> {
            #[inline]
            fn drop(&mut self) {
                unsafe {
                    self.tensor.slice.set_ptr(self.vec.as_mut_ptr());
                    self.tensor.capacity = self.vec.capacity();

                    // Cleanup in case of length mismatch (e.g. due to allocation failure)
                    if self.vec.len() != self.tensor.slice.mapping().len() {
                        assert!(S::default().len() == 0, "default length not zero");

                        *self.tensor.slice.mapping_mut() = DenseMapping::default();
                        ptr::drop_in_place(self.vec.as_mut_slice());
                    }
                }
            }
        }

        let vec = unsafe {
            Vec::from_raw_parts(self.slice.as_mut_ptr(), self.slice.mapping().len(), self.capacity)
        };

        let mut guard = DropGuard { tensor: self, vec: ManuallyDrop::new(vec) };

        let mapping = unsafe { guard.tensor.slice.mapping_mut() };
        let result = f(&mut guard.vec, mapping);

        debug_assert!(Some(guard.vec.len()) == mapping.shape().checked_len(), "length mismatch");

        unsafe {
            guard.tensor.slice.set_ptr(guard.vec.as_mut_ptr());
            guard.tensor.capacity = guard.vec.capacity();
        }

        mem::forget(guard);

        result
    }

    #[cfg(feature = "nightly")]
    #[inline]
    pub(crate) unsafe fn with_mut_parts<U, F>(&mut self, f: F) -> U
    where
        F: FnOnce(&mut Vec<T, A>, &mut DenseMapping<S>) -> U,
    {
        struct DropGuard<'a, T, S: Shape, A: Allocator> {
            tensor: &'a mut RawTensor<T, S, A>,
            vec: ManuallyDrop<Vec<T, A>>,
        }

        impl<T, S: Shape, A: Allocator> Drop for DropGuard<'_, T, S, A> {
            #[inline]
            fn drop(&mut self) {
                unsafe {
                    self.tensor.slice.set_ptr(self.vec.as_mut_ptr());
                    self.tensor.capacity = self.vec.capacity();
                    self.tensor.alloc = ManuallyDrop::new(ptr::read(self.vec.allocator()));

                    // Cleanup in case of length mismatch (e.g. due to allocation failure)
                    if self.vec.len() != self.tensor.slice.mapping().len() {
                        *self.tensor.slice.mapping_mut() = DenseMapping::default();
                        ptr::drop_in_place(self.vec.as_mut_slice());
                    }
                }
            }
        }

        let vec = unsafe {
            Vec::from_raw_parts_in(
                self.slice.as_mut_ptr(),
                self.slice.mapping().len(),
                self.capacity,
                ManuallyDrop::take(&mut self.alloc),
            )
        };

        let mut guard = DropGuard { tensor: self, vec: ManuallyDrop::new(vec) };

        let mapping = unsafe { guard.tensor.slice.mapping_mut() };
        let result = f(&mut guard.vec, mapping);

        debug_assert!(Some(guard.vec.len()) == mapping.shape().checked_len(), "length mismatch");

        unsafe {
            guard.tensor.slice.set_ptr(guard.vec.as_mut_ptr());
            guard.tensor.capacity = guard.vec.capacity();
            guard.tensor.alloc = ManuallyDrop::new(ptr::read(guard.vec.allocator()));
        }

        mem::forget(guard);

        result
    }

    #[inline]
    pub(crate) fn with_vec<U, F: FnOnce(&vec_t!(T, A)) -> U>(&self, f: F) -> U {
        #[cfg(not(feature = "nightly"))]
        let vec = unsafe {
            Vec::from_raw_parts(
                self.slice.as_ptr() as *mut T,
                self.slice.mapping().len(),
                self.capacity,
            )
        };
        #[cfg(feature = "nightly")]
        let vec = unsafe {
            Vec::from_raw_parts_in(
                self.slice.as_ptr() as *mut T,
                self.slice.mapping().len(),
                self.capacity,
                ptr::read(&*self.alloc),
            )
        };

        f(&ManuallyDrop::new(vec))
    }
}

impl<T: Clone, S: Shape, A: Allocator + Clone> Clone for RawTensor<T, S, A> {
    #[inline]
    fn clone(&self) -> Self {
        unsafe { Self::from_parts(self.with_vec(|vec| vec.clone()), self.slice.mapping().clone()) }
    }

    #[inline]
    fn clone_from(&mut self, source: &Self) {
        unsafe {
            self.with_mut_parts(|dst, mapping| {
                source.with_vec(|src| dst.clone_from(src));
                mapping.clone_from(source.slice.mapping());
            });
        }
    }
}

impl<T, S: Shape, A: Allocator> Drop for RawTensor<T, S, A> {
    #[cfg(not(feature = "nightly"))]
    #[inline]
    fn drop(&mut self) {
        _ = unsafe {
            Vec::from_raw_parts(self.slice.as_mut_ptr(), self.slice.mapping().len(), self.capacity)
        };
    }

    #[cfg(feature = "nightly")]
    #[inline]
    fn drop(&mut self) {
        _ = unsafe {
            Vec::from_raw_parts_in(
                self.slice.as_mut_ptr(),
                self.slice.mapping().len(),
                self.capacity,
                ManuallyDrop::take(&mut self.alloc),
            )
        };
    }
}

unsafe impl<T: Send, S: Shape, A: Allocator + Send> Send for RawTensor<T, S, A> {}
unsafe impl<T: Sync, S: Shape, A: Allocator + Sync> Sync for RawTensor<T, S, A> {}

impl<'a, T, A: Allocator> DropGuard<'a, T, A> {
    #[inline]
    fn new(vec: &'a mut vec_t!(T, A)) -> Self {
        let len = vec.len();

        unsafe {
            vec.set_len(0);
        }

        Self { ptr: vec.as_mut_ptr(), len, phantom: PhantomData }
    }
}

impl<T, A: Allocator> Drop for DropGuard<'_, T, A> {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ptr::slice_from_raw_parts_mut(self.ptr, self.len).drop_in_place();
        }
    }
}

#[inline]
unsafe fn copy_dim<T, S: Shape, A: Allocator>(
    old_vec: &mut DropGuard<T, A>,
    new_vec: &mut vec_t!(T, A),
    old_dims: &[usize],
    new_dims: &[usize],
    f: &mut impl FnMut() -> T,
) {
    let old_stride: usize = old_dims[1..].iter().product();
    let new_stride: usize = new_dims[1..].iter().product();

    let old_size = old_dims[0];
    let new_size = new_dims[0];

    let min_size = old_size.min(new_size);

    unsafe {
        if old_dims.len() > 1 {
            // Avoid very long compile times for release build with MIR inlining,
            // by avoiding recursion until types are known.
            //
            // This is a workaround until const if is available, see #3582 and #122301.

            unsafe fn dummy<T, A: Allocator>(
                _: &mut DropGuard<T, A>,
                _: &mut vec_t!(T, A),
                _: &[usize],
                _: &[usize],
                _: &mut impl FnMut() -> T,
            ) {
                unreachable!();
            }

            let g = const {
                match S::RANK {
                    Some(..2) => dummy::<T, A>,
                    _ => copy_dim::<T, S::Tail, A>,
                }
            };

            for _ in 0..min_size {
                g(old_vec, new_vec, &old_dims[1..], &new_dims[1..], f);
            }
        } else {
            debug_assert!(old_vec.len >= min_size, "slice exceeds remainder");
            debug_assert!(new_vec.len() + min_size <= new_vec.capacity(), "slice exceeds capacity");

            ptr::copy_nonoverlapping(
                old_vec.ptr,
                new_vec.as_mut_ptr().add(new_vec.len()),
                min_size,
            );

            old_vec.ptr = old_vec.ptr.add(min_size);
            old_vec.len -= min_size;

            new_vec.set_len(new_vec.len() + min_size);
        }

        if old_size > min_size {
            let count = (old_size - min_size) * old_stride;
            let slice = ptr::slice_from_raw_parts_mut(old_vec.ptr, count);

            debug_assert!(old_vec.len >= count, "slice exceeds remainder");

            old_vec.ptr = old_vec.ptr.add(count);
            old_vec.len -= count;

            ptr::drop_in_place(slice);
        }

        let additional = (new_size - min_size) * new_stride;

        debug_assert!(new_vec.len() + additional <= new_vec.capacity(), "slice exceeds capacity");

        for _ in 0..additional {
            new_vec.as_mut_ptr().add(new_vec.len()).write(f());
            new_vec.set_len(new_vec.len() + 1);
        }
    }
}