rlst 0.10.0

A Rust native linear algebra library.
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Array tools
//!
//! This module contains tools for working with distributed arrays.

use std::ops::Add;

use itertools::Itertools;
use mpi::{
    collective::{SystemOperation, UserOperation},
    datatype::PartitionMut,
    traits::{CommunicatorCollectives, Equivalence, Root},
};
use num::Zero;

use crate::{Max, Min, TotalCmp};

/// Distribute a sorted sequence into bins.
///
/// `bins` contains the inclusive lower bound for each half-open bucket; the
/// final bucket has no upper bound. Empty keys and empty bins produce no
/// buckets. Nonempty keys require nonempty, nondecreasing bounds and must not
/// precede the first bound. Repeated bounds deliberately produce empty buckets.
pub fn sort_to_bins<T: TotalCmp>(sorted_keys: &[T], bins: &[T]) -> Vec<usize> {
    if bins.is_empty() {
        assert!(
            sorted_keys.is_empty(),
            "nonempty keys require at least one bin"
        );
        return Vec::new();
    }
    assert!(
        bins.windows(2).all(|window| window[0].le(window[1])),
        "bin lower bounds must be nondecreasing"
    );
    if let Some(first) = sorted_keys.first() {
        assert!(bins[0].le(*first), "key precedes the first bin lower bound");
    }
    assert!(
        sorted_keys.windows(2).all(|window| window[0].le(window[1])),
        "keys passed to sort_to_bins must be sorted"
    );

    let mut counts = vec![0; bins.len()];
    for key in sorted_keys {
        // The last lower bound not greater than key selects its bucket. This
        // deliberately selects the final of repeated lower bounds.
        let bucket = bins.partition_point(|bound| bound.le(*key)) - 1;
        counts[bucket] += 1;
    }
    counts
}

/// Compute displacements on n processes from a vector of n counts of local indices.
pub fn displacements(counts: &[i32]) -> Vec<i32> {
    checked_displacements(counts).expect("counts and displacements must fit in i32")
}

fn checked_displacements(counts: &[i32]) -> Option<Vec<i32>> {
    let mut total = 0_i32;
    counts
        .iter()
        .map(|&count| {
            if count < 0 {
                return None;
            }
            let displacement = total;
            total += count;
            Some(displacement)
        })
        .collect()
}

fn collective_valid<C: CommunicatorCollectives>(comm: &C, valid: bool, message: &str) {
    let mut all_valid = false;
    comm.all_reduce_into(&valid, &mut all_valid, SystemOperation::logical_and());
    assert!(all_valid, "{message}");
}

fn collective_root_valid<C: CommunicatorCollectives>(comm: &C, root: usize, message: &str) {
    let mut roots = vec![0_usize; comm.size() as usize];
    comm.all_gather_into(&root, &mut roots);
    collective_valid(
        comm,
        root < comm.size() as usize && roots.iter().all(|&candidate| candidate == root),
        message,
    );
}

fn checked_counts_for_data(
    counts: &[usize],
    process_count: usize,
    data_len: usize,
) -> Option<Vec<i32>> {
    let converted = counts
        .iter()
        .map(|&count| i32::try_from(count))
        .collect::<Result<Vec<_>, _>>()
        .ok()?;
    (counts.len() == process_count && counts.iter().copied().sum::<usize>() == data_len)
        .then_some(converted)
}

fn checked_counts<C: CommunicatorCollectives>(
    comm: &C,
    counts: &[usize],
    data_len: usize,
) -> Vec<i32> {
    let converted = checked_counts_for_data(counts, comm.size() as usize, data_len);
    collective_valid(
        comm,
        converted.is_some(),
        "invalid all-to-all counts or data length",
    );
    let converted = converted.expect("collectively validated counts");
    collective_valid(
        comm,
        checked_displacements(&converted).is_some(),
        "all-to-all displacements overflow i32",
    );
    converted
}

