ferrompi 0.4.0

A safe, generic Rust wrapper for MPI with support for MPI 4.0+ features, shared memory windows, and hybrid MPI+OpenMP
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
//! Request handles for nonblocking MPI operations.

use crate::error::{Error, Result};
use crate::ffi;

/// A handle to a nonblocking MPI operation.
///
/// This type represents an in-flight MPI operation. You must call `wait()` or
/// `test()` to complete the operation before the associated buffers can be
/// safely accessed.
///
/// # Safety — Buffer Lifetime
///
/// **The caller must ensure that all buffers passed to the nonblocking operation
/// (e.g., `isend`, `irecv`, `iallreduce`) remain valid and are not moved,
/// reallocated, or dropped until the `Request` is completed (via `wait()` or
/// `test()` returning `true`) or dropped.** MPI holds raw pointers to these
/// buffers; violating this invariant is undefined behavior.
///
/// This cannot currently be enforced by the Rust type system because `Request`
/// does not carry a lifetime parameter tying it to the buffers.
///
/// # Example
///
/// ```no_run
/// use ferrompi::{Mpi, ReduceOp};
///
/// let mpi = Mpi::init().unwrap();
/// let world = mpi.world();
///
/// let send = vec![world.rank() as f64; 10];
/// let mut recv = vec![0.0; 10];
///
/// // Start nonblocking all-reduce
/// let request = world.iallreduce(&send, &mut recv, ReduceOp::Sum).unwrap();
///
/// // Do other work while communication proceeds...
///
/// // Wait for completion
/// request.wait().unwrap();
///
/// // Now recv contains the result
/// println!("Sum: {:?}", recv);
/// ```
pub struct Request {
    handle: i64,
    completed: bool,
}

impl Request {
    /// Create a new request from a raw handle.
    pub(crate) fn new(handle: i64) -> Self {
        Request {
            handle,
            completed: false,
        }
    }

    /// Get the raw request handle (for advanced use).
    pub fn raw_handle(&self) -> i64 {
        self.handle
    }

    /// Check if this request has been completed.
    pub fn is_completed(&self) -> bool {
        self.completed
    }

    /// Wait for this operation to complete.
    ///
    /// Blocks until the operation is finished. After this returns successfully,
    /// the associated buffers can be safely accessed.
    pub fn wait(mut self) -> Result<()> {
        if self.completed {
            return Ok(());
        }
        let ret = unsafe { ffi::ferrompi_wait(self.handle) };
        Error::check_with_op(ret, "wait")?;
        self.completed = true;
        Ok(())
    }

    /// Test if this operation has completed without blocking.
    ///
    /// Returns `true` if the operation is complete, `false` otherwise.
    ///
    /// # Note
    ///
    /// If this returns `true`, the request is consumed and you should not call
    /// `wait()` or `test()` again.
    pub fn test(&mut self) -> Result<bool> {
        if self.completed {
            return Ok(true);
        }
        let mut flag: i32 = 0;
        let ret = unsafe { ffi::ferrompi_test(self.handle, &mut flag) };
        Error::check_with_op(ret, "test")?;
        if flag != 0 {
            self.completed = true;
        }
        Ok(flag != 0)
    }

    /// Wait for any one request in a collection to complete.
    ///
    /// Blocks until at least one request completes and returns its index. Returns
    /// `Ok(None)` when all requests were already `MPI_REQUEST_NULL` on entry.
    ///
    /// The completed `Request` is marked `completed = true` in place. Removing it
    /// from the vector is the caller's responsibility.
    pub fn wait_any(requests: &mut [Request]) -> Result<Option<usize>> {
        if requests.is_empty() {
            return Ok(None);
        }
        let mut handles: Vec<i64> = requests.iter().map(|r| r.handle).collect();
        let mut index: i32 = 0;
        // SAFETY: handles is a valid, mutable Vec<i64> whose length we pass as count.
        // index is a valid stack-allocated i32 output parameter.
        let ret = unsafe {
            ffi::ferrompi_waitany(handles.len() as i64, handles.as_mut_ptr(), &mut index)
        };
        Error::check_with_op(ret, "waitany")?;
        if index < 0 {
            return Ok(None);
        }
        let idx = index as usize;
        requests[idx].completed = true;
        Ok(Some(idx))
    }

