mesh-sieve 4.0.1

Modular, high-performance Rust library for mesh and data management, designed for scientific computing and PDE codes.
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! Communication abstraction for intra-process (Rayon) and inter-process (MPI) message passing.
//!
//! Wire format conventions (for higher-level protocols):
//! - All integers are LE fixed width (u32 counts/tags/ranks, u64 IDs).
//! - Structs are #[repr(C)] and bytemuck::Pod-safe; no #[repr(packed)].
//! - Receivers may truncate to their provided buffer length; higher layers must
//!   exchange sizes first if exact lengths are required.

use once_cell::sync::Lazy;
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Condvar, Mutex};

use crate::mesh_error::{CommError, MeshSieveError};

/// Anything that can be waited on.
pub trait Wait {
    /// Wait for completion and return the received data (if any).
    fn wait(self) -> Option<Vec<u8>>;
}

/// Non-blocking completion test.
pub trait PollWait {
    /// Return `Some(bytes)` if the operation has completed, otherwise `None`.
    fn try_wait(&mut self) -> Option<Vec<u8>>;
}

/// Non-blocking communication interface (minimal by design).
///
/// Implementors provide asynchronous send/receive operations and waitable handles.
pub trait Communicator: Send + Sync + 'static {
    /// Handle returned by `isend`.
    type SendHandle: Wait;
    /// Handle returned by `irecv`.
    type RecvHandle: Wait;

    fn isend(&self, peer: usize, tag: u16, buf: &[u8]) -> Self::SendHandle;
    fn irecv(&self, peer: usize, tag: u16, buf: &mut [u8]) -> Self::RecvHandle;

    /// Fallible send initiation for backends that can error.
    fn isend_result(
        &self,
        peer: usize,
        tag: u16,
        buf: &[u8],
    ) -> Result<Self::SendHandle, MeshSieveError> {
        Ok(self.isend(peer, tag, buf))
    }

    /// Fallible receive initiation for backends that can error.
    fn irecv_result(
        &self,
        peer: usize,
        tag: u16,
        buf: &mut [u8],
    ) -> Result<Self::RecvHandle, MeshSieveError> {
        Ok(self.irecv(peer, tag, buf))
    }

    /// Returns true if this communicator is NoComm (for test logic)
    fn is_no_comm(&self) -> bool {
        false
    }

    /// Rank of this process (0..size-1)
    fn rank(&self) -> usize;
    /// Total number of ranks
    fn size(&self) -> usize;

    /// Synchronization barrier (default: no-op for non-MPI comms)
    fn barrier(&self) {}

    /// Fallible barrier for backends that can error.
    fn barrier_result(&self) -> Result<(), MeshSieveError> {
        self.barrier();
        Ok(())
    }

    /// Broadcast a byte buffer from `root` to all ranks.
    fn broadcast(&self, root: usize, buf: &mut [u8]) {
        if self.size() <= 1 {
            return;
        }
        if self.rank() == root {
            let mut sends = Vec::with_capacity(self.size().saturating_sub(1));
            for peer in 0..self.size() {
                if peer != root {
                    sends.push(self.isend(peer, COLLECTIVE_TAG_BROADCAST, buf));
                }
            }
            for send in sends {
                let _ = send.wait();
            }
        } else {
            let mut tmp = vec![0u8; buf.len()];
            let recv = self.irecv(root, COLLECTIVE_TAG_BROADCAST, &mut tmp);
            if let Some(data) = recv.wait() {
                let copy_len = buf.len().min(data.len());
                buf[..copy_len].copy_from_slice(&data[..copy_len]);
            }
        }
    }

    /// Fallible broadcast for backends that can error.
    fn broadcast_result(&self, root: usize, buf: &mut [u8]) -> Result<(), MeshSieveError> {
        self.broadcast(root, buf);
        Ok(())
    }

    /// All-reduce sum for `u64` buffers.
    fn allreduce_sum(&self, values: &mut [u64]) {
        let size = self.size();
        if size <= 1 {
            return;
        }
        let root = 0;
        let encoded = encode_u64_le(values);
        if self.rank() == root {
            let mut accum = values.to_vec();
            let mut recvs = Vec::with_capacity(size.saturating_sub(1));
            for peer in 0..size {
                if peer != root {
                    let mut tmp = vec![0u8; encoded.len()];
                    let handle = self.irecv(peer, COLLECTIVE_TAG_ALLREDUCE_GATHER, &mut tmp);
                    recvs.push(handle);
                }
            }
            for recv in recvs {
                if let Some(data) = recv.wait() {
                    add_u64_le(&data, &mut accum);
                }
            }
            values.copy_from_slice(&accum);
            let out_bytes = encode_u64_le(values);
            let mut sends = Vec::with_capacity(size.saturating_sub(1));
            for peer in 0..size {
                if peer != root {
                    sends.push(self.isend(peer, COLLECTIVE_TAG_ALLREDUCE_BROADCAST, &out_bytes));
                }
            }
            for send in sends {
                let _ = send.wait();
            }
        } else {
            let send = self.isend(root, COLLECTIVE_TAG_ALLREDUCE_GATHER, &encoded);
            let mut tmp = vec![0u8; encoded.len()];
            let recv = self.irecv(root, COLLECTIVE_TAG_ALLREDUCE_BROADCAST, &mut tmp);
            let _ = send.wait();
            if let Some(data) = recv.wait() {
                decode_u64_le(&data, values);
            }
        }
    }

    /// Fallible all-reduce for backends that can error.
    fn allreduce_sum_result(&self, values: &mut [u64]) -> Result<(), MeshSieveError> {
        self.allreduce_sum(values);
        Ok(())
    }

    /// All-gather fixed-size byte buffers into `recvbuf` (rank-major order).
    fn allgather(&self, sendbuf: &[u8], recvbuf: &mut [u8]) {
        let size = self.size();
        let chunk = sendbuf.len();
        if size == 0 || chunk == 0 {
            return;
        }
        assert_eq!(
            recvbuf.len(),
            size * chunk,
            "recvbuf must be size * sendbuf.len()"
        );
        let rank = self.rank();
        let start = rank * chunk;
        recvbuf[start..start + chunk].copy_from_slice(sendbuf);
        if size <= 1 {
            return;
        }
        let mut sends = Vec::with_capacity(size.saturating_sub(1));
        let mut recvs = Vec::with_capacity(size.saturating_sub(1));
        for peer in 0..size {
            if peer == rank {
                continue;
            }
            sends.push(self.isend(peer, COLLECTIVE_TAG_ALLGATHER, sendbuf));
            let mut tmp = vec![0u8; chunk];
            let recv = self.irecv(peer, COLLECTIVE_TAG_ALLGATHER, &mut tmp);
            recvs.push((peer, recv));
        }
        for send in sends {
            let _ = send.wait();
        }
        for (peer, recv) in recvs {
            if let Some(data) = recv.wait() {
                let offset = peer * chunk;
                assert_eq!(
                    data.len(),
                    chunk,
                    "allgather received unexpected buffer length"
                );
                recvbuf[offset..offset + chunk].copy_from_slice(&data);
            }
        }
    }

    /// Fallible all-gather for backends that can error.
    fn allgather_result(&self, sendbuf: &[u8], recvbuf: &mut [u8]) -> Result<(), MeshSieveError> {
        self.allgather(sendbuf, recvbuf);
        Ok(())
    }

    /// Reserve a contiguous range of tags for this communicator.
    ///
    /// Tags in `[RESERVED_TAGS_START, u16::MAX]` are reserved for collectives and
    /// must not be used by higher-level protocols.
    fn reserve_tag_range(&self, n: u16) -> Result<CommTag, MeshSieveError> {
        reserve_tag_range(n)
    }
}