/// Performs an all-to-all communication.
pub fn all_to_allv<T: Equivalence + Default + Clone, C: CommunicatorCollectives>(
    comm: &C,
    counts: &[usize],
    data: &[T],
) -> (Vec<usize>, Vec<T>) {
    let counts = checked_counts(comm, counts, data.len());
    let mut recv_counts = vec![0_i32; comm.size() as usize];
    comm.all_to_all_into(&counts, &mut recv_counts);
    collective_valid(
        comm,
        checked_displacements(&recv_counts).is_some(),
        "received counts or displacements overflow i32",
    );
    let receive_len = recv_counts
        .iter()
        .map(|&count| usize::try_from(count).expect("validated nonnegative count"))
        .sum::<usize>();
    collective_valid(comm, true, "received count total is valid");
    let mut receive_data = vec![T::default(); receive_len];
    let send_displacements = checked_displacements(&counts).expect("validated displacements");
    let receive_displacements =
        checked_displacements(&recv_counts).expect("validated displacements");
    let send_partition =
        mpi::datatype::Partition::new(data, counts.as_slice(), send_displacements.as_slice());
    let mut receive_partition = PartitionMut::new(
        &mut receive_data,
        recv_counts.as_slice(),
        receive_displacements.as_slice(),
    );
    comm.all_to_all_varcount_into(&send_partition, &mut receive_partition);
    (
        recv_counts
            .into_iter()
            .map(|count| count as usize)
            .collect(),
        receive_data,
    )
}

/// Scatter data across processes from the calling root process.
pub fn scatterv_root<T: Equivalence + Default + Clone, C: CommunicatorCollectives>(
    comm: &C,
    counts: &[usize],
    out_data: &[T],
) -> Vec<T> {
    collective_root_valid(
        comm,
        comm.rank() as usize,
        "scatter root must agree on every rank",
    );
    let counts_total = counts.iter().copied().sum::<usize>();
    let mut valid = counts.len() == comm.size() as usize
        && counts.iter().all(|&count| i32::try_from(count).is_ok())
        && counts_total == out_data.len();
    let send_counts = counts.iter().map(|&count| count as i32).collect_vec();
    valid &= checked_displacements(&send_counts).is_some();
    // The matching non-root helper receives this root-only validation before
    // either rank enters the scatter payload collective.
    comm.this_process().broadcast_into(&mut valid);
    assert!(
        valid,
        "invalid scatter counts, displacements, or data length"
    );
    let rank = comm.rank() as usize;
    let mut recv_count = 0_i32;
    comm.this_process()
        .scatter_into_root(&send_counts, &mut recv_count);
    let mut recvbuf = vec![T::default(); recv_count as usize];
    let displacements = checked_displacements(&send_counts).expect("validated displacements");
    let send_partition =
        mpi::datatype::Partition::new(out_data, send_counts.as_slice(), displacements.as_slice());
    comm.this_process()
        .scatter_varcount_into_root(&send_partition, &mut recvbuf);
    debug_assert_eq!(recvbuf.len(), send_counts[rank] as usize);
    recvbuf
}

/// Receive data scattered from `root`.
pub fn scatterv<T: Equivalence + Default + Clone>(
    comm: &impl CommunicatorCollectives,
    root: usize,
) -> Vec<T> {
    collective_root_valid(comm, root, "scatter root must agree and be in range");
    let root_process = comm.process_at_rank(root as i32);
    let mut valid = false;
    root_process.broadcast_into(&mut valid);
    assert!(
        valid,
        "invalid scatter counts, displacements, or data length"
    );
    let mut recv_count = 0_i32;
    root_process.scatter_into(&mut recv_count);
    assert!(recv_count >= 0, "MPI returned a negative scatter count");
    let mut recvbuf = vec![T::default(); recv_count as usize];
    root_process.scatter_varcount_into(&mut recvbuf);
    recvbuf
}

/// Get the minimum value across all ranks, or `None` when every rank is empty.
pub fn global_min<T: Equivalence + Default + Copy + Min<Output = T>, C: CommunicatorCollectives>(
    arr: &[T],
    comm: &C,
) -> Option<T> {
    global_extremum(arr, comm, |left, right| left.min(right))
}

