summaryrefslogtreecommitdiffstats
path: root/test/vfd_swmr_bigset_writer.c
blob: 9f39c7733f990edb1a3838c408cf0ea149a44318 (plain)
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
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by Akadio, Inc.                                                 *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* This program performs "bigset" tests for VFD SWMR.  In VFD SWMR mode,
 * the bigset tests exercise
 *
 * 1 the two major indices for extensible, chunked datasets: the
 *   extensible array and the version-2 B-tree, with VFD SWMR active.
 *   The maximal dimension can be either fixed or unlimited.
 *
 * 2 reading and writing virtual datasets with source datasets residing
 *   in the same HDF5 file
 *
 * 3 virtual datasets with source datasets spread over a small number of
 *   HDF5 files
 *
 * The program selects between two personalities, reader or writer,
 * using the name it is invoked with (the last component of argv[0]).
 * Reader and writer should run simultaneously.
 *
 * The bigset tests use datasets extensible in one dimension to exercise
 * the extensible array, and tests extensible in two dimensions to
 * exercise the v2 B-tree.
 *
 * The writer opens an HDF5 file and creates `n` chunked datasets
 * extensible in `1 <= d <= 2` dimensions and runs for `i` iterations.
 * The chunk size, `w` x `h`, is user-selectable, as are `d`, `i`, and
 * `n`.  In each iteration, the writer extends each dataset by the width
 * (or the width and height) of a chunk, and writes a test pattern to
 * the dataset on chunk boundaries.
 *
 * For 3D dataset, the extension is always along the first dimension.
 * e.g. the chunk size is `l` x `m` x `n`, after `i` iterations, the
 * dataset size becomes `i x l` x `m` x `n`.
 * It does not test VDS for 3D dataset.
 *
 * The reader should be started with the same user-selectable parameters
 * as the writer: iterations, number of datasets, chunk width and
 * height and depth, dimensions.
 *
 * The reader opens the same HDF5 file, reads and re-reads it until all
 * `n` datasets appear, and then reads and re-reads the datasets until
 * all iteration 0 data is available and contains the expected test
 * pattern.  The reader repeats for the iteration 1 data, iteration 2,
 * and so on, until `i` iterations are complete.
 *
 * The reader reads datasets in chunk-sized units.  To challenge the
 * chunk index a bit, the reader reads on a chunk boundary on even
 * iterations and reads with a small offset from a chunk boundary on odd
 * iterations.
 *
 * The writer adds an attribute to every `a`th dataset, where `a` is
 * a user-selectable parameter.  The reader reads and verifies an
 * attribute on every `a`th dataset.
 *
 * To help ensure that the reader and the writer are simultaneously
 * reading and writing the HDF5 file, both reader and writer pause
 * between each dataset written/verified (if there are at least as
 * many iterations as datasets) or between each iteration (if there
 * are fewer iterations than datasets).  The duration of the pause is
 * user-selectable.
 */

#define H5C_FRIEND /*suppress error about including H5Cpkg   */
#define H5F_FRIEND /*suppress error about including H5Fpkg   */

#include "hdf5.h"

#include "H5Cpkg.h"
#include "H5Fpkg.h"
#include "H5HGprivate.h"
#include "H5VLprivate.h"

#include "testhdf5.h"
#include "H5retry_private.h"
#include "vfd_swmr_common.h"

#ifndef H5_HAVE_WIN32_API

#define MAX_READ_LEN_IN_SECONDS 2
#define TICK_LEN                4
#define MAX_LAG                 7
#define FSP_SIZE                4096
#define PAGE_BUF_SIZE           4096
#define ROWS                    256
#define COLS                    512
#define DEPTH                   1
#define RANK2                   2
#define RANK3                   3
#define NUM_ATTEMPTS            500
#define SKIP_CHUNK              0

/* Calculate the time passed in seconds.
 * X is the beginning time; Y is the ending time.
 * Expects X, Y to be struct timespec from the function call HDclock_gettime.
 */
#define TIME_PASSED(X, Y)                                                                                    \
    ((double)(((uint64_t)Y.tv_sec - (uint64_t)X.tv_sec) * 1000000000LL +                                     \
              ((uint64_t)Y.tv_nsec - (uint64_t)X.tv_nsec))) /                                                \
        1000000000.0

typedef struct _base {
    hsize_t depth, row, col;
} base_t;

typedef struct _mat {
    unsigned depth, rows, cols;
    uint32_t elt[1];
} mat_t;

typedef struct _quadrant {
    hsize_t start[RANK2];
    hsize_t stride[RANK2];
    hsize_t block[RANK2];
    hsize_t count[RANK2];
    hid_t   space, src_space;
} quadrant_t;

typedef struct _sources {
    hid_t ul, ur, bl, br;
} sources_t;

#define N_FILES 4

typedef struct {
    hid_t *     dataset;
    sources_t * sources;
    hid_t       file[N_FILES];
    hid_t       dapl, filetype, memspace, one_by_one_sid, quadrant_dcpl;
    unsigned    ndatasets;
    const char *filename[N_FILES];
    char        progname[PATH_MAX];
    struct {
        quadrant_t ul, ur, bl, br, src;
    } quadrants;
    unsigned int depth;
    unsigned int cols;
    unsigned int rows;
    unsigned int asteps;
    unsigned int nsteps;
    unsigned int part_chunk;
    unsigned int skip_chunk;
    unsigned int over_extend;
    bool         expand_2d;
    bool         test_3d;
    enum { vds_off, vds_single, vds_multi } vds;
    bool            use_vfd_swmr;
    bool            use_legacy_swmr;
    bool            use_named_pipe;
    bool            use_aux_proc;
    bool            do_perf;
    bool            cross_chunk_read;
    bool            writer;
    bool            fixed_array;
    bool            flush_raw_data;
    hsize_t         chunk_dims[RANK2];
    hsize_t         one_dee_max_dims[RANK2];
    hsize_t         fsp_size;
    size_t          page_buf_size;
    uint32_t        tick_len;
    uint32_t        max_lag;
    unsigned        mdc_init_size;
    size_t          chunk_cache_size;
    unsigned int    deflate_level;
    struct timespec ival;
} state_t;

/* Structure to hold info for named pipes */
typedef struct {
    const char *fifo_writer_to_reader; /* Name of fifo for writer to reader */
    const char *fifo_reader_to_writer; /* Name of fifo for reader to writer */
    int         fd_writer_to_reader;   /* File ID for fifo from writer to reader */
    int         fd_reader_to_writer;   /* File ID for fifo from reader to writer */
    int         notify;                /* Value to notify between writer and reader */
    int         verify;                /* Value to verify between writer and reader */
} np_state_t;

typedef struct {
    unsigned        step;
    struct timespec time;
} exchange_info_t;

static bool state_init(state_t *, int, char **);

static hsize_t two_dee_max_dims[RANK2];
static hsize_t three_dee_max_dims[RANK3];

static void
usage(const char *progname)
{
    HDfprintf(
        stderr,
        "usage: %s [-A] [-C] [-F] [-M] [-P] [-R] [-S] [-V] [-W] [-a steps] [-b] [-c cols]\n"
        "    [-d dims] [-e depth] [-f tick_len] [-g max_lag] [-j skip_chunk] [-k part_chunk]\n"
        "    [-l tick_num] [-n iterations] [-o page_buf_size] [-p fsp_size] [-r rows]\n"
        "    [-s datasets] [-t] [-u over_extend] [-v chunk_cache_size] [-w deflate_level]\n"
        "\n"
        "-A:                   use the auxiliary process to update the metadata file\n"
        "-C:                   cross-over chunk read during chunk verification\n"
        "-F:                   fixed maximal dimension for the chunked datasets\n"
        "-M:	               use virtual datasets and many source\n"
        "                      files\n"
        "-N:                   do not use named pipes\n"
        "-P:                   do the performance measurement\n"
        "-R:                   flush raw data\n"
        "-S:	               do not use VFD SWMR\n"
        "-T:                   use legacy SWMR (-S and -N must also be specified)\n"
        "-V:	               use virtual datasets and a single\n"
        "                      source file\n"
        "-a steps:	       `steps` between adding attributes\n"
        "-b:	               write data in big-endian byte order\n"
        "-c cols:	       `cols` columns of the chunk\n"
        "-d 1|one|2|two|both:  select dataset expansion in one or\n"
        "                      both dimensions\n"
        "-e depth:	       the first dimension of the 3D chunk\n"
        "-f tick_len:          tick length\n"
        "-g max_lag:           maximal lag\n"
        "-j skip_chunk:        skip the Nth (skip_chunk) chunks during chunk writing\n"
        "-k part_chunk:        the size for partial chunk write (only along the first dimension)\n"
        "-l tick_num:          expected maximal number of ticks from\n"
        "                      the writer's finishing creation to the reader's finishing validation\n"
        "-m mdc_init_size:     the initial size of metadata cache in megabytes (must be between 1 and 32MB)\n"
        "-n iterations:        how many times to expand each dataset\n"
        "-o page_buf_size:     page buffer size\n"
        "-p fsp_size:          file space page size\n"
        "-r rows:	       `rows` rows of the chunk\n"
        "-s datasets:          number of datasets to create\n"
        "-t:                   enable test for 3D datasets (dataset expansion is along one dimension)\n"
        "                      currently, 3D datasets isn't tested with VDS\n"
        "-u over_extend:       extend the size of the dataset in multiple chunks or partial chunks\n"
        "-v chunk_cache_size:  the size of raw data chunk cache in bytes\n"
        "-w deflate_level:     the level (0 - 9) of gzip compression\n"
        "\n",
        progname);
    HDexit(EXIT_FAILURE);
}

static bool
make_quadrant_dataspace(state_t *s, quadrant_t *q)
{
    if ((q->space = H5Screate_simple(NELMTS(s->chunk_dims), s->chunk_dims,
                                     s->expand_2d ? two_dee_max_dims : s->one_dee_max_dims)) < 0) {
        HDfprintf(stderr, "H5Screate_simple failed\n");
        TEST_ERROR;
    }

    if (H5Sselect_hyperslab(q->space, H5S_SELECT_SET, q->start, q->stride, q->count, q->block) < 0) {
        HDfprintf(stderr, "H5Sselect_hyperslab failed\n");
        TEST_ERROR;
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Sclose(q->space);
    }
    H5E_END_TRY;

    return false;
}