/// Tags in this range are reserved for collectives and internal protocols.
pub const RESERVED_TAGS_START: u16 = COLLECTIVE_TAG_ALLREDUCE_BROADCAST;

const COLLECTIVE_TAG_BROADCAST: u16 = u16::MAX - 1;
const COLLECTIVE_TAG_ALLGATHER: u16 = u16::MAX - 2;
const COLLECTIVE_TAG_ALLREDUCE_GATHER: u16 = u16::MAX - 3;
const COLLECTIVE_TAG_ALLREDUCE_BROADCAST: u16 = u16::MAX - 4;

static NEXT_TAG: AtomicU32 = AtomicU32::new(0);

fn reserve_tag_range(n: u16) -> Result<CommTag, MeshSieveError> {
    if n == 0 {
        return Err(MeshSieveError::Communication(CommError(
            "tag allocation requires n > 0".into(),
        )));
    }
    let n = n as u32;
    let limit = RESERVED_TAGS_START as u32;
    let mut current = NEXT_TAG.load(Ordering::Relaxed);
    loop {
        let end = current + n - 1;
        if end >= limit {
            return Err(MeshSieveError::Communication(CommError(
                "tag allocation exhausted available user tag range".into(),
            )));
        }
        match NEXT_TAG.compare_exchange(current, end + 1, Ordering::SeqCst, Ordering::Relaxed) {
            Ok(_) => return Ok(CommTag::new(current as u16)),
            Err(next) => current = next,
        }
    }
}