/// Get the maximum value across all ranks, or `None` when every rank is empty.
pub fn global_max<T: Equivalence + Default + Copy + Max<Output = T>, C: CommunicatorCollectives>(
    arr: &[T],
    comm: &C,
) -> Option<T> {
    global_extremum(arr, comm, |left, right| left.max(right))
}

fn global_extremum<T: Equivalence + Default + Copy, C: CommunicatorCollectives>(
    arr: &[T],
    comm: &C,
    operation: impl Fn(T, T) -> T + Copy + Sync,
) -> Option<T> {
    let local = arr.iter().copied().reduce(operation);
    let occupied = !arr.is_empty();
    let mut occupancy = vec![false; comm.size() as usize];
    comm.all_gather_into(&occupied, &mut occupancy);
    let seed_rank = occupancy.iter().position(|&present| present)?;
    // Default only initializes storage on non-seed ranks before broadcast; it
    // is never used as an extremum sentinel.
    let mut seed = local.unwrap_or_default();
    comm.process_at_rank(seed_rank as i32)
        .broadcast_into(&mut seed);
    let candidate = local.unwrap_or(seed);
    let mut result = candidate;
    comm.all_reduce_into(
        &candidate,
        &mut result,
        &UserOperation::commutative(move |x, y| {
            let x: &[T] = x.downcast().expect("MPI datatype must be T");
            let y: &mut [T] = y.downcast().expect("MPI datatype must be T");
            for (&value, output) in x.iter().zip(y) {
                *output = operation(value, *output);
            }
        }),
    );
    Some(result)
}

/// Get global size of a distributed array.
pub fn global_size<T, C: CommunicatorCollectives>(arr: &[T], comm: &C) -> usize {
    let mut global_size = 0;
    comm.all_reduce_into(&arr.len(), &mut global_size, SystemOperation::sum());
    global_size
}

/// Gather a distributed array to a specific rank.
pub fn gather_to_rank<T: Equivalence + Default + Clone, C: CommunicatorCollectives>(
    arr: &[T],
    root: usize,
    comm: &C,
) -> Option<Vec<T>> {
    collective_root_valid(comm, root, "gather root must agree and be in range");
    collective_valid(
        comm,
        i32::try_from(arr.len()).is_ok(),
        "local gather count exceeds i32",
    );
    let count = arr.len() as i32;
    // All ranks learn the counts before the payload phase, so overflow is
    // rejected collectively rather than leaving a root panic to strand peers.
    let mut counts = vec![0_i32; comm.size() as usize];
    comm.all_gather_into(&count, &mut counts);
    let len = counts
        .iter()
        .map(|&count| usize::try_from(count).expect("validated nonnegative count"))
        .sum::<usize>();
    collective_valid(
        comm,
        checked_displacements(&counts).is_some(),
        "gather count displacement or receive length overflow",
    );
    let root_process = comm.process_at_rank(root as i32);
    if comm.rank() as usize == root {
        let mut data = vec![T::default(); len];
        let displacements = checked_displacements(&counts).expect("validated");
        let mut partition =
            PartitionMut::new(&mut data, counts.as_slice(), displacements.as_slice());
        root_process.gather_varcount_into_root(arr, &mut partition);
        Some(data)
    } else {
        root_process.gather_varcount_into(arr);
        None
    }
}

/// Gather an array to all processes.
pub fn gather_to_all<T: Equivalence + Default + Clone, C: CommunicatorCollectives>(
    arr: &[T],
    comm: &C,
) -> Vec<T> {
    collective_valid(
        comm,
        i32::try_from(arr.len()).is_ok(),
        "local gather count exceeds i32",
    );
    let mut counts = vec![0_i32; comm.size() as usize];
    comm.all_gather_into(&(arr.len() as i32), &mut counts);
    collective_valid(
        comm,
        checked_displacements(&counts).is_some(),
        "gather count displacement overflow",
    );
    let len = counts
        .iter()
        .map(|&count| usize::try_from(count).expect("validated nonnegative count"))
        .sum::<usize>();
    let mut data = vec![T::default(); len];
    let displacements = checked_displacements(&counts).expect("validated");
    let mut partition = PartitionMut::new(&mut data, counts.as_slice(), displacements.as_slice());
    comm.all_gather_varcount_into(arr, &mut partition);
    data
}

