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
//! Integration test for MPI Group set-theoretic operations.
//!
//! Exercises `Group::union`, `Group::intersection`, and `Group::difference`
//! using a 4-rank world. 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_set_ops
use ferrompi::{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_set_ops requires exactly 4 processes, got {size}"
);
// local_ok tracks whether this rank passed all its assertions.
let mut local_ok = true;
// Build the base groups shared across all tests.
// gw = world group; g1 = {0, 1}; g2 = {1, 2}
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);
}
};
let g1 = 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 g2 = 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);
}
};
// ========================================================================
// Test 1: g1.union(&g2).size() == 3
// g1 = {0, 1}, g2 = {1, 2} → union = {0, 1, 2}
// ========================================================================
{
match g1.union(&g2) {
Ok(u) => match u.size() {
Ok(s) if s == 3 => {
if rank == 0 {
println!("PASS: Test 1 — g1.union(&g2).size() = {s}");
}
}
Ok(s) => {
eprintln!("rank {rank}: FAIL Test 1: union size = {s}, expected 3");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 1: union.size() error: {e}");
local_ok = false;
}
},
Err(e) => {
eprintln!("rank {rank}: FAIL Test 1: g1.union(&g2) failed: {e}");
local_ok = false;
}
}
}
// ========================================================================
// Test 2: g1.intersection(&g2).size() == 1
// g1 = {0, 1}, g2 = {1, 2} → intersection = {1}
// ========================================================================
{
match g1.intersection(&g2) {
Ok(i) => match i.size() {
Ok(s) if s == 1 => {
if rank == 0 {
println!("PASS: Test 2 — g1.intersection(&g2).size() = {s}");
}
}
Ok(s) => {
eprintln!("rank {rank}: FAIL Test 2: intersection size = {s}, expected 1");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 2: intersection.size() error: {e}");
local_ok = false;
}
},
Err(e) => {
eprintln!("rank {rank}: FAIL Test 2: g1.intersection(&g2) failed: {e}");
local_ok = false;
}
}
}
// ========================================================================
// Test 3: g1.difference(&g2).size() == 1
// g1 = {0, 1}, g2 = {1, 2} → difference = {0}
// ========================================================================
{
match g1.difference(&g2) {
Ok(d) => match d.size() {
Ok(s) if s == 1 => {
if rank == 0 {
println!("PASS: Test 3 — g1.difference(&g2).size() = {s}");
}
}
Ok(s) => {
eprintln!("rank {rank}: FAIL Test 3: difference size = {s}, expected 1");
local_ok = false;
}
Err(e) => {
eprintln!("rank {rank}: FAIL Test 3: difference.size() error: {e}");
local_ok = false;
}
},
Err(e) => {
eprintln!("rank {rank}: FAIL Test 3: g1.difference(&g2) failed: {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 set-ops assertion");
}
std::process::exit(1);
}
if rank == 0 {
println!();
println!("========================================");
println!("All group set-ops tests passed!");
println!("========================================");
}
}