lamellar 0.8.1

Lamellar is an asynchronous tasking runtime for HPC systems developed in 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
use std::{
    marker::PhantomData,
    ops::Range,
    ptr::NonNull,
    sync::{atomic::AtomicUsize, Arc},
};

use tracing::trace;

use crate::{
    lamellae::{CommProgress, CommSlice, Lamellae, Remote},
    lamellar_team::IntoLamellarTeam,
    memregion::{LamellarMemoryRegion, OneSidedMemoryRegion, SharedMemoryRegion},
};

/// Trait implemented by types that can serve as the backing store for a [`LamellarBuffer`].
///
/// Implementors include [`Vec<T>`], [`OneSidedMemoryRegion<T>`][crate::memregion::OneSidedMemoryRegion],
/// [`SharedMemoryRegion<T>`][crate::memregion::SharedMemoryRegion], and internal
/// [`CommSlice<T>`][crate::lamellae::CommSlice].
pub trait AsLamellarBuffer<T: Remote>: Send + 'static {
    /// Returns a shared slice of the backing data.
    fn as_slice(&self) -> &[T];
    /// Returns a mutable slice of the backing data.
    fn as_mut_slice(&mut self) -> &mut [T];
}

impl<T: Remote> AsLamellarBuffer<T> for Vec<T> {
    fn as_slice(&self) -> &[T] {
        self.as_slice()
    }
    fn as_mut_slice(&mut self) -> &mut [T] {
        self.as_mut_slice()
    }
}
impl<T: Remote> AsLamellarBuffer<T> for LamellarMemoryRegion<T> {
    fn as_slice(&self) -> &[T] {
        unsafe { self.as_slice() }
    }
    fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { LamellarMemoryRegion::as_mut_slice(self) }
    }
}
impl<T: Remote> AsLamellarBuffer<T> for SharedMemoryRegion<T> {
    fn as_slice(&self) -> &[T] {
        unsafe { self.as_slice() }
    }
    fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { SharedMemoryRegion::as_mut_slice(self) }
    }
}
impl<T: Remote> AsLamellarBuffer<T> for OneSidedMemoryRegion<T> {
    fn as_slice(&self) -> &[T] {
        unsafe { self.as_slice() }
    }
    fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { OneSidedMemoryRegion::as_mut_slice(self) }
    }
}

impl<T: Remote> AsLamellarBuffer<T> for CommSlice<T> {
    fn as_slice(&self) -> &[T] {
        self.as_slice()
    }
    fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { self.as_mut_slice() }
    }
}

struct BufferInner<T> {
    cnt: AtomicUsize,
    data: T,
}

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

impl<T> BufferInner<T> {
    fn new(data: T) -> Self {
        BufferInner {
            cnt: AtomicUsize::new(1),
            data,
        }
    }

    // fn as_slice(&self) -> &[T] {
    //     self.data.as_slice()
    // }

    // fn as_mut_slice(&mut self) -> &mut [T] {
    //     self.data.as_mut_slice()
    // }
}

/// A reference-counted, possibly sub-sliced wrapper around a buffer used as the destination
/// for RDMA get operations.
///
/// `LamellarBuffer<T, B>` is the type returned by and passed into `get_into_buffer` /
/// `get_into_buffer_unmanaged` methods on arrays and memory regions. It tracks the number of
/// outstanding references so that the runtime can determine when the backing store is safe to
/// reclaim or inspect.
///
/// # Constructors
///
/// | Method | Backing store | Safety |
/// |--------|---------------|--------|
/// | [`from_vec`][Self::from_vec] | `Vec<T>` | safe — takes ownership |
/// | [`from_one_sided_memory_region`][Self::from_one_sided_memory_region] | [`OneSidedMemoryRegion<T>`][crate::memregion::OneSidedMemoryRegion] | `unsafe` |
/// | [`from_shared_memory_region`][Self::from_shared_memory_region] | [`SharedMemoryRegion<T>`][crate::memregion::SharedMemoryRegion] | `unsafe` |
///
/// # Examples
///```
/// use lamellar::memregion::prelude::*;
///
/// let world = LamellarWorldBuilder::new().build();
/// let my_pe = world.my_pe();
/// let num_pes = world.num_pes();
///
/// let mem_region: OneSidedMemoryRegion<usize> = world.alloc_one_sided_mem_region(num_pes * 10);
/// unsafe {
///     for (i, elem) in mem_region.as_mut_slice().iter_mut().enumerate() {
///         *elem = i;
///     }
///     let buf = LamellarBuffer::from_vec(&world, vec![0usize; 10]);
///     mem_region.get_into_buffer(my_pe * 10, buf).block();
/// }
///```
pub struct LamellarBuffer<T: Remote, B: AsLamellarBuffer<T>> {
    data: NonNull<BufferInner<B>>,
    range: Range<usize>,
    lamellae: Arc<Lamellae>,
    _phantom: PhantomData<T>,
}
unsafe impl<T: Remote, B: AsLamellarBuffer<T>> Send for LamellarBuffer<T, B> {}
unsafe impl<T: Remote, B: AsLamellarBuffer<T>> Sync for LamellarBuffer<T, B> {}