    /// Wait until at least one request in a collection completes.
    ///
    /// Returns the indices of all requests that completed in this call.
    /// Returns `Ok(vec![])` when no requests were active (all null or all already done).
    ///
    /// The completed `Request`s are marked `completed = true` in place. Removing
    /// them from the vector is the caller's responsibility.
    pub fn wait_some(requests: &mut [Request]) -> Result<Vec<usize>> {
        if requests.is_empty() {
            return Ok(vec![]);
        }
        let mut handles: Vec<i64> = requests.iter().map(|r| r.handle).collect();
        let mut outcount: i64 = 0;
        let mut indices: Vec<i32> = vec![0; requests.len()];
        // SAFETY: handles, outcount, and indices are valid, appropriately-sized
        // output buffers. handles.len() is passed as count.
        let ret = unsafe {
            ffi::ferrompi_waitsome(
                handles.len() as i64,
                handles.as_mut_ptr(),
                &mut outcount,
                indices.as_mut_ptr(),
            )
        };
        Error::check_with_op(ret, "waitsome")?;
        if outcount <= 0 {
            // outcount == -1 means all null; 0 means none completed (shouldn't
            // happen for waitsome, but guard defensively).
            return Ok(vec![]);
        }
        let completed: Vec<usize> = indices[..outcount as usize]
            .iter()
            .map(|&i| i as usize)
            .collect();
        for &idx in &completed {
            requests[idx].completed = true;
        }
        Ok(completed)
    }

    /// Test whether any one request in a collection has completed (non-blocking).
    ///
    /// Returns `Ok(Some(idx))` if a request completed, `Ok(None)` if no request
    /// has completed yet or all requests were already null.
    ///
    /// The completed `Request` is marked `completed = true` in place. Removing it
    /// from the vector is the caller's responsibility.
    pub fn test_any(requests: &mut [Request]) -> Result<Option<usize>> {
        if requests.is_empty() {
            return Ok(None);
        }
        let mut handles: Vec<i64> = requests.iter().map(|r| r.handle).collect();
        let mut index: i32 = 0;
        let mut flag: i32 = 0;
        // SAFETY: handles is a valid, mutable Vec<i64> whose length we pass as count.
        // index and flag are valid stack-allocated i32 output parameters.
        let ret = unsafe {
            ffi::ferrompi_testany(
                handles.len() as i64,
                handles.as_mut_ptr(),
                &mut index,
                &mut flag,
            )
        };
        Error::check_with_op(ret, "testany")?;
        if flag == 0 {
            return Ok(None);
        }
        if index < 0 {
            // All requests were null — nothing to mark.
            return Ok(None);
        }
        let idx = index as usize;
        requests[idx].completed = true;
        Ok(Some(idx))
    }

    /// Test how many requests in a collection have completed (non-blocking).
    ///
    /// Returns the indices of all requests that have completed at the moment of
    /// the call. Returns `Ok(vec![])` when none have completed or all were null.
    ///
    /// The completed `Request`s are marked `completed = true` in place. Removing
    /// them from the vector is the caller's responsibility.
    pub fn test_some(requests: &mut [Request]) -> Result<Vec<usize>> {
        if requests.is_empty() {
            return Ok(vec![]);
        }
        let mut handles: Vec<i64> = requests.iter().map(|r| r.handle).collect();
        let mut outcount: i64 = 0;
        let mut indices: Vec<i32> = vec![0; requests.len()];
        // SAFETY: handles, outcount, and indices are valid, appropriately-sized
        // output buffers. handles.len() is passed as count.
        let ret = unsafe {
            ffi::ferrompi_testsome(
                handles.len() as i64,
                handles.as_mut_ptr(),
                &mut outcount,
                indices.as_mut_ptr(),
            )
        };
        Error::check_with_op(ret, "testsome")?;
        if outcount <= 0 {
            // outcount == -1 means all null; 0 means none completed yet.
            return Ok(vec![]);
        }
        let completed: Vec<usize> = indices[..outcount as usize]
            .iter()
            .map(|&i| i as usize)
            .collect();
        for &idx in &completed {
            requests[idx].completed = true;
        }
        Ok(completed)
    }

    /// Non-destructive query: check whether this request has completed
    /// without consuming it. Unlike [`test`](Request::test), this does NOT
    /// free the request on completion; it only probes.
    ///
    /// Returns `Ok(true)` if the MPI runtime reports the request is complete,
    /// `Ok(false)` otherwise. Does NOT mutate `completed` — this is a probe,
    /// not a commit.
    pub fn get_status(&self) -> Result<bool> {
        if self.completed {
            return Ok(true);
        }
        let mut flag: i32 = 0;
        // SAFETY: self.handle is a valid request handle issued by the C shim.
        // flag is a valid stack-allocated i32 output parameter.
        let ret = unsafe { ffi::ferrompi_request_get_status(self.handle, &mut flag) };
        Error::check_with_op(ret, "request_get_status")?;
        Ok(flag != 0)
    }

