lamellar 0.8.0

Lamellar is an asynchronous tasking runtime for HPC systems developed in RUST.
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
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
//! LamellarArrays provide a safe and high-level abstraction of a distributed array.
//!
//! By distributed, we mean that the memory backing the array is physically located on multiple distributed PEs in the system.
//!
//! # Features
//!
//! **Features**  include
//!  - [Safety](#safety)
//!  - [Multiple array types](#multiple-array-types)
//!  - RDMA like `put` and `get` APIs
//!  - [Block][crate::array::Distribution::Block] or [Cyclic][crate::array::Distribution::Cyclic] data layouts
//!
//! **Tools to work with arrays** include
//!  - [Conversion](#type-conversion) between different array types and other data structures
//!  - Element-wise operations (e.g. [load/store][crate::array::AccessOps], [add][crate::array::ArithmeticOps], [fetch_and][crate::array::BitWiseOps], [compare_exchange][crate::array::CompareExchangeOps], etc)
//!  - Batched operations ([batch_add][crate::array::ArithmeticOps], [batch_fetch_add][crate::array::ArithmeticOps], etc.)
//!  - [Distributed][crate::array::iterator::distributed_iterator], [Local][crate::array::iterator::local_iterator], and [Onesided][crate::array::iterator::one_sided_iterator] Iteration
//!  - [Distributed Reductions][crate::array::LamellarArrayReduce]
//!  - [Sub Arrays][crate::array::SubArray]
//!
//! # Examples
//!
//! Lamellar provides a variety of [examples](https://github.com/pnnl/lamellar-runtime/tree/master/examples/array_examples) for common tasks, e.g. distributed iteration.
//!
//! # Safety
//! Array Data Lifetimes: LamellarArrays are built upon [Darcs][crate::darc::Darc] (Distributed Atomic Reference Counting Pointers) and as such have distributed lifetime management.
//! This means that as long as a single reference to an array exists anywhere in the distributed system, the data for the entire array will remain valid on every PE (even though a given PE may have dropped all its local references).
//! While the compiler handles lifetimes within the context of a single PE, our distributed lifetime management relies on "garbage collecting active messages" to ensure all remote references have been accounted for.
//!
//! # Multiple array types
//! We provide several array types, each with their own saftey gaurantees with respect to how data is accessed (further details can be found in the documentation for each type)
//!  - [UnsafeArray]: No safety gaurantees - PEs are free to read/write to anywhere in the array with no access control
//!  - [ReadOnlyArray]: No write access is permitted, and thus PEs are free to read from anywhere in the array with no access control
//!  - [AtomicArray]: Each Element is atomic (either instrinsically or enforced via the runtime)
//!      - [NativeAtomicArray]: utilizes the language atomic types e.g AtomicUsize, AtomicI8, etc.
//!      - [GenericAtomicArray]: Each element is protected by a 1-byte mutex
//!  - [LocalLockArray]: The data on each PE is protected by a local RwLock
//!  - [GlobalLockArray]: The data on each PE is protected by a global RwLock
//!
//! # Type conversion
//! Lamellar offers a variety of methods to convert between different array types and other data structures.
//! - `into_atomic`, `into_read_only`, etc., convert between disributed array types.
//! - `collect` and `collect_async` provide functionality analogous to the [collect](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect) method for Rust iterators
//! - We also provided access directly to the underlying local data of an array using functions (and container types) that preserve the safety guarantees of a given array type
//!     -`local_data`, `read_local_data`, `write_local_data`, etc. convert to slices and other data types.
//!     - Consequently, these functions can be used to create valid inputs for batched operations,  see [OpInput] for details.
//! ```
//! use lamellar::array::prelude::*;
//!
//! // define an length-10 array of type UnsafeArray<usize>
//! let world = LamellarWorldBuilder::new().build();
//! let array =  UnsafeArray::<usize>::new(&world, 10,Distribution::Block).block();
//!
//! // convert between array types
//! let array = array.into_local_lock().block(); // LocalLockArray
//! let array = array.into_global_lock().block(); // GlobalLockArray
//! let array = array.into_atomic().block(); // AtomicArray
//! let array = array.into_read_only().block(); // ReadOnlyArray
//!
//! // get a reference to the underlying slice: &[usize]
//! let local_data = array.local_data();
//!
//! // export to Vec<usize>
//! let vec = array.local_data().to_vec();
//! ```
use crate::barrier::BarrierHandle;
use crate::darc::Darc;
use crate::lamellar_env::LamellarEnv;
use crate::memregion::{
    one_sided::OneSidedMemoryRegion, shared::SharedMemoryRegion, AsLamellarBuffer, Dist,
    LamellarBuffer, LamellarMemoryRegion, MemregionRdmaInputInner,
};
use crate::scheduler::LamellarTask;
use crate::{active_messaging::*, LamellarTeam, LamellarTeamRT};

// use crate::Darc;
use async_trait::async_trait;
use enum_dispatch::enum_dispatch;
use futures_util::Future;
// use parking_lot::Mutex;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::atomic::Ordering;
use std::sync::Arc;

// use serde::de::DeserializeOwned;

/// This macro automatically derives various LamellarArray "Op" traits for user defined types
///
/// The following "Op" traits are automatically implemented:
/// - [AccessOps]
/// - [ReadOnlyOps]
///
/// Additionally, it is possible to pass any of the following as a list to [ArrayOps] to derive the associated traits
/// - `Arithmetic` -- [ArithmeticOps]
///     - requires [AddAssign][std::ops::AddAssign], [SubAssign][std::ops::SubAssign], [MulAssign][std::ops::MulAssign], [DivAssign][std::ops::DivAssign], [RemAssign][std::ops::RemAssign] to be implemented on your data type
/// - `Bitwise` -- [BitWiseOps]
///     - requires [BitAndAssign][std::ops::BitAndAssign], [BitOrAssign][std::ops::BitOrAssign], [BitXorAssign][std::ops::BitXorAssign] to be implemented on your data type
/// - `CompEx` -- [CompareExchangeOps]
///     - requires [PartialEq], [PartialOrd] to be implemented on your data type
/// - `CompExEps` -- [CompareExchangeEpsilonOps]
///     - requires [PartialEq], [PartialOrd] to be implemented on your data type
/// - `Shift` -- [ShiftOps]
///     - requires [ShlAssign][std::ops::ShlAssign], [ShrAssign][std::ops::ShrAssign] to be implemented on you data type
///
/// Alternatively, if you plan to derive all the above traits you can simply supply `All` as the single argument to [ArrayOps]
///
/// # Examples
///
/// ```
/// // this import includes everything we need
/// use lamellar::array::prelude::*;
///
/// #[lamellar::AmData(
///     // Lamellar traits
///     ArrayOps(Arithmetic,CompExEps,Shift), // needed to derive various LamellarArray Op traits (provided as a list)
///     Default,       // needed to be able to initialize a LamellarArray
///     //  Notice we use `lamellar::AmData` instead of `derive`
///     //  for common traits, e.g. Debug, Clone.
///     PartialEq,     // needed for CompareExchangeEpsilonOps
///     PartialOrd,    // needed for CompareExchangeEpsilonOps
///     Debug,         // any addition traits you want derived
///     Clone,
/// )]
/// struct Custom {
///     int: usize,
///     float: f32,
/// }
///
/// // We need to impl various arithmetic ops if we want to be able to
/// // perform remote arithmetic operations with this type
/// impl std::ops::AddAssign for Custom {
///     fn add_assign(&mut self, other: Self) {
///         *self = Self {
///             int: self.int + other.int,
///             float: self.float + other.float,
///         }
///     }
/// }
///
/// impl std::ops::SubAssign for Custom {
///     fn sub_assign(&mut self, other: Self) {
///         *self = Self {
///             int: self.int - other.int,
///             float: self.float - other.float,
///         }
///     }
/// }
///
/// impl std::ops::Sub for Custom {
///     type Output = Self;
///     fn sub(self, other: Self) -> Self {
///         Self {
///             int: self.int - other.int,
///             float: self.float - other.float,
///         }
///     }
/// }
///
/// impl std::ops::MulAssign for Custom {
///     fn mul_assign(&mut self, other: Self) {
///         *self = Self {
///             int: self.int * other.int,
///             float: self.float * other.float,
///         }
///     }
/// }
///
/// impl std::ops::DivAssign for Custom {
///     fn div_assign(&mut self, other: Self) {
///         *self = Self {
///             int: self.int / other.int,
///             float: self.float / other.float,
///         }
///     }
/// }
/// impl std::ops::ShlAssign for Custom {
///     fn shl_assign(&mut self, other: Self){
///         self.int <<= other.int;
///     }
/// }
///
/// impl std::ops::ShrAssign for Custom {
///     fn shr_assign(&mut self, other: Self){
///         self.int >>= other.int;
///     }
/// }
///
/// impl std::ops::RemAssign for Custom {
///     fn rem_assign(&mut self, other: Self) {
///        self.int %= other.int;
///    }
/// }
///
/// fn main(){
///
///     // initialize
///     // -----------
///
///     let world = LamellarWorldBuilder::new().build(); // the world
///
///     let array =  // the atomic distributed array
///         AtomicArray::<Custom>::new(&world,3,Distribution::Block).block();
///
///     println!();
///     println!("initialize a length-3 array:\n");  // print the entries
///     let _ = array.dist_iter()
///         .enumerate()
///         .for_each(|(i,entry)| println!("entry {:?}: {:?}", i, entry ) );
///     array.wait_all();
///
///     // call various operations on the array!
///     // -------------------------------------
///
///     world.block_on( async move {  // we will just use the world as our future driver so we dont have to deal with cloning array
///
///         println!();
///         println!("add (1, 0.01) to the first entry:\n");
///         let val = Custom{int: 1, float: 0.01};
///         array.add(0, val ).await;
///         let _ = array.dist_iter().enumerate().for_each(|(i,entry)| println!("entry {:?}: {:?}", i, entry ) );
///         array.wait_all();
///
///         println!();
///         println!("batch compare/exchange:");
///         let indices = vec![0,1,2,];
///         let current = val;
///         let new = Custom{int: 1, float: 0.0};
///         let epsilon = Custom{int: 0, float: 0.01};
///         let _results = array.batch_compare_exchange_epsilon(indices,current,new,epsilon).await;
///         println!();
///         println!("(1) the updated array");
///         let _ = array.dist_iter().enumerate().for_each(|(i,entry)| println!("entry {:?}: {:?}", i, entry ) );
///         array.wait_all();
///         println!();
///         println!("(2) the return values");
///         for (i, entry) in _results.iter().enumerate() { println!("entry {:?}: {:?}", i, entry ) }
///     });
///
///     // inspect the results
///     // -------------------------------------
///     // NB:  because we're working with multithreaded async
///     //      environments, entries may be printed out of order
///     //
///     // initialize a length-3 array:
///     //
///     // entry 1: Custom { int: 0, float: 0.0 }
///     // entry 0: Custom { int: 0, float: 0.0 }
///     // entry 2: Custom { int: 0, float: 0.0 }
///     //
///     // add (1, 0.01) to the first entry:
///     //
///     // entry 0: Custom { int: 1, float: 0.01 }
///     // entry 2: Custom { int: 0, float: 0.0 }
///     // entry 1: Custom { int: 0, float: 0.0 }
///     //
///     // batch compare/exchange:
///     //
///     // (1) the updatd array
///     // entry 0: Custom { int: 1, float: 0.0 }
///     // entry 1: Custom { int: 0, float: 0.0 }
///     // entry 2: Custom { int: 0, float: 0.0 }
///     //
///     // (2) the return values
///     // entry 0: Ok(Custom { int: 1, float: 0.01 })
///     // entry 1: Err(Custom { int: 0, float: 0.0 })
///     // entry 2: Err(Custom { int: 0, float: 0.0 })
/// }
/// ```
pub use lamellar_impl::ArrayOps;

// //#[doc(hidden)]

/// The prelude contains all the traits and macros that are required to use the array types
pub mod prelude;

pub(crate) mod r#unsafe;
pub use r#unsafe::{
    local_chunks::{UnsafeLocalChunks, UnsafeLocalChunksMut},
    operations::{
        multi_val_multi_idx_ops, multi_val_multi_idx_ops_new, multi_val_single_idx_ops,
        multi_val_single_idx_ops_new, single_val_multi_idx_ops, single_val_multi_idx_ops_new,
        BatchReturnType,
    },
    UnsafeArray, __UnsafeByteArray,
};
pub(crate) mod read_only;
pub use read_only::{ReadOnlyArray, ReadOnlyLocalChunks, __ReadOnlyByteArray};

pub(crate) mod atomic;
pub use atomic::{AtomicArray, AtomicLocalData, __AtomicByteArray};

pub(crate) mod generic_atomic;
pub use generic_atomic::{GenericAtomicArray, __GenericAtomicByteArray, __GenericAtomicLocalData};

pub(crate) mod native_atomic;
pub use native_atomic::{NativeAtomicArray, __NativeAtomicByteArray, __NativeAtomicLocalData};