impl<T: Remote, B: AsLamellarBuffer<T>> std::fmt::Debug for LamellarBuffer<T, B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "LamellarBuffer(range={:?}, current cnt={:?})",
            self.range,
            unsafe {
                self.data
                    .as_ref()
                    .cnt
                    .load(std::sync::atomic::Ordering::SeqCst)
            }
        )
    }
}

// impl<T: Remote> LamellarBuffer<T, LamellarMemoryRegion<T>> {
//     /// Unsafe because multiple handles to the same memory region can be created,
//     /// thus the caller must ensure no other references mutate the region while
//     /// the buffer exists.
//     pub(crate) unsafe fn from_lamellar_memory_region(
//         mem_region: LamellarMemoryRegion<T>,
//     ) -> Self {
//         let len = mem_region.len();
//         LamellarBuffer {
//             data: NonNull::new(Box::into_raw(Box::new(BufferInner::new(mem_region))).into())
//                 .unwrap(),
//             range: 0..len,
//             _phantom: PhantomData,
//         }
//     }
// }

// impl<T: Remote> From<LamellarMemoryRegion<T>> for LamellarBuffer<T, LamellarMemoryRegion<T>> {
//     fn from(mem_region: LamellarMemoryRegion<T>) -> Self {
//         unsafe { LamellarBuffer::from_lamellar_memory_region(mem_region) }
//     }
// }

impl<T: Remote> LamellarBuffer<T, SharedMemoryRegion<T>> {
    /// Wraps a [`SharedMemoryRegion`] as a [`LamellarBuffer`] for use with RDMA get operations.
    ///
    /// # Safety
    /// Multiple [`LamellarBuffer`] handles to the same region can be created. The caller must
    /// ensure that no other reference mutates the memory region while this buffer exists.
    ///
    /// # Examples
    ///```
    /// use lamellar::memregion::prelude::*;
    ///
    /// let world = LamellarWorldBuilder::new().build();
    /// let my_pe = world.my_pe();
    /// let num_pes = world.num_pes();
    ///
    /// let src: OneSidedMemoryRegion<usize> = world.alloc_one_sided_mem_region(num_pes * 10);
    /// let dst: SharedMemoryRegion<usize> = world.alloc_shared_mem_region(num_pes * 10).block();
    /// unsafe {
    ///     for (i, elem) in src.as_mut_slice().iter_mut().enumerate() {
    ///         *elem = i;
    ///     }
    ///     let buf = LamellarBuffer::from_shared_memory_region(dst);
    ///     src.get_into_buffer(0, buf).block();
    /// }
    ///```
    pub unsafe fn from_shared_memory_region(mem_region: SharedMemoryRegion<T>) -> Self {
        let len = mem_region.len();
        let lamellae = mem_region.lamellae();
        LamellarBuffer {
            data: NonNull::new(Box::into_raw(Box::new(BufferInner::new(mem_region))).into())
                .unwrap(),
            range: 0..len,
            lamellae,
            _phantom: PhantomData,
        }
    }
}

impl<T: Remote> From<SharedMemoryRegion<T>> for LamellarBuffer<T, SharedMemoryRegion<T>> {
    fn from(mem_region: SharedMemoryRegion<T>) -> Self {
        unsafe { LamellarBuffer::from_shared_memory_region(mem_region) }
    }
}

