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
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::*;
pub mod traits {
pub use super::Request;
}
pub trait Request: AsRaw<Raw = MPI_Request> + AsRawMut {
fn is_null(&self) -> bool {
self.as_raw() == unsafe_extern_static!(ffi::RSMPI_REQUEST_NULL)
}
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)
}
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)
}
}
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);
}
}
#[must_use]
pub struct PlainRequest(MPI_Request);
impl PlainRequest {
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.");
}
}
#[must_use]
pub struct ReadRequest<'b, Buf: 'b + ?Sized>(MPI_Request, PhantomData<&'b Buf>);
impl<'b, Buf: 'b + ?Sized> ReadRequest<'b, Buf> {
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.");
}
}
#[must_use]
pub struct WriteRequest<'b, Buf: 'b + ?Sized>(MPI_Request, PhantomData<&'b mut Buf>);
impl<'b, Buf: 'b + ?Sized> WriteRequest<'b, Buf> {
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.");
}
}
#[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> {
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.");
}
}
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))
}
}
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))
}
}