pub(crate) mod network_atomic;
pub use network_atomic::{NetworkAtomicArray, __NetworkAtomicByteArray, __NetworkAtomicLocalData};

pub(crate) mod local_lock_atomic;
pub use local_lock_atomic::{
    LocalLockArray, LocalLockLocalChunks, LocalLockLocalChunksMut, LocalLockLocalData,
    LocalLockMutLocalData, LocalLockReadGuard, LocalLockWriteGuard, __LocalLockByteArray,
};

pub(crate) mod global_lock_atomic;
pub use global_lock_atomic::{
    GlobalLockArray, GlobalLockLocalData, GlobalLockMutLocalData, GlobalLockReadGuard,
    GlobalLockWriteGuard, __GlobalLockByteArray,
};

/// Provides distributed, local, and one-sided iterator types for LamellarArrays.
///
/// See the [iterator module][crate::array::iterator] for full details on the three iterator modes
/// and their associated adapters.
pub mod iterator;
// //#[doc(hidden)]
pub use iterator::distributed_iterator::DistributedIterator;
// //#[doc(hidden)]
pub use iterator::local_iterator::LocalIterator;
// //#[doc(hidden)]
pub use iterator::one_sided_iterator::OneSidedIterator;

pub(crate) mod operations;
pub use operations::*;

pub(crate) mod scalar_one_sided_reduce;
pub use scalar_one_sided_reduce::*;

pub(crate) mod handle;
pub use handle::*;

pub(crate) mod rdma;
use rdma::private::{LamellarRdmaGet, LamellarRdmaPut, Sealed};
pub use rdma::*;

pub(crate) mod collective;

pub(crate) type ReduceGen = fn(LamellarByteArray, usize) -> LamellarArcAm;

lazy_static! {
    pub(crate) static ref REDUCE_OPS: HashMap<(std::any::TypeId, &'static str), ReduceGen> = {
        let mut temp = HashMap::new();
        for reduction_type in crate::inventory::iter::<ReduceKey> {
            temp.insert(
                ((reduction_type.id)(), reduction_type.name),
                reduction_type.gen,
            );
        }
        temp
    };
}

type ReduceIdGen = fn() -> std::any::TypeId;
#[doc(hidden)]
pub struct ReduceKey {
    pub id: ReduceIdGen,
    pub name: &'static str,
    pub gen: ReduceGen,
}
crate::inventory::collect!(ReduceKey);

/// Runtime tag for the 14 primitive scalar types that support array ops.
#[doc(hidden)]
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
#[allow(non_camel_case_types)]
pub(crate) enum ScalarType {
    u8,
    u16,
    u32,
    u64,
    usize,
    u128,
    i8,
    i16,
    i32,
    i64,
    isize,
    i128,
    f32,
    f64,
    bool,
}

impl ScalarType {
    pub(crate) fn get_type<T: 'static>() -> Option<(Self, bool)> {
        // returns (ScalarType, is_option)
        match std::any::TypeId::of::<T>() {
            id if id == std::any::TypeId::of::<u8>() => Some((ScalarType::u8, false)),
            id if id == std::any::TypeId::of::<u16>() => Some((ScalarType::u16, false)),
            id if id == std::any::TypeId::of::<u32>() => Some((ScalarType::u32, false)),
            id if id == std::any::TypeId::of::<u64>() => Some((ScalarType::u64, false)),
            id if id == std::any::TypeId::of::<usize>() => Some((ScalarType::usize, false)),
            id if id == std::any::TypeId::of::<u128>() => Some((ScalarType::u128, false)),
            id if id == std::any::TypeId::of::<i8>() => Some((ScalarType::i8, false)),
            id if id == std::any::TypeId::of::<i16>() => Some((ScalarType::i16, false)),
            id if id == std::any::TypeId::of::<i32>() => Some((ScalarType::i32, false)),
            id if id == std::any::TypeId::of::<i64>() => Some((ScalarType::i64, false)),
            id if id == std::any::TypeId::of::<isize>() => Some((ScalarType::isize, false)),
            id if id == std::any::TypeId::of::<i128>() => Some((ScalarType::i128, false)),
            id if id == std::any::TypeId::of::<f32>() => Some((ScalarType::f32, false)),
            id if id == std::any::TypeId::of::<f64>() => Some((ScalarType::f64, false)),
            id if id == std::any::TypeId::of::<bool>() => Some((ScalarType::bool, false)),
            id if id == std::any::TypeId::of::<Option<u8>>() => Some((ScalarType::u8, true)),
            id if id == std::any::TypeId::of::<Option<u16>>() => Some((ScalarType::u16, true)),
            id if id == std::any::TypeId::of::<Option<u32>>() => Some((ScalarType::u32, true)),
            id if id == std::any::TypeId::of::<Option<u64>>() => Some((ScalarType::u64, true)),
            id if id == std::any::TypeId::of::<Option<usize>>() => Some((ScalarType::usize, true)),
            id if id == std::any::TypeId::of::<Option<u128>>() => Some((ScalarType::u128, true)),
            id if id == std::any::TypeId::of::<Option<i8>>() => Some((ScalarType::i8, true)),
            id if id == std::any::TypeId::of::<Option<i16>>() => Some((ScalarType::i16, true)),
            id if id == std::any::TypeId::of::<Option<i32>>() => Some((ScalarType::i32, true)),
            id if id == std::any::TypeId::of::<Option<i64>>() => Some((ScalarType::i64, true)),
            id if id == std::any::TypeId::of::<Option<isize>>() => Some((ScalarType::isize, true)),
            id if id == std::any::TypeId::of::<Option<i128>>() => Some((ScalarType::i128, true)),
            id if id == std::any::TypeId::of::<Option<f32>>() => Some((ScalarType::f32, true)),
            id if id == std::any::TypeId::of::<Option<f64>>() => Some((ScalarType::f64, true)),
            id if id == std::any::TypeId::of::<Option<bool>>() => Some((ScalarType::bool, true)),
            _ => None,
        }
    }
}

// lamellar_impl::generate_reductions_for_type_rt!(true, u8, usize);
// lamellar_impl::generate_ops_for_type_rt!(true, true, true, u8, usize);

// lamellar_impl::generate_reductions_for_type_rt!(true, isize);
// lamellar_impl::generate_ops_for_type_rt!(true, true, true, isize);

// lamellar_impl::generate_reductions_for_type_rt!(true, u32);
// lamellar_impl::generate_ops_for_type_rt!(true, true, true, u32);

// lamellar_impl::generate_reductions_for_type_rt!(true, i64);
// lamellar_impl::generate_ops_for_type_rt!(true, true, true, i64);

// lamellar_impl::generate_reductions_for_type_rt!(false, f32);
// lamellar_impl::generate_ops_for_type_rt!(false, false, false, f32);

// lamellar_impl::generate_reductions_for_type_rt!(false, u128);
// lamellar_impl::generate_ops_for_type_rt!(true, false, true, u128);
// // //------------------------------------

// lamellar_impl::generate_reductions_for_type_rt!(true, u8, u16, u32, u64, usize);
// lamellar_impl::generate_reductions_for_type_rt!(false, u128);
// lamellar_impl::generate_ops_for_type_rt!(true, true, true, u8, u16, u32, u64, usize);
// lamellar_impl::generate_ops_for_type_rt!(true, false, true, u128);

// lamellar_impl::generate_reductions_for_type_rt!(true, i8, i16, i32, i64, isize);
// lamellar_impl::generate_reductions_for_type_rt!(false, i128);
// lamellar_impl::generate_ops_for_type_rt!(true, true, true, i8, i16, i32, i64, isize);
// lamellar_impl::generate_ops_for_type_rt!(true, false, true, i128);

// lamellar_impl::generate_reductions_for_type_rt!(false, f32, f64);
// lamellar_impl::generate_ops_for_type_rt!(false, false, false, f32, f64);

// lamellar_impl::generate_ops_for_bool_rt!();

impl<T: Dist + ArrayOps> Dist for Option<T> {}
impl<T: Dist + ArrayOps> ArrayOps for Option<T> {}

/// Specifies the distributed data layout of a LamellarArray
///
/// Block: The indicies of the elements on each PE are sequential
///
/// Cyclic: The indicies of the elements on each PE have a stride equal to the number of PEs associated with the array
///
/// # Examples
/// assume we have 4 PEs
/// ## Block
///```
/// use lamellar::array::prelude::*;
/// let world = LamellarWorldBuilder::new().build();
/// let block_array = AtomicArray::<usize>::new(&world,12,Distribution::Block).block();
/// //block array index location  = PE0 [0,1,2,3],  PE1 [4,5,6,7],  PE2 [8,9,10,11], PE3 [12,13,14,15]
///```
/// ## Cyclic
///```
/// use lamellar::array::prelude::*;
/// let world = LamellarWorldBuilder::new().build();
/// let cyclic_array = AtomicArray::<usize>::new(&world,12,Distribution::Cyclic).block();
/// //cyclic array index location = PE0 [0,4,8,12], PE1 [1,5,9,13], PE2 [2,6,10,14], PE3 [3,7,11,15]
///```
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum Distribution {
    /// The indicies of the elements on each PE are sequential
    Block,
    /// The indicies of the elements on each PE have a stride equal to the number of PEs associated with the array
    Cyclic,
}

#[doc(hidden)]
#[derive(Hash, std::cmp::PartialEq, std::cmp::Eq, Clone)]
pub enum ArrayRdmaCmd {
    Put,
    PutAm,
    Get(bool), //bool true == immediate, false = async
    GetAm,
}

/// Registered memory regions that can be used as input to various LamellarArray RDMA operations.
// #[enum_dispatch(RegisteredMemoryRegion<T>, SubRegion<T>, TeamFrom<T>,MemoryRegionRDMA<T>,AsBase)]
#[derive(Clone, Debug)]
pub enum LamellarArrayRdmaInput<T: Dist> {
    /// Variant contiaining a memory region whose local data can be used as an input buffer
    LamellarMemRegion(LamellarMemoryRegion<T>),
    /// Variant contiaining a shared memory region whose local data can be used as an input buffer
    SharedMemRegion(SharedMemoryRegion<T>), //when used as input/output we are only using the local data
    /// Variant contiaining a onessided memory region that can be used as an input buffer
    LocalMemRegion(OneSidedMemoryRegion<T>),
    /// Variant containing an owned value that can be used as an input buffer
    Owned(T),
    /// Variant containing an owned `Vec<T>` whose elements can be used as an input buffer
    OwnedVec(Vec<T>),
}
impl<T: Dist> LamellarArrayRdmaInput<T> {
    // pub(crate) fn as_slice(&self) -> &[T] {
    //     match self {
    //         LamellarArrayRdmaInput::LamellarMemRegion(region) => unsafe { region.as_slice() },
    //         LamellarArrayRdmaInput::SharedMemRegion(region) => unsafe { region.as_slice() },
    //         LamellarArrayRdmaInput::LocalMemRegion(region) => unsafe { region.as_slice() },
    //         LamellarArrayRdmaInput::Owned(value) => std::slice::from_ref(value),
    //         LamellarArrayRdmaInput::OwnedVec(vec) => vec.as_slice(),
    //     }
    // }

    // pub(crate) fn len(&self) -> usize {
    //     match self {
    //         LamellarArrayRdmaInput::LamellarMemRegion(region) => unsafe { region.len() },
    //         LamellarArrayRdmaInput::SharedMemRegion(region) => unsafe { region.len() },
    //         LamellarArrayRdmaInput::LocalMemRegion(region) => unsafe { region.len() },
    //         LamellarArrayRdmaInput::Owned(value) => std::mem::size_of_val(value),
    //         LamellarArrayRdmaInput::OwnedVec(vec) => vec.len() * std::mem::size_of::<T>(),
    //     }
    // }
}

impl<T: Dist> LamellarRead for LamellarArrayRdmaOutput<T> {}

/// Registered memory regions that can be used as output to various LamellarArray RDMA operations.
// #[enum_dispatch(RegisteredMemoryRegion<T>, SubRegion<T>, TeamFrom<T>,MemoryRegionRDMA<T>,AsBase)]
#[derive(Clone, Debug)]
pub enum LamellarArrayRdmaOutput<T: Dist> {
    /// Variant contiaining a memory region whose local data can be used as an output buffer
    LamellarMemRegion(LamellarMemoryRegion<T>),
    /// Variant contiaining a shared memory region whose local data can be used as an output buffer
    SharedMemRegion(SharedMemoryRegion<T>), //when used as input/output we are only using the local data
    /// Variant contiaining a onessided memory region that can be used as an output buffer
    LocalMemRegion(OneSidedMemoryRegion<T>),
    // UnsafeArray(UnsafeArray<T>),
}

impl<T: Dist> LamellarWrite for LamellarArrayRdmaOutput<T> {}

/// Trait for types that can be used as output to various LamellarArray RDMA operations.
pub trait LamellarWrite {}

