pub struct UserOperation<'a> { /* private fields */ }
Expand description
A user-defined operation.
The lifetime 'a
of the operation is limited by the lifetime of the underlying closure.
For safety reasons, UserOperation
is in of itself not considered an Operation
, but a
reference of it is. This limitation may be lifted in the future when Request
objects can
store finalizers.
Note: When a UserOperation
is passed to a non-blocking API call, it must outlive the
completion of the request. This is normally enforced by the safe API, so this is only a concern
if you use the unsafe API. Do not rely on MPI’s internal reference-counting here, because once
UserOperation
is destroyed, the closure object will be deallocated even if the MPI_Op
handle
is still alive due to outstanding references.
§Examples
See examples/reduce.rs
and examples/immediate_reduce.rs
Implementations§
Source§impl<'a> UserOperation<'a>
impl<'a> UserOperation<'a>
Sourcepub fn associative<F>(function: F) -> Self
pub fn associative<F>(function: F) -> Self
Define an operation using a closure. The operation must be associative.
This is a more readable shorthand for the new
method. Refer to new
for
more information.
Sourcepub fn commutative<F>(function: F) -> Self
pub fn commutative<F>(function: F) -> Self
Define an operation using a closure. The operation must be both associative and commutative.
This is a more readable shorthand for the new
method. Refer to new
for
more information.
Examples found in repository?
16fn test_user_operations<C: Communicator>(comm: C) {
17 let rank = comm.rank();
18 let size = comm.size();
19 let mut h = 0;
20 comm.all_reduce_into(
21 &(rank + 1),
22 &mut h,
23 &UserOperation::commutative(|x, y| {
24 let x: &[Rank] = x.downcast().unwrap();
25 let y: &mut [Rank] = y.downcast().unwrap();
26 for (&x_i, y_i) in x.iter().zip(y) {
27 *y_i += x_i;
28 }
29 }),
30 );
31 assert_eq!(h, size * (size + 1) / 2);
32}
More examples
15fn test_user_operations<C: Communicator>(comm: C) {
16 let op = UserOperation::commutative(|x, y| {
17 let x: &[Rank] = x.downcast().unwrap();
18 let y: &mut [Rank] = y.downcast().unwrap();
19 for (&x_i, y_i) in x.iter().zip(y) {
20 *y_i += x_i;
21 }
22 });
23 let rank = comm.rank();
24 let size = comm.size();
25 let mut c = 0;
26 mpi::request::scope(|scope| {
27 comm.immediate_all_reduce_into(scope, &rank, &mut c, &op)
28 .wait();
29 });
30 assert_eq!(c, size * (size - 1) / 2);
31}
Sourcepub fn new<F>(commute: bool, function: F) -> Self
pub fn new<F>(commute: bool, function: F) -> Self
Creates an associative and possibly commutative operation using a closure.
The closure receives two arguments invec
and inoutvec
as dynamically typed buffers. It
shall set inoutvec
to the value of f(invec, inoutvec)
, where f
is a binary associative
operation.
If the operation is also commutative, setting commute
to true
may yield performance
benefits.
Note: If the closure panics, the entire program will abort.
§Standard section(s)
5.9.5