impl<T: Remote> LamellarBuffer<T, OneSidedMemoryRegion<T>> {
    /// Wraps a [`OneSidedMemoryRegion`] as a [`LamellarBuffer`] for use with RDMA get operations.
    ///
    /// Using an RDMA-registered one-sided region as the destination avoids an extra copy compared
    /// to [`from_vec`][LamellarBuffer::<T, Vec<T>>::from_vec].
    ///
    /// # Safety
    /// Multiple [`LamellarBuffer`] handles to the same region can be created. The caller must
    /// ensure that no other reference mutates the memory region while this buffer exists.
    ///
    /// # Examples
    ///```
    /// use lamellar::memregion::prelude::*;
    ///
    /// let world = LamellarWorldBuilder::new().build();
    /// let my_pe = world.my_pe();
    /// let num_pes = world.num_pes();
    ///
    /// let src: OneSidedMemoryRegion<usize> = world.alloc_one_sided_mem_region(num_pes * 10);
    /// let dst: OneSidedMemoryRegion<usize> = world.alloc_one_sided_mem_region(10);
    /// unsafe {
    ///     for (i, elem) in src.as_mut_slice().iter_mut().enumerate() {
    ///         *elem = i;
    ///     }
    ///     let buf = LamellarBuffer::from_one_sided_memory_region(dst);
    ///     src.get_into_buffer(my_pe * 10, buf).block();
    /// }
    ///```
    pub unsafe fn from_one_sided_memory_region(mem_region: OneSidedMemoryRegion<T>) -> Self {
        let len = mem_region.len();
        let lamellae = mem_region.lamellae();
        LamellarBuffer {
            data: NonNull::new(Box::into_raw(Box::new(BufferInner::new(mem_region))).into())
                .unwrap(),
            range: 0..len,
            lamellae,
            _phantom: PhantomData,
        }
    }
}

impl<T: Remote> From<OneSidedMemoryRegion<T>> for LamellarBuffer<T, OneSidedMemoryRegion<T>> {
    fn from(mem_region: OneSidedMemoryRegion<T>) -> Self {
        unsafe { LamellarBuffer::from_one_sided_memory_region(mem_region) }
    }
}

impl<T: Remote> LamellarBuffer<T, CommSlice<T>> {
    /// unsafe because multiple handles to the same memory region can be created,
    /// thus user must ensure that nothing else is mutating the memory region
    /// while this buffer exists
    pub(crate) unsafe fn from_comm_slice(
        comm_slice: CommSlice<T>,
        lamellae: Arc<Lamellae>,
    ) -> Self {
        let len = comm_slice.len();
        trace!(target: "lamellae_debug", "creating LamellarBuffer from CommSlice with len {:?} lamellae cnt: {:?}", len, Arc::strong_count(&lamellae));
        LamellarBuffer {
            data: NonNull::new(Box::into_raw(Box::new(BufferInner::new(comm_slice))).into())
                .unwrap(),
            range: 0..len,
            lamellae,
            _phantom: PhantomData,
        }
    }
}

// impl<T: Remote> From<CommSlice<T>> for LamellarBuffer<T, CommSlice<T>> {
//     fn from(comm_slice: CommSlice<T>) -> Self {
//         unsafe { LamellarBuffer::from_comm_slice(comm_slice) }
//     }
// }

impl<T: Remote> LamellarBuffer<T, Vec<T>> {
    /// Wraps a `Vec<T>` as a [`LamellarBuffer`] for use with RDMA get operations.
    ///
    /// This is the simplest way to create a destination buffer. The [`LamellarBuffer`] takes
    /// ownership of the `Vec`, so no additional safety requirements apply. Retrieve the
    /// `Vec` back with [`try_unwrap`][Self::try_unwrap] or [`async_unwrap`][Self::async_unwrap]
    /// once the transfer is complete.
    ///
    /// # Examples
    ///```
    /// use lamellar::memregion::prelude::*;
    ///
    /// let world = LamellarWorldBuilder::new().build();
    /// let my_pe = world.my_pe();
    /// let num_pes = world.num_pes();
    ///
    /// let mem_region: OneSidedMemoryRegion<usize> = world.alloc_one_sided_mem_region(num_pes * 10);
    /// unsafe {
    ///     for (i, elem) in mem_region.as_mut_slice().iter_mut().enumerate() {
    ///         *elem = i;
    ///     }
    ///     let buf = LamellarBuffer::from_vec(&world, vec![0usize; 10]);
    ///     mem_region.get_into_buffer(my_pe * 10, buf).block();
    /// }
    ///```
    pub fn from_vec<U: Into<IntoLamellarTeam>>(team: U, vec: Vec<T>) -> Self {
        let len = vec.len();
        let lamellae = team.into().team.lamellae.clone();
        LamellarBuffer {
            data: NonNull::new(Box::into_raw(Box::new(BufferInner::new(vec))).into()).unwrap(),
            range: 0..len,
            lamellae,
            _phantom: PhantomData,
        }
    }
    pub(crate) fn from_vec_with_lamellae(vec: Vec<T>, lamellae: Arc<Lamellae>) -> Self {
        let len = vec.len();
        trace!(target: "lamellae_debug", "creating LamellarBuffer from Vec with len {:?} lamellae cnt: {:?}", len, Arc::strong_count(&lamellae));
        LamellarBuffer {
            data: NonNull::new(Box::into_raw(Box::new(BufferInner::new(vec))).into()).unwrap(),
            range: 0..len,
            lamellae,
            _phantom: PhantomData,
        }
    }
}

