1use crate::wire::Tag;
4
5pub trait MpiBackend {
23 type Peer<'a>: Copy;
25
26 type Probed;
28
29 fn is_self(peer: Self::Peer<'_>) -> bool;
31
32 fn send_bytes(&self, to: Self::Peer<'_>, tag: Tag, buffer: &[u8]);
34
35 fn probe(&self, from: Self::Peer<'_>, tag: Tag) -> (Self::Probed, i64);
41
42 fn receive_probed(&self, probed: Self::Probed, buffer: &mut [u8]) -> i64;
44}
45
46#[cfg(feature = "system")]
47mod system {
48 use mpi::point_to_point::Message;
49 use mpi::topology::Process;
50 use mpi::traits::{Destination, Equivalence, Source};
51
52 use super::MpiBackend;
53 use crate::wire::Tag;
54
55 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
67 pub struct System;
68
69 impl MpiBackend for System {
70 type Peer<'a> = Process<'a>;
71 type Probed = Message;
72
73 fn is_self(peer: Self::Peer<'_>) -> bool {
74 peer.is_self()
75 }
76
77 fn send_bytes(&self, to: Self::Peer<'_>, tag: Tag, buffer: &[u8]) {
78 to.send_with_tag(buffer, tag);
79 }
80
81 fn probe(&self, from: Self::Peer<'_>, tag: Tag) -> (Self::Probed, i64) {
82 let (message, status) = from.matched_probe_with_tag(tag);
83 (message, i64::from(status.count(u8::equivalent_datatype())))
84 }
85
86 fn receive_probed(&self, probed: Self::Probed, buffer: &mut [u8]) -> i64 {
87 let status = probed.matched_receive_into(buffer);
88 i64::from(status.count(u8::equivalent_datatype()))
89 }
90 }
91}
92
93#[cfg(feature = "runtime")]
94mod runtime {
95 use mpi::point_to_point::Message;
96 use mpi::topology::Process;
97 use mpi::traits::{Destination, Equivalence, Source};
98
99 use super::MpiBackend;
100 use crate::wire::Tag;
101
102 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
114 pub struct Rt;
115
116 impl MpiBackend for Rt {
117 type Peer<'a> = Process<'a>;
118 type Probed = Message;
119
120 fn is_self(peer: Self::Peer<'_>) -> bool {
121 peer.is_self()
122 }
123
124 fn send_bytes(&self, to: Self::Peer<'_>, tag: Tag, buffer: &[u8]) {
125 to.send_with_tag(buffer, tag);
126 }
127
128 fn probe(&self, from: Self::Peer<'_>, tag: Tag) -> (Self::Probed, i64) {
129 let (message, status) = from.matched_probe_with_tag(tag);
130 (message, i64::from(status.count(u8::equivalent_datatype())))
131 }
132
133 fn receive_probed(&self, probed: Self::Probed, buffer: &mut [u8]) -> i64 {
134 let status = probed.matched_receive_into(buffer);
135 i64::from(status.count(u8::equivalent_datatype()))
136 }
137 }
138}
139
140#[cfg(feature = "runtime")]
141pub use runtime::Rt;
142#[cfg(feature = "system")]
143pub use system::System;