flynnel 0.6.0

K-aware NUMA-aware work-stealing scheduler with extended-Flynn-taxonomy dispatch (MIMD / SIMC / MIMC / MISD / SIMT / MIMT): a rayon-shaped pool reshaped around per-call execution-class plans with first-class CPU+GPU hybrid (join_hybrid, hybrid_pipeline) and per-call dispatch profiles.
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
//! The array kernels: element-wise maps, pairwise zips, reductions, the
//! running total, histograms, the dot product and the sort.

use super::{
    ARRAY_BLOCK_MIN, Block, Blocks, Job, MAX_BLOCKS, Parts, Refusal, Slots, Tracker, tracked,
};

/// Blocks in the first phase of a job cut on `blocks`, or `None` when
/// the input is empty and there is nothing to run.
fn opening(blocks: &Blocks) -> Option<usize> {
    (blocks.count() > 0).then_some(blocks.count())
}

// ---------------------------------------------------------------------
// Operations
// ---------------------------------------------------------------------

/// An element-wise operation over one array.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MapOp {
    /// `x * x`.
    #[default]
    Square,
    /// The magnitude.
    Abs,
    /// `-x`.
    Negate,
    /// `1 / x`, which answers an infinity at zero.
    Reciprocal,
    /// The square root, which answers NaN below zero.
    Sqrt,
    /// The natural logarithm, NaN below zero and negative infinity at
    /// zero.
    Log,
    /// The base-2 logarithm, on the same terms as Log.
    Log2,
    /// `e` to the power x.
    Exp,
    /// To the nearest integer, halves away from zero.
    Round,
    /// To the largest integer at or below x.
    Floor,
    /// To the smallest integer at or above x.
    Ceiling,
    /// Held within Min and Max, both of which are required.
    Clamp,
    /// Multiplied by Factor.
    Scale,
    /// Added to Addend.
    Offset,
}

impl MapOp {
    /// Every operation, in declaration order.
    pub const ALL: [Self; 14] = [
        Self::Square,
        Self::Abs,
        Self::Negate,
        Self::Reciprocal,
        Self::Sqrt,
        Self::Log,
        Self::Log2,
        Self::Exp,
        Self::Round,
        Self::Floor,
        Self::Ceiling,
        Self::Clamp,
        Self::Scale,
        Self::Offset,
    ];

    /// Every operation's name, in the order of [`Self::ALL`].
    pub const NAMES: [&'static str; 14] = [
        "Square",
        "Abs",
        "Negate",
        "Reciprocal",
        "Sqrt",
        "Log",
        "Log2",
        "Exp",
        "Round",
        "Floor",
        "Ceiling",
        "Clamp",
        "Scale",
        "Offset",
    ];
}

/// A pairwise operation over two arrays of the same length.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ZipOp {
    /// `left + right`.
    #[default]
    Add,
    /// `left - right`.
    Subtract,
    /// `left * right`.
    Multiply,
    /// `left / right`, which answers an infinity where right is zero.
    Divide,
    /// The smaller of the two.
    Min,
    /// The larger of the two.
    Max,
}

impl ZipOp {
    /// Every operation, in declaration order.
    pub const ALL: [Self; 6] = [
        Self::Add,
        Self::Subtract,
        Self::Multiply,
        Self::Divide,
        Self::Min,
        Self::Max,
    ];

    /// Every operation's name, in the order of [`Self::ALL`].
    pub const NAMES: [&'static str; 6] = ["Add", "Subtract", "Multiply", "Divide", "Min", "Max"];
}

/// A reduction over one array to a single number.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ReduceOp {
    /// The total.
    #[default]
    Sum,
    /// The smallest element.
    Min,
    /// The largest element.
    Max,
    /// The arithmetic mean.
    Mean,
    /// The population variance, by the pairwise-combining form rather
    /// than a sum of squares, so a large mean does not cancel the spread
    /// away.
    Variance,
    /// The product of every element.
    Product,
    /// How many elements sit within Min and Max inclusive.
    CountMatching,
}

impl ReduceOp {
    /// Every reduction, in declaration order.
    pub const ALL: [Self; 7] = [
        Self::Sum,
        Self::Min,
        Self::Max,
        Self::Mean,
        Self::Variance,
        Self::Product,
        Self::CountMatching,
    ];

    /// Every reduction's name, in the order of [`Self::ALL`].
    pub const NAMES: [&'static str; 7] = [
        "Sum",
        "Min",
        "Max",
        "Mean",
        "Variance",
        "Product",
        "CountMatching",
    ];
}

/// The operands a map operation may take. Each operation reads the ones
/// it needs and ignores the rest.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MapOperands {
    /// The lower bound, for Clamp.
    pub min: Option<f64>,
    /// The upper bound, for Clamp.
    pub max: Option<f64>,
    /// The multiplier, for Scale.
    pub factor: Option<f64>,
    /// The addend, for Offset.
    pub addend: Option<f64>,
}

/// One map operation with its operands read and checked, ready to apply
/// to one element or to a whole slice.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Element {
    /// `x * x`.
    Square,
    /// The magnitude.
    Abs,
    /// `-x`.
    Negate,
    /// `1 / x`.
    Reciprocal,
    /// The square root.
    Sqrt,
    /// The natural logarithm.
    Log,
    /// The base-2 logarithm.
    Log2,
    /// `e` to the power x.
    Exp,
    /// To the nearest integer, halves away from zero.
    Round,
    /// To the largest integer at or below x.
    Floor,
    /// To the smallest integer at or above x.
    Ceiling,
    /// Held within the two bounds.
    Clamp {
        /// The lower bound.
        lo: f64,
        /// The upper bound, not below `lo`.
        hi: f64,
    },
    /// Multiplied by the factor.
    Scale(f64),
    /// Added to the addend.
    Offset(f64),
}

/// `f` applied to every element of `xs` in place. Generic, so each
/// operation's loop is compiled on its own and the compiler sees the
/// arithmetic rather than a call per element, and always inlined, so each
/// level's copy of a block compiles the loop for that level.
#[inline(always)]
fn each(xs: &mut [f64], f: impl Fn(f64) -> f64) {
    for x in xs.iter_mut() {
        *x = f(*x);
    }
}

impl Element {
    /// `op` with the operands it needs, refused in the module's words when
    /// one is missing or the bounds cannot hold a value.
    pub fn new(op: MapOp, operands: MapOperands) -> Result<Self, Refusal> {
        Ok(match op {
            MapOp::Clamp => {
                let (Some(lo), Some(hi)) = (operands.min, operands.max) else {
                    return Err(Refusal::argument("Clamp needs both Min and Max"));
                };
                // f64::clamp panics on a NaN bound, which on a worker would
                // take the whole dispatch down.
                if lo.is_nan() || hi.is_nan() {
                    return Err(Refusal::argument("Min and Max must be numbers, not NaN"));
                }
                if lo > hi {
                    return Err(Refusal::argument("Min must not be above Max"));
                }
                Self::Clamp { lo, hi }
            }
            MapOp::Scale => match operands.factor {
                Some(k) => Self::Scale(k),
                None => return Err(Refusal::argument("Scale needs Factor")),
            },
            MapOp::Offset => match operands.addend {
                Some(k) => Self::Offset(k),
                None => return Err(Refusal::argument("Offset needs Addend")),
            },
            MapOp::Square => Self::Square,
            MapOp::Abs => Self::Abs,
            MapOp::Negate => Self::Negate,
            MapOp::Reciprocal => Self::Reciprocal,
            MapOp::Sqrt => Self::Sqrt,
            MapOp::Log => Self::Log,
            MapOp::Log2 => Self::Log2,
            MapOp::Exp => Self::Exp,
            MapOp::Round => Self::Round,
            MapOp::Floor => Self::Floor,
            MapOp::Ceiling => Self::Ceiling,
        })
    }