fn encode_u64_le(values: &[u64]) -> Vec<u8> {
    let mut out = Vec::with_capacity(std::mem::size_of_val(values));
    for value in values {
        out.extend_from_slice(&value.to_le_bytes());
    }
    out
}

fn decode_u64_le(bytes: &[u8], out: &mut [u64]) {
    debug_assert_eq!(bytes.len(), std::mem::size_of_val(out));
    for (chunk, slot) in bytes
        .chunks_exact(core::mem::size_of::<u64>())
        .zip(out.iter_mut())
    {
        let mut raw = [0u8; 8];
        raw.copy_from_slice(chunk);
        *slot = u64::from_le_bytes(raw);
    }
}

fn add_u64_le(bytes: &[u8], accum: &mut [u64]) {
    debug_assert_eq!(bytes.len(), std::mem::size_of_val(accum));
    for (chunk, slot) in bytes
        .chunks_exact(core::mem::size_of::<u64>())
        .zip(accum.iter_mut())
    {
        let mut raw = [0u8; 8];
        raw.copy_from_slice(chunk);
        *slot += u64::from_le_bytes(raw);
    }
}

/// Tag newtype for safer tag arithmetic.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct CommTag(u16);

impl CommTag {
    /// Create a new tag from a raw `u16`.
    #[inline]
    pub const fn new(tag: u16) -> Self {
        Self(tag)
    }

    /// Return the underlying `u16` value.
    #[inline]
    pub const fn as_u16(self) -> u16 {
        self.0
    }

    /// Safely offset the tag by `dx`, wrapping on overflow.
    #[inline]
    pub const fn offset(self, dx: u16) -> Self {
        Self(self.0.wrapping_add(dx))
    }
}

impl From<u16> for CommTag {
    #[inline]
    fn from(x: u16) -> Self {
        CommTag::new(x)
    }
}

/// Convenience bundle of tags for the multi-phase section completion.
#[derive(Copy, Clone, Debug)]
pub struct SectionCommTags {
    /// Tag used during the size-exchange phase.
    pub sizes: CommTag,
    /// Tag used during the data-exchange phase.
    pub data: CommTag,
}

impl SectionCommTags {
    /// Construct tags from a base, assigning deterministic offsets per phase.
    #[inline]
    pub const fn from_base(base: CommTag) -> Self {
        Self {
            sizes: base,
            data: base.offset(1),
        }
    }
}