/// Trait for types that can be used as input to various LamellarArray RDMA operations.
pub trait LamellarRead {}

impl<T: Dist> LamellarRead for &T {}

impl<T: Dist> LamellarRead for Vec<T> {}
impl<T: Dist> LamellarRead for &Vec<T> {}
impl<T: Dist> LamellarRead for &[T] {}

impl<T: Dist> TeamFrom<&T> for LamellarArrayRdmaInput<T> {
    /// Constructs a single element [OneSidedMemoryRegion] and copies `val` into it
    fn team_from(val: &T, team: &Arc<LamellarTeam>) -> Self {
        let buf: OneSidedMemoryRegion<T> = team.team.alloc_one_sided_mem_region(1);
        unsafe {
            buf.as_mut_slice()[0] = val.clone();
        }
        LamellarArrayRdmaInput::LocalMemRegion(buf)
    }
}

impl<T: Dist> TeamFrom<T> for LamellarArrayRdmaInput<T> {
    /// Constructs a single element [OneSidedMemoryRegion] and copies `val` into it
    fn team_from(val: T, _team: &Arc<LamellarTeam>) -> Self {
        // let buf: OneSidedMemoryRegion<T> = team.team.alloc_one_sided_mem_region(1);
        // unsafe {
        //     buf.as_mut_slice()[0] = val;
        // }
        LamellarArrayRdmaInput::Owned(val)
    }
}

impl<T: Dist> TeamFrom<Vec<T>> for LamellarArrayRdmaInput<T> {
    /// Constructs a [OneSidedMemoryRegion] equal in length to `vals` and copies `vals` into it
    fn team_from(vals: Vec<T>, _team: &Arc<LamellarTeam>) -> Self {
        // let buf: OneSidedMemoryRegion<T> = team.team.alloc_one_sided_mem_region(vals.len());
        // unsafe {
        //     std::ptr::copy_nonoverlapping(
        //         vals.as_ptr(),
        //         buf.as_mut_ptr().expect("Data should exist on PE"),
        //         vals.len(),
        //     );
        // }
        LamellarArrayRdmaInput::OwnedVec(vals)
    }
}
impl<T: Dist> TeamFrom<&Vec<T>> for LamellarArrayRdmaInput<T> {
    /// Constructs a [OneSidedMemoryRegion] equal in length to `vals` and copies `vals` into it
    fn team_from(vals: &Vec<T>, team: &Arc<LamellarTeam>) -> Self {
        let buf: OneSidedMemoryRegion<T> = team.team.alloc_one_sided_mem_region(vals.len());
        unsafe {
            std::ptr::copy_nonoverlapping(
                vals.as_ptr(),
                buf.as_mut_ptr().expect("Data should exist on PE"),
                vals.len(),
            );
        }
        LamellarArrayRdmaInput::LocalMemRegion(buf)
    }
}
impl<T: Dist> TeamFrom<&[T]> for LamellarArrayRdmaInput<T> {
    /// Constructs a [OneSidedMemoryRegion] equal in length to `vals` and copies `vals` into it
    fn team_from(vals: &[T], team: &Arc<LamellarTeam>) -> Self {
        let buf: OneSidedMemoryRegion<T> = team.team.alloc_one_sided_mem_region(vals.len());
        unsafe {
            std::ptr::copy_nonoverlapping(
                vals.as_ptr(),
                buf.as_mut_ptr().expect("Data should exist on PE"),
                vals.len(),
            );
        }
        LamellarArrayRdmaInput::LocalMemRegion(buf)
    }
}

impl<T: Dist> TeamFrom<&LamellarArrayRdmaInput<T>> for LamellarArrayRdmaInput<T> {
    fn team_from(lai: &LamellarArrayRdmaInput<T>, _team: &Arc<LamellarTeam>) -> Self {
        lai.clone()
    }
}

impl<T: Dist> TeamFrom<&LamellarArrayRdmaOutput<T>> for LamellarArrayRdmaOutput<T> {
    fn team_from(lao: &LamellarArrayRdmaOutput<T>, _team: &Arc<LamellarTeam>) -> Self {
        lao.clone()
    }
}

impl<T: Clone> TeamFrom<(&Vec<T>, Distribution)> for Vec<T> {
    fn team_from(vals: (&Vec<T>, Distribution), _team: &Arc<LamellarTeam>) -> Self {
        vals.0.to_vec()
    }
}

impl<T: Clone> TeamFrom<(Vec<T>, Distribution)> for Vec<T> {
    fn team_from(vals: (Vec<T>, Distribution), _team: &Arc<LamellarTeam>) -> Self {
        vals.0.to_vec()
    }
}

impl<T: Dist> TeamTryFrom<T> for LamellarArrayRdmaInput<T> {
    fn team_try_from(val: T, team: &Arc<LamellarTeam>) -> Result<Self, anyhow::Error> {
        Ok(LamellarArrayRdmaInput::team_from(val, team))
    }
}

impl<T: Dist> TeamTryFrom<&T> for LamellarArrayRdmaInput<T> {
    fn team_try_from(val: &T, team: &Arc<LamellarTeam>) -> Result<Self, anyhow::Error> {
        Ok(LamellarArrayRdmaInput::team_from(val, team))
    }
}

impl<T: Dist> TeamTryFrom<Vec<T>> for LamellarArrayRdmaInput<T> {
    fn team_try_from(val: Vec<T>, team: &Arc<LamellarTeam>) -> Result<Self, anyhow::Error> {
        if val.len() == 0 {
            Err(anyhow::anyhow!(
                "Trying to create an empty LamellarArrayRdmaInput"
            ))
        } else {
            Ok(LamellarArrayRdmaInput::team_from(val, team))
        }
    }
}

impl<T: Dist> TeamTryFrom<&Vec<T>> for LamellarArrayRdmaInput<T> {
    fn team_try_from(val: &Vec<T>, team: &Arc<LamellarTeam>) -> Result<Self, anyhow::Error> {
        if val.len() == 0 {
            Err(anyhow::anyhow!(
                "Trying to create an empty LamellarArrayRdmaInput"
            ))
        } else {
            Ok(LamellarArrayRdmaInput::team_from(val, team))
        }
    }
}

impl<T: Dist> TeamTryFrom<&[T]> for LamellarArrayRdmaInput<T> {
    fn team_try_from(val: &[T], team: &Arc<LamellarTeam>) -> Result<Self, anyhow::Error> {
        if val.len() == 0 {
            Err(anyhow::anyhow!(
                "Trying to create an empty LamellarArrayRdmaInput"
            ))
        } else {
            Ok(LamellarArrayRdmaInput::team_from(val, team))
        }
    }
}

impl<T: Dist> TeamTryFrom<&LamellarArrayRdmaInput<T>> for LamellarArrayRdmaInput<T> {
    fn team_try_from(
        lai: &LamellarArrayRdmaInput<T>,
        _team: &Arc<LamellarTeam>,
    ) -> Result<Self, anyhow::Error> {
        Ok(lai.clone())
    }
}

impl<T: Dist> TeamTryFrom<&LamellarArrayRdmaOutput<T>> for LamellarArrayRdmaOutput<T> {
    fn team_try_from(
        lao: &LamellarArrayRdmaOutput<T>,
        _team: &Arc<LamellarTeam>,
    ) -> Result<Self, anyhow::Error> {
        Ok(lao.clone())
    }
}

impl<T: Clone> TeamTryFrom<(&Vec<T>, Distribution)> for Vec<T> {
    fn team_try_from(
        vals: (&Vec<T>, Distribution),
        _team: &Arc<LamellarTeam>,
    ) -> Result<Self, anyhow::Error> {
        Ok(vals.0.to_vec())
    }
}

// #[async_trait]
impl<T: Dist + ArrayOps> AsyncTeamFrom<(Vec<T>, Distribution)> for Vec<T> {
    async fn team_from(input: (Vec<T>, Distribution), _team: &Arc<LamellarTeam>) -> Self {
        input.0
    }
}

#[async_trait]
/// Provides the same abstraction as the `From` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
/// and to be used within an async context
pub(crate) trait AsyncInto<T>: Sized {
    async fn async_into(self) -> T;
}

#[async_trait]
/// Provides the same abstraction as the `From` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
/// and to be used within an async context
pub(crate) trait AsyncFrom<T>: Sized {
    async fn async_from(val: T) -> Self;
}

// AsyncFrom implies AsyncInto
#[async_trait]
impl<T, U> AsyncInto<U> for T
where
    T: Send,
    U: AsyncFrom<T>,
{
    /// Calls `U::from(self).await`.
    ///
    /// That is, this conversion is whatever the implementation of
    /// <code>[AsyncFrom]&lt;T&gt; for U</code> chooses to do.
    #[inline]
    async fn async_into(self) -> U {
        U::async_from(self).await
    }
}

// AsyncFrom (and thus Into) is reflexive
// #[async_trait]
// impl<T> AsyncFrom<T> for T
// where
//     T: Send,
// {
//     /// Returns the argument unchanged.
//     #[inline(always)]
//     async fn async_from(t: T) -> T {
//         t
//     }
// }

/// Provides the same abstraction as the `From` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
pub trait TeamFrom<T: ?Sized> {
    /// Converts to this type from the input type
    fn team_from(val: T, team: &Arc<LamellarTeam>) -> Self;
}

// #[async_trait]
/// Provides the same abstraction as the `From` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
/// and to be used within an async context
// pub trait AsyncTeamFrom<T: ?Sized>: TeamFrom<T> + Sized {
pub trait AsyncTeamFrom<T: ?Sized>: Sized {
    /// Converts to this type from the input type
    fn team_from(val: T, team: &Arc<LamellarTeam>) -> impl Future<Output = Self> + Send;
}

/// Provides the same abstraction as the `TryFrom` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
pub trait TeamTryFrom<T: ?Sized> {
    /// Trys to convert to this type from the input type
    fn team_try_from(val: T, team: &Arc<LamellarTeam>) -> Result<Self, anyhow::Error>
    where
        Self: Sized;
}
/// Provides the same abstraction as the `Into` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
pub trait TeamInto<T: ?Sized> {
    /// converts this type into the (usually inferred) input type
    fn team_into(self, team: &Arc<LamellarTeam>) -> T;
}

/// Provides the same abstraction as the `Into` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated to be used within an async context
#[async_trait]
pub trait AsyncTeamInto<T: ?Sized> {
    /// converts this type into the (usually inferred) input type
    async fn team_into(self, team: &Arc<LamellarTeam>) -> T;
}

/// Provides the same abstraction as the `TryInto` trait in the standard language, but with a `team` parameter so that lamellar memory regions can be allocated
pub trait TeamTryInto<T>: Sized {
    /// Trys to convert this type into the (usually inferred) input type
    fn team_try_into(self, team: &Arc<LamellarTeam>) -> Result<T, anyhow::Error>;
}

impl<T, U> TeamInto<U> for T
where
    U: TeamFrom<T>,
{
    fn team_into(self, team: &Arc<LamellarTeam>) -> U {
        U::team_from(self, team)
    }
}

#[async_trait]
impl<T: Send, U> AsyncTeamInto<U> for T
where
    U: AsyncTeamFrom<T>,
{
    async fn team_into(self, team: &Arc<LamellarTeam>) -> U {
        <U as AsyncTeamFrom<T>>::team_from(self, team).await
    }
}

impl<T, U> TeamTryInto<U> for T
where
    U: TeamTryFrom<T>,
{
    fn team_try_into(self, team: &Arc<LamellarTeam>) -> Result<U, anyhow::Error> {
        U::team_try_from(self, team)
    }
}

/// Represents the array types that allow Read operations
#[enum_dispatch]
#[derive(serde::Serialize, serde::Deserialize, Clone)]
#[serde(bound = "T: Dist + serde::Serialize + serde::de::DeserializeOwned + 'static")]
pub enum LamellarReadArray<T: Dist + 'static> {
    /// An [`UnsafeArray`] that supports read operations.
    UnsafeArray(UnsafeArray<T>),
    /// A [`ReadOnlyArray`] — all elements are immutable by construction.
    ReadOnlyArray(ReadOnlyArray<T>),
    /// An [`AtomicArray`] whose elements support atomic read operations.
    AtomicArray(AtomicArray<T>),
    /// A [`LocalLockArray`] protected by a per-PE read/write lock.
    LocalLockArray(LocalLockArray<T>),
    /// A [`GlobalLockArray`] protected by a single global read/write lock.
    GlobalLockArray(GlobalLockArray<T>),
}

#[doc(hidden)]
#[enum_dispatch]
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub enum LamellarByteArray {
    //we intentially do not include "byte" in the variant name to ease construciton in the proc macros
    UnsafeArray(__UnsafeByteArray),
    ReadOnlyArray(__ReadOnlyByteArray),
    AtomicArray(__AtomicByteArray),
    NativeAtomicArray(__NativeAtomicByteArray),
    GenericAtomicArray(__GenericAtomicByteArray),
    NetworkAtomicArray(__NetworkAtomicByteArray),
    LocalLockArray(__LocalLockByteArray),
    GlobalLockArray(__GlobalLockByteArray),
}