    /// The operation applied to one value.
    pub fn apply(self, x: f64) -> f64 {
        match self {
            Self::Square => x * x,
            Self::Abs => x.abs(),
            Self::Negate => -x,
            Self::Reciprocal => 1.0 / x,
            Self::Sqrt => x.sqrt(),
            Self::Log => x.ln(),
            Self::Log2 => x.log2(),
            Self::Exp => x.exp(),
            Self::Round => x.round(),
            Self::Floor => x.floor(),
            Self::Ceiling => x.ceil(),
            Self::Clamp { lo, hi } => x.clamp(lo, hi),
            Self::Scale(k) => x * k,
            Self::Offset(k) => x + k,
        }
    }

    /// The operation applied to every element of `xs` in place, the
    /// choice of operation made once for the slice.
    #[inline(always)]
    pub fn apply_slice(self, xs: &mut [f64]) {
        match self {
            Self::Square => each(xs, |x| x * x),
            Self::Abs => each(xs, f64::abs),
            Self::Negate => each(xs, |x| -x),
            Self::Reciprocal => each(xs, |x| 1.0 / x),
            Self::Sqrt => each(xs, f64::sqrt),
            Self::Log => each(xs, f64::ln),
            Self::Log2 => each(xs, f64::log2),
            Self::Exp => each(xs, f64::exp),
            Self::Round => each(xs, f64::round),
            Self::Floor => each(xs, f64::floor),
            Self::Ceiling => each(xs, f64::ceil),
            Self::Clamp { lo, hi } => each(xs, |x| x.clamp(lo, hi)),
            Self::Scale(k) => each(xs, |x| x * k),
            Self::Offset(k) => each(xs, |x| x + k),
        }
    }
}

// ---------------------------------------------------------------------
// Map and zip
// ---------------------------------------------------------------------

/// [`map`]'s job: one phase, each block transforming its own range in
/// place.
pub struct MapJob<'a> {
    parts: Parts<'a, f64>,
    element: Element,
    tracker: Tracker,
}

/// Apply `op` to every element of `data` in place.
///
/// The module's Invoke-FlynnelMap runs it over its own copy of the input
/// and answers that copy; Update-FlynnelArray runs it over the caller's
/// pinned buffer. A caller that must keep its input passes a copy.
pub fn map(data: &mut [f64], op: MapOp, operands: MapOperands) -> Result<MapJob<'_>, Refusal> {
    let element = Element::new(op, operands)?;
    let blocks = Blocks::new(data.len(), ARRAY_BLOCK_MIN, MAX_BLOCKS);
    Ok(MapJob {
        tracker: Tracker::new(opening(&blocks)),
        parts: Parts::new(data, &blocks),
        element,
    })
}

impl Block for MapJob<'_> {
    #[inline(always)]
    fn run_block(&self, _phase: usize, block: usize) {
        let mut part = self.parts.lock(block);
        self.element.apply_slice(&mut part);
    }
}

impl Job for MapJob<'_> {
    type Answer = ();

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        self.tracker.advance(None);
        Ok(())
    }

    fn finish(self) -> Result<(), Refusal> {
        self.tracker.check_finished()
    }
}

/// [`zip`]'s job: one phase, each block combining its own range of the
/// left operand with the right one, in place.
pub struct ZipJob<'a> {
    parts: Parts<'a, f64>,
    right: &'a [f64],
    blocks: Blocks,
    op: ZipOp,
    tracker: Tracker,
}

/// Combine `left` with `right` element by element, writing into `left`.
pub fn zip<'a>(left: &'a mut [f64], right: &'a [f64], op: ZipOp) -> Result<ZipJob<'a>, Refusal> {
    if left.len() != right.len() {
        return Err(Refusal::argument(format!(
            "Left has {} element(s) and Right has {}; a pairwise operation needs the same length \
             on both",
            left.len(),
            right.len()
        )));
    }
    let blocks = Blocks::new(left.len(), ARRAY_BLOCK_MIN, MAX_BLOCKS);
    Ok(ZipJob {
        tracker: Tracker::new(opening(&blocks)),
        parts: Parts::new(left, &blocks),
        right,
        blocks,
        op,
    })
}

/// `f` applied to each pair, writing into the left one.
#[inline(always)]
fn pairwise(left: &mut [f64], right: &[f64], f: impl Fn(f64, f64) -> f64) {
    for (a, &b) in left.iter_mut().zip(right) {
        *a = f(*a, b);
    }
}

impl Block for ZipJob<'_> {
    #[inline(always)]
    fn run_block(&self, _phase: usize, block: usize) {
        let mut part = self.parts.lock(block);
        let right = &self.right[self.blocks.range(block)];
        match self.op {
            ZipOp::Add => pairwise(&mut part, right, |a, b| a + b),
            ZipOp::Subtract => pairwise(&mut part, right, |a, b| a - b),
            ZipOp::Multiply => pairwise(&mut part, right, |a, b| a * b),
            ZipOp::Divide => pairwise(&mut part, right, |a, b| a / b),
            // The right operand first. The compiler builds min and max from
            // a compare that answers its second operand when either is NaN
            // and a select that repairs that case with the first operand.
            // Written `a.min(b)`, the repair value is the one already in the
            // slot, so at AVX2 the compiler writes the slot with a masked
            // store that leaves those lanes alone, which Zen runs slower
            // than the baseline's blend: 1.4 to 1.6 times on a Ryzen 9
            // 7900X. Written `b.min(a)`, the repair value is the right
            // operand, so every level blends in registers and stores whole.
            // The two orders answer alike but for which zero a +0 and -0
            // pair gives and which of two NaNs survives.
            ZipOp::Min => pairwise(&mut part, right, |a, b| b.min(a)),
            ZipOp::Max => pairwise(&mut part, right, |a, b| b.max(a)),
        }
    }
}

impl Job for ZipJob<'_> {
    type Answer = ();

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        self.tracker.advance(None);
        Ok(())
    }

    fn finish(self) -> Result<(), Refusal> {
        self.tracker.check_finished()
    }
}

// ---------------------------------------------------------------------
// Reductions
// ---------------------------------------------------------------------