/// Convenience bundle of tags for sieve completion.
#[derive(Copy, Clone, Debug)]
pub struct SieveCommTags {
    /// Tag used during the size exchange phase.
    pub sizes: CommTag,
    /// Tag used during the data exchange phase.
    pub data: CommTag,
}

impl SieveCommTags {
    /// Construct tags from a base, assigning deterministic offsets per phase.
    #[inline]
    pub const fn from_base(base: CommTag) -> Self {
        Self {
            sizes: base,
            data: base.offset(1),
        }
    }
}

/// Convenience bundle of tags for stack completion.
#[derive(Copy, Clone, Debug)]
pub struct StackCommTags {
    /// Tag used during the size exchange phase.
    pub sizes: CommTag,
    /// Tag used during the data exchange phase.
    pub data: CommTag,
}

impl StackCommTags {
    /// Construct tags from a base, assigning deterministic offsets per phase.
    #[inline]
    pub const fn from_base(base: CommTag) -> Self {
        Self {
            sizes: base,
            data: base.offset(1),
        }
    }
}

/// Compile-time no-op comm for pure serial unit tests.
#[derive(Clone, Debug, Default)]
pub struct NoComm;

impl Wait for () {
    fn wait(self) -> Option<Vec<u8>> {
        None
    }
}

impl PollWait for () {
    fn try_wait(&mut self) -> Option<Vec<u8>> {
        None
    }
}

impl Communicator for NoComm {
    type SendHandle = ();
    type RecvHandle = ();

    fn isend(&self, _peer: usize, _tag: u16, _buf: &[u8]) {}

    fn irecv(&self, _peer: usize, _tag: u16, _buf: &mut [u8]) {}

    fn is_no_comm(&self) -> bool {
        true
    }

    fn rank(&self) -> usize {
        0
    }

    fn size(&self) -> usize {
        1
    }
}

// --- RayonComm: intra-process / multi-thread ---

type Key = (usize, usize, u16); // (src, dst, tag)

#[derive(Default)]
struct Slot {
    q: VecDeque<Vec<u8>>,
}

struct Mailbox {
    map: Mutex<HashMap<Key, Arc<(Mutex<Slot>, Condvar)>>>,
}

static MAILBOX: Lazy<Mailbox> = Lazy::new(|| Mailbox {
    map: Mutex::new(HashMap::new()),
});

fn mailbox_entry(key: Key) -> Arc<(Mutex<Slot>, Condvar)> {
    let mut g = MAILBOX.map.lock().expect("MAILBOX poisoned");
    g.entry(key)
        .or_insert_with(|| Arc::new((Mutex::new(Slot::default()), Condvar::new())))
        .clone()
}

pub struct LocalSendHandle;

impl Wait for LocalSendHandle {
    fn wait(self) -> Option<Vec<u8>> {
        None
    }
}

impl PollWait for LocalSendHandle {
    fn try_wait(&mut self) -> Option<Vec<u8>> {
        None
    }
}

pub struct LocalRecvHandle {
    cell: Arc<(Mutex<Slot>, Condvar)>,
    want_len: usize,
}

impl Wait for LocalRecvHandle {
    fn wait(self) -> Option<Vec<u8>> {
        let (lock, cv) = &*self.cell;
        let mut slot = lock.lock().expect("Slot poisoned");
        while slot.q.is_empty() {
            slot = cv.wait(slot).expect("Condvar poisoned");
        }
        let mut msg = slot.q.pop_front().expect("q non-empty");
        msg.truncate(self.want_len.min(msg.len()));
        Some(msg)
    }
}

impl PollWait for LocalRecvHandle {
    fn try_wait(&mut self) -> Option<Vec<u8>> {
        let (lock, _cv) = &*self.cell;
        let mut slot = lock.lock().expect("Slot poisoned");
        if slot.q.is_empty() {
            None
        } else {
            let mut msg = slot.q.pop_front().expect("q non-empty");
            msg.truncate(self.want_len.min(msg.len()));
            Some(msg)
        }
    }
}

