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
//! Point-to-point communication: send, recv, isend, irecv, sendrecv, probe, iprobe.
use crate::comm::Communicator;
use crate::datatype::MpiDatatype;
use crate::error::{Error, Result};
use crate::ffi;
use crate::request::Request;
use crate::status::Status;
impl Communicator {
/// Send a slice of values to another process.
///
/// # Arguments
///
/// * `data` - Buffer to send
/// * `dest` - Destination rank
/// * `tag` - Message tag
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// let data = vec![1.0f64, 2.0, 3.0];
/// world.send(&data, 1, 0).unwrap();
/// ```
pub fn send<T: MpiDatatype>(&self, data: &[T], dest: i32, tag: i32) -> Result<()> {
let ret = unsafe {
ffi::ferrompi_send(
data.as_ptr().cast::<std::ffi::c_void>(),
data.len() as i64,
T::TAG as i32,
dest,
tag,
self.handle,
)
};
Error::check_with_op(ret, "send")
}
/// Receive a slice of values from another process.
///
/// Use `source = -1` for `MPI_ANY_SOURCE` and `tag = -1` for `MPI_ANY_TAG`.
///
/// Returns `(actual_source, actual_tag, actual_count)`.
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// let mut buf = vec![0.0f64; 10];
/// let (source, tag, count) = world.recv(&mut buf, 0, 0).unwrap();
/// ```
pub fn recv<T: MpiDatatype>(
&self,
data: &mut [T],
source: i32,
tag: i32,
) -> Result<(i32, i32, i64)> {
let mut actual_source: i32 = 0;
let mut actual_tag: i32 = 0;
let mut actual_count: i64 = 0;
let ret = unsafe {
ffi::ferrompi_recv(
data.as_mut_ptr().cast::<std::ffi::c_void>(),
data.len() as i64,
T::TAG as i32,
source,
tag,
self.handle,
&mut actual_source,
&mut actual_tag,
&mut actual_count,
)
};
Error::check_with_op(ret, "recv")?;
Ok((actual_source, actual_tag, actual_count))
}
/// Nonblocking send.
///
/// Initiates a send operation and returns immediately with a [`Request`]
/// handle. The send buffer **must not be modified** until the request is
/// completed via [`Request::wait()`] or [`Request::test()`].
///
/// # Arguments
///
/// * `data` - Buffer to send (must remain valid until the request completes)
/// * `dest` - Destination rank
/// * `tag` - Message tag
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// let data = vec![1.0f64, 2.0, 3.0];
/// let req = world.isend(&data, 1, 0).unwrap();
/// // ... do other work ...
/// req.wait().unwrap();
/// ```
pub fn isend<T: MpiDatatype>(&self, data: &[T], dest: i32, tag: i32) -> Result<Request> {
let mut request_handle: i64 = 0;
let ret = unsafe {
ffi::ferrompi_isend(
data.as_ptr().cast::<std::ffi::c_void>(),
data.len() as i64,
T::TAG as i32,
dest,
tag,
self.handle,
&mut request_handle,
)
};
Error::check_with_op(ret, "isend")?;
Ok(Request::new(request_handle))
}
/// Nonblocking receive.
///
/// Initiates a receive operation and returns immediately with a [`Request`]
/// handle. The receive buffer **must not be read** until the request is
/// completed via [`Request::wait()`] or [`Request::test()`].
///
/// Use `source = -1` for `MPI_ANY_SOURCE` and `tag = -1` for `MPI_ANY_TAG`.
///
/// # Arguments
///
/// * `data` - Receive buffer (must remain valid until the request completes)
/// * `source` - Source rank (or -1 for any source)
/// * `tag` - Message tag (or -1 for any tag)
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// let mut buf = vec![0.0f64; 10];
/// let req = world.irecv(&mut buf, 0, 0).unwrap();
/// // ... do other work ...
/// req.wait().unwrap();
/// ```
pub fn irecv<T: MpiDatatype>(&self, data: &mut [T], source: i32, tag: i32) -> Result<Request> {
let mut request_handle: i64 = 0;
let ret = unsafe {
ffi::ferrompi_irecv(
data.as_mut_ptr().cast::<std::ffi::c_void>(),
data.len() as i64,
T::TAG as i32,
source,
tag,
self.handle,
&mut request_handle,
)
};
Error::check_with_op(ret, "irecv")?;
Ok(Request::new(request_handle))
}
/// Blocking send-receive.
///
/// Sends data to one process and receives from another (or the same) in a
/// single operation. This is useful for avoiding deadlocks in ring-style
/// communication patterns where each process both sends and receives.
///
/// Use `source = -1` for `MPI_ANY_SOURCE` and `recvtag = -1` for `MPI_ANY_TAG`.
///
/// Returns `(actual_source, actual_tag, actual_count)`.
///
/// # Arguments
///
/// * `send` - Buffer to send
/// * `dest` - Destination rank
/// * `sendtag` - Send message tag
/// * `recv` - Receive buffer
/// * `source` - Source rank (or -1 for any source)
/// * `recvtag` - Receive message tag (or -1 for any tag)
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// let send = vec![world.rank() as f64; 5];
/// let mut recv = vec![0.0f64; 5];
/// let next = (world.rank() + 1) % world.size();
/// let prev = (world.rank() - 1 + world.size()) % world.size();
/// let (src, tag, count) = world.sendrecv(&send, next, 0, &mut recv, prev, 0).unwrap();
/// ```
pub fn sendrecv<T: MpiDatatype>(
&self,
send: &[T],
dest: i32,
sendtag: i32,
recv: &mut [T],
source: i32,
recvtag: i32,
) -> Result<(i32, i32, i64)> {
let mut actual_source: i32 = 0;
let mut actual_tag: i32 = 0;
let mut actual_count: i64 = 0;
let ret = unsafe {
ffi::ferrompi_sendrecv(
send.as_ptr().cast::<std::ffi::c_void>(),
send.len() as i64,
T::TAG as i32,
dest,
sendtag,
recv.as_mut_ptr().cast::<std::ffi::c_void>(),
recv.len() as i64,
T::TAG as i32,
source,
recvtag,
self.handle,
&mut actual_source,
&mut actual_tag,
&mut actual_count,
)
};
Error::check_with_op(ret, "sendrecv")?;
Ok((actual_source, actual_tag, actual_count))
}
/// Blocking probe for an incoming message.
///
/// Waits until a matching message is available and returns status
/// information (source rank, tag, element count) without actually
/// receiving the message. This is useful for determining the size of an
/// incoming message before allocating a receive buffer.
///
/// Use `source = -1` for `MPI_ANY_SOURCE` and `tag = -1` for `MPI_ANY_TAG`.
///
/// The type parameter `T` determines the MPI datatype used by
/// `MPI_Get_count` to compute the element count in the returned
/// [`Status`].
///
/// # Arguments
///
/// * `source` - Source rank to match (or -1 for any source)
/// * `tag` - Message tag to match (or -1 for any tag)
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// // Probe for any incoming f64 message
/// let status = world.probe::<f64>(-1, -1).unwrap();
/// // Allocate a buffer of exactly the right size (count may be negative on error)
/// assert!(status.count >= 0, "MPI_Get_count returned MPI_UNDEFINED");
/// let mut buf = vec![0.0f64; status.count as usize];
/// world.recv(&mut buf, status.source, status.tag).unwrap();
/// ```
pub fn probe<T: MpiDatatype>(&self, source: i32, tag: i32) -> Result<Status> {
let mut actual_source: i32 = 0;
let mut actual_tag: i32 = 0;
let mut count: i64 = 0;
let ret = unsafe {
ffi::ferrompi_probe(
source,
tag,
self.handle,
&mut actual_source,
&mut actual_tag,
&mut count,
T::TAG as i32,
)
};
Error::check_with_op(ret, "probe")?;
Ok(Status {
source: actual_source,
tag: actual_tag,
count,
})
}
/// Nonblocking probe for an incoming message.
///
/// Checks whether a matching message is available without blocking.
/// Returns `Some(Status)` if a message is available, `None` otherwise.
///
/// Use `source = -1` for `MPI_ANY_SOURCE` and `tag = -1` for `MPI_ANY_TAG`.
///
/// The type parameter `T` determines the MPI datatype used by
/// `MPI_Get_count` to compute the element count in the returned
/// [`Status`].
///
/// # Arguments
///
/// * `source` - Source rank to match (or -1 for any source)
/// * `tag` - Message tag to match (or -1 for any tag)
///
/// # Example
///
/// ```no_run
/// # use ferrompi::Mpi;
/// # let mpi = Mpi::init().unwrap();
/// # let world = mpi.world();
/// // Poll for an incoming f64 message without blocking
/// if let Some(status) = world.iprobe::<f64>(-1, -1).unwrap() {
/// assert!(status.count >= 0, "MPI_Get_count returned MPI_UNDEFINED");
/// let mut buf = vec![0.0f64; status.count as usize];
/// world.recv(&mut buf, status.source, status.tag).unwrap();
/// }
/// ```
pub fn iprobe<T: MpiDatatype>(&self, source: i32, tag: i32) -> Result<Option<Status>> {
let mut flag: i32 = 0;
let mut actual_source: i32 = 0;
let mut actual_tag: i32 = 0;
let mut count: i64 = 0;
let ret = unsafe {
ffi::ferrompi_iprobe(
source,
tag,
self.handle,
&mut flag,
&mut actual_source,
&mut actual_tag,
&mut count,
T::TAG as i32,
)
};
Error::check_with_op(ret, "iprobe")?;
if flag != 0 {
Ok(Some(Status {
source: actual_source,
tag: actual_tag,
count,
}))
} else {
Ok(None)
}
}
}