mpi-rs 0.1.0

A pure-Rust implementation of the Message Passing Interface (MPI), API-compatible with rsmpi. No C library required.
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
//! Non-blocking request handling. Mirrors `mpi::request` in rsmpi: [`Request`]
//! borrows the buffer involved in an in-flight operation for the lifetime of
//! the request, [`Scope`] bounds that borrow, and [`WaitGuard`] / [`CancelGuard`]
//! complete a request when they go out of scope.
//!
//! As in rsmpi, **dropping an in-flight [`Request`] panics** — a request must be
//! consumed by `wait`, `test` or `cancel`, or handed to a guard.

use std::marker::PhantomData;

use crate::point_to_point::Status;
use crate::transport;
use crate::{Count, Rank, Tag};

/// A scope that bounds the lifetime of the buffers borrowed by requests. Safe
/// scopes are [`StaticScope`] (`'static`) and the [`LocalScope`] handed to the
/// closure passed to [`scope`].
///
/// # Safety
///
/// Implementors guarantee that any buffer associated with a request created in
/// this scope outlives the scope.
pub unsafe trait Scope<'a> {}

/// The scope of the entire program (`'static`).
#[derive(Clone, Copy, Debug)]
pub struct StaticScope;

// SAFETY: `'static` data outlives everything.
unsafe impl Scope<'static> for StaticScope {}

/// A dynamically-bounded scope created by [`scope`].
pub struct LocalScope<'a> {
    _invariant: PhantomData<std::cell::Cell<&'a ()>>,
}

// SAFETY: requests created with `&LocalScope<'a>` cannot outlive `'a`, and the
// buffers they borrow are constrained to outlive `'a` by the borrow checker.
unsafe impl<'a> Scope<'a> for &LocalScope<'a> {}

/// Open a request scope. Requests created inside `f` may borrow buffers that
/// outlive the scope; the borrow checker forbids letting a request escape.
pub fn scope<'a, F, R>(f: F) -> R
where
    F: FnOnce(&LocalScope<'a>) -> R,
{
    let scope = LocalScope {
        _invariant: PhantomData,
    };
    f(&scope)
}

/// Complete a pending receive: block for the matching message and copy up to
/// `len` bytes into `ptr`. Free function so it can be used from `Drop` impls
/// (which cannot carry the `Scope` bound).
fn complete_recv(ctx: u32, source: Rank, tag: Tag, ptr: *mut u8, len: usize) -> Status {
    let (src, t, count, _dt, payload) = transport::runtime().recv(ctx, source, tag);
    let n = len.min(payload.len());
    // SAFETY: `ptr` points at a live buffer of at least `len` bytes that is
    // exclusively borrowed for `'a` (which outlives this request).
    unsafe {
        std::ptr::copy_nonoverlapping(payload.as_ptr(), ptr, n);
    }
    Status::new(src, t, count as Count, payload.len())
}

enum State {
    /// The operation has already completed (e.g. an eager send).
    Completed { status: Status },
    /// A receive that has not yet been matched; completed on `wait`/`test`.
    PendingRecv {
        ctx: u32,
        source: Rank,
        tag: Tag,
        ptr: *mut u8,
        len: usize,
    },
    /// An operation progressing on a background thread (a truly-async
    /// collective); completed by joining the thread.
    PendingJoin {
        handle: Option<std::thread::JoinHandle<()>>,
    },
    /// The request has been consumed by `wait`/`test`/`cancel`.
    Consumed,
}

/// A handle to a non-blocking operation, borrowing its buffer for `'a`.
///
/// Type parameters mirror rsmpi: `D` is the buffer type and `S` the [`Scope`].
pub struct Request<'a, D: ?Sized = [u8], S = StaticScope> {
    state: State,
    // Ties the request to the lifetime of the borrowed buffer.
    _life: PhantomData<&'a mut ()>,
    _data: PhantomData<*mut D>,
    _scope: PhantomData<S>,
}