#[derive(Clone, Debug)]
pub struct RayonComm {
    rank: usize,
    size: usize,
}

impl RayonComm {
    pub fn new(rank: usize, size: usize) -> Self {
        Self { rank, size }
    }
}

impl Communicator for RayonComm {
    type SendHandle = LocalSendHandle;
    type RecvHandle = LocalRecvHandle;

    fn isend(&self, peer: usize, tag: u16, buf: &[u8]) -> Self::SendHandle {
        let key = (self.rank, peer, tag);
        let entry = mailbox_entry(key);
        let (lock, cv) = &*entry;
        {
            let mut slot = lock.lock().expect("Slot poisoned");
            slot.q.push_back(buf.to_vec());
        }
        cv.notify_all();
        LocalSendHandle
    }

    fn irecv(&self, peer: usize, tag: u16, buf: &mut [u8]) -> Self::RecvHandle {
        let key = (peer, self.rank, tag);
        LocalRecvHandle {
            cell: mailbox_entry(key),
            want_len: buf.len(),
        }
    }

    fn rank(&self) -> usize {
        self.rank
    }

    fn size(&self) -> usize {
        self.size
    }

    fn barrier(&self) {
        #[cfg(test)]
        {
            test_barrier::set_size(self.size);
            test_barrier::wait();
        }
    }
}

// Optional test barrier for deterministic multi-thread tests.
#[cfg(test)]
mod test_barrier {
    use once_cell::sync::Lazy;
    use std::sync::{Condvar, Mutex};

    pub struct EpochBarrier {
        size: usize,
        arrived: usize,
        epoch: usize,
    }

    static BARRIER: Lazy<(Mutex<EpochBarrier>, Condvar)> = Lazy::new(|| {
        (
            Mutex::new(EpochBarrier {
                size: 1,
                arrived: 0,
                epoch: 0,
            }),
            Condvar::new(),
        )
    });

    pub fn set_size(size: usize) {
        let (lock, _) = &*BARRIER;
        let mut b = lock.lock().unwrap();
        b.size = size;
    }

    pub fn wait() {
        let (lock, cv) = &*BARRIER;
        let mut b = lock.lock().unwrap();
        let e = b.epoch;
        b.arrived += 1;
        if b.arrived == b.size {
            b.arrived = 0;
            b.epoch += 1;
            cv.notify_all();
        } else {
            while e == b.epoch {
                b = cv.wait(b).unwrap();
            }
        }
    }
}

// --- MPI backend ---
#[cfg(feature = "mpi-support")]
mod mpi_backend {
    use super::*;
    use crate::mesh_error::{CommError, MeshSieveError};
    use core::ptr::NonNull;
    use mpi::collective::{CommunicatorCollectives, Root, SystemOperation};
    use mpi::environment::Universe;
    use mpi::point_to_point::{Destination, Source};
    use mpi::topology::{Communicator as _, SimpleCommunicator};

    pub struct MpiComm {
        _universe: Universe,
        pub world: SimpleCommunicator,
        rank: usize,
        size: usize,
    }

    unsafe impl Send for MpiComm {}
    unsafe impl Sync for MpiComm {}

    impl MpiComm {
        pub fn new() -> Result<Self, MeshSieveError> {
            let uni = mpi::initialize().ok_or_else(|| {
                MeshSieveError::Communication(CommError("MPI initialization failed".to_string()))
            })?;
            let world = uni.world();
            let rank = world.rank() as usize;
            let size = world.size() as usize;
            Ok(Self {
                _universe: uni,
                world,
                rank,
                size,
            })
        }
    }

    impl Default for MpiComm {
        fn default() -> Self {
            Self::new().expect("MPI initialization failed")
        }
    }

    impl Communicator for MpiComm {
        type SendHandle = MpiSendHandle;
        type RecvHandle = MpiRecvHandle;