static bool
state_init(state_t *s, int argc, char **argv)
{
    unsigned long     tmp;
    int               opt;
    const hsize_t     dims  = 1;
    char *            tfile = NULL;
    char *            end;
    size_t            rdcc_nslots, rdcc_nbytes;
    double            rdcc_w0;
    quadrant_t *const ul = &s->quadrants.ul, *const ur = &s->quadrants.ur, *const bl = &s->quadrants.bl,
                      *const br = &s->quadrants.br, *const src = &s->quadrants.src;
    const char *           personality;
    const char *           s_opts   = "ACFMNPRSTVa:bc:d:e:f:g:j:k:l:m:n:o:p:qr:s:tu:v:w:";
    struct h5_long_options l_opts[] = {{NULL, 0, '\0'}};

    s->memspace         = H5I_INVALID_HID;
    s->dapl             = H5I_INVALID_HID;
    s->filetype         = H5T_NATIVE_UINT32;
    s->one_by_one_sid   = H5I_INVALID_HID;
    s->quadrant_dcpl    = H5I_INVALID_HID;
    s->depth            = DEPTH;
    s->rows             = ROWS;
    s->cols             = COLS;
    s->ndatasets        = 5;
    s->asteps           = 10;
    s->nsteps           = 100;
    s->part_chunk       = 0;
    s->skip_chunk       = SKIP_CHUNK;
    s->over_extend      = 1;
    s->expand_2d        = false;
    s->test_3d          = false;
    s->vds              = vds_off;
    s->use_vfd_swmr     = true;
    s->use_legacy_swmr  = false;
    s->use_named_pipe   = true;
    s->use_aux_proc     = false;
    s->do_perf          = false;
    s->cross_chunk_read = false;
    s->writer = true, s->fixed_array = false, s->one_dee_max_dims[0] = ROWS;
    s->one_dee_max_dims[1] = H5S_UNLIMITED;
    s->chunk_dims[0]       = ROWS;
    s->chunk_dims[1]       = COLS;
    s->fsp_size            = FSP_SIZE;
    s->page_buf_size       = PAGE_BUF_SIZE;
    s->tick_len            = TICK_LEN;
    s->max_lag             = MAX_LAG;
    s->flush_raw_data      = false;
    s->mdc_init_size       = 0;
    s->chunk_cache_size    = 0;
    s->deflate_level       = 0;

    s->ival.tv_sec  = MAX_READ_LEN_IN_SECONDS;
    s->ival.tv_nsec = 0;

    for (int i = 0; i < N_FILES; i++) {
        s->file[i]     = H5I_INVALID_HID;
        s->filename[i] = "";
    }

    if (H5_basename(argv[0], &tfile) < 0) {
        HDfprintf(stderr, "H5_basename failed\n");
        TEST_ERROR;
    }

    esnprintf(s->progname, sizeof(s->progname), "%s", tfile);

    if (tfile)
        HDfree(tfile);

    while ((opt = H5_get_option(argc, (const char *const *)argv, s_opts, l_opts)) != EOF) {
        switch (opt) {
            case 'A':
                s->use_aux_proc = true;
                break;
            case 'C':
                /* This flag indicates cross-over chunk read during data validation */
                s->cross_chunk_read = true;
                break;
            case 'F':
                /* The flag to indicate whether the maximal dimension of the chunked datasets is fixed or
                 * unlimited */
                s->fixed_array = true;
                break;
            case 'M':
                s->vds = vds_multi;
                break;
            case 'P':
                s->do_perf = true;
                break;
            case 'R':
                s->flush_raw_data = true;
                break;
            case 'S':
                s->use_vfd_swmr = false;
                break;
            case 'T':
                s->use_legacy_swmr = true;
                break;
            case 'V':
                s->vds = vds_single;
                break;
            case 'N':
                /* Disable named pipes, mainly for running the writer and reader separately */
                s->use_named_pipe = false;
                break;
            case 'd':
                if (HDstrcmp(H5_optarg, "1") == 0 || HDstrcmp(H5_optarg, "one") == 0)
                    s->expand_2d = false;
                else if (HDstrcmp(H5_optarg, "2") == 0 || HDstrcmp(H5_optarg, "two") == 0 ||
                         HDstrcmp(H5_optarg, "both") == 0)
                    s->expand_2d = true;
                else {
                    HDfprintf(stderr, "bad -d argument %s\n", H5_optarg);
                    TEST_ERROR;
                }
                break;
            case 'a':
            case 'c':
            case 'e':
            case 'f':
            case 'g':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'r':
            case 's':
            case 'u':
            case 'v':
            case 'w':
                errno = 0;
                tmp   = HDstrtoul(H5_optarg, &end, 0);
                if (end == H5_optarg || *end != '\0') {
                    HDfprintf(stderr, "couldn't parse -%c argument %s\n", opt, H5_optarg);
                    TEST_ERROR;
                }
                else if (errno != 0) {
                    HDfprintf(stderr, "couldn't parse -%c argument %s\n", opt, H5_optarg);
                    TEST_ERROR;
                }
                else if (tmp > UINT_MAX) {
                    HDfprintf(stderr, "-%c argument %lu too large", opt, tmp);
                    TEST_ERROR;
                }

                if ((opt == 'c' || opt == 'r') && tmp == 0) {
                    HDfprintf(stderr, "-%c argument %lu must be >= 1", opt, tmp);
                    TEST_ERROR;
                }

                if (opt == 'a')
                    s->asteps = (unsigned)tmp;
                else if (opt == 'c')
                    s->cols = (unsigned)tmp;
                else if (opt == 'e')
                    s->depth = (unsigned)tmp;
                else if (opt == 'f')
                    s->tick_len = (unsigned)tmp;
                else if (opt == 'g')
                    s->max_lag = (unsigned)tmp;
                else if (opt == 'j')
                    s->skip_chunk = (unsigned)tmp;
                else if (opt == 'k')
                    s->part_chunk = (unsigned)tmp;
                else if (opt == 'l') {
                    /* Translate the tick number to time represented by the timespec struct */
                    unsigned n_ticks = (unsigned)tmp * TICK_LEN;
                    float    time    = (float)n_ticks / 10.0F;
                    long     sec     = (long)time;
                    long     nsec    = (long)((time - (float)sec) * 10 * 1000 * 1000);

                    s->ival.tv_sec  = sec;
                    s->ival.tv_nsec = nsec;
                }
                else if (opt == 'm')
                    s->mdc_init_size = (unsigned)tmp;
                else if (opt == 'n')
                    s->nsteps = (unsigned)tmp;
                else if (opt == 'o')
                    s->page_buf_size = (unsigned)tmp;
                else if (opt == 'p')
                    s->fsp_size = (unsigned)tmp;
                else if (opt == 'r')
                    s->rows = (unsigned)tmp;
                else if (opt == 'u')
                    s->over_extend = (unsigned)tmp;
                else if (opt == 'v')
                    s->chunk_cache_size = (unsigned)tmp;
                else if (opt == 'w')
                    s->deflate_level = (unsigned)tmp;
                else
                    s->ndatasets = (unsigned)tmp;
                break;
            case 't':
                s->test_3d = true;
                break;
            case 'b':
                s->filetype = H5T_STD_U32BE;
                break;
            case 'q':
                verbosity = 0;
                break;
            case '?':
            default:
                usage(s->progname);
                break;
        }
    }
    argc -= H5_optind;
    argv += H5_optind;

    if (argc > 0) {
        HDfprintf(stderr, "unexpected command-line arguments\n");
        TEST_ERROR;
    }

#ifdef H5_HAVE_AUX_PROCESS
    if (s->vds == vds_multi)
        HDexit(EXIT_SUCCESS);
#endif

    if (s->vds != vds_off && s->expand_2d) {
        HDfprintf(stderr, "virtual datasets and 2D datasets are mutually exclusive\n");
        TEST_ERROR;
    }

    if (s->test_3d) {
        if (s->depth < 1) {
            HDfprintf(stderr, "The depth of 3D dataset can't be less than 1\n");
            TEST_ERROR;
        }

        if (s->expand_2d) {
            HDfprintf(stderr, "3D dataset test doesn't support 2D expansion\n");
            TEST_ERROR;
        }

        if (s->vds != vds_off) {
            HDfprintf(stderr, "3D dataset test doesn't support VDS\n");
            TEST_ERROR;
        }
    }

    s->chunk_dims[0] = s->rows;
    s->chunk_dims[1] = s->cols;

    s->one_dee_max_dims[0] = s->rows;
    if (s->fixed_array) {
        s->one_dee_max_dims[1] = s->cols * s->nsteps;
        two_dee_max_dims[0]    = s->rows * s->nsteps;
        two_dee_max_dims[1]    = s->cols * s->nsteps;

        if (s->test_3d) {
            three_dee_max_dims[0] = s->depth * s->nsteps;
            three_dee_max_dims[1] = s->rows;
            three_dee_max_dims[2] = s->cols;
        }
    }
    else {
        s->one_dee_max_dims[1] = H5S_UNLIMITED;
        two_dee_max_dims[0] = two_dee_max_dims[1] = H5S_UNLIMITED;

        if (s->test_3d) {
            three_dee_max_dims[0] = H5S_UNLIMITED;
            three_dee_max_dims[1] = s->rows;
            three_dee_max_dims[2] = s->cols;
        }
    }

    if (s->vds != vds_off) {
        const hsize_t half_chunk_dims[RANK2] = {s->rows / 2, s->cols / 2};
        hsize_t       half_max_dims[RANK2];

        if (s->fixed_array) {
            half_max_dims[0] = s->rows / 2;
            half_max_dims[1] = (s->cols * s->nsteps) / 2;
        }
        else {
            half_max_dims[0] = s->rows / 2;
            half_max_dims[1] = H5S_UNLIMITED;
        }

        if ((s->quadrant_dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) {
            HDfprintf(stderr, "H5Pcreate failed\n");
            TEST_ERROR;
        }

        if (H5Pset_chunk(s->quadrant_dcpl, RANK2, half_chunk_dims) < 0) {
            HDfprintf(stderr, "H5Pset_chunk failed\n");
            TEST_ERROR;
        }

        *ul = (quadrant_t){.start  = {0, 0},
                           .stride = {s->rows, s->cols},
                           .block  = {s->rows / 2, s->cols / 2},
                           .count  = {1, H5S_UNLIMITED}};

        *ur = (quadrant_t){.start  = {s->rows / 2, 0},
                           .stride = {s->rows, s->cols},
                           .block  = {s->rows / 2, s->cols / 2},
                           .count  = {1, H5S_UNLIMITED}};

        *bl = (quadrant_t){.start  = {0, s->cols / 2},
                           .stride = {s->rows, s->cols},
                           .block  = {s->rows / 2, s->cols / 2},
                           .count  = {1, H5S_UNLIMITED}};

        *br = (quadrant_t){.start  = {s->rows / 2, s->cols / 2},
                           .stride = {s->rows, s->cols},
                           .block  = {s->rows / 2, s->cols / 2},
                           .count  = {1, H5S_UNLIMITED}};

        if (!make_quadrant_dataspace(s, ul)) {
            HDfprintf(stderr, "make_quadrant_dataspace failed\n");
            TEST_ERROR;
        }

        if (!make_quadrant_dataspace(s, ur)) {
            HDfprintf(stderr, "make_quadrant_dataspace failed\n");
            TEST_ERROR;
        }

        if (!make_quadrant_dataspace(s, bl)) {
            HDfprintf(stderr, "make_quadrant_dataspace failed\n");
            TEST_ERROR;
        }

        if (!make_quadrant_dataspace(s, br)) {
            HDfprintf(stderr, "make_quadrant_dataspace failed\n");
            TEST_ERROR;
        }

        *src = (quadrant_t){.start  = {0, 0},
                            .stride = {s->rows / 2, s->cols / 2},
                            .block  = {s->rows / 2, s->cols / 2},
                            .count  = {1, H5S_UNLIMITED}};

        if ((src->space = H5Screate_simple(RANK2, half_chunk_dims, half_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }

        if (H5Sselect_hyperslab(src->space, H5S_SELECT_SET, src->start, src->stride, src->count, src->block) <
            0) {
            HDfprintf(stderr, "H5Sselect_hyperslab failed\n");
            TEST_ERROR;
        }

        if ((ul->src_space = H5Screate_simple(RANK2, half_chunk_dims, half_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }

        if ((ur->src_space = H5Screate_simple(RANK2, half_chunk_dims, half_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }

        if ((bl->src_space = H5Screate_simple(RANK2, half_chunk_dims, half_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }

        if ((br->src_space = H5Screate_simple(RANK2, half_chunk_dims, half_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }
    }

    /* space for attributes */
    if ((s->one_by_one_sid = H5Screate_simple(1, &dims, &dims)) < 0) {
        HDfprintf(stderr, "H5Screate_simple failed\n");
        TEST_ERROR;
    }

    if ((s->dataset = HDmalloc(sizeof(hid_t) * s->ndatasets)) == NULL) {
        HDfprintf(stderr, "HDmalloc failed\n");
        TEST_ERROR;
    }

    if ((s->sources = HDmalloc(sizeof(*s->sources) * s->ndatasets)) == NULL) {
        HDfprintf(stderr, "HDmalloc failed\n");
        TEST_ERROR;
    }

    for (unsigned i = 0; i < s->ndatasets; i++) {
        s->dataset[i]    = H5I_INVALID_HID;
        s->sources[i].ul = s->sources[i].ur = s->sources[i].bl = s->sources[i].br = H5I_INVALID_HID;
    }

    if (s->test_3d) {
        hsize_t dims3[RANK3] = {s->depth, s->chunk_dims[0], s->chunk_dims[1]};

        if (s->part_chunk)
            dims3[0] = s->part_chunk;

        if ((s->memspace = H5Screate_simple(RANK3, dims3, NULL)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }
    }
    else {
        hsize_t dims2[RANK2];

        if (s->expand_2d) {
            dims2[0] = s->chunk_dims[0];
            dims2[1] = s->chunk_dims[1];
        }
        else {
            dims2[0] = s->chunk_dims[0];

            if (s->part_chunk)
                dims2[1] = s->part_chunk;
            else
                dims2[1] = s->chunk_dims[1];
        }

        if ((s->memspace = H5Screate_simple(RANK2, dims2, NULL)) < 0) {
            HDfprintf(stderr, "H5Screate_simple failed\n");
            TEST_ERROR;
        }
    }

    /* The default is zero, meaning no skip */
    if (s->skip_chunk == 1) {
        HDfprintf(stderr, "can't skip every chunk\n");
        TEST_ERROR;
    }

    if (s->over_extend == 0) {
        HDfprintf(stderr, "Extension of the dataset can't be zero\n");
        TEST_ERROR;
    }

    s->filename[0] = "vfd_swmr_bigset.h5";
    if (s->vds == vds_multi) {
        s->filename[1] = "vfd_swmr_bigset-ur.h5";
        s->filename[2] = "vfd_swmr_bigset-bl.h5";
        s->filename[3] = "vfd_swmr_bigset-br.h5";
    }
    else {
        s->filename[1] = s->filename[0];
        s->filename[2] = s->filename[0];
        s->filename[3] = s->filename[0];
    }

    personality = HDstrstr(s->progname, "vfd_swmr_bigset_");

    if (personality != NULL && HDstrcmp(personality, "vfd_swmr_bigset_writer") == 0)
        s->writer = true;
    else if (personality != NULL && HDstrcmp(personality, "vfd_swmr_bigset_reader") == 0)
        s->writer = false;
    else {
        HDfprintf(stderr, "unknown personality, expected vfd_swmr_bigset_{reader,writer}\n");
        TEST_ERROR;
    }

    if ((s->dapl = H5Pcreate(H5P_DATASET_ACCESS)) < 0) {
        HDfprintf(stderr, "H5Pcreate failed\n");
        TEST_ERROR;
    }

    if (s->chunk_cache_size) {
        if (H5Pget_chunk_cache(s->dapl, &rdcc_nslots, &rdcc_nbytes, &rdcc_w0) < 0) {
            HDfprintf(stderr, "H5Pget_chunk_cache failed\n");
            TEST_ERROR;
        }

        if (H5Pset_chunk_cache(s->dapl, rdcc_nslots, s->chunk_cache_size, rdcc_w0) < 0) {
            HDfprintf(stderr, "H5Pset_chunk_cache failed\n");
            TEST_ERROR;
        }
    }

    if (s->deflate_level > 9) {
        HDfprintf(stderr, "deflation level must be between 0 and 9\n");
        TEST_ERROR;
    }

    if (s->vds != vds_off && H5Pset_virtual_view(s->dapl, H5D_VDS_FIRST_MISSING) < 0) {
        HDfprintf(stderr, "H5Pset_virtual_view failed\n");
        TEST_ERROR;
    }

    if (s->use_legacy_swmr) {
        if (s->use_vfd_swmr) {
            HDfprintf(stderr, "Can't use both VFD SWMR and Legacy SWMR\n");
            TEST_ERROR;
        }

        if (s->use_named_pipe) {
            HDfprintf(stderr, "Can't use named pipe for the Legacy SWMR\n");
            TEST_ERROR;
        }
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(s->quadrant_dcpl);
        H5Sclose(ul->space);
        H5Sclose(ur->space);
        H5Sclose(bl->space);
        H5Sclose(br->space);
        H5Sclose(ul->src_space);
        H5Sclose(ur->src_space);
        H5Sclose(bl->src_space);
        H5Sclose(br->src_space);
        H5Sclose(src->space);
        H5Sclose(s->one_by_one_sid);
        H5Sclose(s->memspace);
    }
    H5E_END_TRY;

    if (tfile)
        HDfree(tfile);

    if (s->dataset)
        HDfree(s->dataset);

    if (s->sources)
        HDfree(s->sources);

    return false;
}

static bool
state_destroy(state_t *s)
{
    size_t          i;
    struct timespec start_time, end_time;

    if (H5Pclose(s->dapl) < 0) {
        HDfprintf(stderr, "H5Pclose failed\n");
        TEST_ERROR;
    }

    if (s->vds != vds_off) {
        quadrant_t *const ul = &s->quadrants.ul, *const ur = &s->quadrants.ur, *const bl = &s->quadrants.bl,
                          *const br = &s->quadrants.br;

        if (H5Sclose(ul->src_space) < 0 || H5Sclose(ur->src_space) < 0 || H5Sclose(bl->src_space) < 0 ||
            H5Sclose(br->src_space) < 0) {
            HDfprintf(stderr, "H5Sclose failed\n");
            TEST_ERROR;
        }

        if (H5Sclose(ul->space) < 0 || H5Sclose(ur->space) < 0 || H5Sclose(bl->space) < 0 ||
            H5Sclose(br->space) < 0) {
            HDfprintf(stderr, "H5Sclose failed\n");
            TEST_ERROR;
        }

        if (H5Pclose(s->quadrant_dcpl) < 0) {
            HDfprintf(stderr, "H5Pclose failed\n");
            TEST_ERROR;
        }
    }

    if (H5Sclose(s->one_by_one_sid) < 0) {
        HDfprintf(stderr, "H5Sclose failed\n");
        TEST_ERROR;
    }

    if (H5Sclose(s->memspace) < 0) {
        HDfprintf(stderr, "H5Sclose failed\n");
        TEST_ERROR;
    }

    /* The writer ends the tick before closing the file to make the data visible to the reader */
    if (s->writer && s->use_vfd_swmr) {
        unsigned long j;

        if (s->vds != vds_multi) {
            if (H5Fvfd_swmr_end_tick(s->file[0]) < 0) {
                HDfprintf(stderr, "H5Fvfd_swmr_end_tick failed\n");
                TEST_ERROR;
            }
        }
        else {
            for (j = 0; j < NELMTS(s->file); j++)
                if (H5Fvfd_swmr_end_tick(s->file[j]) < 0) {
                    HDfprintf(stderr, "H5Fvfd_swmr_end_tick failed\n");
                    TEST_ERROR;
                }
        }
    }

    /* For checking the time spent in file close.  It's for running the writer alone */
    if (s->do_perf) {
        if (HDclock_gettime(CLOCK_MONOTONIC, &start_time) == -1) {
            HDfprintf(stderr, "HDclock_gettime failed");
            TEST_ERROR;
        }
    }

    for (i = 0; i < NELMTS(s->file); i++) {
        hid_t fid = s->file[i];

        s->file[i] = H5I_INVALID_HID;

        if (s->vds != vds_multi && i > 0)
            continue;

        if (H5Fclose(fid) < 0) {
            HDfprintf(stderr, "H5Fclose failed\n");
            TEST_ERROR;
        }
    }

    /* For checking the time spent in file close.  It's for running the writer alone */
    if (s->do_perf) {
        if (HDclock_gettime(CLOCK_MONOTONIC, &end_time) == -1) {
            HDfprintf(stderr, "HDclock_gettime failed");
            TEST_ERROR;
        }

        HDfprintf(stdout, "File close time (for running the writer alone) = %lf seconds\n",
                  TIME_PASSED(start_time, end_time));
    }

    if (s->dataset)
        HDfree(s->dataset);

    if (s->sources)
        HDfree(s->sources);

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(s->quadrant_dcpl);
        H5Sclose(s->one_by_one_sid);
        H5Sclose(s->memspace);
    }
    H5E_END_TRY;

    if (s->dataset)
        HDfree(s->dataset);

    if (s->sources)
        HDfree(s->sources);

    return false;
}

/*
 * Initialize the named pipes for test synchronization.
 * (This function can go to vfd_swmr_common.c.  It's the same as
 * the one in vfd_swmr_attrdset_writer.c)
 */
static bool
np_init(np_state_t *np, bool writer)
{
    np->fifo_writer_to_reader = "./fifo_bigset_writer_to_reader";
    np->fifo_reader_to_writer = "./fifo_bigset_reader_to_writer";
    np->fd_writer_to_reader   = -1;
    np->fd_reader_to_writer   = -1;
    np->notify                = 0;
    np->verify                = 0;

    /*
     * Use two named pipes(FIFO) to coordinate the writer and reader for
     * two-way communication so that the two sides can move forward together.
     * One is for the writer to write to the reader.
     * The other one is for the reader to signal the writer.
     */
    if (writer) {
        /* If the named pipes are present at the start of the test, remove them */
        if (HDaccess(np->fifo_writer_to_reader, F_OK) == 0)
            if (HDremove(np->fifo_writer_to_reader) != 0) {
                HDfprintf(stderr, "HDremove fifo_writer_to_reader failed\n");
                TEST_ERROR;
            }

        if (HDaccess(np->fifo_reader_to_writer, F_OK) == 0)
            if (HDremove(np->fifo_reader_to_writer) != 0) {
                HDfprintf(stderr, "HDremove fifo_reader_to_writer failed\n");
                TEST_ERROR;
            }

        /* Writer creates two named pipes(FIFO) */
        if (HDmkfifo(np->fifo_writer_to_reader, 0600) < 0) {
            HDfprintf(stderr, "HDmkfifo fifo_writer_to_reader failed\n");
            TEST_ERROR;
        }

        if (HDmkfifo(np->fifo_reader_to_writer, 0600) < 0) {
            HDfprintf(stderr, "HDmkfifo fifo_reader_to_writer failed\n");
            TEST_ERROR;
        }
    }

    /* Both the writer and reader open the pipes */
    if ((np->fd_writer_to_reader = HDopen(np->fifo_writer_to_reader, O_RDWR)) < 0) {
        HDfprintf(stderr, "HDopen fifo_writer_to_reader failed\n");
        TEST_ERROR;
    }

    if ((np->fd_reader_to_writer = HDopen(np->fifo_reader_to_writer, O_RDWR)) < 0) {
        HDfprintf(stderr, "HDopen fifo_reader_to_writer failed\n");
        TEST_ERROR;
    }

    return true;

error:
    return false;
} /* np_init() */

/*
 * Close and remove the named pipes.
 * (This function can go to vfd_swmr_common.c.  It's the same as
 * the one in vfd_swmr_attrdset_writer.c)
 */
static bool
np_close(np_state_t *np, bool writer)
{
    /* Both the writer and reader close the named pipes */
    if (HDclose(np->fd_writer_to_reader) < 0) {
        HDfprintf(stderr, "HDclose fd_writer_to_reader failed\n");
        TEST_ERROR;
    }

    if (HDclose(np->fd_reader_to_writer) < 0) {
        HDfprintf(stderr, "HDclose fd_reader_to_writer failed\n");
        TEST_ERROR;
    }

    /* Reader finishes last and deletes the named pipes */
    if (!writer) {
        if (HDremove(np->fifo_writer_to_reader) != 0) {
            HDfprintf(stderr, "HDremove fifo_writer_to_reader failed\n");
            TEST_ERROR;
        }

        if (HDremove(np->fifo_reader_to_writer) != 0) {
            HDfprintf(stderr, "HDremove fifo_reader_to_writer failed\n");
            TEST_ERROR;
        }
    }
    return true;

error:
    return false;
} /* np_close() */

/* Wait for the writer's notice before starting validation */
static int
reader_verify(np_state_t *np, int verify)
{
    int notify;

    if (HDread(np->fd_writer_to_reader, &notify, sizeof(int)) < 0) {
        HDfprintf(stderr, "HDread failed\n");
        TEST_ERROR;
    }

    if (notify != verify) {
        HDfprintf(stderr, "expected %d but read %d\n", verify, notify);
        TEST_ERROR;
    }

    return 0;

error:
    return -1;
}

/* Notify the reader of finishing creation by sending the timestamp
 * and wait for the reader to finish validation before proceeding */
static int
notify_and_wait_for_reader(state_t *s, np_state_t *np)
{
    int             notify;
    unsigned int    i;
    struct timespec last = {0, 0};

    /* Get the time when finishing creation */
    if (HDclock_gettime(CLOCK_MONOTONIC, &last) < 0) {
        HDfprintf(stderr, "HDclock_gettime failed\n");
        TEST_ERROR;
    }

    /* Notify the reader of finishing creation by sending the timestamp */
    if (HDwrite(np->fd_writer_to_reader, &last, sizeof(last)) < 0) {
        HDfprintf(stderr, "HDwrite failed\n");
        TEST_ERROR;
    }

    /* During the wait, writer makes repeated HDF5 API calls so as to trigger
     * EOT at approximately the correct time */
    for (i = 0; i < MAX_LAG + 1; i++) {
        decisleep(TICK_LEN);

        H5E_BEGIN_TRY
        {
            H5Aexists(s->file[0], "nonexistent");
        }
        H5E_END_TRY;
    }

    /* Wait until the reader finishes validating creation */
    if (HDread(np->fd_reader_to_writer, &notify, sizeof(int)) < 0) {
        HDfprintf(stderr, "HDread failed\n");
        TEST_ERROR;
    }

    if (notify != np->verify) {
        HDfprintf(stderr, "expected %d but read %d\n", np->verify, notify);
        TEST_ERROR;
    }

    return 0;

error:
    return -1;
}

/* Receive the notice of the writer finishing dataset creation (timestamp)
 * Make sure the dataset validation doesn't take longer than the expected time.
 * This time period is from the writer finishing dataset creation to the reader finishing
 * the validation of dataset creation */
static int
reader_check_time_and_notify_writer(np_state_t *np, state_t *s)
{
    struct timespec last = {0, 0};

    /* Receive the notice of the writer finishing creation (timestamp) */
    if (HDread(np->fd_writer_to_reader, &last, sizeof(last)) < 0) {
        HDfprintf(stderr, "HDread failed\n");
        TEST_ERROR;
    }

    /* If the dataset validation takes longer than the expected time, issue a warning.
     * This time period is from the writer finishing dataset creation to the reader finishing
     * the validation of dataset creation */
    if (below_speed_limit(&last, &(s->ival))) {
        AT();
        HDfprintf(stderr, "Warning: dataset validation took too long to finish\n");
    }

    /* Notify the writer that dataset validation is finished */
    if (HDwrite(np->fd_reader_to_writer, &(np->notify), sizeof(int)) < 0) {
        HDfprintf(stderr, "HDwrite failed\n");
        TEST_ERROR;
    }

    return 0;

error:
    return -1;
}

/* Notify the reader by sending the timestamp and the number of chunks written */
static int
notify_reader(np_state_t *np, unsigned step)
{
    exchange_info_t *last = HDcalloc(1, sizeof(exchange_info_t));

    /* Get the time */
    if (HDclock_gettime(CLOCK_MONOTONIC, &(last->time)) < 0) {
        HDfprintf(stderr, "HDclock_gettime failed\n");
        TEST_ERROR;
    }

    last->step = step;

    /* Notify the reader by sending the timestamp and the number of chunks written */
    if (HDwrite(np->fd_writer_to_reader, last, sizeof(exchange_info_t)) < 0) {
        HDfprintf(stderr, "HDwrite failed");
        TEST_ERROR;
    }

    if (last)
        HDfree(last);

    return 0;

error:
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    md_ck_cb()
 *
 * Purpose:     This is the callback function for debugging only.  It's used
 *              when the H5F_ACS_GENERATE_MD_CK_CB_NAME property is set in fapl.
 *                  --Opens and read the metadata file into a buffer.
 *                  --Generate checksum for the metadata file
 *                  --Write the tick number and the checksum to the checksum file
 *
 * Return:      0 if test is successful
 *              1 if test fails
 *
 *-------------------------------------------------------------------------
 */
#ifdef TMP
static herr_t
md_ck_cb(char *md_file_path, uint64_t updater_seq_num)
{
    FILE *   md_fp  = NULL;      /* Metadata file pointer */
    FILE *   chk_fp = NULL;      /* Checksum file pointer */
    long     size   = 0;         /* File size returned from HDftell() */
    void *   buf    = NULL;      /* Buffer for holding the metadata file content */
    uint32_t chksum = 0;         /* The checksum generated for the metadata file */
    char     chk_name[1024 + 4]; /* Buffer for the checksum file name */
    size_t   ret;                /* Return value */

    /* Open the metadata file */
    if ((md_fp = HDfopen(md_file_path, "r")) == NULL)
        FAIL_STACK_ERROR;

    /* Set file pointer at end of file.*/
    if (HDfseek(md_fp, 0, SEEK_END) < 0)
        FAIL_STACK_ERROR;

    /* Get the current position of the file pointer.*/
    if ((size = HDftell(md_fp)) < 0)
        FAIL_STACK_ERROR;

    if (size != 0) {

        HDrewind(md_fp);

        if ((buf = HDmalloc((size_t)size)) == NULL)
            FAIL_STACK_ERROR;

        /* Read the metadata file to buf */
        if ((ret = HDfread(buf, 1, (size_t)size, md_fp)) != (size_t)size)
            FAIL_STACK_ERROR;

        /* Calculate checksum of the metadata file */
        chksum = H5_checksum_metadata(buf, (size_t)size, 0);
    }

    /* Close the metadata file */
    if (md_fp && HDfclose(md_fp) < 0)
        FAIL_STACK_ERROR;

    /*
     *  Checksum file
     */

    /* Generate checksum file name: <md_file_path>.chk */
    HDsprintf(chk_name, "%s.chk", md_file_path);

    /* Open checksum file for append */
    if ((chk_fp = HDfopen(chk_name, "a")) == NULL)
        FAIL_STACK_ERROR;

    /* Write the updater sequence number to the checksum file */
    if ((ret = HDfwrite(&updater_seq_num, sizeof(uint64_t), 1, chk_fp)) != 1)
        FAIL_STACK_ERROR;

    /* Write the checksum to the checksum file */
    if ((ret = HDfwrite(&chksum, sizeof(uint32_t), 1, chk_fp)) != 1)
        FAIL_STACK_ERROR;

    /* Close the checksum file */
    if (chk_fp && HDfclose(chk_fp) != 0)
        FAIL_STACK_ERROR;

    HDfree(buf);

    return 0;

error:
    HDfree(buf);

    if (md_fp)
        HDfclose(md_fp);
    if (chk_fp)
        HDfclose(chk_fp);

    return -1;
} /* md_ck_cb() */
#endif

static bool
create_extensible_dset(state_t *s, unsigned int which)
{
    quadrant_t *const ul = &s->quadrants.ul, *const ur = &s->quadrants.ur, *const bl = &s->quadrants.bl,
                      *const br = &s->quadrants.br, *const src = &s->quadrants.src;
    char dname[sizeof("/dataset-9999999999")];
    char ul_dname[sizeof("/ul-dataset-9999999999")], ur_dname[sizeof("/ur-dataset-9999999999")],
        bl_dname[sizeof("/bl-dataset-9999999999")], br_dname[sizeof("/br-dataset-9999999999")];
    hid_t   dcpl = H5I_INVALID_HID, dset_id = H5I_INVALID_HID, filespace = H5I_INVALID_HID;
    hsize_t dims3[3] = {s->depth, s->chunk_dims[0], s->chunk_dims[1]};

    esnprintf(dname, sizeof(dname), "/dataset-%d", which);

    if ((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) {
        HDfprintf(stderr, "H5Pcreate failed\n");
        TEST_ERROR;
    }

    if (s->test_3d) {
        /* The chunk is L x M x N and grows along the first dimension */
        if (H5Pset_chunk(dcpl, RANK3, dims3) < 0) {
            HDfprintf(stderr, "H5Pset_chunk for 3D dataset failed\n");
            TEST_ERROR;
        }
    }
    else {
        if (H5Pset_chunk(dcpl, RANK2, s->chunk_dims) < 0) {
            HDfprintf(stderr, "H5Pset_chunk for 2D dataset failed\n");
            TEST_ERROR;
        }
    }

    /* Never write fill value when new chunks are allocated */
    if (H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) {
        HDfprintf(stderr, "H5Pset_fill_time failed\n");
        TEST_ERROR;
    }

    /* Early space allocation */
    if (H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_EARLY) < 0) {
        HDfprintf(stderr, "H5Pset_alloc_time failed\n");
        TEST_ERROR;
    }

    /* GZIP compression */
    if (s->deflate_level && H5Pset_deflate(dcpl, s->deflate_level) < 0) {
        HDfprintf(stderr, "H5Pset_deflate failed\n");
        TEST_ERROR;
    }

    if (s->vds != vds_off) {
        sources_t *const srcs = &s->sources[which];

        esnprintf(ul_dname, sizeof(ul_dname), "/ul-dataset-%d", which);
        esnprintf(ur_dname, sizeof(ur_dname), "/ur-dataset-%d", which);
        esnprintf(bl_dname, sizeof(bl_dname), "/bl-dataset-%d", which);
        esnprintf(br_dname, sizeof(br_dname), "/br-dataset-%d", which);

        if ((srcs->ul = H5Dcreate2(s->file[0], ul_dname, s->filetype, ul->src_space, H5P_DEFAULT,
                                   s->quadrant_dcpl, s->dapl)) < 0) {
            HDfprintf(stderr, "H5Dcreate2 failed\n");
            TEST_ERROR;
        }

        if ((srcs->ur = H5Dcreate2(s->file[1], ur_dname, s->filetype, ur->src_space, H5P_DEFAULT,
                                   s->quadrant_dcpl, s->dapl)) < 0) {
            HDfprintf(stderr, "H5Dcreate2 failed\n");
            TEST_ERROR;
        }

        if ((srcs->bl = H5Dcreate2(s->file[2], bl_dname, s->filetype, bl->src_space, H5P_DEFAULT,
                                   s->quadrant_dcpl, s->dapl)) < 0) {
            HDfprintf(stderr, "H5Dcreate2 failed\n");
            TEST_ERROR;
        }

        if ((srcs->br = H5Dcreate2(s->file[3], br_dname, s->filetype, br->src_space, H5P_DEFAULT,
                                   s->quadrant_dcpl, s->dapl)) < 0) {
            HDfprintf(stderr, "H5Dcreate2 failed\n");
            TEST_ERROR;
        }

        if (H5Pset_virtual(dcpl, ul->space, s->filename[0], ul_dname, src->space) < 0) {
            HDfprintf(stderr, "H5Pset_virtual failed\n");
            TEST_ERROR;
        }

        if (H5Pset_virtual(dcpl, ur->space, s->filename[1], ur_dname, src->space) < 0) {
            HDfprintf(stderr, "H5Pset_virtual failed\n");
            TEST_ERROR;
        }

        if (H5Pset_virtual(dcpl, bl->space, s->filename[2], bl_dname, src->space) < 0) {
            HDfprintf(stderr, "H5Pset_virtual failed\n");
            TEST_ERROR;
        }

        if (H5Pset_virtual(dcpl, br->space, s->filename[3], br_dname, src->space) < 0) {
            HDfprintf(stderr, "H5Pset_virtual failed\n");
            TEST_ERROR;
        }
    }

    if (s->test_3d) {
        if ((filespace = H5Screate_simple(RANK3, dims3, three_dee_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple 3D dataspace failed\n");
            TEST_ERROR;
        }
    }
    else {
        if ((filespace = H5Screate_simple(RANK2, s->chunk_dims,
                                          s->expand_2d ? two_dee_max_dims : s->one_dee_max_dims)) < 0) {
            HDfprintf(stderr, "H5Screate_simple 2D dataspace failed\n");
            TEST_ERROR;
        }
    }

    if ((dset_id = H5Dcreate2(s->file[0], dname, s->filetype, filespace, H5P_DEFAULT, dcpl, s->dapl)) < 0) {
        HDfprintf(stderr, "H5Dcreate2 failed\n");
        TEST_ERROR;
    }

    if (H5Sclose(filespace) < 0) {
        HDfprintf(stderr, "H5Sclose failed\n");
        TEST_ERROR;
    }

    if (H5Pclose(dcpl) < 0) {
        HDfprintf(stderr, "H5Pclose failed\n");
        TEST_ERROR;
    }

    s->dataset[which] = dset_id;

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Dclose(dset_id);
        H5Pclose(dcpl);
        H5Sclose(filespace);
    }
    H5E_END_TRY;

    return false;
}

static bool
close_extensible_dset(state_t *s, unsigned int which)
{
    char  dname[sizeof("/dataset-9999999999")];
    hid_t dset_id = H5I_INVALID_HID;

    if (which >= s->ndatasets) {
        HDfprintf(stderr, "index is out of range\n");
        TEST_ERROR;
    }

    esnprintf(dname, sizeof(dname), "/dataset-%d", which);

    dset_id = s->dataset[which];

    if (H5Dclose(dset_id) < 0) {
        HDfprintf(stderr, "H5Dclose failed\n");
        TEST_ERROR;
    }

    s->dataset[which] = H5I_INVALID_HID;

    if (s->vds != vds_off && s->writer) {
        sources_t *const srcs = &s->sources[which];

        if (H5Dclose(srcs->ul) < 0 || H5Dclose(srcs->ur) < 0 || H5Dclose(srcs->bl) < 0 ||
            H5Dclose(srcs->br) < 0) {
            HDfprintf(stderr, "H5Dclose failed\n");
            TEST_ERROR;
        }
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Dclose(dset_id);
    }
    H5E_END_TRY;

    return false;
}

static bool
open_extensible_dset(state_t *s)
{
    hsize_t      dims2[RANK2], maxdims2[RANK2];
    hsize_t      dims3[RANK3], maxdims3[RANK3];
    char         dname[sizeof("/dataset-9999999999")];
    hid_t        dset_id   = H5I_INVALID_HID;
    hid_t        filespace = H5I_INVALID_HID;
    hid_t        dtype     = H5I_INVALID_HID;
    int          rank;
    unsigned int which, i;

    for (which = 0; which < s->ndatasets; which++) {
        esnprintf(dname, sizeof(dname), "/dataset-%d", which);

        /* Tries to open the dataset repeatedly until successful.  After trying
         * NUM_ATTEMPTS times without success, report it as a failure
         */
        for (i = 0; i < NUM_ATTEMPTS; i++) {
            H5E_BEGIN_TRY
            {
                dset_id = H5Dopen2(s->file[0], dname, s->dapl);
            }
            H5E_END_TRY;

            if (dset_id >= 0)
                break;
            else
                decisleep(1);
        }

        if (i == NUM_ATTEMPTS) {
            HDfprintf(stderr, "chunk verification reached the maximal number of attempts\n");
            TEST_ERROR;
        }

        if ((dtype = H5Dget_type(dset_id)) < 0) {
            HDfprintf(stderr, "H5Dget_type failed\n");
            TEST_ERROR;
        }

        if (H5Tequal(dtype, s->filetype) <= 0) {
            HDfprintf(stderr, "Unexpected data type\n");
            TEST_ERROR;
        }

        if ((filespace = H5Dget_space(dset_id)) < 0) {
            HDfprintf(stderr, "H5Dget_space failed\n");
            TEST_ERROR;
        }

        if ((rank = H5Sget_simple_extent_ndims(filespace)) < 0) {
            HDfprintf(stderr, "H5Sget_simple_extent_ndims failed\n");
            TEST_ERROR;
        }

        if ((s->test_3d && rank != RANK3) || (!s->test_3d && rank != RANK2)) {
            HDfprintf(stderr, "Unexpected data rank: %d\n", rank);
            TEST_ERROR;
        }

        if (s->test_3d) {
            if (H5Sget_simple_extent_dims(filespace, dims3, maxdims3) < 0) {
                HDfprintf(stderr, "H5Sget_simple_extent_dims failed\n");
                TEST_ERROR;
            }
        }
        else {
            if (H5Sget_simple_extent_dims(filespace, dims2, maxdims2) < 0) {
                HDfprintf(stderr, "H5Sget_simple_extent_dims failed\n");
                TEST_ERROR;
            }
        }

        if (H5Sclose(filespace) < 0) {
            HDfprintf(stderr, "H5Sclose failed\n");
            TEST_ERROR;
        }

        if (H5Tclose(dtype) < 0) {
            HDfprintf(stderr, "H5Tclose failed\n");
            TEST_ERROR;
        }

        if (s->test_3d) {
            if (maxdims3[0] != three_dee_max_dims[0] || maxdims3[1] != three_dee_max_dims[1] ||
                maxdims3[2] != three_dee_max_dims[2]) {
                HDfprintf(stderr,
                          "Unexpected maximum dimensions %" PRIuHSIZE " x %" PRIuHSIZE " x %" PRIuHSIZE,
                          maxdims3[0], maxdims3[1], maxdims3[2]);
                TEST_ERROR;
            }
        }
        else {
            if (s->expand_2d) {
                if (maxdims2[0] != two_dee_max_dims[0] || maxdims2[1] != two_dee_max_dims[1] ||
                    maxdims2[0] != maxdims2[1]) {
                    HDfprintf(stderr, "Unexpected maximum dimensions %" PRIuHSIZE " x %" PRIuHSIZE,
                              maxdims2[0], maxdims2[1]);
                    TEST_ERROR;
                }
            }
            else if (maxdims2[0] != s->one_dee_max_dims[0] || maxdims2[1] != s->one_dee_max_dims[1] ||
                     dims2[0] != s->chunk_dims[0]) {
                HDfprintf(stderr,
                          "Unexpected maximum dimensions %" PRIuHSIZE " x %" PRIuHSIZE
                          " or columns %" PRIuHSIZE,
                          maxdims2[0], maxdims2[1], dims2[1]);
            }
        }

        s->dataset[which] = dset_id;
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Dclose(dset_id);
        H5Tclose(dtype);
        H5Sclose(filespace);
    }
    H5E_END_TRY;

    return false;
}

static bool
create_dsets(state_t *s)
{
    struct timespec start_time, end_time;
    unsigned int    which;

    /* For checking the time spent in dataset creation.  It's for running the writer alone */
    if (s->do_perf) {
        if (HDclock_gettime(CLOCK_MONOTONIC, &start_time) == -1) {
            HDfprintf(stderr, "HDclock_gettime failed");
            TEST_ERROR;
        }
    }

    /* Create NDATASETS datasets as the reader is doing verification.  So no communication with
     * the reader during the creation of datasets.
     */
    for (which = 0; which < s->ndatasets; which++)
        if (!create_extensible_dset(s, which)) {
            HDfprintf(stderr, "create_extensible_dset failed: number %u\n", which);
            TEST_ERROR;
        }

    /* For checking the time spent in dataset creation.  It's for running the writer alone */
    if (s->do_perf) {
        if (HDclock_gettime(CLOCK_MONOTONIC, &end_time) == -1) {
            HDfprintf(stderr, "HDclock_gettime failed");
            TEST_ERROR;
        }

        HDfprintf(stdout, "Dataset creation time (for running the writer alone) = %lf seconds\n",
                  TIME_PASSED(start_time, end_time));
    }

    return true;

error:
    return false;
}

static uint32_t
matget(const mat_t *mat, unsigned k, unsigned i, unsigned j)
{
    return mat->elt[k * mat->rows * mat->cols + i * mat->cols + j];
}

static bool
matset(mat_t *mat, unsigned k, unsigned i, unsigned j, uint32_t v)
{
    if (k >= mat->depth || i >= mat->rows || j >= mat->cols) {
        HDfprintf(stderr, "index out of boundary\n");
        TEST_ERROR;
    }

    mat->elt[k * mat->rows * mat->cols + i * mat->cols + j] = v;

    return true;

error:
    return false;
}

static mat_t *
newmat(state_t *s)
{
    mat_t *mat;

    /*
     * If partial chunk is enabled, the chunk size along the growing dimension
     * is replaced with the partial size
     */
    if (s->test_3d) {
        if (s->part_chunk) {
            mat = HDmalloc(sizeof(*mat) + (s->part_chunk * s->rows * s->cols - 1) * sizeof(mat->elt[0]));
            mat->depth = s->part_chunk;
        }
        else {
            mat        = HDmalloc(sizeof(*mat) + (s->depth * s->rows * s->cols - 1) * sizeof(mat->elt[0]));
            mat->depth = s->depth;
        }

        mat->rows = s->rows;
        mat->cols = s->cols;
    }
    else {
        if (s->part_chunk && !s->expand_2d) {
            mat        = HDmalloc(sizeof(*mat) + (s->rows * s->part_chunk - 1) * sizeof(mat->elt[0]));
            mat->depth = 1;
            mat->rows  = s->rows;
            mat->cols  = s->part_chunk;
        }
        else {
            mat        = HDmalloc(sizeof(*mat) + (s->rows * s->cols - 1) * sizeof(mat->elt[0]));
            mat->depth = 1;
            mat->rows  = s->rows;
            mat->cols  = s->cols;
        }
    }

    if (mat == NULL) {
        HDfprintf(stderr, "HDmalloc failed\n");
        TEST_ERROR;
    }

    return mat;

error:
    return NULL;
}

/* Write or verify the dataset test pattern in the matrix `mat`.
 * `mat` is a "subview" of the `which`th dataset with origin
 * `(base.row, base.col)`.
 *
 * If `do_set` is true, write the pattern; otherwise, verify.
 *
 * For 2D datasets, the basic test pattern consists of increasing
 * integers written in nested corners of the dataset
 * starting at element (0, 0):
 *
 *  0
 *
 *  0  1
 *  3  2
 *
 *  0  1  4
 *  3  2  5
 *  8  7  6
 *
 *  0  1  4  9
 *  3  2  5 10
 *  8  7  6 11
 * 15 14 13 12
 *
 * In an actual pattern, the dataset number, `which`, is added to each integer.
 *
 * For 3D datasets, the increment of chunks is along the first dimension.
 */
static bool
set_or_verify_matrix(mat_t *mat, unsigned int which, base_t base, bool do_set)
{
    unsigned depth, row, col;
    bool     ret = true;

    /* For 2D datasets, `depth` is one */
    for (depth = 0; depth < mat->depth; depth++) {
        for (row = 0; row < mat->rows; row++) {
            for (col = 0; col < mat->cols; col++) {
                uint32_t v;
                hsize_t  k = base.depth + depth, i = base.row + row, j = base.col + col, u;

                if (j <= i)
                    u = k * 10 + (i + 1) * (i + 1) - 1 - j;
                else
                    u = k * 10 + j * j + i;

                v = (uint32_t)(u + which);

                if (do_set) {
                    if (!matset(mat, depth, row, col, v)) {
                        HDfprintf(stderr, "data initialization failed\n");
                        ret = false;
                        break;
                    }
                }
                else if (matget(mat, depth, row, col) != v) {
                    /* If the data doesn't match, simply return false and
                     * let the caller repeat this step
                     */
                    ret = false;
                    break;
                }
            }
        }
    }

    return ret;
}

static bool
init_matrix(mat_t *mat, unsigned int which, base_t base)
{
    return set_or_verify_matrix(mat, which, base, true);
}

static bool
verify_matrix(mat_t *mat, unsigned int which, base_t base)
{
    return set_or_verify_matrix(mat, which, base, false);
}

static unsigned int
calc_total_steps(state_t *s)
{
    unsigned int total_steps = 0;

    /* Calculate the number of steps depending on if partial chunk is enabled.
     * e.g. the original number of steps is 10 and the size of the chunk along
     * the growing dimension is 6.  The number of elements for this dimension is
     * 60.  When the size of the partial chunk along the growing dimension is 5
     * (treated as the new chunk size), the number of steps becomes 12.
     */
    if (s->test_3d) {
        if (s->part_chunk)
            total_steps = s->nsteps * s->depth / s->part_chunk;
        else
            total_steps = s->nsteps;
    }
    else if (s->expand_2d) {
        total_steps = s->nsteps;
    }
    else {
        if (s->part_chunk)
            total_steps = s->nsteps * s->cols / s->part_chunk;
        else
            total_steps = s->nsteps;
    }

    return total_steps;
}

static bool
verify_chunk(state_t *s, hid_t filespace, mat_t *mat, unsigned which, base_t base)
{
    herr_t status;
    hid_t  dset_id;

    if (which >= s->ndatasets) {
        HDfprintf(stderr, "the dataset order is bigger than the number of datasets");
        TEST_ERROR;
    }

    dset_id = s->dataset[which];

    if (s->test_3d) {
        hsize_t offset3[RANK3] = {base.depth, base.row, base.col};
        hsize_t count3[RANK3]  = {s->depth, s->chunk_dims[0], s->chunk_dims[1]};

        if (s->part_chunk)
            count3[0] = s->part_chunk;

        if (H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset3, NULL, count3, NULL) < 0) {
            HDfprintf(stderr, "H5Sselect_hyperslab failed\n");
            TEST_ERROR;
        }
    }
    else {
        hsize_t offset2[RANK2] = {base.row, base.col};
        hsize_t count2[RANK2];

        if (s->expand_2d) {
            count2[0] = s->chunk_dims[0];
            count2[1] = s->chunk_dims[1];
        }
        else {
            count2[0] = s->chunk_dims[0];

            if (s->part_chunk)
                count2[1] = s->part_chunk;
            else
                count2[1] = s->chunk_dims[1];
        }

        if (H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset2, NULL, count2, NULL) < 0) {
            HDfprintf(stderr, "H5Sselect_hyperslab failed\n");
            TEST_ERROR;
        }
    }

    /* A failure to read the data may indicate the data isn't ready yet.  Instead of displaying the error
     * stack, simply return false and let the caller repeat this step.
     */
    H5E_BEGIN_TRY
    {
        status = H5Dread(dset_id, H5T_NATIVE_UINT32, s->memspace, filespace, H5P_DEFAULT, mat->elt);
    }
    H5E_END_TRY;

    if (status < 0)
        TEST_ERROR;

    return verify_matrix(mat, which, base);

error:
    return false;
}

/* Try to verify a chunk NUM_ATTEMPTS times until the data is correct */
static bool
repeat_verify_chunk(state_t *s, hid_t filespace, mat_t *mat, unsigned which, base_t base)
{
    hid_t    dset_id = s->dataset[which];
    unsigned i;

    /* If the chunk data isn't good after reading it NUM_ATTEMPTS times, report it as a failure */
    for (i = 0; i < NUM_ATTEMPTS; i++) {
        if (verify_chunk(s, filespace, mat, which, base))
            break;
        else {
            decisleep(1);

            /* Refresh the dataset and try it again */
            if (H5Drefresh(dset_id) < 0) {
                HDfprintf(stderr, "H5Drefresh failed\n");
                TEST_ERROR;
            }
        }
    }

    if (i == NUM_ATTEMPTS) {
        HDfprintf(stderr, "chunk verification reached the maximal number of attempts\n");
        TEST_ERROR;
    }

    return true;

error:
    return false;
}

static bool
init_and_write_chunk(state_t *s, hid_t filespace, mat_t *mat, unsigned which, base_t base)
{
    hid_t dset_id;

    dset_id = s->dataset[which];

    if (!init_matrix(mat, which, base)) {
        HDfprintf(stderr, "data initialization failed\n");
        TEST_ERROR;
    }

    if (s->test_3d) {
        hsize_t offset3[RANK3] = {base.depth, base.row, base.col};
        hsize_t count3[RANK3]  = {s->depth, s->chunk_dims[0], s->chunk_dims[1]};

        /* Handling partial chunk */
        if (s->part_chunk)
            count3[0] = s->part_chunk;

        /* The chunk dimensions are L x M x N.  It grows along the first dimension */
        if (H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset3, NULL, count3, NULL) < 0) {
            HDfprintf(stderr, "H5Sselect_hyperslab for 2D dataset failed\n");
            TEST_ERROR;
        }
    }
    else {
        hsize_t offset2[RANK2] = {base.row, base.col};
        hsize_t count2[RANK2];

        if (s->expand_2d) {
            count2[0] = s->chunk_dims[0];
            count2[1] = s->chunk_dims[1];
        }
        else {
            count2[0] = s->chunk_dims[0];

            /* Handling partial chunk */
            if (s->part_chunk)
                count2[1] = s->part_chunk;
            else
                count2[1] = s->chunk_dims[1];
        }

        if (H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset2, NULL, count2, NULL) < 0) {
            HDfprintf(stderr, "H5Sselect_hyperslab for 2D dataset failed\n");
            TEST_ERROR;
        }
    }

    if (H5Dwrite(dset_id, H5T_NATIVE_UINT32, s->memspace, filespace, H5P_DEFAULT, mat->elt) < 0) {
        HDfprintf(stderr, "H5Dwrite failed\n");
        TEST_ERROR;
    }

    return true;

error:
    return false;
}

static bool
verify_dset_attribute(hid_t dset_id, unsigned int which, unsigned int step)
{
    unsigned int read_step;
    hid_t        aid;
    char         name[sizeof("attr-9999999999")];

    esnprintf(name, sizeof(name), "attr-%u", step);

    dbgf(1, "verifying attribute %s on dataset %u equals %u\n", name, which, step);

    if ((aid = H5Aopen(dset_id, name, H5P_DEFAULT)) < 0) {
        HDfprintf(stderr, "H5Aopen failed\n");
        TEST_ERROR;
    }

    if (H5Aread(aid, H5T_NATIVE_UINT, &read_step) < 0) {
        HDfprintf(stderr, "H5Aread failed\n");
        TEST_ERROR;
    }

    if (H5Aclose(aid) < 0) {
        HDfprintf(stderr, "H5Aclose failed\n");
        TEST_ERROR;
    }

    if (read_step != step) {
        HDfprintf(stderr, "expected %u read %u\n", step, read_step);
        TEST_ERROR;
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Aclose(aid);
    }
    H5E_END_TRY;

    return false;
}

static bool
verify_extensible_dset(state_t *s, unsigned int which, mat_t *mat, unsigned finished_step, unsigned last_step)
{
    hid_t        dset_id = H5I_INVALID_HID, filespace = H5I_INVALID_HID;
    hsize_t      size2[RANK2], size3[RANK3];
    base_t       base, last;
    unsigned int nchunks, step, ofs;
    h5_retry_t   retry;
    hbool_t      do_try; /* more tries remain */

    if (which >= s->ndatasets) {
        HDfprintf(stderr, "the dataset order is bigger than the number of datasets");
        TEST_ERROR;
    }

    dset_id = s->dataset[which];

    /* Probably can do the same things in other parts that use NUM_ATTEMPTS */
    for (do_try = H5_retry_init(&retry, NUM_ATTEMPTS, H5_RETRY_DEFAULT_MINIVAL, H5_RETRY_DEFAULT_MAXIVAL);
         do_try; do_try = H5_retry_next(&retry)) {

        if (H5Drefresh(dset_id) < 0) {
            HDfprintf(stderr, "H5Drefresh failed\n");
            TEST_ERROR;
        }

        if ((filespace = H5Dget_space(dset_id)) < 0) {
            HDfprintf(stderr, "H5Dget_space failed\n");
            TEST_ERROR;
        }

        if (s->test_3d) {
            if (H5Sget_simple_extent_dims(filespace, size3, NULL) < 0) {
                HDfprintf(stderr, "H5Sget_simple_extent_dims failed\n");
                TEST_ERROR;
            }

            /* Handling partial chunks */
            if (s->part_chunk)
                nchunks = (unsigned)size3[0] / s->part_chunk;
            else
                nchunks = (unsigned)size3[0] / s->depth;
        }
        else {
            if (H5Sget_simple_extent_dims(filespace, size2, NULL) < 0) {
                HDfprintf(stderr, "H5Sget_simple_extent_dims failed\n");
                TEST_ERROR;
            }

            /* Handling partial chunks */
            if (s->part_chunk)
                nchunks = (unsigned)(size2[1] / s->part_chunk);
            else
                nchunks = (unsigned)(size2[1] / s->chunk_dims[1]);
        }

        /* Make sure the chunks show up on the reader side.  Otherwise sleep a while and try again */
        if (nchunks >= last_step)
            break;
    }
    if (!do_try) {
        HDfprintf(stderr, "chunk verification reached the maximal number of attempts");
        TEST_ERROR;
    }

    for (step = finished_step; step < last_step; step++) {
        dbgf(1, "%s: which %u step %u\n", __func__, which, step);

        if (s->skip_chunk && step % s->skip_chunk == 0)
            continue;

        /* Read data that randomly crosses over chunks. But it should not happen to
         * the last chunk being written
         */
        if (s->cross_chunk_read) {
            if (step == last_step - 1)
                ofs = 0;
            else
                ofs = step % 2;
        }
        else
            ofs = 0;

        if (s->test_3d) {
            if (s->part_chunk) {
                last.depth = s->part_chunk * step + ofs;
            }
            else {
                last.depth = s->depth * step + ofs;
            }

            last.row = 0;
            last.col = 0;
        }
        else {
            last.depth = 0;

            if (s->expand_2d) {
                last.row = s->chunk_dims[0] * step + ofs;
                last.col = s->chunk_dims[1] * step + ofs;
            }
            else {
                last.row = 0;

                if (s->part_chunk) {
                    last.col = s->part_chunk * step + ofs;
                }
                else {
                    last.col = s->chunk_dims[1] * step + ofs;
                }
            }
        }

        if (s->test_3d)
            dbgf(1, "last row %" PRIuHSIZE " col %" PRIuHSIZE " depth %" PRIuHSIZE "\n", last.row, last.col,
                 last.depth);
        else
            dbgf(1, "last row %" PRIuHSIZE " col %" PRIuHSIZE "\n", last.row, last.col);

        if (s->test_3d || !s->expand_2d) {
            if (!repeat_verify_chunk(s, filespace, mat, which, last)) {
                HDfprintf(stderr, "chunk verification failed\n");
                TEST_ERROR;
            }
        }
        else {
            /* Down the right side, intersecting the bottom row. */
            base.col   = last.col;
            base.depth = 0;
            for (base.row = 0; base.row <= last.row; base.row += s->chunk_dims[0]) {
                if (!repeat_verify_chunk(s, filespace, mat, which, base)) {
                    HDfprintf(stderr, "chunk verification failed\n");
                    TEST_ERROR;
                }
            }

            /* Across the bottom, stopping before the last column to
             * avoid re-reading the bottom-right chunk.
             */
            base.row = last.row;
            for (base.col = 0; base.col < last.col; base.col += s->chunk_dims[1]) {
                if (!repeat_verify_chunk(s, filespace, mat, which, base)) {
                    HDfprintf(stderr, "chunk verification failed\n");
                    TEST_ERROR;
                }
            }
        }

        if (s->asteps != 0 && step % s->asteps == 0) {
            if (!verify_dset_attribute(dset_id, which, step)) {
                HDfprintf(stderr, "verify_dset_attribute failed\n");
                TEST_ERROR;
            }
        }
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Sclose(filespace);
    }
    H5E_END_TRY;

    return false;
}

static bool
verify_dsets(state_t *s, np_state_t *np, mat_t *mat)
{
    unsigned        finished_step = 0;
    unsigned        which;
    unsigned        counter     = 0;
    unsigned        total_steps = 0;
    double          passed_time = 0.0, total_time = 0.0, min_time = 1000000.0, max_time = 0.0;
    exchange_info_t last;
    struct timespec end_time;

    total_steps = calc_total_steps(s);

    do {
        /* Receive the notice of the writer finishing creation,
         * including the number of chunks finished and the timestamp
         */
        if (s->use_named_pipe && HDread(np->fd_writer_to_reader, &last, sizeof(last)) < 0) {
            HDfprintf(stderr, "HDread failed\n");
            TEST_ERROR;
        }

        for (which = 0; which < s->ndatasets; which++) {
            /* Verify the chunks starting from the finished one in last round
             * to the ones written in this round
             */
            if (!verify_extensible_dset(s, which, mat, finished_step, last.step)) {
                HDfprintf(stderr, "verify_extensible_dset failed\n");
                TEST_ERROR;
            }

            /* Reset the finished one */
            finished_step = last.step;
        }

        /* Make sure the chunk verification doesn't take longer than the expected time.
         * This time period is from the writer finishing chunks to the reader finishing
         * the validation of the chunks */
        if (s->use_named_pipe && below_speed_limit(&(last.time), &(s->ival))) {
            AT();
            HDfprintf(stderr, "Warning: verify_extensible_dset took too long to finish\n");
        }

        /* For checking the time lapse between the writer's finishing writing a batch of chunks
         * within a tick and the reader's finishing verifying those chunks
         */
        if (s->use_named_pipe && s->do_perf) {
            if (HDclock_gettime(CLOCK_MONOTONIC, &end_time) == -1) {
                HDfprintf(stderr, "HDclock_gettime failed");
                TEST_ERROR;
            }

            counter++;
            passed_time = TIME_PASSED(last.time, end_time);

            total_time += passed_time;

            if (passed_time > max_time)
                max_time = passed_time;

            if (passed_time < min_time)
                min_time = passed_time;
        }
    } while (finished_step < total_steps);

    /* Print out the performance information */
    if (s->use_named_pipe && s->do_perf && counter)
        HDfprintf(stdout, "Dataset verification: mean time = %lf, max time = %lf, min time = %lf\n",
                  total_time / (double)counter, max_time, min_time);

    return true;

error:
    return false;
}

static bool
add_dset_attribute(const state_t *s, hid_t ds, hid_t sid, unsigned int which, unsigned int step)
{
    hid_t aid;
    char  name[sizeof("attr-9999999999")];

    esnprintf(name, sizeof(name), "attr-%u", step);

    dbgf(1, "setting attribute %s on dataset %u to %u\n", name, which, step);

    if ((aid = H5Acreate2(ds, name, s->filetype, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
        HDfprintf(stderr, "H5Acreate2 failed\n");
        TEST_ERROR;
    }

    if (H5Awrite(aid, H5T_NATIVE_UINT, &step) < 0) {
        HDfprintf(stderr, "H5Awrite failed\n");
        TEST_ERROR;
    }

    if (H5Aclose(aid) < 0) {
        HDfprintf(stderr, "H5Aclose failed\n");
        TEST_ERROR;
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Aclose(aid);
    }
    H5E_END_TRY;

    return false;
}

static bool
write_extensible_dset(state_t *s, unsigned int which, unsigned int step, mat_t *mat)
{
    hid_t   dset_id = H5I_INVALID_HID, filespace = H5I_INVALID_HID;
    hsize_t size2[RANK2], size3[RANK3];
    base_t  base, last;
    char    dname[sizeof("/dataset-9999999999")];

    esnprintf(dname, sizeof(dname), "/dataset-%d", which);

    dbgf(1, "%s: which %u step %u\n", __func__, which, step);

    if (which >= s->ndatasets) {
        HDfprintf(stderr, "index is out of range\n");
        TEST_ERROR;
    }

    dset_id = s->dataset[which];

    if (s->asteps != 0 && step % s->asteps == 0) {
        if (!add_dset_attribute(s, dset_id, s->one_by_one_sid, which, step)) {
            HDfprintf(stderr, "add_dset_attribute failed\n");
            TEST_ERROR;
        }
    }

    /* Handling both over extension of the datasets and partial chunks.  Datasets
     * can be extended multiple chunks instead of one chunk at a time.
     * e.g. if the over extension is set to 10 chunks, the datasets are extended
     * 10 chunks along the growing dimension after every 10 chunks are written.
     */
    if (s->test_3d) {
        if (s->part_chunk) {
            size3[0]   = s->over_extend * s->part_chunk * (1 + step / s->over_extend);
            last.depth = s->part_chunk * step;
        }
        else {
            size3[0]   = s->over_extend * s->depth * (1 + step / s->over_extend);
            last.depth = s->depth * step;
        }

        size3[1] = s->chunk_dims[0];
        size3[2] = s->chunk_dims[1];

        last.row = 0;
        last.col = 0;
    }
    else {
        if (s->expand_2d) {
            size2[0] = s->over_extend * s->chunk_dims[0] * (1 + step / s->over_extend);
            size2[1] = s->over_extend * s->chunk_dims[1] * (1 + step / s->over_extend);

            last.row = s->chunk_dims[0] * step;
            last.col = s->chunk_dims[1] * step;
        }
        else {
            size2[0] = s->chunk_dims[0];
            last.row = 0;

            if (s->part_chunk) {
                size2[1] = s->over_extend * s->part_chunk * (1 + step / s->over_extend);
                last.col = s->part_chunk * step;
            }
            else {
                size2[1] = s->over_extend * s->chunk_dims[1] * (1 + step / s->over_extend);
                last.col = s->chunk_dims[1] * step;
            }
        }
        last.depth = 0;
    }

    if (s->test_3d)
        dbgf(1, "new size %" PRIuHSIZE ", %" PRIuHSIZE ", %" PRIuHSIZE "\n", size3[0], size3[1], size3[2]);
    else
        dbgf(1, "new size %" PRIuHSIZE ", %" PRIuHSIZE "\n", size2[0], size2[1]);

    if (s->vds != vds_off) {
        const hsize_t    half_size[RANK2] = {size2[0] / 2, size2[1] / 2};
        sources_t *const srcs             = &s->sources[which];

        if (H5Dset_extent(srcs->ul, half_size) < 0) {
            HDfprintf(stderr, "H5Dset_extent failed\n");
            TEST_ERROR;
        }

        if (H5Dset_extent(srcs->ur, half_size) < 0) {
            HDfprintf(stderr, "H5Dset_extent failed\n");
            TEST_ERROR;
        }

        if (H5Dset_extent(srcs->bl, half_size) < 0) {
            HDfprintf(stderr, "H5Dset_extent failed\n");
            TEST_ERROR;
        }

        if (H5Dset_extent(srcs->br, half_size) < 0) {
            HDfprintf(stderr, "H5Dset_extent failed\n");
            TEST_ERROR;
        }
    }
    else {
        /* Handling over extension.  Making sure the dataset size doesn't exceed the fixed maximal size */
        if (step % s->over_extend == 0) {
            if (s->test_3d) {
                if (size3[0] <= three_dee_max_dims[0] && H5Dset_extent(dset_id, size3) < 0) {
                    HDfprintf(stderr, "H5Dset_extent for 3D dataset failed\n");
                    TEST_ERROR;
                }
            }
            else {
                if ((s->expand_2d && size2[0] <= two_dee_max_dims[0] && size2[0] <= two_dee_max_dims[0]) ||
                    (!s->expand_2d && size2[1] <= two_dee_max_dims[1])) {
                    if (H5Dset_extent(dset_id, size2) < 0) {
                        HDfprintf(stderr, "H5Dset_extent for 2D dataset failed\n");
                        TEST_ERROR;
                    }
                }
            }
        }
    }

    if ((filespace = H5Dget_space(dset_id)) < 0) {
        HDfprintf(stderr, "H5Dget_space failed\n");
        TEST_ERROR;
    }

    if (s->test_3d || !s->expand_2d) {
        if (!init_and_write_chunk(s, filespace, mat, which, last)) {
            HDfprintf(stderr, "init_and_write_chunk failed\n");
            TEST_ERROR;
        }
    }
    else if (s->expand_2d) {
        base.col   = last.col;
        base.depth = 0;
        for (base.row = 0; base.row <= last.row; base.row += s->chunk_dims[0]) {
            dbgf(1, "writing chunk %" PRIuHSIZE ", %" PRIuHSIZE "\n", base.row, base.col);
            if (!init_and_write_chunk(s, filespace, mat, which, base)) {
                HDfprintf(stderr, "init_and_write_chunk failed\n");
                TEST_ERROR;
            }
        }

        base.row = last.row;
        for (base.col = 0; base.col < last.col; base.col += s->chunk_dims[1]) {
            dbgf(1, "writing chunk %" PRIuHSIZE ", %" PRIuHSIZE "\n", base.row, base.col);
            if (!init_and_write_chunk(s, filespace, mat, which, base)) {
                HDfprintf(stderr, "init_and_write_chunk failed\n");
                TEST_ERROR;
            }
        }
    }

    if (H5Sclose(filespace) < 0) {
        HDfprintf(stderr, "H5Sclose failed\n");
        TEST_ERROR;
    }

    return true;

error:
    H5E_BEGIN_TRY
    {
        H5Sclose(filespace);
    }
    H5E_END_TRY;

    return false;
}

static bool
write_dsets(state_t *s, np_state_t *np, mat_t *mat)
{
    unsigned           last_step, step, total_steps, which;
    unsigned long long old_tick_num;
    H5F_t *            f = NULL;
    struct timespec    start_time, end_time;

    if (NULL == (f = (H5F_t *)H5VL_object(s->file[0]))) {
        HDfprintf(stderr, "H5VL_object failed\n");
        TEST_ERROR;
    }

    /* For checking the time spent in writing data.  It's for running the writer alone */
    if (s->do_perf) {
        if (HDclock_gettime(CLOCK_MONOTONIC, &start_time) == -1) {
            HDfprintf(stderr, "HDclock_gettime failed");
            TEST_ERROR;
        }
    }

    old_tick_num = f->shared->tick_num;

    /* Write as many as chunks within the same tick number before notifying
     * the reader to verify them.  Take account of partial chunk write
     * here by multiplying the dividing factor for partial chunk. Treat each
     * partial chunk as if it's a chunk.
     */
    total_steps = calc_total_steps(s);

    for (step = 0; step < total_steps; step++) {
        /* Write as many as chunks before the tick number changes */
        if (f->shared->tick_num == old_tick_num) {
            if (!s->skip_chunk || (s->skip_chunk && step % s->skip_chunk != 0)) {
                for (which = 0; which < s->ndatasets; which++) {
                    dbgf(2, "step %d which %d\n", step, which);
                    if (!write_extensible_dset(s, which, step, mat)) {
                        HDfprintf(stderr, "write_extensible_dset failed\n");
                        TEST_ERROR;
                    }
                }
            }
        }

        /* Notify the reader to start verification by
         * sending the timestamp and the number of chunks written
         */
        if (f->shared->tick_num > old_tick_num || step == (total_steps - 1)) {
            last_step = step + 1;
            if (s->use_named_pipe && notify_reader(np, last_step) < 0) {
                HDfprintf(stderr, "notify_reader failed\n");
                TEST_ERROR;
            }

            old_tick_num = f->shared->tick_num;
        }
    }

    /* For checking the time spent in writing data.  It's for running the writer alone */
    if (s->do_perf) {
        double throughput;
        double time_passed;

        if (HDclock_gettime(CLOCK_MONOTONIC, &end_time) == -1) {
            HDfprintf(stderr, "HDclock_gettime failed");
            TEST_ERROR;
        }

        time_passed = TIME_PASSED(start_time, end_time);

        /* Calculate the write speed */
        if (s->test_3d)
            throughput =
                ((double)(sizeof(unsigned int) * s->depth * s->rows * s->cols * s->nsteps * s->ndatasets)) /
                time_passed;
        else
            throughput =
                ((double)(sizeof(unsigned int) * s->rows * s->cols * s->nsteps * s->ndatasets)) / time_passed;

        /* Print out the performance information */
        HDfprintf(stdout,
                  "Dataset write time (for running the writer alone) = %lf seconds, write speed = %.2lf "
                  "bytes/second\n",
                  time_passed, throughput);
    }

    return true;

error:
    return false;
}

int
main(int argc, char **argv)
{
    mat_t *                mat    = NULL;
    hid_t                  fcpl   = H5I_INVALID_HID;
    state_t *              s      = NULL;
    np_state_t *           np     = NULL;
    H5F_vfd_swmr_config_t *config = NULL;

    if (NULL == (s = HDcalloc(1, sizeof(state_t))))
        TEST_ERROR;
    if (NULL == (np = HDcalloc(1, sizeof(np_state_t))))
        TEST_ERROR;
    if (NULL == (config = HDcalloc(1, sizeof(H5F_vfd_swmr_config_t))))
        TEST_ERROR;

    if (!state_init(s, argc, argv)) {
        HDfprintf(stderr, "state_init failed\n");
        TEST_ERROR;
    }

    if ((mat = newmat(s)) == NULL) {
        HDfprintf(stderr, "could not allocate matrix\n");
        TEST_ERROR;
    }

    /* Set fs_strategy (file space strategy) and fs_page_size (file space page size) */
    if ((fcpl = vfd_swmr_create_fcpl(H5F_FSPACE_STRATEGY_PAGE, s->fsp_size)) < 0) {
        HDfprintf(stderr, "vfd_swmr_create_fcpl failed\n");
        TEST_ERROR;
    }

    for (size_t i = 0; i < NELMTS(s->file); i++) {
        hid_t               fapl;
        H5AC_cache_config_t mdc_config;

        HDmemset(config, 0, sizeof(H5F_vfd_swmr_config_t));

        if (s->vds != vds_multi && i > 0) {
            s->file[i] = s->file[0];
            continue;
        }

        /* config, tick_len, max_lag, presume_posix_semantics, writer,
         * maintain_metadata_file, generate_updater_files, flush_raw_data, md_pages_reserved,
         * md_file_path, md_file_name, updater_file_path */

#ifdef H5_HAVE_AUX_PROCESS

        /* If using the auxiliary process, the writer creates the updater files.
         * The reader uses the metadata file generated by the auxiliary process. */
        if (s->writer) {
            init_vfd_swmr_config(config, s->tick_len, s->max_lag, FALSE, s->writer, FALSE, TRUE,
                                 s->flush_raw_data, 128, "./", "bigset-shadow-%zu", "bigset_updater", i);
        }
        else {
            init_vfd_swmr_config(config, s->tick_len, s->max_lag, FALSE, s->writer, TRUE, FALSE,
                                 s->flush_raw_data, 128, "./", "mdfile", NULL);
        }
#else

        if (s->vds == vds_multi || s->vds == vds_single) {
            init_vfd_swmr_config(config, s->tick_len, s->max_lag, TRUE, s->writer, TRUE, FALSE,
                                 s->flush_raw_data, 128, "", "%s", NULL, "");
        }
        else {
            init_vfd_swmr_config(config, s->tick_len, s->max_lag, FALSE, s->writer, TRUE, FALSE,
                                 s->flush_raw_data, 128, "./", "bigset-shadow-%zu", NULL, i);
        }

#endif

        /* use_latest_format, use_vfd_swmr, only_meta_page, page_buf_size, config */
        if ((fapl = vfd_swmr_create_fapl(true, s->use_vfd_swmr, true, s->page_buf_size, config)) < 0) {
            HDfprintf(stderr, "vfd_swmr_create_fapl failed");
            TEST_ERROR;
        }

        /* Set the initial size for the metadata cache between 1 and 32 in megabytes.
         * Zero means using the default value, which is no-op.
         */
        if (s->mdc_init_size) {
            mdc_config.version = H5AC__CURR_CACHE_CONFIG_VERSION;

            if (H5Pget_mdc_config(fapl, &mdc_config) < 0) {
                HDfprintf(stderr, "H5Pget_mdc_config failed");
                TEST_ERROR;
            }

            /* Convert the value to megabytes */
            mdc_config.set_initial_size = TRUE;
            mdc_config.initial_size     = s->mdc_init_size * 1024 * 1024;

            if (H5Pset_mdc_config(fapl, &mdc_config) < 0) {
                HDfprintf(stderr, "H5Pset_mdc_config failed");
                TEST_ERROR;
            }
        }

        /* This part is for debugging only */
#ifdef TMP
        {
            H5F_generate_md_ck_cb_t cb_info;

            /* Set up callback to generate checksums for updater's metadata files */
            cb_info.func = md_ck_cb;

            /* Activate private property to generate checksums for updater's metadata file */
            H5Pset(fapl, H5F_ACS_GENERATE_MD_CK_CB_NAME, &cb_info);
        }
#endif

        s->file[i] = s->writer ? H5Fcreate(s->filename[i], H5F_ACC_TRUNC, fcpl, fapl)
                               : H5Fopen(s->filename[i], H5F_ACC_RDONLY, fapl);

        if (s->file[i] == H5I_INVALID_HID) {
            HDfprintf(stderr, s->writer ? "H5Fcreate failed" : "H5Fopen failed");
            TEST_ERROR;
        }

        if (H5Pclose(fapl) < 0) {
            HDfprintf(stderr, "H5Pclose failed\n");
            TEST_ERROR;
        }
    }

    /* Initiailze named pipes */
    if (s->use_named_pipe && !np_init(np, s->writer)) {
        HDfprintf(stderr, "np_init() failed\n");
        TEST_ERROR;
    }

    if (s->writer) {
        /* Writer tells reader to start */
        np->notify = 1;
        if (s->use_named_pipe && HDwrite(np->fd_writer_to_reader, &(np->notify), sizeof(int)) < 0) {
            HDfprintf(stderr, "HDwrite failed\n");
            TEST_ERROR;
        }

        /* Creates multiple datasets */
        if (!create_dsets(s)) {
            HDfprintf(stderr, "create_dsets failed");
            TEST_ERROR;
        }

        /* Call H5Fvfd_swmr_end_tick to end the tick.  No communication with the reader in this step */
        if (s->use_vfd_swmr && s->use_named_pipe) {
            if (s->vds != vds_multi) {
                if (H5Fvfd_swmr_end_tick(s->file[0]) < 0) {
                    HDfprintf(stderr, "H5Fvfd_swmr_end_tick failed\n");
                    TEST_ERROR;
                }
            }
            else {
                for (unsigned long j = 0; j < NELMTS(s->file); j++)
                    if (H5Fvfd_swmr_end_tick(s->file[j]) < 0) {
                        HDfprintf(stderr, "H5Fvfd_swmr_end_tick failed\n");
                        TEST_ERROR;
                    }
            }
        }

        /* Notify the reader of finishing dataset creation by sending the timestamp
         * and wait for the reader to finish validation before proceeding */
        np->verify = 2;
        if (s->use_named_pipe && notify_and_wait_for_reader(s, np) < 0) {
            HDfprintf(stderr, "notify_and_wait_for_reader failed\n");
            TEST_ERROR;
        }

        /* Enable the Legacy SWMR writing mode if specified */
        if (s->use_legacy_swmr && H5Fstart_swmr_write(s->file[0]) < 0) {
            HDfprintf(stderr, "failed to start the Legacy SWMR writing mode\n");
            TEST_ERROR;
        }

        /* Start to write chunks.  The writer writes as many chunks as possible within a tick, then
         * notify the reader.  But it doesn't receive back the reader's notice. */
        if (!write_dsets(s, np, mat)) {
            HDfprintf(stderr, "write_dsets failed");
            TEST_ERROR;
        }
    }
    else {
        /* Wait for the writer's notice before starting the validation of dataset creation */
        np->verify = 1;
        if (s->use_named_pipe && reader_verify(np, np->verify) < 0) {
            HDfprintf(stderr, "reader_verify failed\n");
            TEST_ERROR;
        }

        /* Open all the datasets as the writer is creating them.  No communication with
         * the writer during this step.
         */
        if (!open_extensible_dset(s)) {
            HDfprintf(stderr, "open_extensible_dset failed\n");
            TEST_ERROR;
        }

        /* Receive the notice of the writer finishing dataset creation (timestamp)
         * Make sure the dataset creation doesn't take longer than the expected time.
         * This time period is from the writer finishing dataset creation to the reader finishing
         * the validation of dataset creation */
        np->notify = 2;
        if (s->use_named_pipe && reader_check_time_and_notify_writer(np, s) < 0) {
            HDfprintf(stderr, "reader_check_time_and_notify_writer failed\n");
            TEST_ERROR;
        }

        /* Once the reader starts to verify the datasets, it doesn't notify the writer any info.
         * Both the reader and writer finish by themselves.
         */
        if (!verify_dsets(s, np, mat)) {
            HDfprintf(stderr, "verify_dsets failed\n");
            TEST_ERROR;
        }
    }

    for (unsigned which = 0; which < s->ndatasets; which++)
        if (!close_extensible_dset(s, which)) {
            HDfprintf(stderr, "close_extensible_dset failed\n");
            TEST_ERROR;
        }

    if (H5Pclose(fcpl) < 0) {
        HDfprintf(stderr, "H5Pclose failed\n");
        TEST_ERROR;
    }

    if (s->use_named_pipe && !np_close(np, s->writer)) {
        HDfprintf(stderr, "np_close() failed\n");
        TEST_ERROR;
    }

    if (!state_destroy(s)) {
        HDfprintf(stderr, "state_destroy failed\n");
        TEST_ERROR;
    }

    HDfree(mat);
    HDfree(s);
    HDfree(np);
    HDfree(config);

    return EXIT_SUCCESS;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fcpl);

        for (size_t i = 0; i < NELMTS(s->file); i++)
            H5Fclose(s->file[i]);
    }
    H5E_END_TRY;

    if (s->use_named_pipe && np->fd_writer_to_reader >= 0)
        HDclose(np->fd_writer_to_reader);

    if (s->use_named_pipe && np->fd_reader_to_writer >= 0)
        HDclose(np->fd_reader_to_writer);

    if (s->use_named_pipe && !s->writer) {
        HDremove(np->fifo_writer_to_reader);
        HDremove(np->fifo_reader_to_writer);
    }

    HDfree(mat);
    HDfree(s);
    HDfree(np);
    HDfree(config);

    return EXIT_FAILURE;
}
#else /* H5_HAVE_WIN32_API */

int
main(void)
{
    HDfprintf(stderr, "Non-POSIX platform. Skipping.\n");
    return EXIT_SUCCESS;
}

#endif /* H5_HAVE_WIN32_API */