impl<'a, D: ?Sized, S: Scope<'a>> Request<'a, D, S> {
    /// A request for an operation that has already completed.
    pub(crate) fn completed(_scope: S) -> Request<'a, D, S> {
        Request {
            state: State::Completed {
                status: Status::new(0, 0, 0, 0),
            },
            _life: PhantomData,
            _data: PhantomData,
            _scope: PhantomData,
        }
    }

    /// A request for a receive that will be matched when completed.
    pub(crate) fn pending_recv(
        _scope: S,
        ptr: *mut u8,
        len: usize,
        ctx: u32,
        source: Rank,
        tag: Tag,
    ) -> Request<'a, D, S> {
        Request {
            state: State::PendingRecv {
                ctx,
                source,
                tag,
                ptr,
                len,
            },
            _life: PhantomData,
            _data: PhantomData,
            _scope: PhantomData,
        }
    }

    /// A request for a collective progressing on a background thread; completed
    /// by joining that thread (a truly-async non-blocking collective).
    pub(crate) fn from_join(_scope: S, handle: std::thread::JoinHandle<()>) -> Request<'a, D, S> {
        Request {
            state: State::PendingJoin {
                handle: Some(handle),
            },
            _life: PhantomData,
            _data: PhantomData,
            _scope: PhantomData,
        }
    }

    /// Whether the request can complete without blocking.
    fn ready(&self) -> bool {
        match &self.state {
            State::Completed { .. } => true,
            State::Consumed => true,
            State::PendingRecv {
                ctx, source, tag, ..
            } => transport::runtime().probe(*ctx, *source, *tag).is_some(),
            State::PendingJoin { handle } => {
                handle.as_ref().map(|h| h.is_finished()).unwrap_or(true)
            }
        }
    }

    /// Wait for the operation to complete (`MPI_Wait`), returning its status.
    pub fn wait(mut self) -> Status {
        let state = std::mem::replace(&mut self.state, State::Consumed);
        match state {
            State::Completed { status } => status,
            State::PendingRecv {
                ctx,
                source,
                tag,
                ptr,
                len,
            } => complete_recv(ctx, source, tag, ptr, len),
            State::PendingJoin { mut handle } => {
                if let Some(h) = handle.take() {
                    let _ = h.join();
                }
                Status::new(0, 0, 0, 0)
            }
            State::Consumed => unreachable!("request already consumed"),
        }
    }

    /// Wait for completion, discarding the status.
    pub fn wait_without_status(self) {
        let _ = self.wait();
    }

    /// Test for completion without blocking (`MPI_Test`). Returns `Ok(status)`
    /// if complete, otherwise `Err(self)` so the request can be retried.
    pub fn test(mut self) -> Result<Status, Request<'a, D, S>> {
        let is_ready = self.ready();
        if !is_ready {
            return Err(self);
        }
        let state = std::mem::replace(&mut self.state, State::Consumed);
        let status = match state {
            State::Completed { status } => status,
            State::PendingRecv {
                ctx,
                source,
                tag,
                ptr,
                len,
            } => complete_recv(ctx, source, tag, ptr, len),
            State::PendingJoin { mut handle } => {
                if let Some(h) = handle.take() {
                    let _ = h.join();
                }
                Status::new(0, 0, 0, 0)
            }
            State::Consumed => unreachable!(),
        };
        Ok(status)
    }

    /// Cancel the operation (`MPI_Cancel`). Because incoming messages are
    /// already buffered by the transport, this simply consumes the request.
    pub fn cancel(mut self) {
        self.state = State::Consumed;
    }
}

impl<D: ?Sized, S> Drop for Request<'_, D, S> {
    fn drop(&mut self) {
        match &mut self.state {
            // A background collective must be joined before its borrowed buffers
            // go out of scope, so completing it on drop is required for soundness
            // (not a misuse to warn about).
            State::PendingJoin { handle } => {
                if let Some(h) = handle.take() {
                    let _ = h.join();
                }
            }
            State::Completed { .. } | State::PendingRecv { .. } => {
                if !std::thread::panicking() {
                    panic!(
                        "an in-flight mpi::request::Request was dropped; complete it with \
                         wait()/test()/cancel() or hold it in a WaitGuard"
                    );
                }
            }
            State::Consumed => {}
        }
    }
}

/// Waits on the contained [`Request`] when dropped (`RAII` completion of a
/// send). Mirrors rsmpi's `WaitGuard`.
pub struct WaitGuard<'a, D: ?Sized = [u8], S = StaticScope>(Option<Request<'a, D, S>>);