// impl<T: Remote> From<Vec<T>> for LamellarBuffer<T, Vec<T>> {
//     fn from(vec: Vec<T>) -> Self {
//         LamellarBuffer::from_vec(vec)
//     }
// }

impl<T: Remote, B: AsLamellarBuffer<T>> LamellarBuffer<T, B> {
    /// Returns the number of elements in the (possibly sub-sliced) buffer.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3]);
    /// assert_eq!(buf.len(), 3);
    ///```
    pub fn len(&self) -> usize {
        self.range.end - self.range.start
    }

    /// Returns `true` if the buffer contains no elements.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let buf: LamellarBuffer<usize, Vec<usize>> = LamellarBuffer::from_vec(&world, vec![]);
    /// assert!(buf.is_empty());
    ///```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Splits the buffer at `at`, consuming `self` and returning two sub-buffers that share
    /// the same backing store.
    ///
    /// Both halves must be driven to completion (or dropped) before the backing store is reclaimed.
    ///
    /// # Panics
    /// Panics if `at > self.len()`.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3, 4, 5]);
    /// let (left, right) = buf.split(2);
    /// assert_eq!(left.len(), 2);
    /// assert_eq!(right.len(), 3);
    ///```
    pub fn split(self, at: usize) -> (Self, Self) {
        unsafe {
            self.data
                .as_ref()
                .cnt
                .fetch_add(2, std::sync::atomic::Ordering::SeqCst)
        }; //+ 2 as we technically creating two new buffers and dropping this one
        assert!(at <= self.len());
        let left = LamellarBuffer {
            data: self.data.clone(),
            range: self.range.start..(self.range.start + at),
            lamellae: self.lamellae.clone(),
            _phantom: PhantomData,
        };
        let right = LamellarBuffer {
            data: self.data,
            range: (self.range.start + at)..self.range.end,
            lamellae: self.lamellae.clone(),
            _phantom: PhantomData,
        };
        trace!(target: "lamellae_debug", "split LamellarBuffer at {:?} into left range {:?} and right range {:?} lamellae cnt: {:?}", at, left.range, right.range, Arc::strong_count(&self.lamellae));
        (left, right)
    }

    /// Splits off the tail of this buffer starting at `at`, returning it as a new sub-buffer.
    /// `self` is truncated to `[0, at)` in-place.
    ///
    /// # Panics
    /// Panics if `at > self.len()`.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let mut buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3, 4, 5]);
    /// let tail = buf.split_off(3);
    /// assert_eq!(buf.len(), 3);
    /// assert_eq!(tail.len(), 2);
    ///```
    pub fn split_off(&mut self, at: usize) -> Self {
        unsafe {
            self.data
                .as_ref()
                .cnt
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
        };
        assert!(at <= self.len());

        let right = LamellarBuffer {
            data: self.data,
            range: (self.range.start + at)..self.range.end,
            lamellae: self.lamellae.clone(),
            _phantom: PhantomData,
        };
        self.range = self.range.start..(self.range.start + at);
        trace!(target: "lamellae_debug", "split_off LamellarBuffer at {:?} into left range {:?} and right range {:?} lamellae cnt: {:?}", at, self.range, right.range, Arc::strong_count(&self.lamellae));
        right
    }

    /// Attempts to reclaim ownership of the backing store.
    ///
    /// Succeeds (returns `Ok(B)`) when this is the sole remaining reference; otherwise
    /// returns `Err(self)` so the caller can retry.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3]);
    /// match buf.try_unwrap() {
    ///     Ok(vec) => println!("Reclaimed Vec: {:?}", vec),
    ///     Err(buf) => println!("Other references exist"),
    /// }
    ///```
    pub fn try_unwrap(self) -> Result<B, Self> {
        if unsafe {
            self.data
                .as_ref()
                .cnt
                .load(std::sync::atomic::Ordering::SeqCst)
        } == 1
        {
            let mut this = std::mem::ManuallyDrop::new(self);
            let data = unsafe { Box::from_raw(this.data.as_ptr()) };
            // drop Arc<Lamellae> that ManuallyDrop suppresses
            unsafe { std::ptr::drop_in_place(&mut this.lamellae as *mut Arc<Lamellae>) };
            Ok(data.data)
        } else {
            Err(self)
        }
    }

