complex_datatype/
complex_datatype.rs

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