impl std::fmt::Debug for LamellarByteArray {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LamellarByteArray::UnsafeArray(_) => write!(f, "LamellarByteArray::UnsafeArray"),
            LamellarByteArray::ReadOnlyArray(_) => write!(f, "LamellarByteArray::ReadOnlyArray"),
            LamellarByteArray::AtomicArray(_) => write!(f, "LamellarByteArray::AtomicArray"),
            LamellarByteArray::NativeAtomicArray(_) => {
                write!(f, "LamellarByteArray::NativeAtomicArray")
            }
            LamellarByteArray::GenericAtomicArray(_) => {
                write!(f, "LamellarByteArray::GenericAtomicArray")
            }
            LamellarByteArray::NetworkAtomicArray(_) => {
                write!(f, "LamellarByteArray::NetworkAtomicArray")
            }
            LamellarByteArray::LocalLockArray(_) => write!(f, "LamellarByteArray::LocalLockArray"),
            LamellarByteArray::GlobalLockArray(_) => {
                write!(f, "LamellarByteArray::GlobalLockArray")
            }
        }
    }
}

impl LamellarByteArray {
    pub fn type_id(&self) -> std::any::TypeId {
        match self {
            LamellarByteArray::UnsafeArray(_) => std::any::TypeId::of::<__UnsafeByteArray>(),
            LamellarByteArray::ReadOnlyArray(_) => std::any::TypeId::of::<__ReadOnlyByteArray>(),
            LamellarByteArray::AtomicArray(_) => std::any::TypeId::of::<__AtomicByteArray>(),
            LamellarByteArray::NativeAtomicArray(_) => {
                std::any::TypeId::of::<__NativeAtomicByteArray>()
            }
            LamellarByteArray::GenericAtomicArray(_) => {
                std::any::TypeId::of::<__GenericAtomicByteArray>()
            }
            LamellarByteArray::LocalLockArray(_) => std::any::TypeId::of::<__LocalLockByteArray>(),
            LamellarByteArray::GlobalLockArray(_) => {
                std::any::TypeId::of::<__GlobalLockByteArray>()
            }

            LamellarByteArray::NetworkAtomicArray(_) => {
                std::any::TypeId::of::<__NetworkAtomicByteArray>()
            }
        }
    }

    pub fn num_elems_local(&self) -> usize {
        match self {
            LamellarByteArray::UnsafeArray(array) => array.inner.num_elems_local(),
            LamellarByteArray::ReadOnlyArray(array) => array.array.inner.num_elems_local(),
            LamellarByteArray::AtomicArray(array) => array.num_elems_local(),
            LamellarByteArray::NativeAtomicArray(array) => array.array.inner.num_elems_local(),
            LamellarByteArray::GenericAtomicArray(array) => array.array.inner.num_elems_local(),
            LamellarByteArray::LocalLockArray(array) => array.array.inner.num_elems_local(),
            LamellarByteArray::GlobalLockArray(array) => array.array.inner.num_elems_local(),
            LamellarByteArray::NetworkAtomicArray(array) => array.array.inner.num_elems_local(),
        }
    }

    pub(crate) fn team(&self) -> Darc<LamellarTeamRT> {
        match self {
            LamellarByteArray::UnsafeArray(array) => array.inner.data.inner().darc_rt_team(),
            LamellarByteArray::ReadOnlyArray(array) => {
                array.array.inner.data.inner().darc_rt_team()
            }
            LamellarByteArray::AtomicArray(array) => array.team(),
            LamellarByteArray::NativeAtomicArray(array) => {
                array.array.inner.data.inner().darc_rt_team()
            }
            LamellarByteArray::GenericAtomicArray(array) => {
                array.array.inner.data.inner().darc_rt_team()
            }
            LamellarByteArray::LocalLockArray(array) => {
                array.array.inner.data.inner().darc_rt_team()
            }
            LamellarByteArray::GlobalLockArray(array) => {
                array.array.inner.data.inner().darc_rt_team()
            }
            LamellarByteArray::NetworkAtomicArray(array) => {
                array.array.inner.data.inner().darc_rt_team()
            }
        }
    }

    pub(crate) fn spawn<F>(&self, f: F) -> LamellarTask<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send,
    {
        match self {
            LamellarByteArray::UnsafeArray(array) => array.inner.spawn(f),
            LamellarByteArray::ReadOnlyArray(array) => array.array.inner.spawn(f),
            LamellarByteArray::AtomicArray(array) => array.spawn(f),
            LamellarByteArray::NativeAtomicArray(array) => array.array.inner.spawn(f),
            LamellarByteArray::GenericAtomicArray(array) => array.array.inner.spawn(f),
            LamellarByteArray::LocalLockArray(array) => array.array.inner.spawn(f),
            LamellarByteArray::GlobalLockArray(array) => array.array.inner.spawn(f),
            LamellarByteArray::NetworkAtomicArray(array) => array.array.inner.spawn(f),
        }
    }

    pub async fn local_data<'a, T: Dist>(&'a self) -> __LamellarLocalData<'a, T> {
        match self {
            LamellarByteArray::UnsafeArray(array) => __LamellarLocalData::Slice(array.local_data()),
            LamellarByteArray::ReadOnlyArray(array) => {
                __LamellarLocalData::Slice(array.local_data())
            }
            LamellarByteArray::AtomicArray(array) => match AtomicArray::from(array) {
                AtomicArray::NativeAtomicArray(array) => {
                    __LamellarLocalData::NativeAtomic(array.local_data())
                }
                AtomicArray::GenericAtomicArray(array) => {
                    __LamellarLocalData::GenericAtomic(array.local_data())
                }
                AtomicArray::NetworkAtomicArray(array) => {
                    __LamellarLocalData::NetworkAtomic(array.local_data())
                }
            },
            LamellarByteArray::NativeAtomicArray(array) => {
                __LamellarLocalData::NativeAtomic(NativeAtomicArray::from(array).local_data())
            }
            LamellarByteArray::GenericAtomicArray(array) => {
                __LamellarLocalData::GenericAtomic(GenericAtomicArray::from(array).local_data())
            }
            LamellarByteArray::LocalLockArray(array) => {
                __LamellarLocalData::LocalLock(LocalLockArray::from(array).read_local_data().await)
            }
            LamellarByteArray::GlobalLockArray(array) => __LamellarLocalData::GlobalLock(
                GlobalLockArray::from(array).read_local_data().await,
            ),
            LamellarByteArray::NetworkAtomicArray(array) => {
                __LamellarLocalData::NetworkAtomic(NetworkAtomicArray::from(array).local_data())
            }
        }
    }

    async fn mut_local_data<'a, T: Dist>(&'a mut self) -> __LamellarMutLocalData<'a, T> {
        match self {
            LamellarByteArray::UnsafeArray(ref mut array) => {
                __LamellarMutLocalData::Slice(array.mut_local_data())
            }
            LamellarByteArray::ReadOnlyArray(ref mut _array) => {
                panic!("ReadOnlyArray does not support mut_local_data")
            }
            LamellarByteArray::AtomicArray(ref mut array) => match AtomicArray::from(array) {
                AtomicArray::NativeAtomicArray(ref mut array) => {
                    __LamellarMutLocalData::NativeAtomic(array.mut_local_data())
                }
                AtomicArray::GenericAtomicArray(ref mut array) => {
                    __LamellarMutLocalData::GenericAtomic(array.mut_local_data())
                }
                AtomicArray::NetworkAtomicArray(ref mut array) => {
                    __LamellarMutLocalData::NetworkAtomic(array.mut_local_data())
                }
            },
            LamellarByteArray::NativeAtomicArray(ref mut array) => {
                __LamellarMutLocalData::NativeAtomic(
                    NativeAtomicArray::from(array).mut_local_data(),
                )
            }
            LamellarByteArray::GenericAtomicArray(ref mut array) => {
                __LamellarMutLocalData::GenericAtomic(
                    GenericAtomicArray::from(array).mut_local_data(),
                )
            }
            LamellarByteArray::LocalLockArray(ref mut array) => __LamellarMutLocalData::LocalLock(
                LocalLockArray::from(array).write_local_data().await,
            ),
            LamellarByteArray::GlobalLockArray(ref mut array) => {
                __LamellarMutLocalData::GlobalLock(
                    GlobalLockArray::from(array).write_local_data().await,
                )
            }
            LamellarByteArray::NetworkAtomicArray(ref mut array) => {
                __LamellarMutLocalData::NetworkAtomic(
                    NetworkAtomicArray::from(array).mut_local_data(),
                )
            }
        }
    }
}

impl crate::active_messaging::DarcSerde for LamellarByteArray {
    fn ser(&self, num_pes: usize, darcs: &mut Vec<RemotePtr>) {
        match self {
            LamellarByteArray::UnsafeArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::ReadOnlyArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::AtomicArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::NativeAtomicArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::GenericAtomicArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::LocalLockArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::GlobalLockArray(array) => array.ser(num_pes, darcs),
            LamellarByteArray::NetworkAtomicArray(array) => array.ser(num_pes, darcs),
        }
    }
    // fn des(&self, cur_pe: Result<usize, crate::IdError>) {
    //     match self {
    //         LamellarByteArray::UnsafeArray(array) => array.des(cur_pe),
    //         LamellarByteArray::ReadOnlyArray(array) => array.des(cur_pe),
    //         LamellarByteArray::AtomicArray(array) => array.des(cur_pe),
    //         LamellarByteArray::NativeAtomicArray(array) => array.des(cur_pe),
    //         LamellarByteArray::GenericAtomicArray(array) => array.des(cur_pe),
    //         LamellarByteArray::LocalLockArray(array) => array.des(cur_pe),
    //         LamellarByteArray::GlobalLockArray(array) => array.des(cur_pe),
    //     }
    // }
}

/// Internal runtime representation of mutable local buffers.
///
/// These variants exist so that active messages and other runtime components can have
/// direct access to the underlying storage, which requires the enum and its variants
/// to be public within the crate. User code should not construct or depend on this enum
/// directly; prefer `AtomicLocalData`/`LamellarArray::mut_local_data` for public APIs.
enum __LamellarMutLocalData<'a, T: Dist> {
    Slice(&'a mut [T]),
    LocalLock(LocalLockMutLocalData<T>),
    GlobalLock(GlobalLockMutLocalData<T>),
    NativeAtomic(__NativeAtomicLocalData<T>),
    GenericAtomic(__GenericAtomicLocalData<T>),
    NetworkAtomic(__NetworkAtomicLocalData<T>),
}

/// Internal runtime representation of read-only local buffers used by active messages.
///
/// The enum exists to expose storage (slices, local locks, atomics) to serialization and
/// AM dispatch logic, even though user code should remain on the public-facing
/// `AtomicLocalData`/`LamellarArray::local_data` APIs.
#[doc(hidden)]
pub enum __LamellarLocalData<'a, T: Dist> {
    Slice(&'a [T]),
    LocalLock(LocalLockLocalData<T>),
    GlobalLock(GlobalLockLocalData<T>),
    NativeAtomic(__NativeAtomicLocalData<T>),
    GenericAtomic(__GenericAtomicLocalData<T>),
    NetworkAtomic(__NetworkAtomicLocalData<T>),
}

impl<T: Dist> __LamellarLocalData<'_, T> {
    pub fn reduce<Op>(self, reduce: Op) -> Option<T>
    where
        Op: Fn(T, T) -> T,
    {
        match self {
            __LamellarLocalData::Slice(slice) => slice.iter().copied().reduce(reduce),
            __LamellarLocalData::LocalLock(local_lock) => local_lock.iter().copied().reduce(reduce),
            __LamellarLocalData::GlobalLock(global_lock) => {
                global_lock.iter().copied().reduce(reduce)
            }
            __LamellarLocalData::NativeAtomic(native_atomic) => {
                native_atomic.iter().map(|e| e.load()).reduce(reduce)
            }
            __LamellarLocalData::GenericAtomic(generic_atomic) => {
                generic_atomic.iter().map(|e| e.load()).reduce(reduce)
            }
            __LamellarLocalData::NetworkAtomic(network_atomic) => {
                network_atomic.iter().map(|e| e.load()).reduce(reduce)
            }
        }
    }
}

