-
Notifications
You must be signed in to change notification settings - Fork 52
/
request.rs
335 lines (297 loc) · 9.02 KB
/
request.rs
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
//! Request objects for non-blocking operations
//!
//! Non-blocking operations such as `immediate_send()` return request objects that borrow any
//! buffers involved in the operation so as to ensure proper access restrictions. In order to
//! release the borrowed buffers from the request objects, a completion operation such as `wait()`
//! or `test()` must be used on the request object. To enforce this rule, the request objects
//! implement a `Drop` bomb which will `panic!()` when a request object is dropped.
//!
//! To handle request completion in a RAII style, requests can be wrapped in either `WaitGuard` or
//! `CancelGuard` which will follow the respective policy for completing the operation upon being
//! dropped instead of `panic!()`ing.
//!
//! # Unfinished features
//!
//! - **3.7**: Nonblocking mode:
//! - Completion, `MPI_Waitany()`, `MPI_Waitall()`, `MPI_Waitsome()`,
//! `MPI_Testany()`, `MPI_Testall()`, `MPI_Testsome()`, `MPI_Request_get_status()`
//! - **3.8**:
//! - Cancellation, `MPI_Cancel()`, `MPI_Test_cancelled()`
use std::mem;
use std::marker::PhantomData;
use std::os::raw::c_int;
use ffi;
use ffi::{MPI_Request, MPI_Status};
use point_to_point::Status;
use raw::traits::*;
/// Request object traits
pub mod traits {
pub use super::Request;
}
/// A request for a non-blocking operation
pub trait Request: AsRaw<Raw = MPI_Request> + AsRawMut {
/// Returns true for a null request handle.
fn is_null(&self) -> bool {
self.as_raw() == unsafe_extern_static!(ffi::RSMPI_REQUEST_NULL)
}
/// Wait for an operation to finish.
///
/// Will block execution of the calling thread until the associated operation has finished.
///
/// # Examples
///
/// See `examples/immediate.rs`
///
/// # Standard section(s)
///
/// 3.7.3
fn wait(mut self) -> Status
where Self: Sized
{
let mut status: MPI_Status = unsafe { mem::uninitialized() };
unsafe {
ffi::MPI_Wait(self.as_raw_mut(), &mut status);
}
assert!(self.is_null());
mem::forget(self);
Status::from_raw(status)
}
/// Test whether an operation has finished.
///
/// If the operation has finished returns the `Status` otherwise returns the unfinished
/// `Request`.
/// # Examples
///
/// See `examples/immediate.rs`
///
/// # Standard section(s)
///
/// 3.7.3
fn test(mut self) -> Result<Status, Self>
where Self: Sized
{
let mut status: MPI_Status = unsafe { mem::uninitialized() };
let mut flag: c_int = 0;
unsafe {
ffi::MPI_Test(self.as_raw_mut(), &mut flag, &mut status);
}
assert!(flag == 0 || self.is_null());
if flag != 0 {
mem::forget(self);
Ok(Status::from_raw(status))
} else {
Err(self)
}
}
/// Cancel an operation.
///
/// # Examples
///
/// See `examples/immediate.rs`
///
/// # Standard section(s)
///
/// 3.8.4
fn cancel(mut self)
where Self: Sized
{
unsafe {
ffi::MPI_Cancel(self.as_raw_mut());
ffi::MPI_Request_free(self.as_raw_mut());
}
assert!(self.is_null());
mem::forget(self);
}
}
/// A request object for an non-blocking operation that holds no references
///
/// # Examples
///
/// See `examples/immediate_barrier.rs`
///
/// # Standard section(s)
///
/// 3.7.1
#[must_use]
pub struct PlainRequest(MPI_Request);
impl PlainRequest {
/// Construct a request object from the raw MPI type
pub fn from_raw(request: MPI_Request) -> PlainRequest {
PlainRequest(request)
}
}
unsafe impl AsRaw for PlainRequest {
type Raw = MPI_Request;
fn as_raw(&self) -> Self::Raw {
self.0
}
}
unsafe impl AsRawMut for PlainRequest {
fn as_raw_mut(&mut self) -> *mut <Self as AsRaw>::Raw {
&mut (self.0)
}
}
impl Request for PlainRequest {}
impl Drop for PlainRequest {
fn drop(&mut self) {
assert!(self.is_null(),
"request dropped without ascertaining completion.");
}
}
/// A request object for a non-blocking operation that holds a reference to an immutable buffer
///
/// # Examples
///
/// See `examples/immediate.rs`
///
/// # Standard section(s)
///
/// 3.7.1
#[must_use]
pub struct ReadRequest<'b, Buf: 'b + ?Sized>(MPI_Request, PhantomData<&'b Buf>);
impl<'b, Buf: 'b + ?Sized> ReadRequest<'b, Buf> {
/// Construct a request object from the raw MPI type
pub fn from_raw(request: MPI_Request, _: &'b Buf) -> ReadRequest<'b, Buf> {
ReadRequest(request, PhantomData)
}
}
unsafe impl<'b, Buf: 'b + ?Sized> AsRaw for ReadRequest<'b, Buf> {
type Raw = MPI_Request;
fn as_raw(&self) -> Self::Raw {
self.0
}
}
unsafe impl<'b, Buf: 'b + ?Sized> AsRawMut for ReadRequest<'b, Buf> {
fn as_raw_mut(&mut self) -> *mut <Self as AsRaw>::Raw {
&mut (self.0)
}
}
impl<'b, Buf: 'b + ?Sized> Request for ReadRequest<'b, Buf> {}
impl<'b, Buf: 'b + ?Sized> Drop for ReadRequest<'b, Buf> {
fn drop(&mut self) {
assert!(self.is_null(),
"read request dropped without ascertaining completion.");
}
}
/// A request object for a non-blocking operation that holds a reference to a mutable buffer
///
/// # Examples
///
/// See `examples/immediate.rs`
///
/// # Standard section(s)
///
/// 3.7.1
#[must_use]
pub struct WriteRequest<'b, Buf: 'b + ?Sized>(MPI_Request, PhantomData<&'b mut Buf>);
impl<'b, Buf: 'b + ?Sized> WriteRequest<'b, Buf> {
/// Construct a request object from the raw MPI type
pub fn from_raw(request: MPI_Request, _: &'b Buf) -> WriteRequest<'b, Buf> {
WriteRequest(request, PhantomData)
}
}
unsafe impl<'b, Buf: 'b + ?Sized> AsRaw for WriteRequest<'b, Buf> {
type Raw = MPI_Request;
fn as_raw(&self) -> Self::Raw {
self.0
}
}
unsafe impl<'b, Buf: 'b + ?Sized> AsRawMut for WriteRequest<'b, Buf> {
fn as_raw_mut(&mut self) -> *mut <Self as AsRaw>::Raw {
&mut (self.0)
}
}
impl<'b, Buf: 'b + ?Sized> Request for WriteRequest<'b, Buf> {}
impl<'b, Buf: 'b + ?Sized> Drop for WriteRequest<'b, Buf> {
fn drop(&mut self) {
assert!(self.is_null(),
"write request dropped without ascertaining completion.");
}
}
/// A request object for a non-blocking operation that holds a reference to a mutable and an
/// immutable buffer
///
/// # Examples
///
/// See `examples/immediate_gather.rs`
///
/// # Standard section(s)
///
/// 3.7.1
#[must_use]
pub struct ReadWriteRequest<'s, 'r, S: 's + ?Sized, R: 'r + ?Sized>(MPI_Request,
PhantomData<&'s S>,
PhantomData<&'r mut R>);
impl<'s, 'r, S: 's + ?Sized, R: 'r + ?Sized> ReadWriteRequest<'s, 'r, S, R> {
/// Construct a request object from the raw MPI type
pub fn from_raw(request: MPI_Request, _: &'s S, _: &'r R) -> ReadWriteRequest<'s, 'r, S, R> {
ReadWriteRequest(request, PhantomData, PhantomData)
}
}
unsafe impl<'s, 'r, S: 's + ?Sized, R: 'r + ?Sized> AsRaw for ReadWriteRequest<'s, 'r, S, R> {
type Raw = MPI_Request;
fn as_raw(&self) -> Self::Raw {
self.0
}
}
unsafe impl<'s, 'r, S: 's + ?Sized, R: 'r + ?Sized> AsRawMut for ReadWriteRequest<'s, 'r, S, R> {
fn as_raw_mut(&mut self) -> *mut <Self as AsRaw>::Raw {
&mut (self.0)
}
}
impl<'s, 'r, S: 's + ?Sized, R: 'r + ?Sized> Request for ReadWriteRequest<'s, 'r, S, R> {}
impl<'s, 'r, S: 's + ?Sized, R: 'r + ?Sized> Drop for ReadWriteRequest<'s, 'r, S, R> {
fn drop(&mut self) {
assert!(self.is_null(),
"read-write request dropped without ascertaining completion.");
}
}
/// Guard object that waits for the completion of an operation when it is dropped
///
/// # Examples
///
/// See `examples/immediate.rs`
pub struct WaitGuard<Req>(Option<Req>) where Req: Request;
impl<Req> Drop for WaitGuard<Req> where Req: Request
{
fn drop(&mut self) {
self.0.take().map(|mut req| {
unsafe {
ffi::MPI_Wait(req.as_raw_mut(), ffi::RSMPI_STATUS_IGNORE);
}
assert!(req.is_null());
mem::forget(req);
});
}
}
impl<Req> From<Req> for WaitGuard<Req> where Req: Request
{
fn from(req: Req) -> WaitGuard<Req> {
WaitGuard(Some(req))
}
}
/// Guard object that cancels an operation when it is dropped
///
/// # Examples
///
/// See `examples/immediate.rs`
pub struct CancelGuard<Req>(Option<Req>) where Req: Request;
impl<Req> Drop for CancelGuard<Req> where Req: Request
{
fn drop(&mut self) {
self.0.take().map(|mut req| {
unsafe {
ffi::MPI_Cancel(req.as_raw_mut());
ffi::MPI_Request_free(req.as_raw_mut());
}
assert!(req.is_null());
mem::forget(req);
});
}
}
impl<Req> From<Req> for CancelGuard<Req> where Req: Request
{
fn from(req: Req) -> CancelGuard<Req> {
CancelGuard(Some(req))
}
}