/// What a reduction answered.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Reduction {
    /// The operation that produced it.
    pub operation: ReduceOp,
    /// How many elements it read.
    pub count: u64,
    /// The answer. `None` for a reduction with no defined value over an
    /// empty input, which is every one but Sum, Product and
    /// CountMatching.
    pub value: Option<f64>,
}

/// The running state of a pairwise-combining mean and variance.
///
/// Carried rather than a sum of squares because the sum-of-squares form
/// subtracts two large nearly equal numbers, and over a long array with a
/// large mean that cancellation is the whole answer.
#[derive(Clone, Copy, Debug, Default)]
struct Moments {
    n: u64,
    mean: f64,
    m2: f64,
}

impl Moments {
    #[inline(always)]
    fn push(mut self, x: f64) -> Self {
        self.n += 1;
        let delta = x - self.mean;
        self.mean += delta / self.n as f64;
        self.m2 += delta * (x - self.mean);
        self
    }

    fn merge(self, other: Self) -> Self {
        if self.n == 0 {
            return other;
        }
        if other.n == 0 {
            return self;
        }
        let n = self.n + other.n;
        let delta = other.mean - self.mean;
        let mean = self.mean + delta * (other.n as f64 / n as f64);
        let m2 = self.m2 + other.m2 + delta * delta * (self.n as f64 * other.n as f64 / n as f64);
        Self { n, mean, m2 }
    }
}

/// One block's part of a reduction.
#[derive(Clone, Copy, Debug)]
enum Partial {
    Value(f64),
    Count(u64),
    Moments(Moments),
}

/// [`reduce`]'s job: one phase of per-block partials, folded in block
/// order.
pub struct ReduceJob<'a> {
    input: &'a [f64],
    op: ReduceOp,
    bounds: (f64, f64),
    blocks: Blocks,
    partials: Slots<Partial>,
    folded: Vec<Partial>,
    tracker: Tracker,
}

/// Reduce `input` to one number. `min` and `max` are CountMatching's
/// bounds and are read by no other operation.
pub fn reduce(
    input: &[f64],
    op: ReduceOp,
    min: Option<f64>,
    max: Option<f64>,
) -> Result<ReduceJob<'_>, Refusal> {
    let bounds = if op == ReduceOp::CountMatching {
        let (Some(lo), Some(hi)) = (min, max) else {
            return Err(Refusal::argument("CountMatching needs both Min and Max"));
        };
        if lo > hi {
            return Err(Refusal::argument("Min must not be above Max"));
        }
        (lo, hi)
    } else {
        (f64::NEG_INFINITY, f64::INFINITY)
    };
    let blocks = Blocks::new(input.len(), ARRAY_BLOCK_MIN, MAX_BLOCKS);
    Ok(ReduceJob {
        input,
        op,
        bounds,
        partials: Slots::new(blocks.count()),
        folded: Vec::new(),
        tracker: Tracker::new(opening(&blocks)),
        blocks,
    })
}

impl ReduceJob<'_> {
    #[inline(always)]
    fn partial(&self, s: &[f64]) -> Partial {
        match self.op {
            ReduceOp::Sum => Partial::Value(s.iter().sum::<f64>()),
            ReduceOp::Product => Partial::Value(s.iter().product::<f64>()),
            ReduceOp::Min => Partial::Value(s.iter().fold(f64::INFINITY, |a, &x| a.min(x))),
            ReduceOp::Max => Partial::Value(s.iter().fold(f64::NEG_INFINITY, |a, &x| a.max(x))),
            ReduceOp::Mean | ReduceOp::Variance => {
                Partial::Moments(s.iter().fold(Moments::default(), |a, &x| a.push(x)))
            }
            ReduceOp::CountMatching => {
                let (lo, hi) = self.bounds;
                Partial::Count(s.iter().filter(|&&x| x >= lo && x <= hi).count() as u64)
            }
        }
    }
}

/// The value inside a partial that must be one, or the refusal naming
/// the defect when a block produced the wrong kind.
fn value_of(p: &Partial) -> Result<f64, Refusal> {
    match p {
        Partial::Value(v) => Ok(*v),
        other => Err(Refusal::internal(format!(
            "a reduction block produced {other:?} where a value was due"
        ))),
    }
}

impl Block for ReduceJob<'_> {
    #[inline(always)]
    fn run_block(&self, _phase: usize, block: usize) {
        let p = self.partial(&self.input[self.blocks.range(block)]);
        self.partials.put(block, p);
    }
}

impl Job for ReduceJob<'_> {
    type Answer = Reduction;

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        self.folded = self.partials.take_all()?;
        self.tracker.advance(None);
        Ok(())
    }

    fn finish(self) -> Result<Reduction, Refusal> {
        self.tracker.check_finished()?;
        let n = self.input.len();
        let value = match self.op {
            ReduceOp::Sum => Some(
                self.folded
                    .iter()
                    .map(value_of)
                    .try_fold(0.0f64, |a, v| v.map(|v| a + v))?,
            ),
            ReduceOp::Product => Some(
                self.folded
                    .iter()
                    .map(value_of)
                    .try_fold(1.0f64, |a, v| v.map(|v| a * v))?,
            ),
            ReduceOp::CountMatching => {
                let mut total = 0u64;
                for p in &self.folded {
                    match p {
                        Partial::Count(c) => total += c,
                        other => {
                            return Err(Refusal::internal(format!(
                                "a CountMatching block produced {other:?}"
                            )));
                        }
                    }
                }
                Some(total as f64)
            }
            // No value over an empty input: a zero would read as a
            // measured answer, so the value is absent and the count says
            // why.
            ReduceOp::Min | ReduceOp::Max | ReduceOp::Mean | ReduceOp::Variance if n == 0 => None,
            ReduceOp::Min => Some(
                self.folded
                    .iter()
                    .map(value_of)
                    .try_fold(f64::INFINITY, |a, v| v.map(|v| a.min(v)))?,
            ),
            ReduceOp::Max => Some(
                self.folded
                    .iter()
                    .map(value_of)
                    .try_fold(f64::NEG_INFINITY, |a, v| v.map(|v| a.max(v)))?,
            ),
            ReduceOp::Mean | ReduceOp::Variance => {
                let mut m = Moments::default();
                for p in &self.folded {
                    match p {
                        Partial::Moments(b) => m = m.merge(*b),
                        other => {
                            return Err(Refusal::internal(format!(
                                "a {:?} block produced {other:?}",
                                self.op
                            )));
                        }
                    }
                }
                Some(if self.op == ReduceOp::Mean {
                    m.mean
                } else {
                    m.m2 / m.n as f64
                })
            }
        };
        Ok(Reduction {
            operation: self.op,
            count: n as u64,
            value,
        })
    }
}

// ---------------------------------------------------------------------
// The running total
// ---------------------------------------------------------------------

/// [`prefix_sum`]'s job: block sums, a serial scan of them, then each
/// block's own scan from its offset, in place.
pub struct PrefixSumJob<'a> {
    parts: Parts<'a, f64>,
    sums: Slots<f64>,
    offsets: Vec<f64>,
    blocks: Blocks,
    tracker: Tracker,
}