impl<T: Dist + 'static> crate::active_messaging::DarcSerde for LamellarReadArray<T> {
    fn ser(&self, num_pes: usize, darcs: &mut Vec<RemotePtr>) {
        // println!("in shared ser");
        match self {
            LamellarReadArray::UnsafeArray(array) => array.ser(num_pes, darcs),
            LamellarReadArray::ReadOnlyArray(array) => array.ser(num_pes, darcs),
            LamellarReadArray::AtomicArray(array) => array.ser(num_pes, darcs),
            LamellarReadArray::LocalLockArray(array) => array.ser(num_pes, darcs),
            LamellarReadArray::GlobalLockArray(array) => array.ser(num_pes, darcs),
        }
    }
    // fn des(&self, cur_pe: Result<usize, crate::IdError>) {
    //     // println!("in shared des");
    //     match self {
    //         LamellarReadArray::UnsafeArray(array) => array.des(cur_pe),
    //         LamellarReadArray::ReadOnlyArray(array) => array.des(cur_pe),
    //         LamellarReadArray::AtomicArray(array) => array.des(cur_pe),
    //         LamellarReadArray::LocalLockArray(array) => array.des(cur_pe),
    //         LamellarReadArray::GlobalLockArray(array) => array.des(cur_pe),
    //     }
    // }
}

impl<T: Dist> ActiveMessaging for LamellarReadArray<T> {
    type SinglePeAmHandle<R: AmDist> = AmHandle<R>;
    type MultiAmHandle<R: AmDist> = MultiAmHandle<R>;
    type LocalAmHandle<L> = LocalAmHandle<L>;
    fn exec_am_all<F>(&self, am: F) -> Self::MultiAmHandle<F::Output>
    where
        F: RemoteActiveMessage + LamellarAM + Serde + AmDist,
    {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.exec_am_all(am),
            LamellarReadArray::ReadOnlyArray(array) => array.exec_am_all(am),
            LamellarReadArray::AtomicArray(array) => array.exec_am_all(am),
            LamellarReadArray::LocalLockArray(array) => array.exec_am_all(am),
            LamellarReadArray::GlobalLockArray(array) => array.exec_am_all(am),
        }
    }
    fn exec_am_pe<F>(&self, pe: usize, am: F) -> Self::SinglePeAmHandle<F::Output>
    where
        F: RemoteActiveMessage + LamellarAM + Serde + AmDist,
    {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.exec_am_pe(pe, am),
            LamellarReadArray::ReadOnlyArray(array) => array.exec_am_pe(pe, am),
            LamellarReadArray::AtomicArray(array) => array.exec_am_pe(pe, am),
            LamellarReadArray::LocalLockArray(array) => array.exec_am_pe(pe, am),
            LamellarReadArray::GlobalLockArray(array) => array.exec_am_pe(pe, am),
        }
    }
    fn exec_am_local<F>(&self, am: F) -> Self::LocalAmHandle<F::Output>
    where
        F: LamellarActiveMessage + LocalAM + 'static,
    {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.exec_am_local(am),
            LamellarReadArray::ReadOnlyArray(array) => array.exec_am_local(am),
            LamellarReadArray::AtomicArray(array) => array.exec_am_local(am),
            LamellarReadArray::LocalLockArray(array) => array.exec_am_local(am),
            LamellarReadArray::GlobalLockArray(array) => array.exec_am_local(am),
        }
    }
    fn wait_all(&self) {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.wait_all(),
            LamellarReadArray::ReadOnlyArray(array) => array.wait_all(),
            LamellarReadArray::AtomicArray(array) => array.wait_all(),
            LamellarReadArray::LocalLockArray(array) => array.wait_all(),
            LamellarReadArray::GlobalLockArray(array) => array.wait_all(),
        }
    }
    fn await_all(&self) -> impl Future<Output = ()> + Send {
        let fut: Pin<Box<dyn Future<Output = ()> + Send>> = match self {
            LamellarReadArray::UnsafeArray(array) => Box::pin(array.await_all()),
            LamellarReadArray::ReadOnlyArray(array) => Box::pin(array.await_all()),
            LamellarReadArray::AtomicArray(array) => Box::pin(array.await_all()),
            LamellarReadArray::LocalLockArray(array) => Box::pin(array.await_all()),
            LamellarReadArray::GlobalLockArray(array) => Box::pin(array.await_all()),
        };
        fut
    }
    fn barrier(&self) {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.barrier(),
            LamellarReadArray::ReadOnlyArray(array) => array.barrier(),
            LamellarReadArray::AtomicArray(array) => array.barrier(),
            LamellarReadArray::LocalLockArray(array) => array.barrier(),
            LamellarReadArray::GlobalLockArray(array) => array.barrier(),
        }
    }
    fn async_barrier(&self) -> BarrierHandle {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.async_barrier(),
            LamellarReadArray::ReadOnlyArray(array) => array.async_barrier(),
            LamellarReadArray::AtomicArray(array) => array.async_barrier(),
            LamellarReadArray::LocalLockArray(array) => array.async_barrier(),
            LamellarReadArray::GlobalLockArray(array) => array.async_barrier(),
        }
    }
    fn spawn<F>(&self, f: F) -> LamellarTask<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send,
    {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.spawn(f),
            LamellarReadArray::ReadOnlyArray(array) => array.spawn(f),
            LamellarReadArray::AtomicArray(array) => array.spawn(f),
            LamellarReadArray::LocalLockArray(array) => array.spawn(f),
            LamellarReadArray::GlobalLockArray(array) => array.spawn(f),
        }
    }
    fn block_on<F: Future>(&self, f: F) -> F::Output {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.block_on(f),
            LamellarReadArray::ReadOnlyArray(array) => array.block_on(f),
            LamellarReadArray::AtomicArray(array) => array.block_on(f),
            LamellarReadArray::LocalLockArray(array) => array.block_on(f),
            LamellarReadArray::GlobalLockArray(array) => array.block_on(f),
        }
    }
    fn block_on_all<I>(&self, iter: I) -> Vec<<<I as IntoIterator>::Item as Future>::Output>
    where
        I: IntoIterator,
        <I as IntoIterator>::Item: Future + Send + 'static,
        <<I as IntoIterator>::Item as Future>::Output: Send,
    {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.block_on_all(iter),
            LamellarReadArray::ReadOnlyArray(array) => array.block_on_all(iter),
            LamellarReadArray::AtomicArray(array) => array.block_on_all(iter),
            LamellarReadArray::LocalLockArray(array) => array.block_on_all(iter),
            LamellarReadArray::GlobalLockArray(array) => array.block_on_all(iter),
        }
    }
}

impl<T: Dist> LamellarEnv for LamellarReadArray<T> {
    fn my_pe(&self) -> usize {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.my_pe(),
            LamellarReadArray::ReadOnlyArray(array) => array.my_pe(),
            LamellarReadArray::AtomicArray(array) => array.my_pe(),
            LamellarReadArray::LocalLockArray(array) => array.my_pe(),
            LamellarReadArray::GlobalLockArray(array) => array.my_pe(),
        }
    }

    fn num_pes(&self) -> usize {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.num_pes(),
            LamellarReadArray::ReadOnlyArray(array) => array.num_pes(),
            LamellarReadArray::AtomicArray(array) => array.num_pes(),
            LamellarReadArray::LocalLockArray(array) => array.num_pes(),
            LamellarReadArray::GlobalLockArray(array) => array.num_pes(),
        }
    }

    fn num_threads_per_pe(&self) -> usize {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.num_threads_per_pe(),
            LamellarReadArray::ReadOnlyArray(array) => array.num_threads_per_pe(),
            LamellarReadArray::AtomicArray(array) => array.num_threads_per_pe(),
            LamellarReadArray::LocalLockArray(array) => array.num_threads_per_pe(),
            LamellarReadArray::GlobalLockArray(array) => array.num_threads_per_pe(),
        }
    }

    fn world(&self) -> Arc<LamellarTeam> {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.world(),
            LamellarReadArray::ReadOnlyArray(array) => array.world(),
            LamellarReadArray::AtomicArray(array) => array.world(),
            LamellarReadArray::LocalLockArray(array) => array.world(),
            LamellarReadArray::GlobalLockArray(array) => array.world(),
        }
    }

    fn team(&self) -> Arc<LamellarTeam> {
        match self {
            LamellarReadArray::UnsafeArray(array) => array.team(),
            LamellarReadArray::ReadOnlyArray(array) => array.team(),
            LamellarReadArray::AtomicArray(array) => array.team(),
            LamellarReadArray::LocalLockArray(array) => array.team(),
            LamellarReadArray::GlobalLockArray(array) => array.team(),
        }
    }
}

/// Represents the array types that allow write operations
#[enum_dispatch]
#[derive(serde::Serialize, serde::Deserialize, Clone)]
#[serde(bound = "T: Dist + serde::Serialize + serde::de::DeserializeOwned")]
pub enum LamellarWriteArray<T: Dist> {
    ///
    UnsafeArray(UnsafeArray<T>),
    ///
    AtomicArray(AtomicArray<T>),
    ///
    LocalLockArray(LocalLockArray<T>),
    ///
    GlobalLockArray(GlobalLockArray<T>),
}

impl<T: Dist + 'static> crate::active_messaging::DarcSerde for LamellarWriteArray<T> {
    fn ser(&self, num_pes: usize, darcs: &mut Vec<RemotePtr>) {
        // println!("in shared ser");
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.ser(num_pes, darcs),
            LamellarWriteArray::AtomicArray(array) => array.ser(num_pes, darcs),
            LamellarWriteArray::LocalLockArray(array) => array.ser(num_pes, darcs),
            LamellarWriteArray::GlobalLockArray(array) => array.ser(num_pes, darcs),
        }
    }
    // fn des(&self, cur_pe: Result<usize, crate::IdError>) {
    //     // println!("in shared des");
    //     match self {
    //         LamellarWriteArray::UnsafeArray(array) => array.des(cur_pe),
    //         LamellarWriteArray::AtomicArray(array) => array.des(cur_pe),
    //         LamellarWriteArray::LocalLockArray(array) => array.des(cur_pe),
    //         LamellarWriteArray::GlobalLockArray(array) => array.des(cur_pe),
    //     }
    // }
}

impl<T: Dist> ActiveMessaging for LamellarWriteArray<T> {
    type SinglePeAmHandle<R: AmDist> = AmHandle<R>;
    type MultiAmHandle<R: AmDist> = MultiAmHandle<R>;
    type LocalAmHandle<L> = LocalAmHandle<L>;
    fn exec_am_all<F>(&self, am: F) -> Self::MultiAmHandle<F::Output>
    where
        F: RemoteActiveMessage + LamellarAM + Serde + AmDist,
    {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.exec_am_all(am),
            LamellarWriteArray::AtomicArray(array) => array.exec_am_all(am),
            LamellarWriteArray::LocalLockArray(array) => array.exec_am_all(am),
            LamellarWriteArray::GlobalLockArray(array) => array.exec_am_all(am),
        }
    }
    fn exec_am_pe<F>(&self, pe: usize, am: F) -> Self::SinglePeAmHandle<F::Output>
    where
        F: RemoteActiveMessage + LamellarAM + Serde + AmDist,
    {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.exec_am_pe(pe, am),
            LamellarWriteArray::AtomicArray(array) => array.exec_am_pe(pe, am),
            LamellarWriteArray::LocalLockArray(array) => array.exec_am_pe(pe, am),
            LamellarWriteArray::GlobalLockArray(array) => array.exec_am_pe(pe, am),
        }
    }
    fn exec_am_local<F>(&self, am: F) -> Self::LocalAmHandle<F::Output>
    where
        F: LamellarActiveMessage + LocalAM + 'static,
    {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.exec_am_local(am),
            LamellarWriteArray::AtomicArray(array) => array.exec_am_local(am),
            LamellarWriteArray::LocalLockArray(array) => array.exec_am_local(am),
            LamellarWriteArray::GlobalLockArray(array) => array.exec_am_local(am),
        }
    }
    fn wait_all(&self) {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.wait_all(),
            LamellarWriteArray::AtomicArray(array) => array.wait_all(),
            LamellarWriteArray::LocalLockArray(array) => array.wait_all(),
            LamellarWriteArray::GlobalLockArray(array) => array.wait_all(),
        }
    }
    fn await_all(&self) -> impl Future<Output = ()> + Send {
        let fut: Pin<Box<dyn Future<Output = ()> + Send>> = match self {
            LamellarWriteArray::UnsafeArray(array) => Box::pin(array.await_all()),
            LamellarWriteArray::AtomicArray(array) => Box::pin(array.await_all()),
            LamellarWriteArray::LocalLockArray(array) => Box::pin(array.await_all()),
            LamellarWriteArray::GlobalLockArray(array) => Box::pin(array.await_all()),
        };
        fut
    }
    fn barrier(&self) {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.barrier(),
            LamellarWriteArray::AtomicArray(array) => array.barrier(),
            LamellarWriteArray::LocalLockArray(array) => array.barrier(),
            LamellarWriteArray::GlobalLockArray(array) => array.barrier(),
        }
    }
    fn async_barrier(&self) -> BarrierHandle {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.async_barrier(),
            LamellarWriteArray::AtomicArray(array) => array.async_barrier(),
            LamellarWriteArray::LocalLockArray(array) => array.async_barrier(),
            LamellarWriteArray::GlobalLockArray(array) => array.async_barrier(),
        }
    }
    fn spawn<F>(&self, f: F) -> LamellarTask<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send,
    {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.spawn(f),
            LamellarWriteArray::AtomicArray(array) => array.spawn(f),
            LamellarWriteArray::LocalLockArray(array) => array.spawn(f),
            LamellarWriteArray::GlobalLockArray(array) => array.spawn(f),
        }
    }
    fn block_on<F: Future>(&self, f: F) -> F::Output {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.block_on(f),
            LamellarWriteArray::AtomicArray(array) => array.block_on(f),
            LamellarWriteArray::LocalLockArray(array) => array.block_on(f),
            LamellarWriteArray::GlobalLockArray(array) => array.block_on(f),
        }
    }
    fn block_on_all<I>(&self, iter: I) -> Vec<<<I as IntoIterator>::Item as Future>::Output>
    where
        I: IntoIterator,
        <I as IntoIterator>::Item: Future + Send + 'static,
        <<I as IntoIterator>::Item as Future>::Output: Send,
    {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.block_on_all(iter),
            LamellarWriteArray::AtomicArray(array) => array.block_on_all(iter),
            LamellarWriteArray::LocalLockArray(array) => array.block_on_all(iter),
            LamellarWriteArray::GlobalLockArray(array) => array.block_on_all(iter),
        }
    }
}

