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
//! Integration test for `Group::compare` and `GroupComparison`.
//!
//! Exercises all four acceptance criteria from ticket-048:
//!
//! 1. `include(&[1, 2])` vs `include(&[2, 1])` — same members, different
//! ordering → `Similar`. Using reversed ranks forces MPI_SIMILAR even on
//! MPICH, which canonicalises equal-order groups to MPI_IDENT.
//! 2. `include(&[0, 1, 2])` vs `include(&[2, 1, 0])` — same member set,
//! reversed order → `Similar`.
//! 3. `include(&[0, 1])` vs `include(&[2, 3])` — disjoint rank sets →
//! `Unequal`.
//! 4. `gw.compare(&gw)` — same group object → `Identical`.
//!
//! All assertions are guarded by a sentinel `allreduce(Min)` before any
//! `process::exit` so that no rank exits while others are still inside MPI
//! collective calls.
//!
//! Run with: mpiexec -n 4 ./target/debug/examples/test_group_compare
use ferrompi::{GroupComparison, Mpi, ReduceOp};
fn main() {
let mpi = Mpi::init().expect("MPI init failed");
let world = mpi.world();
let rank = world.rank();
let size = world.size();
assert!(
size == 4,
"test_group_compare requires exactly 4 processes, got {size}"
);
let mut local_ok = true;
// Obtain the world group once; used as the source for all sub-groups.
let gw = match world.group() {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: world.group() failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
// ========================================================================
// Test 1: same member set, different ordering → Similar
//
// g1 = include([1, 2]) — ordering: rank 1 first, rank 2 second
// g2 = include([2, 1]) — ordering: rank 2 first, rank 1 second
//
// Both groups contain the same two processes {1, 2} but in a different
// order. MPI guarantees MPI_SIMILAR for this case on all conforming
// implementations. (Using same-order groups with MPICH can yield
// MPI_IDENT instead of MPI_SIMILAR because MPICH may canonicalise
// identical-order groups to the same internal object.)
// ========================================================================
let g1 = match gw.include(&[1, 2]) {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: gw.include(&[1,2]) failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
let g2 = match gw.include(&[2, 1]) {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: gw.include(&[2,1]) failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
match g1.compare(&g2) {
Ok(GroupComparison::Similar) => {
if rank == 0 {
println!("PASS: Test 1 — same members, different order → Similar");
}
}
Ok(other) => {
eprintln!("rank {rank}: FAIL Test 1: expected Similar, got {other:?}");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 1: compare() error: {e}");
local_ok = false;
}
}
// ========================================================================
// Test 2: same member set, reversed 3-element ordering → Similar
//
// g_fwd = include([0, 1, 2])
// g_rev = include([2, 1, 0])
//
// Same set {0, 1, 2}, different rank order → MPI_SIMILAR.
// ========================================================================
let g_fwd = match gw.include(&[0, 1, 2]) {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: gw.include(&[0,1,2]) failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
let g_rev = match gw.include(&[2, 1, 0]) {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: gw.include(&[2,1,0]) failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
match g_fwd.compare(&g_rev) {
Ok(GroupComparison::Similar) => {
if rank == 0 {
println!("PASS: Test 2 — same members, reversed 3-element order → Similar");
}
}
Ok(other) => {
eprintln!("rank {rank}: FAIL Test 2: expected Similar, got {other:?}");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 2: compare() error: {e}");
local_ok = false;
}
}
// ========================================================================
// Test 3: different rank sets → Unequal
//
// g_lo = include([0, 1])
// g_hi = include([2, 3])
//
// Completely disjoint sets → MPI_UNEQUAL.
// ========================================================================
let g_lo = match gw.include(&[0, 1]) {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: gw.include(&[0,1]) failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
let g_hi = match gw.include(&[2, 3]) {
Ok(g) => g,
Err(e) => {
eprintln!("rank {rank}: FAIL: gw.include(&[2,3]) failed: {e}");
let _ = world.allreduce_scalar(0i32, ReduceOp::Min);
std::process::exit(1);
}
};
match g_lo.compare(&g_hi) {
Ok(GroupComparison::Unequal) => {
if rank == 0 {
println!("PASS: Test 3 — disjoint rank sets → Unequal");
}
}
Ok(other) => {
eprintln!("rank {rank}: FAIL Test 3: expected Unequal, got {other:?}");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 3: compare() error: {e}");
local_ok = false;
}
}
// ========================================================================
// Test 4: same group object → Identical
//
// gw.compare(&gw) — both sides are the same MPI group object.
// MPI guarantees MPI_IDENT for this case.
// ========================================================================
match gw.compare(&gw) {
Ok(GroupComparison::Identical) => {
if rank == 0 {
println!("PASS: Test 4 — same group object → Identical");
}
}
Ok(other) => {
eprintln!("rank {rank}: FAIL Test 4: expected Identical, got {other:?}");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 4: compare() error: {e}");
local_ok = false;
}
}
// ========================================================================
// Sentinel allreduce(Min) — gate process::exit so no rank exits early
// ========================================================================
let global_ok = world
.allreduce_scalar(local_ok as i32, ReduceOp::Min)
.expect("allreduce_scalar failed");
if global_ok == 0 {
if rank == 0 {
eprintln!("FAIL: at least one rank failed a group_compare assertion");
}
std::process::exit(1);
}
if rank == 0 {
println!();
println!("========================================");
println!("All group_compare tests passed!");
println!("========================================");
}
}