/// Replace every element of `data` with the inclusive running total up
/// to it. A caller that must keep its input passes a copy.
pub fn prefix_sum(data: &mut [f64]) -> PrefixSumJob<'_> {
    let blocks = Blocks::new(data.len(), ARRAY_BLOCK_MIN, MAX_BLOCKS);
    PrefixSumJob {
        tracker: Tracker::new(opening(&blocks)),
        sums: Slots::new(blocks.count()),
        offsets: Vec::new(),
        parts: Parts::new(data, &blocks),
        blocks,
    }
}

impl Block for PrefixSumJob<'_> {
    #[inline(always)]
    fn run_block(&self, phase: usize, block: usize) {
        let mut part = self.parts.lock(block);
        if phase == 0 {
            self.sums.put(block, part.iter().sum::<f64>());
        } else {
            let mut acc = self.offsets[block];
            for x in part.iter_mut() {
                acc += *x;
                *x = acc;
            }
        }
    }
}

impl Job for PrefixSumJob<'_> {
    type Answer = ();

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        if phase == 0 {
            let sums = self.sums.take_all()?;
            let mut running = 0.0f64;
            self.offsets = sums
                .iter()
                .map(|s| {
                    let at = running;
                    running += *s;
                    at
                })
                .collect();
            self.tracker.advance(Some(self.blocks.count()));
        } else {
            self.tracker.advance(None);
        }
        Ok(())
    }

    fn finish(self) -> Result<(), Refusal> {
        self.tracker.check_finished()
    }
}

// ---------------------------------------------------------------------
// Histograms
// ---------------------------------------------------------------------

/// One bin of a histogram.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HistogramBin {
    /// The bin's position, counting from zero.
    pub index: u32,
    /// The lowest value the bin takes.
    pub low: f64,
    /// The lowest value the next bin takes. The last bin takes this value
    /// itself, so the whole range is covered.
    pub high: f64,
    /// How many elements landed here.
    pub count: u64,
}

/// A whole histogram, with the counts in bin order.
#[derive(Clone, Debug, PartialEq)]
pub struct Histogram {
    /// The lowest value the first bin takes.
    pub low: f64,
    /// The highest value the last bin takes, which that bin takes itself.
    pub high: f64,
    /// How wide one bin is. Zero when every element has one value, which
    /// puts all of them in the first bin.
    pub width: f64,
    /// How many elements landed in each bin, as long as the bins asked
    /// for.
    pub counts: Vec<u64>,
}

impl Histogram {
    /// One row per bin: bin `i` runs from `low + width * i` to
    /// `low + width * (i + 1)`.
    pub fn bins(&self) -> Vec<HistogramBin> {
        self.counts
            .iter()
            .enumerate()
            .map(|(i, &count)| HistogramBin {
                index: i as u32,
                low: self.low + self.width * i as f64,
                high: self.low + self.width * (i + 1) as f64,
                count,
            })
            .collect()
    }
}

/// [`histogram`]'s job: the data's range when a bound is missing, then
/// per-block bin counts added in block order.
pub struct HistogramJob<'a> {
    input: &'a [f64],
    bins: usize,
    given: (Option<f64>, Option<f64>),
    extremes: Slots<(f64, f64)>,
    extreme_blocks: Blocks,
    counts: Slots<Vec<u64>>,
    bin_blocks: Blocks,
    /// The range and width, once known.
    scale: Option<(f64, f64, f64)>,
    binning_phase: usize,
    total: Option<Vec<u64>>,
    tracker: Tracker,
}

/// Bin `input` into `bins` equal-width buckets over `[min, max]`, each
/// bound taken from the data where it is not given.
///
/// A block holds at least four elements for every bin, so the per-block
/// counts it adds together total at most a quarter of the input's own
/// size and the fold stays small beside the binning.
pub fn histogram(
    input: &[f64],
    bins: u32,
    min: Option<f64>,
    max: Option<f64>,
) -> Result<HistogramJob<'_>, Refusal> {
    if bins == 0 {
        return Err(Refusal::argument("Bins must be at least one"));
    }
    let n = input.len();
    let bins = bins as usize;
    let extreme_blocks = Blocks::new(n, ARRAY_BLOCK_MIN, MAX_BLOCKS);
    let bin_blocks = Blocks::new(n, ARRAY_BLOCK_MIN.max(bins.saturating_mul(4)), MAX_BLOCKS);
    let mut job = HistogramJob {
        input,
        bins,
        given: (min, max),
        extremes: Slots::new(0),
        extreme_blocks,
        counts: Slots::new(0),
        bin_blocks,
        scale: None,
        binning_phase: 0,
        total: None,
        tracker: Tracker::new(None),
    };
    if n == 0 {
        return Ok(job);
    }
    match (min, max) {
        (Some(lo), Some(hi)) => {
            job.scale = Some(scale_for(lo, hi, bins)?);
            job.counts = Slots::new(bin_blocks.count());
            job.tracker = Tracker::new(Some(bin_blocks.count()));
        }
        _one_missing => {
            job.binning_phase = 1;
            job.extremes = Slots::new(extreme_blocks.count());
            job.tracker = Tracker::new(Some(extreme_blocks.count()));
        }
    }
    Ok(job)
}

/// The range and bin width for `[lo, hi]`, refused in the module's words
/// when the range cannot be binned.
fn scale_for(lo: f64, hi: f64, bins: usize) -> Result<(f64, f64, f64), Refusal> {
    if !(lo.is_finite() && hi.is_finite()) {
        return Err(Refusal::argument(
            "the range is not finite; pass Min and Max when the data holds an infinity or a NaN",
        ));
    }
    if lo > hi {
        return Err(Refusal::argument("Min must not be above Max"));
    }
    // A range of zero width would divide by zero. One bin holding
    // everything is what the data says.
    let width = if hi > lo {
        (hi - lo) / bins as f64
    } else {
        0.0
    };
    Ok((lo, hi, width))
}

impl Block for HistogramJob<'_> {
    #[inline(always)]
    fn run_block(&self, phase: usize, block: usize) {
        if phase < self.binning_phase {
            let s = &self.input[self.extreme_blocks.range(block)];
            let lo = s.iter().fold(f64::INFINITY, |a, &x| a.min(x));
            let hi = s.iter().fold(f64::NEG_INFINITY, |a, &x| a.max(x));
            self.extremes.put(block, (lo, hi));
            return;
        }
        let Some((lo, hi, width)) = self.scale else {
            panic!("block {block} of the binning phase ran before the range was known");
        };
        let mut local = vec![0u64; self.bins];
        for &x in &self.input[self.bin_blocks.range(block)] {
            if x.is_nan() || x < lo || x > hi {
                continue;
            }
            let slot = if width > 0.0 {
                // The top of the range belongs to the last bin rather than
                // to a bin past the end.
                (((x - lo) / width) as usize).min(self.bins - 1)
            } else {
                0
            };
            local[slot] += 1;
        }
        self.counts.put(block, local);
    }
}