    /// Asynchronously waits until this is the sole remaining reference, then reclaims the
    /// backing store.
    ///
    /// Yields via `async_std::task::yield_now` while other references exist.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// async fn example() {
    ///     let world = LamellarWorldBuilder::new().build();
    ///     let buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3]);
    ///     let vec = buf.async_unwrap().await;
    ///     println!("Unwrapped: {:?}", vec);
    /// }
    ///```
    pub async fn async_unwrap(self) -> B {
        while unsafe {
            self.data
                .as_ref()
                .cnt
                .load(std::sync::atomic::Ordering::SeqCst)
        } != 1
        {
            // println!("Waiting to unwrap LamellarBuffer: {:?}", self);
            trace!("Waiting to unwrap LamellarBuffer: {:?}", self);
            async_std::task::yield_now().await;
        }
        let mut this = std::mem::ManuallyDrop::new(self);
        let data = unsafe { Box::from_raw(this.data.as_ptr()) };
        trace!(target: "lamellae_debug", "successfully unwrapped LamellarBuffer, lamellae cnt: {:?}", Arc::strong_count(&this.lamellae));
        // drop Arc<Lamellae> that ManuallyDrop suppresses
        unsafe { std::ptr::drop_in_place(&mut this.lamellae as *mut Arc<Lamellae>) };
        data.data
    }

    /// Resets the active slice window back to the full backing buffer, if this is the sole
    /// remaining reference.
    ///
    /// Returns `true` on success; `false` if other references still exist.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let mut buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3, 4, 5]);
    /// let _tail = buf.split_off(2);
    /// // After split, window is [0, 2); try_reset returns false due to other reference
    /// assert_eq!(buf.try_reset(), false);
    ///```
    pub fn try_reset(&mut self) -> bool {
        if unsafe {
            self.data
                .as_ref()
                .cnt
                .load(std::sync::atomic::Ordering::SeqCst)
        } == 1
        {
            let len = self.as_slice().len();
            self.range = 0..len;
            true
        } else {
            false
        }
    }

    /// Returns a shared slice of the active window of the buffer.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3]);
    /// let slice = buf.as_slice();
    /// assert_eq!(slice.len(), 3);
    ///```
    pub fn as_slice(&self) -> &[T] {
        unsafe { &self.data.as_ref().data.as_slice()[self.range.clone()] }
    }

    /// Returns a mutable slice of the active window of the buffer.
    ///
    /// # Examples
    ///```no_run
    /// use lamellar::memregion::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let mut buf = LamellarBuffer::from_vec(&world, vec![1, 2, 3]);
    /// let slice = buf.as_mut_slice();
    /// slice[0] = 42;
    ///```
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { &mut self.data.as_mut().data.as_mut_slice()[self.range.clone()] }
    }

    #[allow(dead_code)]
    pub(crate) unsafe fn orig_as_slice(&self) -> &[T] {
        unsafe { self.data.as_ref().data.as_slice() }
    }
    #[allow(dead_code)]
    pub(crate) unsafe fn orig_as_casted_slice<P>(&self) -> &[P] {
        unsafe {
            std::slice::from_raw_parts(
                self.data.as_ref().data.as_slice().as_ptr() as *const P,
                self.data.as_ref().data.as_slice().len() * std::mem::size_of::<T>()
                    / std::mem::size_of::<P>(),
            )
        }
    }

    #[allow(dead_code)]
    pub(crate) unsafe fn orig_as_ptr(&self) -> *const T {
        unsafe { self.data.as_ref().data.as_slice().as_ptr() as *const T }
    }

    #[allow(dead_code)]
    pub(crate) unsafe fn orig_as_casted_ptr<P>(&self) -> *const P {
        unsafe { self.data.as_ref().data.as_slice().as_ptr() as *const P }
    }

    #[allow(dead_code)]
    pub(crate) fn orig_num_bytes(&self) -> usize {
        unsafe { &self.data.as_ref().data.as_slice().len() * std::mem::size_of::<T>() }
    }
}

impl<T: Remote, B: AsLamellarBuffer<T>> Drop for LamellarBuffer<T, B> {
    fn drop(&mut self) {
        trace!(target: "drop", "begin drop LamellarBuffer");
        // println!("LamellarBuffer dropped: {:?}", self);
        if unsafe {
            self.data
                .as_ref()
                .cnt
                .fetch_sub(1, std::sync::atomic::Ordering::SeqCst)
        } == 1
        {
            // ensure all pending RDMA operations using this buffer are flushed before we drop the backing store
            self.lamellae.comm().flush_all();
            unsafe {
                let _ = Box::from_raw(self.data.as_ptr());
            }
        }
        trace!(target: "lamellae_debug", "end drop LamellarBuffer lamellae cnt: {:?}", Arc::strong_count(&self.lamellae));
    }
}