    /// Request cancellation of a pending nonblocking operation.
    ///
    /// # Portability
    ///
    /// Per the MPI 4.0 standard, `MPI_Cancel` is effectively deprecated for
    /// send requests. Open MPI refuses to cancel sends; MPICH may report
    /// success but not actually cancel the send. Cancellation reliably works
    /// only for receives.
    ///
    /// # Usage
    ///
    /// `cancel` does NOT complete the request. The caller must follow up with
    /// [`wait`](Request::wait) to reclaim the handle:
    ///
    /// ```no_run
    /// # use ferrompi::{Mpi, Result};
    /// # fn main() -> Result<()> {
    /// # let mpi = Mpi::init()?;
    /// # let world = mpi.world();
    /// # let mut buf = vec![0u8; 10];
    /// # let mut req = world.irecv(&mut buf, 0, 0)?;
    /// req.cancel()?;
    /// req.wait()?;
    /// # Ok(()) }
    /// ```
    pub fn cancel(&mut self) -> Result<()> {
        if self.completed {
            return Ok(());
        }
        // SAFETY: self.handle is a valid request handle issued by the C shim.
        let ret = unsafe { ffi::ferrompi_cancel(self.handle) };
        Error::check_with_op(ret, "cancel")
    }

    /// Wait for all requests in a collection to complete.
    ///
    /// This is more efficient than waiting for each request individually.
    pub fn wait_all(requests: Vec<Request>) -> Result<()> {
        if requests.is_empty() {
            return Ok(());
        }

        let mut handles: Vec<i64> = requests.iter().map(|r| r.handle).collect();
        let ret = unsafe { ffi::ferrompi_waitall(handles.len() as i64, handles.as_mut_ptr()) };

        if ret == 0 {
            // Success: all requests completed, skip Drop (handles already freed by MPI)
            for mut req in requests {
                req.completed = true;
                std::mem::forget(req);
            }
            Ok(())
        } else {
            // Error: let Drop handle cleanup (will re-wait each active request)
            Err(Error::from_code_with_op(ret, "waitall"))
        }
    }
}

impl Drop for Request {
    fn drop(&mut self) {
        if !self.completed {
            // If the request wasn't waited on, we need to wait now to avoid
            // leaving the operation in an undefined state
            unsafe { ffi::ferrompi_wait(self.handle) };
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::mem::forget;

    #[test]
    fn new_request_is_not_completed() {
        let req = Request::new(0);
        assert!(!req.is_completed());
        assert_eq!(req.raw_handle(), 0);
        forget(req);
    }

    #[test]
    fn raw_handle_returns_constructor_value() {
        let req = Request::new(99);
        assert_eq!(req.raw_handle(), 99);
        forget(req);
    }

    #[test]
    fn test_when_already_completed_returns_true() {
        let mut req = Request {
            handle: 0,
            completed: true,
        };
        let result = req.test();
        assert!(matches!(result, Ok(true)));
        forget(req);
    }

    #[test]
    fn wait_when_already_completed_returns_ok() {
        // wait() takes self by value (consuming).
        // With completed: true, it returns Ok(()) on line 63 before any FFI.
        // Drop then runs, but !self.completed is false, so Drop is a no-op.
        let req = Request {
            handle: 0,
            completed: true,
        };
        let result = req.wait();
        assert!(result.is_ok());
        // No forget() needed — wait() consumed the value, and Drop was a no-op
    }

    #[test]
    fn wait_all_empty_vec_returns_ok() {
        let result = Request::wait_all(vec![]);
        assert!(result.is_ok());
    }

    #[test]
    fn wait_any_empty_vec_returns_none() {
        let mut v: Vec<Request> = vec![];
        assert_eq!(Request::wait_any(&mut v).unwrap(), None);
    }

    #[test]
    fn wait_some_empty_vec_returns_empty() {
        let mut v: Vec<Request> = vec![];
        assert_eq!(Request::wait_some(&mut v).unwrap(), Vec::<usize>::new());
    }

    #[test]
    fn test_any_empty_vec_returns_none() {
        let mut v: Vec<Request> = vec![];
        assert_eq!(Request::test_any(&mut v).unwrap(), None);
    }

    #[test]
    fn test_some_empty_vec_returns_empty() {
        let mut v: Vec<Request> = vec![];
        assert_eq!(Request::test_some(&mut v).unwrap(), Vec::<usize>::new());
    }

    #[test]
    fn get_status_on_completed_request_returns_true_without_ffi() {
        let req = Request {
            handle: 0,
            completed: true,
        };
        let result = req.get_status();
        assert!(matches!(result, Ok(true)));
        forget(req);
    }

    #[test]
    fn cancel_on_completed_request_returns_ok_without_ffi() {
        let mut req = Request {
            handle: 0,
            completed: true,
        };
        let result = req.cancel();
        assert!(matches!(result, Ok(())));
        forget(req);
    }
}