complex_datatype/
complex_datatype.rs

1#![deny(warnings)]
2#![allow(clippy::forget_copy)]
3
4#[macro_use]
5extern crate memoffset;
6
7use mpi::{
8    datatype::{UncommittedUserDatatype, UserDatatype},
9    traits::*,
10    Address,
11};
12
13#[derive(Default)]
14struct TupleType([f32; 2], u8);
15
16#[derive(Default)]
17struct ComplexDatatype {
18    b: bool,
19    ints: [i32; 4],
20    tuple: TupleType,
21}
22
23unsafe impl Equivalence for ComplexDatatype {
24    type Out = UserDatatype;
25    fn equivalent_datatype() -> Self::Out {
26        UserDatatype::structured(
27            &[1, 1, 1],
28            &[
29                offset_of!(ComplexDatatype, b) as Address,
30                offset_of!(ComplexDatatype, ints) as Address,
31                offset_of!(ComplexDatatype, tuple) as Address,
32            ],
33            &[
34                bool::equivalent_datatype().into(),
35                UncommittedUserDatatype::contiguous(4, &i32::equivalent_datatype()).as_ref(),
36                UncommittedUserDatatype::structured(
37                    &[2, 1],
38                    &[
39                        offset_of!(TupleType, 0) as Address,
40                        offset_of!(TupleType, 1) as Address,
41                    ],
42                    &[f32::equivalent_datatype(), u8::equivalent_datatype()],
43                )
44                .as_ref(),
45            ],
46        )
47    }
48}
49
50fn main() {
51    let universe = mpi::initialize().unwrap();
52    let world = universe.world();
53
54    let root_process = world.process_at_rank(0);
55
56    let mut data = if world.rank() == 0 {
57        ComplexDatatype {
58            b: true,
59            ints: [1, -2, 3, -4],
60            tuple: TupleType([-0.1, 0.1], 7),
61        }
62    } else {
63        ComplexDatatype::default()
64    };
65
66    root_process.broadcast_into(&mut data);
67
68    assert_eq!(true, data.b);
69    assert_eq!([1, -2, 3, -4], data.ints);
70    assert_eq!([-0.1, 0.1], data.tuple.0);
71    assert_eq!(7, data.tuple.1);
72}