impl Job for HistogramJob<'_> {
    type Answer = Option<Histogram>;

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        if phase < self.binning_phase {
            let found = self.extremes.take_all()?;
            let data_lo = found.iter().fold(f64::INFINITY, |a, e| a.min(e.0));
            let data_hi = found.iter().fold(f64::NEG_INFINITY, |a, e| a.max(e.1));
            let lo = self.given.0.unwrap_or(data_lo);
            let hi = self.given.1.unwrap_or(data_hi);
            self.scale = Some(scale_for(lo, hi, self.bins)?);
            self.counts = Slots::new(self.bin_blocks.count());
            self.tracker.advance(Some(self.bin_blocks.count()));
            return Ok(());
        }
        let mut total = vec![0u64; self.bins];
        for part in self.counts.take_all()? {
            for (slot, count) in part.iter().enumerate() {
                total[slot] += *count;
            }
        }
        self.total = Some(total);
        self.tracker.advance(None);
        Ok(())
    }

    fn finish(self) -> Result<Option<Histogram>, Refusal> {
        self.tracker.check_finished()?;
        if self.input.is_empty() {
            return Ok(None);
        }
        let (Some((low, high, width)), Some(counts)) = (self.scale, self.total) else {
            return Err(Refusal::internal(
                "the histogram ended with no range or no counts",
            ));
        };
        Ok(Some(Histogram {
            low,
            high,
            width,
            counts,
        }))
    }
}

// ---------------------------------------------------------------------
// The dot product
// ---------------------------------------------------------------------

/// [`dot_product`]'s job: per-block sums of products, added in block
/// order.
pub struct DotProductJob<'a> {
    left: &'a [f64],
    right: &'a [f64],
    blocks: Blocks,
    partials: Slots<f64>,
    folded: Vec<f64>,
    tracker: Tracker,
}

/// The dot product of `left` and `right`.
pub fn dot_product<'a>(left: &'a [f64], right: &'a [f64]) -> Result<DotProductJob<'a>, Refusal> {
    if left.len() != right.len() {
        return Err(Refusal::argument(format!(
            "Left has {} element(s) and Right has {}; a dot product needs the same length on both",
            left.len(),
            right.len()
        )));
    }
    let blocks = Blocks::new(left.len(), ARRAY_BLOCK_MIN, MAX_BLOCKS);
    Ok(DotProductJob {
        left,
        right,
        partials: Slots::new(blocks.count()),
        folded: Vec::new(),
        tracker: Tracker::new(opening(&blocks)),
        blocks,
    })
}

impl Block for DotProductJob<'_> {
    #[inline(always)]
    fn run_block(&self, _phase: usize, block: usize) {
        let r = self.blocks.range(block);
        let s = self.left[r.clone()]
            .iter()
            .zip(&self.right[r])
            .map(|(x, y)| x * y)
            .sum::<f64>();
        self.partials.put(block, s);
    }
}

impl Job for DotProductJob<'_> {
    type Answer = f64;

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        self.folded = self.partials.take_all()?;
        self.tracker.advance(None);
        Ok(())
    }

    fn finish(self) -> Result<f64, Refusal> {
        self.tracker.check_finished()?;
        if self.folded.is_empty() {
            return Ok(0.0);
        }
        Ok(self.folded.iter().sum::<f64>())
    }
}

// ---------------------------------------------------------------------
// The sort
// ---------------------------------------------------------------------

/// The least number of elements a sort run holds, where the input has
/// that many.
const SORT_RUN_MIN: usize = 4096;

/// The most runs a sort is cut into, which bounds its merge rounds at
/// six.
const SORT_RUNS_MAX: usize = 64;

/// [`sort`]'s job: each run sorted, then merge rounds, each a phase whose
/// blocks merge one pair of runs.
pub struct SortJob<'a> {
    input: &'a [f64],
    descending: bool,
    blocks: Blocks,
    runs: Vec<Vec<f64>>,
    next: Slots<Vec<f64>>,
    /// The unpaired run of an odd round, carried to the next round's end.
    carry: Option<Vec<f64>>,
    done: Option<Vec<f64>>,
    tracker: Tracker,
}

/// Sort `input`, NaN above every number: the total order
/// `f64::total_cmp` gives, the only one a comparison sort can use. The
/// sorted order under it is unique, so the answer is the same for any
/// cut of the input.
pub fn sort(input: &[f64], descending: bool) -> SortJob<'_> {
    let blocks = Blocks::new(input.len(), SORT_RUN_MIN, SORT_RUNS_MAX);
    let trivial = input.len() <= 1;
    SortJob {
        input,
        descending,
        runs: Vec::new(),
        next: Slots::new(if trivial { 0 } else { blocks.count() }),
        carry: None,
        done: trivial.then(|| input.to_vec()),
        tracker: Tracker::new(if trivial { None } else { opening(&blocks) }),
        blocks,
    }
}

/// One merge of two ascending runs.
fn merge_runs(left: &[f64], right: &[f64]) -> Vec<f64> {
    let mut out = Vec::with_capacity(left.len() + right.len());
    let (mut i, mut j) = (0usize, 0usize);
    while i < left.len() && j < right.len() {
        if left[i].total_cmp(&right[j]).is_le() {
            out.push(left[i]);
            i += 1;
        } else {
            out.push(right[j]);
            j += 1;
        }
    }
    out.extend_from_slice(&left[i..]);
    out.extend_from_slice(&right[j..]);
    out
}

impl Block for SortJob<'_> {
    #[inline(always)]
    fn run_block(&self, phase: usize, block: usize) {
        if phase == 0 {
            let mut run = self.input[self.blocks.range(block)].to_vec();
            run.sort_unstable_by(f64::total_cmp);
            self.next.put(block, run);
        } else {
            let merged = merge_runs(&self.runs[2 * block], &self.runs[2 * block + 1]);
            self.next.put(block, merged);
        }
    }
}