impl<T: Dist> LamellarEnv for LamellarWriteArray<T> {
    fn my_pe(&self) -> usize {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.my_pe(),
            LamellarWriteArray::AtomicArray(array) => array.my_pe(),
            LamellarWriteArray::LocalLockArray(array) => array.my_pe(),
            LamellarWriteArray::GlobalLockArray(array) => array.my_pe(),
        }
    }
    fn num_pes(&self) -> usize {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.num_pes(),
            LamellarWriteArray::AtomicArray(array) => array.num_pes(),
            LamellarWriteArray::LocalLockArray(array) => array.num_pes(),
            LamellarWriteArray::GlobalLockArray(array) => array.num_pes(),
        }
    }
    fn num_threads_per_pe(&self) -> usize {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.num_threads_per_pe(),
            LamellarWriteArray::AtomicArray(array) => array.num_threads_per_pe(),
            LamellarWriteArray::LocalLockArray(array) => array.num_threads_per_pe(),
            LamellarWriteArray::GlobalLockArray(array) => array.num_threads_per_pe(),
        }
    }
    fn world(&self) -> Arc<LamellarTeam> {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.world(),
            LamellarWriteArray::AtomicArray(array) => array.world(),
            LamellarWriteArray::LocalLockArray(array) => array.world(),
            LamellarWriteArray::GlobalLockArray(array) => array.world(),
        }
    }
    fn team(&self) -> Arc<LamellarTeam> {
        match self {
            LamellarWriteArray::UnsafeArray(array) => array.team(),
            LamellarWriteArray::AtomicArray(array) => array.team(),
            LamellarWriteArray::LocalLockArray(array) => array.team(),
            LamellarWriteArray::GlobalLockArray(array) => array.team(),
        }
    }
}

// private sealed trait
#[doc(hidden)]
pub trait InnerArray: Sized {
    fn as_inner(&self) -> &r#unsafe::private::UnsafeArrayInner;
}

pub(crate) mod private {
    use crate::array::{
        rdma::private::LamellarRdmaGet, AtomicArray, GenericAtomicArray, LamellarByteArray,
        LamellarReadArray, LamellarWriteArray, NativeAtomicArray, NetworkAtomicArray, UnsafeArray,
    };
    use crate::memregion::Dist;
    use crate::LamellarTeamRT;
    use crate::{active_messaging::*, Darc};
    use enum_dispatch::enum_dispatch;
    use std::sync::Arc;
    //#[doc(hidden)]
    #[enum_dispatch(LamellarReadArray<T>,LamellarWriteArray<T>)]
    pub trait LamellarArrayPrivate<T: Dist>: Clone + LamellarRdmaGet<T> {
        // // fn my_pe(&self) -> usize;
        fn inner_array(&self) -> &UnsafeArray<T>;
        fn local_as_ptr(&self) -> *const T;
        fn local_as_mut_ptr(&self) -> *mut T;
        fn pe_for_dist_index(&self, index: usize) -> Option<usize>;
        fn pe_offset_for_dist_index(&self, pe: usize, index: usize) -> Option<usize>;
        unsafe fn into_inner(self) -> UnsafeArray<T>;
        fn as_lamellar_byte_array(&self) -> LamellarByteArray;
    }

    //#[doc(hidden)]
    #[enum_dispatch(LamellarReadArray<T>,LamellarWriteArray<T>)]
    pub(crate) trait ArrayExecAm<T: Dist> {
        fn team_rt(&self) -> Darc<LamellarTeamRT>;
        fn team_counters(&self) -> Arc<AMCounters>;
        fn exec_am_local_tg<F>(&self, am: F) -> LocalAmHandle<F::Output>
        where
            F: LamellarActiveMessage + LocalAM + 'static,
        {
            self.team_rt()
                .exec_am_local_tg(am, Some(self.team_counters()), None)
        }

        fn spawn_am_local_tg<F>(&self, am: F) -> LocalAmHandle<F::Output>
        where
            F: LamellarActiveMessage + LocalAM + 'static,
        {
            self.team_rt()
                .spawn_am_local_tg(am, Some(self.team_counters()), None)
        }

        fn exec_am_pe_tg<F>(&self, pe: usize, am: F) -> AmHandle<F::Output>
        where
            F: RemoteActiveMessage + LamellarAM + AmDist,
        {
            self.team_rt()
                .exec_am_pe_tg(pe, am, Some(self.team_counters()))
        }
        fn spawn_am_pe_tg<F>(&self, pe: usize, am: F) -> AmHandle<F::Output>
        where
            F: RemoteActiveMessage + LamellarAM + AmDist,
        {
            self.team_rt()
                .spawn_am_pe_tg(pe, am, Some(self.team_counters()))
        }
        // fn exec_arc_am_pe<F>(&self, pe: usize, am: LamellarArcAm) -> AmHandle<F>
        // where
        //     F: AmDist,
        // {
        //     self.team()
        //         .exec_arc_am_pe(pe, am, Some(self.team_counters()))
        // }
        fn exec_am_all_tg<F>(&self, am: F) -> MultiAmHandle<F::Output>
        where
            F: RemoteActiveMessage + LamellarAM + AmDist,
        {
            self.team_rt()
                .exec_am_all_tg(am, Some(self.team_counters()))
        }

        fn spawn_am_all_tg<F>(&self, am: F) -> MultiAmHandle<F::Output>
        where
            F: RemoteActiveMessage + LamellarAM + AmDist,
        {
            self.team_rt()
                .spawn_am_all_tg(am, Some(self.team_counters()))
        }
    }
}

/// Represents a distributed array, providing some convenience functions for getting simple information about the array.
/// This is mostly intended for use within the runtime (specifically for use in Proc Macros) but the available functions may be useful to endusers as well.
#[enum_dispatch(LamellarReadArray<T>,LamellarWriteArray<T>)]
pub trait LamellarArray<T: Dist>:
    private::LamellarArrayPrivate<T> + ActiveMessaging + LamellarEnv
{
    // #[doc(alias("One-sided", "onesided"))]
    // /// Returns the team used to construct this array, the PEs in the team represent the same PEs which have a slice of data of the array
    // ///
    // /// # One-sided Operation
    // /// the result is returned only on the calling PE
    // ///
    // /// # Examples
    // ///```
    // /// use lamellar::array::prelude::*;
    // /// let world = LamellarWorldBuilder::new().build();
    // /// let array: LocalLockArray<usize> = LocalLockArray::new(&world,100,Distribution::Cyclic).block();
    // ///
    // /// let a_team = array.team();
    // ///```
    // fn team(&self) -> Arc<LamellarTeam>; //todo turn this into Arc<LamellarTeam>

    #[doc(alias("One-sided", "onesided"))]
    /// Return the total number of elements in this array
    ///
    /// # One-sided Operation
    /// the result is returned only on the calling PE
    ///
    /// # Examples
    ///```
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let array: UnsafeArray<usize> = UnsafeArray::new(&world,100,Distribution::Cyclic).block();
    ///
    /// assert_eq!(100,array.len());
    ///```
    fn len(&self) -> usize;

    #[doc(alias("One-sided", "onesided"))]
    /// Return the number of elements of the array local to this PE
    ///
    /// # One-sided Operation
    /// the result is returned only on the calling PE
    ///
    /// # Examples
    /// Assume a 4 PE system
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let array = ReadOnlyArray::<usize>::new(&world,100,Distribution::Cyclic).block();
    ///
    /// assert_eq!(25,array.num_elems_local());
    ///```
    fn num_elems_local(&self) -> usize;

    #[doc(alias("One-sided", "onesided"))]
    /// Given a global index, calculate the PE and offset on that PE where the element actually resides.
    /// Returns None if the index is Out of bounds
    ///
    /// # One-sided Operation
    /// the result is returned only on the calling PE
    ///
    /// # Examples
    /// assume we have 4 PEs
    /// ## Block
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    ///
    /// let block_array: UnsafeArray<usize> = UnsafeArray::new(&world,16,Distribution::Block).block();
    /// // block array index location  = PE0 [0,1,2,3],  PE1 [4,5,6,7],  PE2 [8,9,10,11], PE3 [12,13,14,15]
    /// let  Some((pe,offset)) = block_array.pe_and_offset_for_global_index(6) else { panic!("out of bounds");};
    /// assert_eq!((pe,offset) ,(1,2));
    ///```
    /// ## Cyclic
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    ///
    /// let cyclic_array: UnsafeArray<usize> = UnsafeArray::new(&world,16,Distribution::Cyclic).block();
    /// // cyclic array index location = PE0 [0,4,8,12], PE1 [1,5,9,13], PE2 [2,6,10,14], PE3 [3,7,11,15]
    /// let  Some((pe,offset)) = cyclic_array.pe_and_offset_for_global_index(6) else { panic!("out of bounds");};
    /// assert_eq!((pe,offset) ,(2,1));
    ///```
    fn pe_and_offset_for_global_index(&self, index: usize) -> Option<(usize, usize)>;

    #[doc(alias("One-sided", "onesided"))]
    /// Given a PE, return the global index of the first element on that PE
    /// Returns None if no data exists on that PE
    ///
    /// # One-sided Operation
    /// the result is returned only on the calling PE
    ///
    /// # Examples
    /// assume we have 4 PEs
    /// ## Block
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    ///
    /// let block_array: UnsafeArray<usize> = UnsafeArray::new(&world,16,Distribution::Block).block();
    /// // block array index location  = PE0 [0,1,2,3],  PE1 [4,5,6,7],  PE2 [8,9,10,11], PE3 [12,13,14,15]
    /// let index = block_array.first_global_index_for_pe(0).unwrap();
    /// assert_eq!(index , 0);
    /// let index = block_array.first_global_index_for_pe(1).unwrap();
    /// assert_eq!(index , 4);
    /// let index = block_array.first_global_index_for_pe(2).unwrap();
    /// assert_eq!(index , 8);
    /// let index = block_array.first_global_index_for_pe(3).unwrap();
    /// assert_eq!(index , 12);
    ///```
    /// ## Cyclic
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    ///
    /// let cyclic_array: UnsafeArray<usize> = UnsafeArray::new(world,16,Distribution::Cyclic).block();
    /// // cyclic array index location = PE0 [0,4,8,12], PE1 [1,5,9,13], PE2 [2,6,10,14], PE3 [3,7,11,15]
    /// let index = cyclic_array.first_global_index_for_pe(0).unwrap();
    /// assert_eq!(index , 0);
    /// let index = cyclic_array.first_global_index_for_pe(1).unwrap();
    /// assert_eq!(index , 1);
    /// let index = cyclic_array.first_global_index_for_pe(2).unwrap();
    /// assert_eq!(index , 2);
    /// let index = cyclic_array.first_global_index_for_pe(3).unwrap();
    /// assert_eq!(index , 3);
    ///```
    fn first_global_index_for_pe(&self, pe: usize) -> Option<usize>;

    #[doc(alias("One-sided", "onesided"))]
    /// Given a PE, return the global index of the last element on that PE
    /// Returns None if no data exists on that PE
    ///
    /// # One-sided Operation
    /// the result is returned only on the calling PE
    ///
    /// # Examples
    /// assume we have 4 PEs
    /// ## Block
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    ///
    /// let block_array: UnsafeArray<usize> = UnsafeArray::new(&world,16,Distribution::Block).block();
    /// // block array index location  = PE0 [0,1,2,3],  PE1 [4,5,6,7],  PE2 [8,9,10,11], PE3 [12,13,14,15]
    /// let index = block_array.last_global_index_for_pe(0).unwrap();
    /// assert_eq!(index , 3);
    /// let index = block_array.last_global_index_for_pe(1).unwrap();
    /// assert_eq!(index , 7);
    /// let index = block_array.last_global_index_for_pe(2).unwrap();
    /// assert_eq!(index , 11);
    /// let index = block_array.last_global_index_for_pe(3).unwrap();
    /// assert_eq!(index , 15);
    ///```
    /// ## Cyclic
    ///```no_run
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    ///
    /// let cyclic_array: UnsafeArray<usize> = UnsafeArray::new(world,16,Distribution::Cyclic).block();
    /// // cyclic array index location = PE0 [0,4,8,12], PE1 [1,5,9,13], PE2 [2,6,10,14], PE3 [3,7,11,15]
    /// let index = cyclic_array.last_global_index_for_pe(0).unwrap();
    /// assert_eq!(index , 12);
    /// let index = cyclic_array.last_global_index_for_pe(1).unwrap();
    /// assert_eq!(index , 13);
    /// let index = cyclic_array.last_global_index_for_pe(2).unwrap();
    /// assert_eq!(index , 14);
    /// let index = cyclic_array.last_global_index_for_pe(3).unwrap();
    /// assert_eq!(index , 15);
    ///```
    fn last_global_index_for_pe(&self, pe: usize) -> Option<usize>;
}