impl<'a, D: ?Sized, S: Scope<'a>> From<Request<'a, D, S>> for WaitGuard<'a, D, S> {
    fn from(r: Request<'a, D, S>) -> Self {
        WaitGuard(Some(r))
    }
}

impl<'a, D: ?Sized, S: Scope<'a>> WaitGuard<'a, D, S> {
    /// Explicitly wait, returning the status.
    pub fn wait(mut self) -> Status {
        self.0.take().unwrap().wait()
    }
}

impl<D: ?Sized, S> Drop for WaitGuard<'_, D, S> {
    fn drop(&mut self) {
        if let Some(r) = self.0.take() {
            // Reconstruct the wait path without the Request's own Drop guard.
            let mut r = std::mem::ManuallyDrop::new(r);
            let state = std::mem::replace(&mut r.state, State::Consumed);
            match state {
                State::PendingRecv {
                    ctx,
                    source,
                    tag,
                    ptr,
                    len,
                } => {
                    let _ = complete_recv(ctx, source, tag, ptr, len);
                }
                State::PendingJoin { mut handle } => {
                    if let Some(h) = handle.take() {
                        let _ = h.join();
                    }
                }
                State::Completed { .. } | State::Consumed => {}
            }
        }
    }
}

/// Cancels (then completes) the contained request when dropped. Mirrors
/// rsmpi's `CancelGuard`.
pub struct CancelGuard<'a, D: ?Sized = [u8], S = StaticScope>(Option<Request<'a, D, S>>);

impl<'a, D: ?Sized, S: Scope<'a>> From<Request<'a, D, S>> for CancelGuard<'a, D, S> {
    fn from(r: Request<'a, D, S>) -> Self {
        CancelGuard(Some(r))
    }
}

impl<D: ?Sized, S> Drop for CancelGuard<'_, D, S> {
    fn drop(&mut self) {
        if let Some(r) = self.0.take() {
            let mut r = std::mem::ManuallyDrop::new(r);
            // A background collective can't be safely cancelled mid-flight (its
            // peers are participating), so it must still be joined.
            if let State::PendingJoin { handle } = &mut r.state {
                if let Some(h) = handle.take() {
                    let _ = h.join();
                }
            }
            r.state = State::Consumed;
        }
    }
}

/// Wait for any one of the requests to complete, returning its index and
/// status and removing it from the vector (`MPI_Waitany`). Returns `None` if
/// the vector is empty.
pub fn wait_any<'a, D: ?Sized, S: Scope<'a>>(
    requests: &mut Vec<Request<'a, D, S>>,
) -> Option<(usize, Status)> {
    if requests.is_empty() {
        return None;
    }
    loop {
        for i in 0..requests.len() {
            if requests[i].ready() {
                let r = requests.remove(i);
                return Some((i, r.wait()));
            }
        }
        std::thread::yield_now();
    }
}

/// Wait for all requests to complete (`MPI_Waitall`), returning their statuses.
pub fn wait_all<'a, D: ?Sized, S: Scope<'a>>(requests: Vec<Request<'a, D, S>>) -> Vec<Status> {
    requests.into_iter().map(|r| r.wait()).collect()
}

// ---- Generalized requests (MPI_Grequest_*) ----

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

struct GReqState {
    done: AtomicBool,
    status: std::sync::Mutex<Option<Status>>,
}

/// A user-defined ("generalized") request, completed by external code rather
/// than by the MPI runtime (`MPI_Grequest_start`). Pair it with a
/// [`GeneralizedRequestCompleter`]: whichever code performs the underlying work
/// calls [`GeneralizedRequestCompleter::complete`], after which `wait`/`test`
/// on the request return.
pub struct GeneralizedRequest {
    state: Arc<GReqState>,
}

/// The completion handle for a [`GeneralizedRequest`]
/// (`MPI_Grequest_complete`).
pub struct GeneralizedRequestCompleter {
    state: Arc<GReqState>,
}

impl GeneralizedRequest {
    /// Start a generalized request, returning it together with the completer
    /// used to mark it done (`MPI_Grequest_start`).
    pub fn start() -> (GeneralizedRequest, GeneralizedRequestCompleter) {
        let state = Arc::new(GReqState {
            done: AtomicBool::new(false),
            status: std::sync::Mutex::new(None),
        });
        (
            GeneralizedRequest {
                state: Arc::clone(&state),
            },
            GeneralizedRequestCompleter { state },
        )
    }