        fn isend(&self, peer: usize, tag: u16, buf: &[u8]) -> Self::SendHandle {
            use mpi::request::StaticScope;
            let boxed = buf.to_vec().into_boxed_slice();
            let raw: *mut [u8] = Box::into_raw(boxed);
            let slice: &[u8] = unsafe { &*raw };
            let req = self
                .world
                .process_at_rank(peer as i32)
                .immediate_send_with_tag(StaticScope, slice, tag as i32);
            MpiSendHandle {
                req: Some(req),
                buf: Some(unsafe { NonNull::new_unchecked(raw) }),
            }
        }

        fn irecv(&self, peer: usize, tag: u16, template: &mut [u8]) -> Self::RecvHandle {
            use mpi::request::StaticScope;
            let len = template.len();
            let boxed = vec![0u8; len].into_boxed_slice();
            let raw: *mut [u8] = Box::into_raw(boxed);
            let slice_mut: &mut [u8] = unsafe { &mut *raw };
            let req = self
                .world
                .process_at_rank(peer as i32)
                .immediate_receive_into_with_tag(StaticScope, slice_mut, tag as i32);
            MpiRecvHandle {
                req: Some(req),
                buf: Some(unsafe { NonNull::new_unchecked(raw) }),
                len,
            }
        }

        fn rank(&self) -> usize {
            self.rank
        }
        fn size(&self) -> usize {
            self.size
        }
        fn barrier(&self) {
            self.world.barrier();
        }

        fn broadcast(&self, root: usize, buf: &mut [u8]) {
            self.world.process_at_rank(root as i32).broadcast_into(buf);
        }

        fn allreduce_sum(&self, values: &mut [u64]) {
            let mut out = vec![0u64; values.len()];
            self.world
                .all_reduce_into(values, &mut out, SystemOperation::sum());
            values.copy_from_slice(&out);
        }

        fn allgather(&self, sendbuf: &[u8], recvbuf: &mut [u8]) {
            self.world.all_gather_into(sendbuf, recvbuf);
        }
    }

    pub struct MpiSendHandle {
        req: Option<mpi::request::Request<'static, [u8], mpi::request::StaticScope>>,
        buf: Option<NonNull<[u8]>>,
    }
    impl Wait for MpiSendHandle {
        fn wait(mut self) -> Option<Vec<u8>> {
            if let Some(r) = self.req.take() {
                let _ = r.wait();
            }
            if let Some(ptr) = self.buf.take() {
                unsafe {
                    drop(Box::from_raw(ptr.as_ptr()));
                }
            }
            None
        }
    }
    impl Drop for MpiSendHandle {
        fn drop(&mut self) {
            if let Some(r) = self.req.take() {
                let _ = r.wait();
            }
            if let Some(ptr) = self.buf.take() {
                unsafe {
                    drop(Box::from_raw(ptr.as_ptr()));
                }
            }
        }
    }

    pub struct MpiRecvHandle {
        req: Option<mpi::request::Request<'static, [u8], mpi::request::StaticScope>>,
        buf: Option<NonNull<[u8]>>,
        len: usize,
    }
    impl Wait for MpiRecvHandle {
        fn wait(mut self) -> Option<Vec<u8>> {
            if let Some(r) = self.req.take() {
                let _ = r.wait();
            }
            let ptr = self.buf.take().expect("buffer missing");
            let boxed: Box<[u8]> = unsafe { Box::from_raw(ptr.as_ptr()) };
            let mut v = Vec::from(boxed);
            v.truncate(self.len);
            Some(v)
        }
    }
    impl Drop for MpiRecvHandle {
        fn drop(&mut self) {
            if let Some(r) = self.req.take() {
                let _ = r.wait();
            }
            if let Some(ptr) = self.buf.take() {
                unsafe {
                    drop(Box::from_raw(ptr.as_ptr()));
                }
            }
        }
    }
}

#[cfg(feature = "mpi-support")]
pub use mpi_backend::MpiComm;