/// Sub arrays are contiguous subsets of the elements of an array.
///
/// A sub array increments the parent arrays reference count, so the same lifetime guarantees apply to the subarray
///
/// There can exist mutliple subarrays to the same parent array and creating sub arrays are onesided operations
pub trait SubArray<T: Dist>: LamellarArray<T> {
    #[doc(hidden)]
    type Array: LamellarArray<T>;
    #[doc(alias("One-sided", "onesided"))]
    /// Create a sub array of this array which consists of the elements specified by the range
    ///
    /// Note: it is possible that the subarray does not contain any data on this PE
    ///
    ///
    /// # One-sided Operation
    /// this does not affect how data in the array is distributed, nor require communication/coordination with other PEs in the array,
    /// rather it creates a handle on the calling PE which only has access to the elements in the specified range.
    ///
    /// # Panic
    /// This call will panic if the end of the range exceeds the size of the array.
    ///
    /// # Examples
    ///```
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let my_pe = world.my_pe();
    /// let array: AtomicArray<usize> = AtomicArray::new(&world,100,Distribution::Cyclic).block();
    ///
    /// let sub_array = array.sub_array(25..75);
    ///```
    fn sub_array<R: std::ops::RangeBounds<usize>>(&self, range: R) -> Self::Array;

    #[doc(alias("One-sided", "onesided"))]
    /// Given an index with respect to the SubArray, return the index with respect to original array.
    ///
    /// # One-sided Operation
    /// the result is returned only on the calling PE
    ///
    /// # Panic
    /// This call will panic if the end of the range exceeds the size of the array.
    ///
    /// # Examples
    ///```
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let my_pe = world.my_pe();
    /// let array: AtomicArray<usize> = AtomicArray::new(&world,100,Distribution::Cyclic).block();
    ///
    /// let sub_array = array.sub_array(25..75);
    /// assert_eq!(25,sub_array.global_index(0));
    ///```
    fn global_index(&self, sub_index: usize) -> usize;
}

/// Interface defining low level APIs for copying data from an array into a buffer or local variable
// pub trait LamellarArrayGet<T: Dist>: LamellarArrayInternalGet<T> {
//     #[doc(alias("One-sided", "onesided"))]
//     /// Performs an RDMA (Remote Direct Memory Access)  "Get" of the data in this array starting at the provided `index` into the specified `dst`
//     ///
//     /// The length of the Get is dictated by the length of the buffer.
//     ///
//     /// This call returns a future that can be awaited to determine when the `get` has finished
//     ///
//     /// Lock-based array types are not supported with RDMA calls
//     ///
//     /// # Warning
//     /// This is a low-level API, unless you are very confident in low level distributed memory access it is highly recommended
//     /// you use a safe Array type and utilize the LamellarArray load/store operations instead.
//     ///
//     /// # Safety
//     /// when using this call we need to think about safety in terms of the array and the destination buffer
//     /// ## Arrays
//     /// - [UnsafeArray] - always unsafe as there are no protections on the arrays data.
//     /// - [AtomicArray] - technically safe, but potentially not what you want, `loads` of individual elements are atomic, but a copy of a range of elements its not atomic (we iterate through the range copying each element individually)
//     /// - [ReadOnlyArray] - always safe, read only arrays are never modified.
//     /// ## Destination Buffer
//     /// - [SharedMemoryRegion] - always unsafe as there are no guarantees that there may be other local and remote readers/writers.
//     /// - [OneSidedMemoryRegion] - always unsafe as there are no guarantees that there may be other local and remote readers/writers.
//     ///
//     /// # One-sided Operation
//     /// the remote transfer is initiated by the calling PE
//     /// # Note
//     /// The future retuned by this function is lazy and does nothing unless awaited, [spawned][ArrayRdmaHandle::spawn] or [blocked on][ArrayRdmaHandle::block]
//     /// # Examples
//     ///```
//     /// use lamellar::array::prelude::*;
//     /// use lamellar::memregion::prelude::*;
//     ///
//     /// let world = LamellarWorldBuilder::new().build();
//     /// let my_pe = world.my_pe();
//     /// let array = LocalLockArray::<usize>::new(&world,12,Distribution::Block).block();
//     /// let buf = world.alloc_one_sided_mem_region::<usize>(12);
//     /// let _ = array.dist_iter_mut().enumerate().for_each(|(i,elem)| *elem = i); //we will used this val as completion detection
//     /// unsafe { // we just created buf and have not shared it so free to mutate safely
//     ///     for elem in buf.as_mut_slice()
//     ///                          .expect("we just created it so we know its local") { //initialize mem_region
//     ///         *elem = buf.len();
//     ///     }
//     /// }
//     /// array.wait_all();
//     /// array.barrier();
//     /// println!("PE{my_pe} array data: {:?}",unsafe{buf.as_slice().unwrap()});
//     /// if my_pe == 0 { //only perfrom the transfer from one PE
//     ///     println!();
//     ///      unsafe { array.get(0,&buf).block()}; //safe because we have not shared buf, and we block immediately on the request
//     /// }
//     /// println!("PE{my_pe} buf data: {:?}",unsafe{buf.as_slice().unwrap()});
//     ///
//     ///```
//     /// Possible output on A 4 PE system (ordering with respect to PEs may change)
//     ///```text
//     /// PE0: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     /// PE1: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     /// PE2: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     /// PE3: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     ///
//     /// PE1: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     /// PE2: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     /// PE3: buf data [12,12,12,12,12,12,12,12,12,12,12,12]
//     /// PE0: buf data [0,1,2,3,4,5,6,7,8,9,10,11] //we only did the "get" on PE0, also likely to be printed last since the other PEs do not wait for PE0 in this example
//     ///```
//     #[must_use = "this function is lazy and does nothing unless awaited. Either await the returned future, or call 'spawn()' or 'block()' on it "]
//     unsafe fn get<U: TeamTryInto<LamellarArrayRdmaOutput<T>> + LamellarWrite>(
//         &self,
//         index: usize,
//         dst: U,
//     ) -> ArrayRdmaHandle<T>;

//     #[doc(alias("One-sided", "onesided"))]
//     /// Retrieves the element in this array located at the specified `index`
//     ///
//     /// This call returns a future that can be awaited to retrieve to requested element
//     ///
//     /// # Safety
//     /// when using this call we need to think about safety in terms of the array type
//     /// ## Arrays
//     /// - [UnsafeArray] - always unsafe as there are no protections on the arrays data.
//     /// - [AtomicArray] - always safe as loads of a single element are atomic
//     /// - [LocalLockArray] - always safe as we grab a local read lock before transfering the data (preventing any modifcation from happening on the array)
//     /// - [ReadOnlyArray] - always safe, read only arrays are never modified.
//     ///
//     /// # One-sided Operation
//     /// the remote transfer is initiated by the calling PE
//     /// # Note
//     /// The future retuned by this function is lazy and does nothing unless awaited, [spawned][ArrayRdmaHandle::spawn] or [blocked on][ArrayRdmaHandle::block]
//     /// # Examples
//     ///```
//     /// use lamellar::array::prelude::*;
//     /// use lamellar::memregion::prelude::*;
//     ///
//     /// let world = LamellarWorldBuilder::new().build();
//     /// let my_pe = world.my_pe();
//     /// let num_pes = world.num_pes();
//     /// let array = LocalLockArray::<usize>::new(&world,12,Distribution::Block).block();
//     /// let _ = array.dist_iter_mut().enumerate().for_each(move |(i,elem)| *elem = my_pe).block(); //we will used this val as completion detection
//     /// array.barrier();
//     /// println!("PE{my_pe} array data: {:?}",array.read_local_data().block());
//     /// let index = ((my_pe+1)%num_pes) * array.num_elems_local(); // get first index on PE to the right (with wrap arround)
//     /// let at_req = array.at(index);
//     /// //do some other work
//     /// let val = at_req.block();
//     /// println!("PE{my_pe} array[{index}] = {val}");
//     ///```
//     /// Possible output on A 4 PE system (ordering with respect to PEs may change)
//     ///```text
//     /// PE0: buf data [0,0,0]
//     /// PE1: buf data [1,1,1]
//     /// PE2: buf data [2,2,2]
//     /// PE3: buf data [3,3,3]
//     ///
//     /// PE0: array[3] = 1
//     /// PE1: array[6] = 2
//     /// PE2: array[9] = 3
//     /// PE3: array[0] = 0
//     ///```
//     #[must_use = "this function is lazy and does nothing unless awaited. Either await the returned future, or call 'spawn()' or 'block()' on it "]
//     fn at(&self, index: usize) -> ArrayAtHandle<T>;
// }

// #[doc(hidden)]
// #[enum_dispatch(LamellarReadArray<T>,LamellarWriteArray<T>)]
// pub trait LamellarArrayInternalGet<T: Dist>: LamellarArray<T> {
//     unsafe fn internal_get<U: Into<LamellarMemoryRegion<T>>>(
//         &self,
//         index: usize,
//         dst: U,
//     ) -> ArrayRdmaHandle<T>;

//     // blocking call that gets the value stored and the provided index
//     unsafe fn internal_at(&self, index: usize) -> ArrayAtHandle<T>;
// }

/// Interface defining low level APIs for copying data from a buffer or local variable into this array
// pub trait LamellarArrayPut<T: Dist>: LamellarArrayInternalPut<T> {
//     #[doc(alias("One-sided", "onesided"))]
//     /// Performs an (active message based) "Put" of the data in the specified `src` buffer into this array starting from the provided `index`
//     ///
//     /// The length of the Put is dictated by the length of the `src` buffer.
//     ///
//     /// This call returns a future that can be awaited to determine when the `put` has finished
//     ///
//     /// # Warning
//     /// This is a low-level API, unless you are very confident in low level distributed memory access it is highly recommended
//     /// you use a safe Array type and utilize the LamellarArray load/store operations instead.
//     ///
//     ///
//     /// # Safety
//     /// when using this call we need to think about safety in terms of the array and the source buffer
//     ///
//     /// ## Arrays
//     /// - [UnsafeArray] - always unsafe as there are no protections on the arrays data.
//     /// - [AtomicArray] - technically safe, but potentially not what you want, `stores` of individual elements are atomic, but writing to a range of elements its not atomic overall (we iterate through the range writing to each element individually)
//     /// - [LocalLockArray] - always safe as we grab a local write lock before writing the data (ensuring mutual exclusitivity when modifying the array)
//     /// ## Source Buffer
//     /// - [SharedMemoryRegion] - always unsafe as there are no guarantees that there may be other local and remote readers/writers
//     /// - [OneSidedMemoryRegion] - always unsafe as there are no guarantees that there may be other local and remote readers/writers
//     /// - `Vec`,`T` - always safe as ownership is transfered to the `Put`
//     /// - `&Vec`, `&T` - always safe as these are immutable borrows
//     ///
//     /// # One-sided Operation
//     /// the remote transfer is initiated by the calling PE
//     /// # Note
//     /// The future retuned by this function is lazy and does nothing unless awaited, [spawned][ArrayRdmaHandle::spawn] or [blocked on][ArrayRdmaHandle::block]
//     /// # Examples
//     ///```
//     /// use lamellar::array::prelude::*;
//     /// use lamellar::memregion::prelude::*;
//     ///
//     /// let world = LamellarWorldBuilder::new().build();
//     /// let my_pe = world.my_pe();
//     /// let array = LocalLockArray::<usize>::new(&world,12,Distribution::Block).block();
//     /// let buf = world.alloc_one_sided_mem_region::<usize>(12);
//     /// let len = buf.len();
//     /// let _ = array.dist_iter_mut().for_each(move |elem| *elem = len); //we will used this val as completion detection
//     ///
//     /// //Safe as we are this is the only reference to buf
//     /// unsafe {
//     ///     for (i,elem) in buf.as_mut_slice()
//     ///                       .expect("we just created it so we know its local")
//     ///                       .iter_mut()
//     ///                        .enumerate(){ //initialize mem_region
//     ///       *elem = i;
//     ///     }
//     /// }
//     /// array.wait_all();
//     /// array.barrier();
//     /// println!("PE{my_pe} array data: {:?}",array.read_local_data().block());
//     /// if my_pe == 0 { //only perfrom the transfer from one PE
//     ///     unsafe {array.put(0,&buf).block( )};
//     ///     println!();
//     /// }
//     /// array.barrier(); //block other PEs until PE0 has finised "putting" the data
//     ///
//     /// println!("PE{my_pe} array data: {:?}",array.read_local_data().block());
//     ///
//     ///
//     ///```
//     /// Possible output on A 4 PE system (ordering with respect to PEs may change)
//     ///```text
//     /// PE0: array data [12,12,12]
//     /// PE1: array data [12,12,12]
//     /// PE2: array data [12,12,12]
//     /// PE3: array data [12,12,12]
//     ///
//     /// PE0: array data [0,1,2]
//     /// PE1: array data [3,4,5]
//     /// PE2: array data [6,7,8]
//     /// PE3: array data [9,10,11]
//     ///```
//     #[must_use = "this function is lazy and does nothing unless awaited. Either await the returned future, or call 'spawn()' or 'block()' on it "]
//     unsafe fn put<U: TeamTryInto<LamellarArrayRdmaInput<T>>>(
//         &self,
//         index: usize,
//         src: U,
//     ) -> ArrayRdmaHandle<T>;
// }