impl Job for SortJob<'_> {
    type Answer = Vec<f64>;

    tracked!();

    fn end_phase(&mut self, phase: usize) -> Result<(), Refusal> {
        self.tracker.check_ended(phase)?;
        let mut produced = self.next.take_all()?;
        if let Some(carried) = self.carry.take() {
            produced.push(carried);
        }
        if produced.len() <= 1 {
            self.done = produced.pop();
            self.runs = Vec::new();
            self.tracker.advance(None);
            return Ok(());
        }
        if produced.len() % 2 == 1 {
            self.carry = produced.pop();
        }
        let pairs = produced.len() / 2;
        self.runs = produced;
        self.next = Slots::new(pairs);
        self.tracker.advance(Some(pairs));
        Ok(())
    }

    fn finish(self) -> Result<Vec<f64>, Refusal> {
        self.tracker.check_finished()?;
        let Some(mut out) = self.done else {
            return Err(Refusal::internal("the merge rounds left no run"));
        };
        if self.descending {
            out.reverse();
        }
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::super::{BlockError, BlockState, drive, drive_serial, simd};
    use super::*;

    /// Whether two slices hold the same values bit for bit, which tells
    /// -0.0 from 0.0 and one NaN from another where `==` cannot.
    fn same_bits(a: &[f64], b: &[f64]) -> bool {
        a.len() == b.len() && a.iter().zip(b).all(|(p, q)| p.to_bits() == q.to_bits())
    }

    /// Deterministic values with a wide spread and some repeats.
    fn sample(n: usize) -> Vec<f64> {
        let mut state = 0x9E37_79B9_7F4A_7C15u64;
        (0..n)
            .map(|i| {
                state ^= state << 13;
                state ^= state >> 7;
                state ^= state << 17;
                let unit = (state >> 11) as f64 / (1u64 << 53) as f64;
                if i % 97 == 0 {
                    0.5
                } else {
                    (unit - 0.5) * 1.0e6
                }
            })
            .collect()
    }

    /// `sample(n)` with every seventh element an edge case, taken in turn
    /// from a list that `shift` rotates: signed zeros, infinities, NaNs
    /// with two payloads and both signs, a signaling NaN, subnormals,
    /// halves, the largest double below one half, values at the edge of
    /// the integers, and the extremes. Two inputs one shift apart pair
    /// each case with the next, the zeros and the NaNs in both orders.
    fn with_edge_cases(n: usize, shift: usize) -> Vec<f64> {
        let cases = [
            0.0,
            -0.0,
            0.0,
            f64::INFINITY,
            f64::NEG_INFINITY,
            f64::INFINITY,
            f64::from_bits(0x7FF8_0000_0000_0001),
            f64::from_bits(0xFFF8_0000_0000_0002),
            f64::from_bits(0x7FF8_0000_0000_0001),
            f64::from_bits(0x7FF4_0000_0000_0003),
            f64::from_bits(1),
            -f64::from_bits(0x000F_FFFF_FFFF_FFFF),
            2.5,
            -2.5,
            f64::from_bits(0x3FDF_FFFF_FFFF_FFFF),
            -0.5,
            4_503_599_627_370_495.5,
            4_503_599_627_370_497.0,
            1.0e300,
            -1.0e-300,
            f64::MAX,
            f64::MIN_POSITIVE,
        ];
        let mut values = sample(n);
        for (i, v) in values.iter_mut().enumerate().step_by(7) {
            *v = cases[(i / 7 + shift) % cases.len()];
        }
        values
    }

    /// `n` zeros, every third one negative counting from `shift`, so every
    /// reduction's answer, and a histogram's range, is a zero whose sign
    /// the order of the arithmetic decides.
    fn signed_zeros(n: usize, shift: usize) -> Vec<f64> {
        (0..n)
            .map(|i| {
                if (i + shift).is_multiple_of(3) {
                    -0.0
                } else {
                    0.0
                }
            })
            .collect()
    }

    /// Whether two words of an answer agree: the same bits, or two NaNs,
    /// whose sign and payload a kernel leaves unspecified at every level.
    fn same_word(a: u64, b: u64) -> bool {
        a == b || (f64::from_bits(a).is_nan() && f64::from_bits(b).is_nan())
    }

    /// What `answer` gives at each level this CPU offers set against the
    /// baseline's, a NaN agreeing with any NaN: one line for each level
    /// that differs, naming the first element that does and, where
    /// `inputs` line up with the answer element for element, the input
    /// bits there.
    fn level_differences(
        what: &str,
        inputs: &[&[f64]],
        answer: impl Fn() -> Vec<u64>,
    ) -> Vec<String> {
        let base = simd::with_level(simd::Level::Base, &answer);
        let mut found = Vec::new();
        for level in simd::levels_offered() {
            let got = simd::with_level(level, &answer);
            let first = got.iter().zip(&base).position(|(g, b)| !same_word(*g, *b));
            if first.is_none() && got.len() == base.len() {
                continue;
            }
            found.push(match first {
                Some(i) => {
                    let at: Vec<String> = inputs
                        .iter()
                        .map(|x| format!("{:#018x}", x[i].to_bits()))
                        .collect();
                    format!(
                        "{what} at {level:?}: element {i}, input [{}], answered {:#018x} where the \
                         baseline answered {:#018x}",
                        at.join(", "),
                        got[i],
                        base[i]
                    )
                }
                None => format!(
                    "{what} at {level:?}: {} elements where the baseline has {}",
                    got.len(),
                    base.len()
                ),
            });
        }
        found
    }

    /// Run every phase's blocks in reverse order on the calling thread.
    fn drive_reversed<J: Job>(job: J) -> Result<J::Answer, Refusal> {
        drive(job, |n, body| {
            for b in (0..n).rev() {
                body(b)?;
            }
            Ok(())
        })
    }

    /// Run every phase's blocks on four threads at once.
    fn drive_threaded<J: Job>(job: J) -> Result<J::Answer, Refusal> {
        drive(job, |n, body| {
            let next = std::sync::atomic::AtomicUsize::new(0);
            let first_error = std::sync::Mutex::new(None);
            std::thread::scope(|scope| {
                for _worker in 0..4 {
                    scope.spawn(|| {
                        loop {
                            let b = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                            if b >= n {
                                break;
                            }
                            if let Err(e) = body(b) {
                                let mut held = first_error.lock().expect("the error lock");
                                if held.is_none() {
                                    *held = Some(e);
                                }
                            }
                        }
                    });
                }
            });
            match first_error.into_inner().expect("the error lock") {
                Some(e) => Err(e),
                None => Ok(()),
            }
        })
    }

    #[test]
    fn every_map_operation_matches_its_scalar_form() {
        let operands = MapOperands {
            min: Some(-10.0),
            max: Some(10.0),
            factor: Some(2.5),
            addend: Some(-3.0),
        };
        let input = sample(10_000);
        for op in MapOp::ALL {
            let element = Element::new(op, operands).expect("every operand is present");
            let mut data = input.clone();
            drive_threaded(map(&mut data, op, operands).expect("the operands hold"))
                .expect("the map finishes");
            let want: Vec<f64> = input.iter().map(|x| element.apply(*x)).collect();
            assert!(same_bits(&data, &want), "{op:?}");
        }
    }

    #[test]
    fn a_map_refuses_in_the_module_words() {
        let refusal = |op, operands| {
            let mut data = vec![1.0];
            map(&mut data, op, operands).err().map(|r| r.message)
        };
        assert_eq!(
            refusal(
                MapOp::Clamp,
                MapOperands {
                    min: Some(0.0),
                    ..MapOperands::default()
                }
            )
            .as_deref(),
            Some("Clamp needs both Min and Max")
        );
        assert_eq!(
            refusal(
                MapOp::Clamp,
                MapOperands {
                    min: Some(2.0),
                    max: Some(1.0),
                    ..MapOperands::default()
                }
            )
            .as_deref(),
            Some("Min must not be above Max")
        );
        assert_eq!(
            refusal(
                MapOp::Clamp,
                MapOperands {
                    min: Some(f64::NAN),
                    max: Some(1.0),
                    ..MapOperands::default()
                }
            )
            .as_deref(),
            Some("Min and Max must be numbers, not NaN")
        );
        assert_eq!(
            refusal(MapOp::Scale, MapOperands::default()).as_deref(),
            Some("Scale needs Factor")
        );
        assert_eq!(
            refusal(MapOp::Offset, MapOperands::default()).as_deref(),
            Some("Offset needs Addend")
        );
        let mut empty: Vec<f64> = Vec::new();
        drive_serial(
            map(&mut empty, MapOp::Sqrt, MapOperands::default()).expect("Sqrt takes no operand"),
        )
        .expect("an empty map finishes");
    }

    #[test]
    fn a_zip_pairs_every_element_and_refuses_unequal_lengths() {
        let a = sample(9000);
        let b: Vec<f64> = sample(9001)[1..].to_vec();
        for op in ZipOp::ALL {
            let mut left = a.clone();
            drive_reversed(zip(&mut left, &b, op).expect("equal lengths"))
                .expect("the zip finishes");
            let want: Vec<f64> = a
                .iter()
                .zip(&b)
                .map(|(x, y)| match op {
                    ZipOp::Add => x + y,
                    ZipOp::Subtract => x - y,
                    ZipOp::Multiply => x * y,
                    ZipOp::Divide => x / y,
                    ZipOp::Min => y.min(*x),
                    ZipOp::Max => y.max(*x),
                })
                .collect();
            assert!(same_bits(&left, &want), "{op:?}");
        }
        let mut short = vec![1.0];
        let refused = zip(&mut short, &b, ZipOp::Add).err().map(|r| r.message);
        assert_eq!(
            refused.as_deref(),
            Some(
                "Left has 1 element(s) and Right has 9000; a pairwise operation needs the same \
                 length on both"
            )
        );
    }

    #[test]
    fn a_reduction_answers_the_same_bits_whatever_order_its_blocks_ran_in() {
        let x = sample(300_001);
        for op in ReduceOp::ALL {
            let job = || reduce(&x, op, Some(-1.0e5), Some(1.0e5)).expect("bounds given");
            let serial = drive_serial(job()).expect("serial");
            let reversed = drive_reversed(job()).expect("reversed");
            let threaded = drive_threaded(job()).expect("threaded");
            let bits = |r: &Reduction| r.value.map(f64::to_bits);
            assert_eq!(bits(&serial), bits(&reversed), "{op:?}");
            assert_eq!(bits(&serial), bits(&threaded), "{op:?}");
            assert_eq!(serial.count, 300_001);
        }
    }

    #[test]
    fn a_reduction_agrees_with_the_plain_fold() {
        let x = sample(50_000);
        let value = |op| {
            drive_serial(reduce(&x, op, Some(-2.0e5), Some(2.0e5)).expect("bounds given"))
                .expect("finishes")
                .value
                .expect("a value over a non-empty input")
        };
        let sum: f64 = x.iter().sum();
        assert!((value(ReduceOp::Sum) - sum).abs() <= 1.0e-6 * x.len() as f64);
        let smallest = x.iter().fold(f64::INFINITY, |a, &v| a.min(v));
        assert_eq!(value(ReduceOp::Min), smallest);
        let largest = x.iter().fold(f64::NEG_INFINITY, |a, &v| a.max(v));
        assert_eq!(value(ReduceOp::Max), largest);
        let mean = sum / x.len() as f64;
        assert!((value(ReduceOp::Mean) - mean).abs() < 1.0e-6);
        let var = x.iter().map(|v| (v - mean) * (v - mean)).sum::<f64>() / x.len() as f64;
        assert!((value(ReduceOp::Variance) - var).abs() / var < 1.0e-9);
        let inside = x.iter().filter(|&&v| (-2.0e5..=2.0e5).contains(&v)).count();
        assert_eq!(value(ReduceOp::CountMatching), inside as f64);
    }

    #[test]
    fn an_empty_reduction_answers_what_each_operation_means_there() {
        let answer = |op| {
            drive_serial(reduce(&[], op, Some(0.0), Some(1.0)).expect("bounds given"))
                .expect("finishes")
        };
        assert_eq!(answer(ReduceOp::Sum).value, Some(0.0));
        assert_eq!(answer(ReduceOp::Product).value, Some(1.0));
        assert_eq!(answer(ReduceOp::CountMatching).value, Some(0.0));
        for op in [
            ReduceOp::Min,
            ReduceOp::Max,
            ReduceOp::Mean,
            ReduceOp::Variance,
        ] {
            assert_eq!(answer(op).value, None, "{op:?}");
            assert_eq!(answer(op).count, 0);
        }
        let refused = reduce(&[], ReduceOp::CountMatching, Some(1.0), None)
            .err()
            .map(|r| r.message);
        assert_eq!(
            refused.as_deref(),
            Some("CountMatching needs both Min and Max")
        );
    }

    #[test]
    fn a_running_total_is_exact_on_integers_and_invariant_to_block_order() {
        let ints: Vec<f64> = (0..20_000).map(|i| (i % 13) as f64).collect();
        let mut scanned = ints.clone();
        drive_threaded(prefix_sum(&mut scanned)).expect("finishes");
        let mut running = 0.0;
        let want: Vec<f64> = ints
            .iter()
            .map(|x| {
                running += x;
                running
            })
            .collect();
        assert!(same_bits(&scanned, &want));
        let x = sample(70_000);
        let mut a = x.clone();
        let mut b = x;
        drive_serial(prefix_sum(&mut a)).expect("finishes");
        drive_reversed(prefix_sum(&mut b)).expect("finishes");
        assert!(same_bits(&a, &b));
    }

    #[test]
    fn a_histogram_counts_what_a_serial_pass_counts() {
        let x = sample(100_000);
        let got = drive_threaded(histogram(&x, 16, None, None).expect("bins"))
            .expect("finishes")
            .expect("a non-empty input");
        let lo = x.iter().fold(f64::INFINITY, |a, &v| a.min(v));
        let hi = x.iter().fold(f64::NEG_INFINITY, |a, &v| a.max(v));
        let width = (hi - lo) / 16.0;
        let mut want = vec![0u64; 16];
        for &v in &x {
            want[(((v - lo) / width) as usize).min(15)] += 1;
        }
        assert_eq!(got.counts, want);
        assert_eq!((got.low, got.high, got.width), (lo, hi, width));
        assert_eq!(got.bins()[3].count, want[3]);
        assert_eq!(got.counts.iter().sum::<u64>(), 100_000);
    }

    #[test]
    fn a_histogram_refuses_in_the_module_words() {
        let message = |x: &[f64], bins, min, max| match histogram(x, bins, min, max) {
            Err(r) => Some(r.message),
            Ok(job) => drive_serial(job).err().map(|r| r.message),
        };
        assert_eq!(
            message(&[1.0], 0, None, None).as_deref(),
            Some("Bins must be at least one")
        );
        assert_eq!(
            message(&[1.0, f64::INFINITY], 4, None, None).as_deref(),
            Some(
                "the range is not finite; pass Min and Max when the data holds an infinity or a NaN"
            )
        );
        assert_eq!(
            message(&[1.0], 4, Some(2.0), Some(1.0)).as_deref(),
            Some("Min must not be above Max")
        );
        assert_eq!(
            drive_serial(histogram(&[], 4, None, None).expect("bins")).expect("finishes"),
            None
        );
        let flat = drive_serial(histogram(&[3.0, 3.0], 4, None, None).expect("bins"))
            .expect("finishes")
            .expect("non-empty");
        assert_eq!(flat.counts, vec![2, 0, 0, 0]);
        assert_eq!(flat.width, 0.0);
    }

    #[test]
    fn a_dot_product_is_invariant_to_block_order() {
        let a = sample(123_457);
        let b: Vec<f64> = a.iter().map(|v| v * 0.5 + 1.0).collect();
        let serial = drive_serial(dot_product(&a, &b).expect("lengths")).expect("finishes");
        let threaded = drive_threaded(dot_product(&a, &b).expect("lengths")).expect("finishes");
        assert!(same_bits(&[serial], &[threaded]));
        assert_eq!(
            drive_serial(dot_product(&[], &[]).expect("lengths")).expect("finishes"),
            0.0
        );
    }

    #[test]
    fn a_sort_answers_the_standard_sort_in_either_direction() {
        let mut x = sample(300_000);
        x.push(f64::NAN);
        x.push(-0.0);
        x.push(0.0);
        let mut want = x.clone();
        want.sort_by(f64::total_cmp);

        let got = drive_threaded(sort(&x, false)).expect("finishes");
        assert!(same_bits(&got, &want));

        let down = drive_serial(sort(&x, true)).expect("finishes");
        want.reverse();
        assert!(same_bits(&down, &want));

        assert_eq!(
            drive_serial(sort(&[2.0], false)).expect("finishes"),
            vec![2.0]
        );
        assert!(drive_serial(sort(&[], false)).expect("finishes").is_empty());
    }

    #[test]
    fn a_stopped_phase_resumes_with_the_blocks_it_did_not_run() {
        let mut data = vec![3.0; 50_000];
        let mut job = map(&mut data, MapOp::Square, MapOperands::default()).expect("no operand");
        let n = job.blocks(0).expect("one phase");
        assert!(n > 2);
        job.run(0, 0).expect("block 0 runs");
        job.run(0, 2).expect("block 2 runs");
        assert_eq!(
            job.run(0, 0),
            Err(BlockError::AlreadyRan {
                phase: 0,
                block: 0,
                found: BlockState::Done
            })
        );
        let refused = job.end_phase(0).expect_err("block 1 has not run");
        assert_eq!(refused.message, "block 1 of phase 0 never ran");
        for b in 0..n {
            if job.state(0, b) == Some(BlockState::NotRun) {
                job.run(0, b).expect("a block not yet run runs");
            }
        }
        job.end_phase(0).expect("every block finished");
        job.finish().expect("every phase ended");
        assert!(data.iter().all(|&v| v == 9.0), "each element squared once");
    }

    #[test]
    fn the_names_follow_the_declaration_order() {
        for (op, name) in MapOp::ALL.iter().zip(MapOp::NAMES) {
            assert_eq!(format!("{op:?}"), name);
        }
        for (op, name) in ZipOp::ALL.iter().zip(ZipOp::NAMES) {
            assert_eq!(format!("{op:?}"), name);
        }
        for (op, name) in ReduceOp::ALL.iter().zip(ReduceOp::NAMES) {
            assert_eq!(format!("{op:?}"), name);
        }
    }

    #[test]
    fn every_kernel_answers_the_baseline_s_bits_at_every_level() {
        let operands = MapOperands {
            min: Some(-10.0),
            max: Some(10.0),
            factor: Some(2.5),
            addend: Some(-3.0),
        };
        let inputs = [
            (
                "plain",
                sample(100_003),
                sample(100_004)[1..].to_vec(),
                None,
            ),
            (
                "edge cases",
                with_edge_cases(100_003, 0),
                with_edge_cases(100_003, 1),
                Some((-1.0e5, 1.0e5)),
            ),
            (
                "signed zeros",
                signed_zeros(100_003, 0),
                signed_zeros(100_003, 1),
                None,
            ),
        ];
        let bits = |v: &[f64]| -> Vec<u64> { v.iter().map(|p| p.to_bits()).collect() };
        let mut found = Vec::new();
        for (input, x, y, range) in &inputs {
            let (x, y) = (x.as_slice(), y.as_slice());
            for op in MapOp::ALL {
                found.extend(level_differences(
                    &format!("{input}: map {op:?}"),
                    &[x],
                    || {
                        let mut data = x.to_vec();
                        drive_threaded(map(&mut data, op, operands).expect("the operands hold"))
                            .expect("the map finishes");
                        bits(&data)
                    },
                ));
            }
            for op in ZipOp::ALL {
                found.extend(level_differences(
                    &format!("{input}: zip {op:?}"),
                    &[x, y],
                    || {
                        let mut left = x.to_vec();
                        drive_threaded(zip(&mut left, y, op).expect("equal lengths"))
                            .expect("the zip finishes");
                        bits(&left)
                    },
                ));
            }
            for op in ReduceOp::ALL {
                found.extend(level_differences(
                    &format!("{input}: reduce {op:?}"),
                    &[],
                    || {
                        let r = drive_threaded(
                            reduce(x, op, Some(-1.0e5), Some(1.0e5)).expect("bounds"),
                        )
                        .expect("the reduction finishes");
                        vec![
                            u64::from(r.value.is_some()),
                            r.value.map_or(0, f64::to_bits),
                            r.count,
                        ]
                    },
                ));
            }
            found.extend(level_differences(
                &format!("{input}: prefix sum"),
                &[x],
                || {
                    let mut a = x.to_vec();
                    drive_threaded(prefix_sum(&mut a)).expect("the scan finishes");
                    bits(&a)
                },
            ));
            found.extend(level_differences(
                &format!("{input}: histogram"),
                &[],
                || {
                    let (min, max) = range.map_or((None, None), |(lo, hi)| (Some(lo), Some(hi)));
                    let h = drive_threaded(histogram(x, 16, min, max).expect("bins"))
                        .expect("the histogram finishes")
                        .expect("a non-empty input");
                    let mut answer = h.counts;
                    answer.extend(bits(&[h.low, h.high, h.width]));
                    answer
                },
            ));
            found.extend(level_differences(
                &format!("{input}: dot product"),
                &[],
                || {
                    vec![
                        drive_threaded(dot_product(x, y).expect("equal lengths"))
                            .expect("the dot product finishes")
                            .to_bits(),
                    ]
                },
            ));
            found.extend(level_differences(&format!("{input}: sort"), &[], || {
                bits(&drive_threaded(sort(x, false)).expect("the sort finishes"))
            }));
        }
        assert!(
            found.is_empty(),
            "{} kernel(s) answered other bits at some level than at the baseline, a NaN \
             agreeing with any NaN:\n{}",
            found.len(),
            found.join("\n")
        );
    }
}