    /// Whether the request has been completed.
    pub fn is_complete(&self) -> bool {
        self.state.done.load(Ordering::Acquire)
    }

    /// Block until the request is completed, returning its status.
    pub fn wait(self) -> Status {
        while !self.state.done.load(Ordering::Acquire) {
            std::thread::yield_now();
        }
        self.state
            .status
            .lock()
            .unwrap()
            .unwrap_or(Status::new(0, 0, 0, 0))
    }

    /// Return `Ok(status)` if complete, otherwise `Err(self)`.
    pub fn test(self) -> Result<Status, GeneralizedRequest> {
        if self.state.done.load(Ordering::Acquire) {
            Ok(self
                .state
                .status
                .lock()
                .unwrap()
                .unwrap_or(Status::new(0, 0, 0, 0)))
        } else {
            Err(self)
        }
    }
}

impl GeneralizedRequestCompleter {
    /// Mark the associated request complete (`MPI_Grequest_complete`).
    pub fn complete(self) {
        *self.state.status.lock().unwrap() = Some(Status::new(0, 0, 0, 0));
        self.state.done.store(true, Ordering::Release);
    }
}

// ---- Persistent requests (MPI_Send_init / MPI_Recv_init) ----

enum PersistentKind {
    Send {
        src: Rank,
        dest_world: i32,
        dt: u32,
        count: u64,
    },
    Recv {
        source: Rank,
    },
}

/// A persistent (re-usable) communication request. Created with
/// [`crate::point_to_point::Destination::send_init`] /
/// [`crate::point_to_point::Source::receive_init`], then repeatedly `start`ed
/// and `wait`ed. The associated buffer is borrowed for the request's lifetime.
pub struct PersistentRequest<'a> {
    ctx: u32,
    tag: Tag,
    kind: PersistentKind,
    ptr: *mut u8,
    len: usize,
    last: Option<Status>,
    _life: PhantomData<&'a mut ()>,
}

impl<'a> PersistentRequest<'a> {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new_send(
        ctx: u32,
        src: Rank,
        dest_world: i32,
        tag: Tag,
        dt: u32,
        count: u64,
        ptr: *const u8,
        len: usize,
    ) -> PersistentRequest<'a> {
        PersistentRequest {
            ctx,
            tag,
            kind: PersistentKind::Send {
                src,
                dest_world,
                dt,
                count,
            },
            ptr: ptr as *mut u8,
            len,
            last: None,
            _life: PhantomData,
        }
    }

    pub(crate) fn new_recv(
        ctx: u32,
        source: Rank,
        tag: Tag,
        ptr: *mut u8,
        len: usize,
    ) -> PersistentRequest<'a> {
        PersistentRequest {
            ctx,
            tag,
            kind: PersistentKind::Recv { source },
            ptr,
            len,
            last: None,
            _life: PhantomData,
        }
    }

    /// Activate the operation (`MPI_Start`).
    pub fn start(&mut self) {
        match self.kind {
            PersistentKind::Send {
                src,
                dest_world,
                dt,
                count,
            } => {
                // SAFETY: `ptr`/`len` describe the borrowed buffer, valid for 'a.
                let bytes = unsafe { std::slice::from_raw_parts(self.ptr, self.len) };
                transport::runtime()
                    .send(self.ctx, src, dest_world, self.tag, count, dt, bytes)
                    .expect("persistent send failed");
                self.last = Some(Status::new(dest_world, self.tag, count as Count, self.len));
            }
            PersistentKind::Recv { .. } => {}
        }
    }

    /// Complete the current activation (`MPI_Wait`), returning its status.
    pub fn wait(&mut self) -> Status {
        match self.kind {
            PersistentKind::Send { .. } => self.last.take().unwrap_or(Status::new(0, 0, 0, 0)),
            PersistentKind::Recv { source } => {
                let (s, t, count, _dt, payload) =
                    transport::runtime().recv(self.ctx, source, self.tag);
                let n = self.len.min(payload.len());
                // SAFETY: `ptr`/`len` describe the borrowed receive buffer.
                unsafe {
                    std::ptr::copy_nonoverlapping(payload.as_ptr(), self.ptr, n);
                }
                Status::new(s, t, count as Count, payload.len())
            }
        }
    }
}