// #[doc(hidden)]
// #[enum_dispatch(LamellarWriteArray<T>)]
// pub trait LamellarArrayInternalPut<T: Dist>: LamellarArray<T> {
//     //put data from buf into self
//     unsafe fn internal_put<U: Into<LamellarMemoryRegion<T>>>(
//         &self,
//         index: usize,
//         src: U,
//     ) -> ArrayRdmaHandle<T>;
// }

/// An interfacing allowing for conveiniently printing the data contained within a lamellar array
pub trait ArrayPrint<T: Dist + std::fmt::Debug>: LamellarArray<T> {
    #[doc(alias = "Collective")]
    /// Print the data within a lamellar array
    ///
    /// # Collective Operation
    /// Requires all PEs associated with the array to enter the print call otherwise deadlock will occur (i.e. barriers are being called internally)
    ///
    /// # Examples
    ///```
    /// use lamellar::array::prelude::*;
    /// let world = LamellarWorldBuilder::new().build();
    /// let block_array = AtomicArray::<usize>::new(&world,100,Distribution::Block).block();
    /// let cyclic_array = AtomicArray::<usize>::new(&world,100,Distribution::Block).block();
    ///
    /// let _ = block_array.dist_iter_mut().enumerate().for_each(move |(i,elem)| {
    ///     elem.store(i);
    /// }).spawn();
    /// let _ =cyclic_array.dist_iter_mut().enumerate().for_each(move |(i,elem)| {
    ///     elem.store(i);
    /// }).spawn();
    /// world.wait_all();
    /// block_array.print();
    /// println!();
    /// cyclic_array.print();
    ///```
    fn print(&self);
}

// pub(crate) trait LamellarArrayReduceInner<T>: LamellarArrayInternalGet<T>
// where
//     T: Dist + AmDist + 'static,
// {
//     fn get_reduction_op(&self, op: &str) -> LamellarArcAm;
//     fn reduce_data(&self, func: LamellarArcAm) -> Box<dyn LamellarRequest<Output = T>>;
//     fn reduce_req(&self, op: &str) -> Box<dyn LamellarRequest<Output = T>>;
// }

/// An interface for performing distributed reductions accross a lamellar array.
///
/// This trait exposes a few common reductions implemented by the runtime
/// as well as the ability the launch user defined reductions that have been registered with the runtime at compile time
///
/// Please see the documentation for the [register_reduction] procedural macro for
/// more details and examples on how to create your own reductions.
///
/// Currently these are one sided reductions, meaning the calling PE will initiate the reduction, and launch the appropriate Active Messages
/// with out requiring synchronization with the other PEs
///
/// We plan to expose a collective reductions (e.g. reduce_all) in a future release, as well as support for broadcast operations.
///
/// # Safety
/// This trait is only implelemted by the safe array types, for UnsafeArray we expose unsafe APIs of the functions.
///
/// One thing to consider is that due to being a one sided reduction, safety is only gauranteed with respect to Atomicity of individual elements,
/// not with respect to the entire global array. This means that while one PE is performing a reduction, other PEs can atomically update their local
/// elements. While this is technically safe with respect to the integrity of an indivdual element (and with respect to the compiler),
/// it may not be your desired behavior.
///
/// To be clear this behavior is not an artifact of lamellar, but rather the language itself,
/// for example if you have an `Arc<Vec<AtomicUsize>>` shared on multiple threads, you could safely update the elements from each thread,
/// but performing a reduction could result in safe but non deterministic results.
///
/// In Lamellar converting to a [ReadOnlyArray] before the reduction is a straightforward workaround to enusre the data is not changing during the reduction.
///
/// # Examples
/// We provide a series of examples illustrating the above issues
///```
/// use lamellar::array::prelude::*;
/// let world = LamellarWorldBuilder::new().build();
/// let array = AtomicArray::<usize>::new(&world,1000000,Distribution::Block).block();
/// use rand::Rng;
///
/// let array_clone = array.clone();
/// let _ = array.local_iter().for_each(move |_| {
///     let index = rand::thread_rng().gen_range(0..array_clone.len());
///     let _ = array_clone.add(index,1).spawn(); //randomly at one to an element in the array.
/// }).block();
/// let sum = array.sum().block().expect("array len > 0"); // atomic updates still possibly happening, output non deterministic
/// println!("sum {sum}");
///```
/// Waiting for local operations to finish not enough by itself
///```
/// use lamellar::array::prelude::*;
/// use rand::Rng;
/// let world = LamellarWorldBuilder::new().build();
/// let array = AtomicArray::<usize>::new(&world,1000000,Distribution::Block).block();
/// let array_clone = array.clone();
/// let req = array.local_iter().for_each(move |_| {
///     let index = rand::thread_rng().gen_range(0..array_clone.len());
///     let _ = array_clone.add(index,1).spawn(); //randomly at one to an element in the array.
/// }).spawn();
/// req.block();// this is not sufficient, we also need to "wait_all" as each "add" call is another request
/// array.wait_all();
/// let sum = array.sum().block().expect("array len > 0"); // atomic updates still possibly happening (on remote nodes), output non deterministic
/// println!("sum {sum}");
///```
/// Need to add a barrier after local operations on all PEs have finished
///```
/// use lamellar::array::prelude::*;
/// use rand::Rng;
/// let world = LamellarWorldBuilder::new().build();
/// let num_pes = world.num_pes();
/// let array = AtomicArray::<usize>::new(&world,1000000,Distribution::Block).block();
/// let array_clone = array.clone();
/// let req = array.local_iter().for_each(move |_| {
///     let index = rand::thread_rng().gen_range(0..array_clone.len());
///     let _ = array_clone.add(index,1).spawn(); //randomly at one to an element in the array.
/// }).spawn();
/// req.block();// this is not sufficient, we also need to "wait_all" as each "add" call is another request
/// array.wait_all();
/// array.barrier();
/// let sum = array.sum().block().expect("array len > 0"); // No updates occuring anywhere anymore so we have a deterministic result
/// assert_eq!(array.len()*num_pes,sum);
///```
/// Alternatively we can convert our AtomicArray into a ReadOnlyArray before the reduction
/// ```
/// use lamellar::array::prelude::*;
/// use rand::Rng;
/// let world = LamellarWorldBuilder::new().build();
/// let num_pes = world.num_pes();
/// let array = AtomicArray::<usize>::new(&world,1000000,Distribution::Block).block();
/// let array_clone = array.clone();
/// let _ = array.local_iter().for_each(move |_| {
///     let index = rand::thread_rng().gen_range(0..array_clone.len());
///     let _ = array_clone.add(index,1).spawn(); //randomly at one to an element in the array.
/// }).block();
/// let array = array.into_read_only().block(); //only returns once there is a single reference remaining on each PE
/// let sum = array.sum().block().expect("array len > 0"); // No updates occuring anywhere anymore so we have a deterministic result
/// assert_eq!(array.len()*num_pes,sum);
///```
/// Finally, we are including a `Arc<Vec<AtomicUsize>>` highlighting the same issue
///```
/// use std::sync::atomic::{AtomicUsize,Ordering};
/// use std::sync::Arc;
/// use std::thread;
/// use rand::prelude::*;
/// use std::time::Duration;
///
/// let  mut data = vec![];
/// for _i in 0..1000{
///     data.push(AtomicUsize::new(0));
/// }
/// let shared_data = Arc::new(data);
/// for _i in 0..4{
///     let shared_data = shared_data.clone();
///     thread::spawn ( move ||{
///         let mut rng = rand::thread_rng();
///         for _i in 0..10000{
///             let index = rng.gen_range(0..shared_data.len());
///             shared_data[index].fetch_add(1,Ordering::SeqCst);
///         }
///     });
/// }
/// let mut sum = shared_data.iter().map(|elem| elem.load(Ordering::SeqCst)).reduce(|sum,item| sum+item).expect("iter has more than one element");
/// println!{"sum {sum:?}"};
/// while sum < 40000 {
///     sum = shared_data.iter().map(|elem| elem.load(Ordering::SeqCst)).reduce(|sum,item| sum+item).expect("iter has more than one element");
///     println!{"sum {sum:?}"};
/// }
///```
pub trait LamellarArrayReduce<T>
where
    T: Dist + AmDist + 'static,
{
    /// The Handle type returned by the reduce operation
    type Handle;
    #[doc(alias("One-sided", "onesided"))]
    /// Perform a reduction on the entire distributed array, returning the value to the calling PE.
    ///
    /// Please see the documentation for the [register_reduction] procedural macro for
    /// more details and examples on how to create your own reductions.
    ///
    /// # One-sided Operation
    /// The calling PE is responsible for launching `Reduce` active messages on the other PEs associated with the array.
    /// the returned reduction result is only available on the calling PE
    ///
    /// # Examples
    /// ```
    /// use lamellar::array::prelude::*;
    /// use rand::Rng;
    ///
    /// register_reduction!(my_sum, |a,b| a+b, usize);
    ///
    /// let world = LamellarWorldBuilder::new().build();
    /// let num_pes = world.num_pes();
    /// let array = AtomicArray::<usize>::new(&world,1000000,Distribution::Block).block();
    /// let array_clone = array.clone();
    /// let _ = array.local_iter().for_each(move |_| {
    ///     let index = rand::thread_rng().gen_range(0..array_clone.len());
    ///     let _ = array_clone.add(index,1).spawn(); //randomly at one to an element in the array.
    /// }).block();
    /// let array = array.into_read_only().block(); //only returns once there is a single reference remaining on each PE
    /// let sum = array.registered_reduce("my_sum").block().expect("array len > 0"); // equivalent to calling array.sum()
    /// assert_eq!(array.len()*num_pes,sum);
    ///```
    fn registered_reduce(&self, reduction: &str) -> Self::Handle;
}

/// This procedural macro is used to enable the execution of user defined reductions on LamellarArrays.
///
/// The general form of using this macro is:
/// ```register_reduction!(name,closure,type1,type2,...)```
/// - `name` is how the reduction will be registered with runtime and used to launch the reduction
/// - `closure` is the user defined reduction and takes the form of:
///     - ```FnMut(T, T) -> T```
/// - `type1`, `type2`,... are the types for which we would like this reduction to work for
///     - reductions get implemented as [Active Messages][crate::active_messaging] and as such must use concrete types (no generics) to register correctly
///
/// The procedural macro will appropriately construct various implmentation so that the safety guarantees of each lamellary array type are maintained.
///
/// # Panics
/// This will panic at Runtime initialization if the name of the reduction is duplicated.
///
/// # Examples
/// Recreating the "Sum" reduction
/// ```
/// use lamellar::array::prelude::*;
/// use rand::Rng;
///
/// register_reduction!(
///     my_sum, // the name of our new reduction
///     |acc,elem| acc+elem , //the reduction closure
///     usize, // will be implementd for usize,f32, and u8
///     f32,
///     u8,
/// );
/// let world = LamellarWorldBuilder::new().build();
/// let num_pes = world.num_pes();
/// let array = AtomicArray::<usize>::new(&world,1000000,Distribution::Block).block();
/// let array_clone = array.clone();
/// let _ = array.local_iter().for_each(move |_| {
///     let index = rand::thread_rng().gen_range(0..array_clone.len());
///     let _ = array_clone.add(index,1).spawn(); //randomly at one to an element in the array.
/// }).block();
/// let array = array.into_read_only().block(); //only returns once there is a single reference remaining on each PE
/// let sum =array.sum().block();
/// let my_sum = array.registered_reduce("my_sum").block(); //pass a &str containing the reduction to use
/// assert_eq!(sum,my_sum);
///```
pub use lamellar_impl::register_reduction;