/// Perform a global inclusive cumulative sum operation.
pub fn global_inclusive_cumsum<
    T: Equivalence + Zero + Copy + Add<Output = T>,
    C: CommunicatorCollectives,
>(
    arr: &[T],
    comm: &C,
) -> Vec<T> {
    let mut scan = arr
        .iter()
        .scan(T::zero(), |sum, value| {
            *sum = *sum + *value;
            Some(*sum)
        })
        .collect_vec();
    let local_total = scan.last().copied().unwrap_or_else(T::zero);
    let mut offset = T::zero();
    comm.exclusive_scan_into(&local_total, &mut offset, SystemOperation::sum());
    if comm.rank() == 0 {
        offset = T::zero();
    }
    for value in &mut scan {
        *value = *value + offset;
    }
    scan
}

/// Return the first value on the next nonempty rank, if one exists.
pub fn communicate_back<T: Equivalence + Default + Clone, C: CommunicatorCollectives>(
    arr: &[T],
    comm: &C,
) -> Option<T> {
    let occupied = !arr.is_empty();
    let mut occupancy = vec![false; comm.size() as usize];
    comm.all_gather_into(&occupied, &mut occupancy);
    let first_values = gather_to_all(&arr[..std::cmp::min(arr.len(), 1)], comm);
    let mut value_index = 0;
    let mut first_by_rank = vec![None; comm.size() as usize];
    for (rank, &present) in occupancy.iter().enumerate() {
        if present {
            first_by_rank[rank] = Some(first_values[value_index].clone());
            value_index += 1;
        }
    }
    first_by_rank
        .into_iter()
        .skip(comm.rank() as usize + 1)
        .flatten()
        .next()
}

/// Check whether a distributed array is sorted.
pub fn is_sorted_array<T: Equivalence + Default + Clone + TotalCmp, C: CommunicatorCollectives>(
    arr: &[T],
    comm: &C,
) -> bool {
    let local_sorted = arr.windows(2).all(|pair| pair[0].le(pair[1]));
    let boundary_sorted = match (arr.last(), communicate_back(arr, comm)) {
        (Some(last), Some(next)) => last.le(next),
        _ => true,
    };
    let mut sorted = false;
    comm.all_reduce_into(
        &(local_sorted && boundary_sorted),
        &mut sorted,
        SystemOperation::logical_and(),
    );
    sorted
}

#[cfg(test)]
mod test {
    use super::checked_displacements;
    use crate::distributed_tools::{displacements, sort_to_bins};

    #[test]
    fn test_sort_to_bins() {
        assert_eq!(sort_to_bins::<i32>(&[], &[]), []);
        assert_eq!(sort_to_bins(&[1, 1, 2], &[1, 1, 2]), [0, 2, 1]);
    }

    #[test]
    #[should_panic(expected = "nonempty keys require at least one bin")]
    fn sort_to_bins_rejects_nonempty_keys_without_bins() {
        sort_to_bins(&[1_i32], &[]);
    }

    #[test]
    #[should_panic(expected = "bin lower bounds must be nondecreasing")]
    fn sort_to_bins_rejects_descending_bounds() {
        sort_to_bins(&[], &[1_i32, 0]);
    }

    #[test]
    #[should_panic(expected = "key precedes the first bin lower bound")]
    fn sort_to_bins_rejects_keys_before_first_bound() {
        sort_to_bins(&[0_i32], &[1]);
    }

    #[test]
    fn test_displacements() {
        assert_eq!(displacements(&[3, 4, 5]), [0, 3, 7]);
        assert_eq!(checked_displacements(&[-1]), None);
    }
}