File: synthetictest.cpp

package info (click to toggle)
libhmsbeagle 4.0.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,436 kB
  • sloc: xml: 133,356; cpp: 36,477; ansic: 5,842; java: 2,400; python: 643; sh: 338; makefile: 50
file content (3374 lines) | stat: -rw-r--r-- 132,476 bytes parent folder | download | duplicates (2)
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
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
/*
 *  synthetictest.cpp
 *  @author Daniel Ayres
 *  Based on tinyTest.cpp by Andrew Rambaut, genomictest.cpp by Aaron Darling.
 *  PLL comparison based on test files by Diego Darriba and Tomas Flouri
 */
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdarg>
#include <thread>
#include <future>
#include <queue>
#include <condition_variable>
#include <mutex>
#include <functional>

#ifdef _WIN32
    #include <winsock.h>
    #include <string>
#else
    #include <sys/time.h>
#endif

#include "libhmsbeagle/beagle.h"
#include "linalg.h"

#ifdef HAVE_NCL
    #include "ncl/nxsmultiformat.h"
    #include <regex>
#endif // HAVE_NCL

#ifdef HAVE_PLL
    #include "libpll/pll.h"
#endif // HAVE_PLL


#define MAX_DIFF    0.01        //max discrepancy in scoring between reps
#define GT_RAND_MAX 0x7fffffff

#ifdef _WIN32
    //From January 1, 1601 (UTC). to January 1,1970
    #define FACTOR 0x19db1ded53e8000 

    int gettimeofday(struct timeval *tp,void * tz) {
        FILETIME f;
        ULARGE_INTEGER ifreq;
        LONGLONG res; 
        GetSystemTimeAsFileTime(&f);
        ifreq.HighPart = f.dwHighDateTime;
        ifreq.LowPart = f.dwLowDateTime;

        res = ifreq.QuadPart - FACTOR;
        tp->tv_sec = (long)((LONGLONG)res/10000000);
        tp->tv_usec =(long)(((LONGLONG)res%10000000)/10); // Micro Seconds

        return 0;
    }
#endif

double cpuTimeSetPartitions, cpuTimeUpdateTransitionMatrices, cpuTimeUpdatePartials, cpuTimeAccumulateScaleFactors, cpuTimeCalculateRootLogLikelihoods, cpuTimeTotal;

bool useStdlibRand;

static unsigned int rand_state = 1;

int gt_rand_r(unsigned int *seed)
{
    *seed = *seed * 1103515245 + 12345;
    return (*seed % ((unsigned int)GT_RAND_MAX + 1));
}

int gt_rand(void)
{
    if (!useStdlibRand) {
        return (gt_rand_r(&rand_state));
    } else {
        return rand();
    }
}

void gt_srand(unsigned int seed)
{
    if (!useStdlibRand) {
        rand_state = seed;
    } else {
        srand(seed);
    }
}

void abort(std::string msg) {
    std::cerr << msg << "\nAborting..." << std::endl;
    std::exit(1);
}

void abortf(const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
    std::cerr << "\nAborting..." << std::endl;        \
    std::exit(1);
}


double* getRandomTipPartials( int nsites, int stateCount )
{
    double *partials = (double*) calloc(sizeof(double), nsites * stateCount); // 'malloc' was a bug
    for( int i=0; i<nsites*stateCount; i+=stateCount )
    {
        int s = gt_rand()%stateCount;
        // printf("%d ", s);
        partials[i+s]=1.0;
    }
    return partials;
}

int* getRandomTipStates( int nsites, int stateCount )
{
    int *states = (int*) calloc(sizeof(int), nsites); 
    for( int i=0; i<nsites; i++ )
    {
        int s = gt_rand()%stateCount;
        states[i]=s;
    }
    return states;
}

struct threadData
{
    std::thread t; // The thread object
    std::queue<std::packaged_task<void()>> jobs; // The job queue
    std::condition_variable cv; // The condition variable to wait for threads
    std::mutex m; // Mutex used for avoiding data races
    bool stop = false; // When set, this flag tells the thread that it should exit
};

struct node
{
    int data;
    double edge;
    struct node* left;
    struct node* right;
    struct node* parent;
};


node* createNewNode(int data)
{
    node* temp = new node;
    temp->data = data;
    temp->edge = 0.0;
    temp->left = NULL;
    temp->right = NULL;
    temp->parent = NULL;
 
    return (temp);
}

#ifdef HAVE_NCL

typedef std::vector<double>             pattern_counts_t;
typedef std::vector<int>                pattern_t;
typedef std::map< pattern_t, unsigned > pattern_map_t;
typedef std::vector< pattern_t >        data_matrix_t;

pattern_map_t                           _pattern_map;       // used as workspace
data_matrix_t                           _data_matrix;
pattern_counts_t                        _pattern_counts;

NxsTaxaBlock* taxaBlock;
NxsCharactersBlock* charBlock;

// code adapted from NCL documentation 
//  (http://phylo.bio.ku.edu/ncldocs/v2.1/funcdocs/index.html)
// and Phylogenetic Software Development Tutorial (version 2)
//  (Paul O. Lewis Laboratory, https://phylogeny.uconn.edu/tutorial-v2)

void ncl_readAlignmentDNA(char* filename, int* ntaxa, int* nsites, bool compress)
{
    MultiFormatReader nexusReader(-1, NxsReader::IGNORE_WARNINGS);
    nexusReader.ReadFilepath(filename, MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT);

    taxaBlock = nexusReader.GetTaxaBlock(0);
    charBlock = nexusReader.GetCharactersBlock(taxaBlock, 0);

    *ntaxa = taxaBlock->GetNTax();
    *nsites = charBlock->GetNCharTotal();

    std::cout << "\nReading DNA alignment from file " << filename;
    std::cout << ", with " << *ntaxa << " taxa and " << *nsites << " sites";

    unsigned ntax = *ntaxa;

    _data_matrix.resize(ntax);

    for (unsigned t = 0; t < ntax; ++t) {
        const NxsDiscreteStateRow & row = charBlock->GetDiscreteMatrixRow(t);
        unsigned seqlen = (unsigned)row.size();
        _data_matrix[t].resize(seqlen);
        unsigned k = 0;
        for (auto state_code : row) {
            if (state_code < 0 || state_code > 3) {
                _data_matrix[t][k++] = 4; 
            } else {
                _data_matrix[t][k++] = state_code;
            }
        }
    }

    if (compress) {
        // create map with keys equal to patterns and values equal to site counts
        _pattern_map.clear();

        std::vector<int> pattern;
        unsigned numtaxa = (unsigned)_data_matrix.size();
        unsigned seqlen = (unsigned)_data_matrix[0].size();
        for (unsigned i = 0; i < seqlen; ++i)
            {
            // Create vector representing pattern at site i
            pattern.clear();
            for (unsigned j = 0; j < numtaxa; ++j)
                {
                pattern.push_back(_data_matrix[j][i]);
                }

            // Add this pattern to pattern_counts
            // If pattern is not already in pattern_map, insert it and set value to 1.
            // If it does exist, increment its current value.
            // (see item 24, p. 110, in Meyers' Efficient STL for more info on the technique used here)
            pattern_map_t::iterator lowb = _pattern_map.lower_bound(pattern);
            if (lowb != _pattern_map.end() && !(_pattern_map.key_comp()(pattern, lowb->first)))
                {
                // this pattern has already been seen
                lowb->second += 1;
                }
            else
                {
                // this pattern has not yet been seen
                _pattern_map.insert(lowb, pattern_map_t::value_type(pattern, 1));
                }
            }
            
        // resize _pattern_counts
        unsigned npatterns = (unsigned)_pattern_map.size();
        _pattern_counts.resize(npatterns);
        
        // resize _data_matrix so that we can use operator[] to assign values
        _data_matrix.resize(numtaxa);
        for (auto & row : _data_matrix)
            {
            row.resize(npatterns);
            }

        unsigned j = 0;
        for (auto & pc : _pattern_map)
            {
            _pattern_counts[j] = pc.second;

            unsigned i = 0;
            for (auto sc : pc.first)
                {
                _data_matrix[i][j] = sc;
                ++i;
                }

            ++j;
            }

        // Everything has been transferred to _data_matrix and _pattern_counts, so can now free this memory
        _pattern_map.clear();

        std::cout << " (" << npatterns << " unique patterns)";

        *nsites = npatterns;
    }
}

int* ncl_getAlignmentTipStates( int nsites, int taxa)
{
    int *states = (int*) calloc(sizeof(int), nsites); 
    for( int i=0; i<nsites; i++ )
    {
        states[i]= _data_matrix[taxa][i];
    }
    return states;
}


void ncl_generateTreeFromNewick(char* filename, int ntaxa, std::vector <node*> &nodes, node* root)
{
    bool rooted = true;

    // readTreefile
    MultiFormatReader nexusReader(-1, NxsReader::IGNORE_WARNINGS);
    try {
        nexusReader.ReadFilepath(filename, MultiFormatReader::RELAXED_PHYLIP_TREE_FORMAT);
        }
    catch(...)
        {
        nexusReader.DeleteBlocksFromFactories();
        abort("Error reading Newick file");
        }
    const NxsTreesBlock * treesBlock = nexusReader.GetTreesBlock(nexusReader.GetTaxaBlock(0), 0);
    const NxsFullTreeDescription & d = treesBlock->GetFullTreeDescription(0);
    // store the newick tree description
    std::string raw_newick = d.GetNewick();
    nexusReader.DeleteBlocksFromFactories();

    // stripOutNexusComments
    std::regex commentexpr("\\[.*?\\]");
    std::string newick = std::regex_replace(raw_newick, commentexpr, std::string(""));

    // countNewickLeaves
    std::regex taxonexpr("[(,]\\s*(\\d+|\\S+?|['].+?['])\\s*(?=[,):])");
    std::sregex_iterator m1(newick.begin(), newick.end(), taxonexpr);
    std::sregex_iterator m2;
    int ntaxa_newick = (unsigned)std::distance(m1, m2);

    if (ntaxa_newick != ntaxa) {
        abortf("Wrong number of taxa in Newick file (%d != %d)", ntaxa_newick, ntaxa);
    }

    // CHECK if 0 should be 1
    unsigned max_nodes = 2*ntaxa - (rooted ? 0 : 2);

    // std::vector <node*> nodes;

    std::set<unsigned> used; // used to ensure that two tips do not have the same number
    unsigned curr_leaf = 0;
    node* first_tip = NULL;

    unsigned num_edge_lengths = 0;
    unsigned curr_node_index = 0;
    unsigned curr_internal_node = ntaxa;

    // Root node
    node* nd = root;
    root->data = curr_node_index;
    nodes.push_back(root);

    // if (rooted)
    //     {
    //     curr_node_index++;
    //     nd = createNewNode(curr_node_index);
    //     nodes.push_back(nd);    
    //     nd->parent = root;
    //     nd->parent->left = nd;
    //     }

    // Some flags to keep track of what we did last
    enum {
        Prev_Tok_LParen     = 0x01, // previous token was a left parenthesis ('(') 
        Prev_Tok_RParen     = 0x02, // previous token was a right parenthesis (')') 
        Prev_Tok_Colon      = 0x04, // previous token was a colon (':') 
        Prev_Tok_Comma      = 0x08, // previous token was a comma (',') 
        Prev_Tok_Name       = 0x10, // previous token was a node name (e.g. '2', 'P._articulata')
        Prev_Tok_EdgeLen    = 0x20  // previous token was an edge length (e.g. '0.1', '1.7e-3') 
        };
    unsigned previous = Prev_Tok_LParen;


    // Some useful flag combinations 
    unsigned LParen_Valid = (Prev_Tok_LParen | Prev_Tok_Comma);
    unsigned RParen_Valid = (Prev_Tok_RParen | Prev_Tok_Name | Prev_Tok_EdgeLen);
    unsigned Comma_Valid  = (Prev_Tok_RParen | Prev_Tok_Name | Prev_Tok_EdgeLen);
    unsigned Colon_Valid  = (Prev_Tok_RParen | Prev_Tok_Name);
    unsigned Name_Valid   = (Prev_Tok_RParen | Prev_Tok_LParen | Prev_Tok_Comma);

    // Set to true while reading an edge length
    bool inside_edge_length = false;
    std::string edge_length_str;
    unsigned edge_length_position = 0;

    // Set to true while reading a node name surrounded by (single) quotes
    bool inside_quoted_name = false;

    // Set to true while reading a node name not surrounded by (single) quotes
    bool inside_unquoted_name = false;

    // Set to start of each node name and used in case of error
    unsigned node_name_position = 0;

    // loop through the characters in newick, building up tree as we go
    unsigned position_in_string = 0;

    for (auto ch : newick)
        {
        position_in_string++;

        if (inside_quoted_name)
            {
            if (ch == '\'')
                {
                inside_quoted_name = false;
                node_name_position = 0;
                if (!nd->left)
                    {
                    // extractNodeNumberFromName(nd, used);
                    if (nd->data != curr_leaf)
                        {
                        node* tmp_node;
                        for (auto n : nodes)
                            {
                            if (n->data == curr_leaf)
                                {
                                tmp_node = n;
                                break;
                                }
                            }
                        tmp_node->data = nd->data;
                        nd->data = curr_leaf; 
                        }
                    curr_leaf++;
                    if (!first_tip)
                        first_tip = nd;
                    }

                previous = Prev_Tok_Name;
                }
            // else if (iswspace(ch))
            //     nd->_name += ' ';
            // else
            //     nd->_name += ch;

            continue;
            }
            else if (inside_unquoted_name)
                {
                if (ch == '(')
                    abortf("Unexpected left parenthesis inside node name at position %d in tree description", node_name_position);

                if (iswspace(ch) || ch == ':' || ch == ',' || ch == ')')
                    {
                    inside_unquoted_name = false;

                    // Expect node name only after a left paren (child's name), a comma (sib's name) or a right paren (parent's name)
                    if (!(previous & Name_Valid))
                        abortf("Unexpected node name at position %d in tree description", node_name_position);

                    if (!nd->left)
                        {
                        // extractNodeNumberFromName(nd, used);
                        if (nd->data != curr_leaf)
                            {
                            node* tmp_node;
                            for (auto n : nodes)
                                {
                                if (n->data == curr_leaf)
                                    {
                                    tmp_node = n;
                                    break;
                                    }
                                }
                            tmp_node->data = nd->data;
                            nd->data = curr_leaf; 
                            }
                        curr_leaf++;
                        if (!first_tip)
                            first_tip = nd;
                        }

                    previous = Prev_Tok_Name;
                    }
                else
                    {
                    // nd->_name += ch;
                    continue;
                    }
                }
        else if (inside_edge_length)
            {
            if (ch == ',' || ch == ')' || iswspace(ch))
                {
                inside_edge_length = false;
                edge_length_position = 0;
                // extractEdgeLen(nd, edge_length_str);
                double d = 0.0;
                try
                    {
                    d = std::stof(edge_length_str);
                    }
                catch(std::invalid_argument &)
                    {
                    // edge_length_string could not be converted to a double value
                    abortf("%s is not interpretable as an edge length", edge_length_str.c_str());
                    }
                // conversion succeeded
                nd->edge = (d < 0.0 ? 0.0 : d);
                ++num_edge_lengths;
                previous = Prev_Tok_EdgeLen;
                }
            else
                {
                bool valid = (ch =='e' || ch == 'E' || ch =='.' || ch == '-' || ch == '+' || isdigit(ch));
                if (!valid)
                    abortf("Invalid branch length character (%c) at position %d in tree description", ch, position_in_string);

                edge_length_str += ch;
                continue;
                }
            }

        if (iswspace(ch))
            continue;

        switch(ch)
            {
            case ';':
                break;

            case ')':
                // If nd is bottommost node, expecting left paren or semicolon, but not right paren
                if (!nd->parent)
                    abortf("Too many right parentheses at position %d in tree description", position_in_string);

                // Expect right paren only after an edge length, a node name, or another right paren
                if (!(previous & RParen_Valid))
                    abortf("Unexpected right parenthesisat position %d in tree description", position_in_string);

                // Go down a level
                nd = nd->parent;
                if (!nd->right)
                    abortf("Internal node has only one child at position %d in tree description", position_in_string);
                previous = Prev_Tok_RParen;
                break;

            case ':':
                // Expect colon only after a node name or another right paren
                if (!(previous & Colon_Valid))
                    abortf("Unexpected colon at position %d in tree description", position_in_string);
                previous = Prev_Tok_Colon;
                break;

            case ',':
                // Expect comma only after an edge length, a node name, or a right paren
                if (!nd->parent || !(previous & Comma_Valid))
                    abortf("Unexpected comma at position %d in tree description", position_in_string);

                // Check for polytomies
                {
                bool nd_can_have_sibling = true;
                if (!nd->parent)
                    {
                    // trying to give root node a sibling
                    nd_can_have_sibling = false;
                    }

                if (nd != nd->parent->left)
                    {
                    if (nd->parent->parent)
                        {
                        // trying to give a sibling to a sibling of nd, and nd's parent is not the root
                        nd_can_have_sibling = false;
                        }
                    else
                        {
                        if (rooted)
                            {
                            // root node has exactly 2 children in rooted trees
                            nd_can_have_sibling = false;
                            }
                        else if (nd != nd->parent->right)
                            {
                            // trying to give root node more than 3 children
                            nd_can_have_sibling = false;
                            }
                        }
                    }

                    if (!nd_can_have_sibling)
                        abortf("Polytomy found in the following tree description but polytomies prohibited:\n%s", newick.c_str());
                }

                // Create the sibling
                curr_node_index++;
                if (curr_node_index == max_nodes)
                    abortf("Wrong number of nodes specified by tree description (%d nodes allocated for %d leaves)", max_nodes, ntaxa);
                nd->parent->right = createNewNode(curr_node_index);
                nd->parent->right->parent = nd->parent;
                nd = nd->parent->right;
                nodes.push_back(nd);
                previous = Prev_Tok_Comma;
                break;

            case '(':
                // Expect left paren only after a comma or another left paren
                if (!(previous & LParen_Valid))
                    abortf("Not expecting left parenthesis at position %d in tree description", position_in_string);

                // Create new node above and to the left of the current node
                assert(!nd->left);
                curr_node_index++;
                if (curr_node_index == max_nodes)
                    abortf("malformed tree description (more than %d nodes specified)", max_nodes);
                nd->left = createNewNode(curr_node_index);
                nd->left->parent = nd;
                nd = nd->left;
                nodes.push_back(nd);
                previous = Prev_Tok_LParen;
                break;

            case '\'':
                // Encountered an apostrophe, which always indicates the start of a
                // node name (but note that node names do not have to be quoted)

                // Expect node name only after a left paren (child's name), a comma (sib's name)
                // or a right paren (parent's name)
                if (!(previous & Name_Valid))
                    abortf("Not expecting node name at position %d in tree description", position_in_string);

                // Get the rest of the name
                // nd->_name.clear();

                inside_quoted_name = true;
                node_name_position = position_in_string;

                break;

            default:
                // Get here if ch is not one of ();:,'

                // Expecting either an edge length or an unquoted node name
                if (previous == Prev_Tok_Colon)
                    {
                    // Edge length expected (e.g. "235", "0.12345", "1.7e-3")
                    inside_edge_length = true;
                    edge_length_position = position_in_string;
                    edge_length_str = ch;
                    }
                else
                    {
                    // Get the node name
                    // nd->_name = ch;

                    inside_unquoted_name = true;
                    node_name_position = position_in_string;
                    }

            }   // end of switch statement
        }   // loop over characters in newick string    


        if (inside_unquoted_name)
            abortf("Tree description ended before end of node name starting at position %d was found", node_name_position);
        if (inside_edge_length)
            abortf("Tree description ended before end of edge length starting at position %d was found", edge_length_position);
        if (inside_quoted_name)
            abortf("Expecting single quote to mark the end of node name at position %d in tree description", node_name_position);

        // root has to be highest index
        if (root->data != max_nodes-2)
            {
            node* tmp_node;
            for (auto n : nodes)
                {
                if (n->data == max_nodes-2)
                    {
                    tmp_node = n;
                    break;
                    }
                }
            tmp_node->data = nd->data;
            nd->data = max_nodes-2; 
            }

        // if (!rooted)
        //     {
        //     rerootAt(0);
        //     }
}

#endif // HAVE_NCL

#ifdef HAVE_PLL

char* pll_getNucleotideCharStates( int* states, int nsites )
{
    char *charStates = (char*) malloc(sizeof(char) * nsites); 
    for( int i=0; i<nsites; i++ )
    {
        switch(states[i]) {
            case 0:
                charStates[i] = 'A';
                break;
            case 1:
                charStates[i] = 'C';
                break;
            case 2:
                charStates[i] = 'G';
                break;
            case 3:
                charStates[i] = 'T';
                break;
            default:
                charStates[i] = '-';
        }
    }
    return charStates;
}


void pll_printTiming(double timingValue,
                double beagleTimingValue,
                 int timePrecision,
                 bool printSpeedup,
                 double cpuTimingValue,
                 int speedupPrecision,
                 bool printPercent,
                 double totalTime,
                 int percentPrecision) {
    std::cout << std::setprecision(timePrecision) << timingValue << " ms";
    if (printSpeedup) std::cout << " (" << std::setprecision(speedupPrecision) << beagleTimingValue/timingValue << "x BEAGLE)";
    if (printPercent) std::cout << " (" << std::setw(3+percentPrecision) << std::setfill('0') << std::setprecision(percentPrecision) << (double)(timingValue/totalTime)*100 << "%)";
    std::cout << "\n";
}

#endif // HAVE_PLL

void printTiming(double timingValue,
                 int timePrecision,
                 bool printSpeedup,
                 double cpuTimingValue,
                 int speedupPrecision,
                 bool printPercent,
                 double totalTime,
                 int percentPrecision) {
    std::cout << std::setprecision(timePrecision) << timingValue << " ms";
    if (printSpeedup) std::cout << " (" << std::setprecision(speedupPrecision) << cpuTimingValue/timingValue << "x CPU)";
    if (printPercent) std::cout << " (" << std::setw(3+percentPrecision) << std::setfill('0') << std::setprecision(percentPrecision) << (double)(timingValue/totalTime)*100 << "%)";
    std::cout << "\n";
}

double getTimeDiff(struct timeval t1,
                   struct timeval t2) {
    return ((double)(t2.tv_sec - t1.tv_sec)*1000.0 + (double)(t2.tv_usec-t1.tv_usec)/1000.0);
}

/* Given a binary tree, print its nodes according to the
"bottom-up" postorder traversal. */
void traversePostorder(node* currentNode, std::deque <node*> &S)
{
    if (currentNode == NULL)
        return;
 
    // first recur on left subtree
    traversePostorder(currentNode->left, S);
 
    // then recur on right subtree
    traversePostorder(currentNode->right, S);
 
    // now deal with the currentNode
    if (currentNode->left != NULL) {
        S.push_front(currentNode);
    }
}
 

/* Given a binary tree, print its nodes in reverse level order */
void reverseLevelOrder(node* root, std::deque <node*> &S)
{
    std::queue <node*> Q;
    Q.push(root);
 
    // Do something like normal level order traversal order. Following are the
    // differences with normal level order traversal
    // 1) Instead of printing a node, we push the node to stack
    // 2) Right subtree is visited before left subtree
    while (Q.empty() == false)
    {
        /* Dequeue node and make it root */
        root = Q.front();
        Q.pop();

        if (root->left!=NULL) {
            S.push_back(root);
        }
 
        /* Enqueue right child */
        if (root->right)
            Q.push(root->right); // NOTE: RIGHT CHILD IS ENQUEUED BEFORE LEFT
 
        /* Enqueue left child */
        if (root->left)
            Q.push(root->left);
    }
 
}


/* Given a binary tree, count number of parallel launches */
int countLaunches(node* root, bool postorderTraversal)
{
    std::deque <node *> S;
    if (postorderTraversal == true) {
        traversePostorder(root, S);
    } else {
        reverseLevelOrder(root, S);
    }

    int opCount = S.size();

    int launchCount = 0;
    std::vector<int> gridStartOp(opCount);
    std::vector<int> operationsTmp(opCount);
    int parentMinIndex = 0;

    for(int op=0; op<opCount; op++){
        node* parent = S.back();
        S.pop_back();
        int parentIndex = parent->data;
        int child1Index = parent->left->data;
        int child2Index = parent->right->data;

        operationsTmp[op] = parentIndex;
        
        // printf("op %02d dest %02d c1 %02d c2 %02d\n",
        //        op, parentIndex, child1Index, child2Index);

        bool newLaunch = false;

        if (op == 0) {
            newLaunch = true;
        } else if (child1Index >= parentMinIndex || child2Index >= parentMinIndex) {
            for (int i=gridStartOp[launchCount-1]; i < op; i++) {
                int previousParentIndex = operationsTmp[i];
                if (child1Index == previousParentIndex || child2Index == previousParentIndex) {
                    newLaunch = true;
                    break;
                }
            }
        }

       if (newLaunch) {
            gridStartOp[launchCount] = op;
            parentMinIndex = parentIndex;

            launchCount++;
        } 

        if (parentIndex < parentMinIndex)
            parentMinIndex = parentIndex;
    }

    return launchCount;
}

void addChildren(node* newNode, node* originalNode, std::vector<node*> newNodes)
{
    if (originalNode->left != NULL) {
        newNode->left = createNewNode(originalNode->left->data);
        newNode->left->parent = newNode;
        newNodes.push_back(newNode->left);
        
        addChildren(newNode->left, originalNode->left, newNodes);

        newNode->right = createNewNode(originalNode->right->data);
        newNode->right->parent = newNode;
        newNodes.push_back(newNode->right);

        addChildren(newNode->right, originalNode->right, newNodes);
    }
}


void addParentChildren(node* newNode, node* originalNode, std::vector<node*> newNodes)
{

    if (originalNode->parent != NULL) {

        if (originalNode->left->data == newNode->parent->data) {
            newNode->left = createNewNode(originalNode->parent->data);
            newNode->left->parent = newNode;
            newNodes.push_back(newNode->left);

            addParentChildren(newNode->left, originalNode->parent, newNodes);

            newNode->right = createNewNode(originalNode->right->data);
            newNode->right->parent = newNode;
            newNodes.push_back(newNode->right);

            addChildren(newNode->right, originalNode->right, newNodes);
        } else {
            newNode->right = createNewNode(originalNode->parent->data);
            newNode->right->parent = newNode;
            newNodes.push_back(newNode->right);

            addParentChildren(newNode->right, originalNode->parent, newNodes);

            newNode->left = createNewNode(originalNode->left->data);
            newNode->left->parent = newNode;
            newNodes.push_back(newNode->left);

            addChildren(newNode->left, originalNode->left, newNodes);
        }

    } else { // original is root node

        if (newNode->parent->data == originalNode->left->data) {
            newNode->data = originalNode->right->data;
            addChildren(newNode, originalNode->right, newNodes);
        } else {
            newNode->data = originalNode->left->data;
            addChildren(newNode, originalNode->left, newNodes);
        }
    }
}

node* reroot(node* rerootNode, node* root, std::vector<node*> newNodes)
{
    struct node* newRoot = createNewNode(rerootNode->data);
    newNodes.push_back(newRoot);

        if (rerootNode->parent->left == rerootNode) {

            newRoot->left = createNewNode(rerootNode->data);
            newRoot->left->parent = newRoot;
            newNodes.push_back(newRoot->left);

            addChildren(newRoot->left, rerootNode, newNodes);

            newRoot->right = createNewNode(rerootNode->parent->data);
            newRoot->right->parent = newRoot;
            newNodes.push_back(newRoot->right);

            addParentChildren(newRoot->right, rerootNode->parent, newNodes);

        } else {

            newRoot->right = createNewNode(rerootNode->data);
            newRoot->right->parent = newRoot;
            newNodes.push_back(newRoot->right);

            addChildren(newRoot->right, rerootNode, newNodes);

            newRoot->left = createNewNode(rerootNode->parent->data);
            newRoot->left->parent = newRoot;
            newNodes.push_back(newRoot->left);

            addParentChildren(newRoot->left, rerootNode->parent, newNodes);

        }

        newRoot->data = root->data;

        return newRoot;
}

void generateNewTree(int ntaxa,
                    bool rerootTrees,
                    bool pectinate,
                    bool postorderTraversal,
                    bool dynamicScaling,
                    int edgeCount,
                    int internalCount,
                    int unpartOpsCount,
                    int partitionCount,
                    int beagleOpCount,
#ifdef HAVE_PLL
                    bool pllTest,
                    pll_operation_t* pll_operations,
#endif
#ifdef HAVE_NCL
                    char* treenewick,
#endif 
                    int* operations)
{
    std::vector <node*> nodes;
    node* root = NULL;


    bool useNewickTree = false;

#ifdef HAVE_NCL
    useNewickTree = treenewick;
#endif 

    if (!useNewickTree)
    {
        nodes.push_back(createNewNode(0));
        int tipsAdded = 1;
        node* newParent;
        while (tipsAdded < ntaxa) {
            int sibling;
            if (pectinate)
                sibling = nodes.size()-1;
            else
                sibling = gt_rand() % nodes.size();
            node* newTip = createNewNode(tipsAdded);
            newParent = createNewNode(ntaxa + tipsAdded - 1);
            nodes.push_back(newTip);
            nodes.push_back(newParent);
            tipsAdded++;            
            newParent->left  = nodes[sibling];
            newParent->right = newTip;            
            if (nodes[sibling]->parent != NULL) {
                newParent->parent = nodes[sibling]->parent;
                if (nodes[sibling]->parent->left == nodes[sibling]) {
                    nodes[sibling]->parent->left = newParent;
                } else {
                    nodes[sibling]->parent->right = newParent;
                }
            }
            nodes[sibling]->parent = newParent;
            newTip->parent         = newParent;
        }
        root = nodes[0];
        while(root->parent != NULL) {
            root = root->parent;
        }
        int rootIndex = newParent->data;
        newParent->data = root->data;
        root->data = rootIndex;
    } else {
        root = createNewNode(0);
#ifdef HAVE_NCL
        ncl_generateTreeFromNewick(treenewick, ntaxa, nodes, root);
#endif 
    }

    if (rerootTrees) {
        int bestRerootNode = -1;
        int bestLaunchCount = countLaunches(root, postorderTraversal);

        // printf("\nroot node   = %d\tparallel launches = %d\n", root->data, bestLaunchCount);


        std::vector<node*> newNodes;

        for(int i = 0; i < nodes.size(); i++) {

            // printf("reroot node = %02d\t", nodes[i]->data);

            node* rerootNode = nodes[i];

            if (rerootNode->parent != NULL && rerootNode->parent != root) {
                
                node* newRoot = reroot(rerootNode, root, newNodes);

                int launchCount = countLaunches(newRoot, postorderTraversal);

                newNodes.clear();

                // printf("parallel launches = %d\n", launchCount);

                if (launchCount < bestLaunchCount) {
                    bestLaunchCount = launchCount;
                    bestRerootNode = i;
                }

            }
            // else {printf("doesn't change tree\n");}

        }

        if (bestRerootNode != -1) {
            // printf("\nbestLaunchCount = %d, node index = %d\n\n", bestLaunchCount, bestRerootNode);
            node* rerootNode = nodes[bestRerootNode];
            node* oldRoot = root;
            root = reroot(rerootNode, oldRoot, newNodes);
        }

    } 

    std::deque <node *> S;
    if (postorderTraversal == true) {
        traversePostorder(root, S);
    } else {
        reverseLevelOrder(root, S);
    }

    // while (S.empty() == false) {
    //     node* tmpNode = S.back();
    //     std::cout << tmpNode->data << " ";
    //     S.pop_back();
    // }
    // std::cout << std::endl;
    // reverseLevelOrder(root, S);

    // struct node *root = createNewNode(4);
    // root->left        = createNewNode(0);
    // root->right       = createNewNode(6);
    // root->right->left  = createNewNode(5);
    // root->right->right = createNewNode(3);
    // root->right->left->left  = createNewNode(1);
    // root->right->left->right = createNewNode(2);
    // std::deque <node *> S;
    // reverseLevelOrder(root, S);

    // printf("launch count = %03d", countLaunches(root));

    for(int op=0; op<unpartOpsCount; op++){
        node* parent = S.back();
        S.pop_back();
        int parentIndex = parent->data;
        int child1Index = parent->left->data;
        int child2Index = parent->right->data;

        for (int j=0; j<partitionCount; j++) {
            int opJ = partitionCount*op + j;
            operations[opJ*beagleOpCount+0] = parentIndex;
            operations[opJ*beagleOpCount+1] = (dynamicScaling ? parentIndex : BEAGLE_OP_NONE);
            operations[opJ*beagleOpCount+2] = (dynamicScaling ? parentIndex : BEAGLE_OP_NONE);
            operations[opJ*beagleOpCount+3] = child1Index;
            operations[opJ*beagleOpCount+4] = child1Index + j*edgeCount;;
            operations[opJ*beagleOpCount+5] = child2Index;
            operations[opJ*beagleOpCount+6] = child2Index + j*edgeCount;
            if (partitionCount > 1) {
                operations[opJ*beagleOpCount+7] = j;
                operations[opJ*beagleOpCount+8] = (dynamicScaling ? internalCount : BEAGLE_OP_NONE);
            }

    #ifdef HAVE_PLL
            if (pllTest) {
                pll_operations[op].parent_clv_index    = parentIndex;
                pll_operations[op].child1_clv_index    = child1Index;
                pll_operations[op].child2_clv_index    = child2Index;
                pll_operations[op].child1_matrix_index = child1Index + j*edgeCount;
                pll_operations[op].child2_matrix_index = child2Index + j*edgeCount;
                pll_operations[op].parent_scaler_index = PLL_SCALE_BUFFER_NONE;
                pll_operations[op].child1_scaler_index = PLL_SCALE_BUFFER_NONE;
                pll_operations[op].child2_scaler_index = PLL_SCALE_BUFFER_NONE;
            }
    #endif // HAVE_PLL

        // printf("op %02d part %02d dest %02d c1 %02d c2 %02d\n",
               // opJ, j, parentIndex, child1Index, child2Index);
        }
        // printf("\n");
    }   

}

void setNewCategoryRates(int partitionCount,
                         int rateCategoryCount,
                         int instanceCount,
                         std::vector<int> instances,
#ifdef HAVE_PLL
                         bool pllTest,
                         bool pllOnly,
                         pll_partition_t* pll_partition,
#endif
                         double* rates)
{
    for (int i = 0; i < rateCategoryCount; i++) {
        rates[i] = gt_rand() / (double) GT_RAND_MAX;
    }
    
    if (partitionCount > 1) {
        for (int i=0; i < partitionCount; i++) {
#ifdef HAVE_PLL
            if (!pllOnly) {
#endif
            beagleSetCategoryRatesWithIndex(instances[0], i, &rates[0]);
#ifdef HAVE_PLL
            } //if (!pllOnly) {
#endif
        }
    } else {
        for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
            if (!pllOnly) {
#endif
            beagleSetCategoryRates(instances[inst], &rates[0]);
#ifdef HAVE_PLL
            } //if (!pllOnly) {
#endif
        }
#ifdef HAVE_PLL
        if (pllTest) {
            pll_set_category_rates(pll_partition, rates);
        }
#endif // HAVE_PLL
    }
}

void setNewPatternWeights(int nsites,
                          int instanceCount,
                          std::vector<int> instances,
                          std::vector<int> instanceSitesCount,
#ifdef HAVE_PLL
                          bool pllTest,
                          bool pllOnly,
                          pll_partition_t* pll_partition,
#endif
                          double* patternWeights)
{
    for (int i = 0; i < nsites; i++) {
        patternWeights[i] =  gt_rand()%10;
    }    

    size_t instanceOffset = 0;
    for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
        if (!pllOnly) {
#endif
        beagleSetPatternWeights(instances[inst], patternWeights + instanceOffset);
        instanceOffset += instanceSitesCount[inst];
#ifdef HAVE_PLL
        } //if (!pllOnly) {
#endif
    }

#ifdef HAVE_PLL
    if (pllTest) {
        unsigned int* pll_patternWeights = (unsigned int*) malloc(sizeof(unsigned int) * nsites);
        for (int i = 0; i < nsites; i++) {
            pll_patternWeights[i] = (unsigned int) patternWeights[i];
        }    
        pll_set_pattern_weights(pll_partition, pll_patternWeights);
        free(pll_patternWeights);
    }
#endif // HAVE_PLL
}

void setNewCategoryWeights(int eigenCount,
                           int rateCategoryCount,
                           int instanceCount,
                           std::vector<int> instances,
#ifdef HAVE_PLL
                           bool pllTest,
                           bool pllOnly,
                           pll_partition_t* pll_partition,
#endif
                           double* weights)

{
    for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
        for (int i = 0; i < rateCategoryCount; i++) {
            weights[i] = gt_rand() / (double) GT_RAND_MAX;
        } 

        for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
            if (!pllOnly) {
#endif
            beagleSetCategoryWeights(instances[inst], eigenIndex, &weights[0]);
#ifdef HAVE_PLL
            } //if (!pllOnly) {
#endif
        }

#ifdef HAVE_PLL
        if (pllTest) {
            pll_set_category_weights(pll_partition, &weights[0]);
        }
#endif // HAVE_PLL

    }
}

void setNewEigenModels(int modelCount,
                       int stateCount,
                       double* freqs,
                       bool eigencomplex,
                       bool ievectrans,
                       bool setmatrix,
                       int eigenCount,
                       int instanceCount,
#ifdef HAVE_PLL
                       bool pllTest,
                       bool pllOnly,
                       pll_partition_t* pll_partition,
#endif
                       std::vector<int> instances)
{
    double* eval;
    if (!eigencomplex)
        eval = (double*)malloc(sizeof(double)*stateCount);
    else
        eval = (double*)malloc(sizeof(double)*stateCount*2);
    double* evec = (double*)malloc(sizeof(double)*stateCount*stateCount);
    double* ivec = (double*)malloc(sizeof(double)*stateCount*stateCount);
    
    for (int eigenIndex=0; eigenIndex < modelCount; eigenIndex++) {
        if (!eigencomplex && ((stateCount & (stateCount-1)) == 0)) {
            
            for (int i=0; i<stateCount; i++) {
                freqs[i] = 1.0 / stateCount;
            }

            // an eigen decomposition for the general state-space JC69 model
            // If stateCount = 2^n is a power-of-two, then Sylvester matrix H_n describes
            // the eigendecomposition of the infinitesimal rate matrix
             
            double* Hn = evec;
            Hn[0*stateCount+0] = 1.0; Hn[0*stateCount+1] =  1.0; 
            Hn[1*stateCount+0] = 1.0; Hn[1*stateCount+1] = -1.0; // H_1
         
            for (int k=2; k < stateCount; k <<= 1) {
                // H_n = H_1 (Kronecker product) H_{n-1}
                for (int i=0; i<k; i++) {
                    for (int j=i; j<k; j++) {
                        double Hijold = Hn[i*stateCount + j];
                        Hn[i    *stateCount + j + k] =  Hijold;
                        Hn[(i+k)*stateCount + j    ] =  Hijold;
                        Hn[(i+k)*stateCount + j + k] = -Hijold;
                        
                        Hn[j    *stateCount + i + k] = Hn[i    *stateCount + j + k];
                        Hn[(j+k)*stateCount + i    ] = Hn[(i+k)*stateCount + j    ];
                        Hn[(j+k)*stateCount + i + k] = Hn[(i+k)*stateCount + j + k];                                
                    }
                }        
            }
            
            // Since evec is Hadamard, ivec = (evec)^t / stateCount;    
            for (int i=0; i<stateCount; i++) {
                for (int j=i; j<stateCount; j++) {
                    ivec[i*stateCount+j] = evec[j*stateCount+i] / stateCount;
                    ivec[j*stateCount+i] = ivec[i*stateCount+j]; // Symmetric
                }
            }
           
            eval[0] = 0.0;
            for (int i=1; i<stateCount; i++) {
                eval[i] = -stateCount / (stateCount - 1.0);
            }
       
        } else if (!eigencomplex) {
            for (int i=0; i<stateCount; i++) {
                freqs[i] = gt_rand() / (double) GT_RAND_MAX;
            }
        
            double** qmat=New2DArray<double>(stateCount, stateCount);    
            double* relNucRates = new double[(stateCount * stateCount - stateCount) / 2];
            
            int rnum=0;
            for(int i=0;i<stateCount;i++){
                for(int j=i+1;j<stateCount;j++){
                    relNucRates[rnum] = gt_rand() / (double) GT_RAND_MAX;
                    qmat[i][j]=relNucRates[rnum] * freqs[j];
                    qmat[j][i]=relNucRates[rnum] * freqs[i];
                    rnum++;
                }
            }

            //set diags to sum rows to 0
            double sum;
            for(int x=0;x<stateCount;x++){
                sum=0.0;
                for(int y=0;y<stateCount;y++){
                    if(x!=y) sum+=qmat[x][y];
                        }
                qmat[x][x]=-sum;
            } 
            
            double* eigvalsimag=new double[stateCount];
            double** eigvecs=New2DArray<double>(stateCount, stateCount);//eigenvecs
            double** teigvecs=New2DArray<double>(stateCount, stateCount);//temp eigenvecs
            double** inveigvecs=New2DArray<double>(stateCount, stateCount);//inv eigenvecs    
            int* iwork=new int[stateCount];
            double* work=new double[stateCount];
            
            EigenRealGeneral(stateCount, qmat, eval, eigvalsimag, eigvecs, iwork, work);
            memcpy(*teigvecs, *eigvecs, stateCount*stateCount*sizeof(double));
            InvertMatrix(teigvecs, stateCount, work, iwork, inveigvecs);
            
            for(int x=0;x<stateCount;x++){
                for(int y=0;y<stateCount;y++){
                    evec[x * stateCount + y] = eigvecs[x][y];
                    if (ievectrans)
                        ivec[x * stateCount + y] = inveigvecs[y][x];
                    else
                        ivec[x * stateCount + y] = inveigvecs[x][y];
                }
            } 
            
            Delete2DArray(qmat);
            delete[] relNucRates;
            
            delete[] eigvalsimag;
            Delete2DArray(eigvecs);
            Delete2DArray(teigvecs);
            Delete2DArray(inveigvecs);
            delete[] iwork;
            delete[] work;
        } else if (eigencomplex && stateCount==4 && eigenCount==1) {
            // create base frequency array
            double temp_freqs[4] = { 0.25, 0.25, 0.25, 0.25 };
            
            // an eigen decomposition for the 4-state 1-step circulant infinitesimal generator
            double temp_evec[4 * 4] = {
                -0.5,  0.6906786606674509,   0.15153543380548623, 0.5,
                0.5, -0.15153543380548576,  0.6906786606674498,  0.5,
                -0.5, -0.6906786606674498,  -0.15153543380548617, 0.5,
                0.5,  0.15153543380548554, -0.6906786606674503,  0.5
            };
            
            double temp_ivec[4 * 4] = {
                -0.5,  0.5, -0.5,  0.5,
                0.6906786606674505, -0.15153543380548617, -0.6906786606674507,   0.15153543380548645,
                0.15153543380548568, 0.6906786606674509,  -0.15153543380548584, -0.6906786606674509,
                0.5,  0.5,  0.5,  0.5
            };
            
            double temp_eval[8] = { -2.0, -1.0, -1.0, 0, 0, 1, -1, 0 };
            
            for(int x=0;x<stateCount;x++){
                freqs[x] = temp_freqs[x];
                eval[x] = temp_eval[x];
                eval[x+stateCount] = temp_eval[x+stateCount];
                for(int y=0;y<stateCount;y++){
                    evec[x * stateCount + y] = temp_evec[x * stateCount + y];
                    if (ievectrans)
                        ivec[x * stateCount + y] = temp_ivec[x + y * stateCount];
                    else
                        ivec[x * stateCount + y] = temp_ivec[x * stateCount + y];
                }
            } 
        } else {
            abort("should not be here");
        }

        for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
           if (!pllOnly) {
#endif
            beagleSetStateFrequencies(instances[inst], eigenIndex, &freqs[0]);
#ifdef HAVE_PLL
            } //if (!pllOnly) {
#endif
        }

#ifdef HAVE_PLL
        if (pllTest) {
            pll_set_frequencies(pll_partition, 0, &freqs[0]);
        }
#endif // HAVE_PLL
        if (!setmatrix) {
            // set the Eigen decomposition
            for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
               if (!pllOnly) {
#endif
                beagleSetEigenDecomposition(instances[inst], eigenIndex, &evec[0], &ivec[0], &eval[0]);
#ifdef HAVE_PLL
                } //if (!pllOnly) {
#endif
            }
#ifdef HAVE_PLL
            if (pllTest) {
                double pll_subst_params[6] = {1,1,1,1,1,1};
                pll_set_subst_params(pll_partition, 0, pll_subst_params);
            }
#endif // HAVE_PLL
        }
    }
    
    free(eval);
    free(evec);
    free(ivec);
}

void printFlags(long inFlags) {
    if (inFlags & BEAGLE_FLAG_PRECISION_SINGLE   ) fprintf(stdout, " PRECISION_SINGLE"   );
    if (inFlags & BEAGLE_FLAG_PRECISION_DOUBLE   ) fprintf(stdout, " PRECISION_DOUBLE"   );
    if (inFlags & BEAGLE_FLAG_COMPUTATION_SYNCH  ) fprintf(stdout, " COMPUTATION_SYNCH"  );
    if (inFlags & BEAGLE_FLAG_COMPUTATION_ASYNCH ) fprintf(stdout, " COMPUTATION_ASYNCH" );
    if (inFlags & BEAGLE_FLAG_EIGEN_REAL         ) fprintf(stdout, " EIGEN_REAL"         );
    if (inFlags & BEAGLE_FLAG_EIGEN_COMPLEX      ) fprintf(stdout, " EIGEN_COMPLEX"      );
    if (inFlags & BEAGLE_FLAG_SCALING_MANUAL     ) fprintf(stdout, " SCALING_MANUAL"     );
    if (inFlags & BEAGLE_FLAG_SCALING_AUTO       ) fprintf(stdout, " SCALING_AUTO"       );
    if (inFlags & BEAGLE_FLAG_SCALING_ALWAYS     ) fprintf(stdout, " SCALING_ALWAYS"     );
    if (inFlags & BEAGLE_FLAG_SCALING_DYNAMIC    ) fprintf(stdout, " SCALING_DYNAMIC"    );
    if (inFlags & BEAGLE_FLAG_SCALERS_RAW        ) fprintf(stdout, " SCALERS_RAW"        );
    if (inFlags & BEAGLE_FLAG_SCALERS_LOG        ) fprintf(stdout, " SCALERS_LOG"        );
    if (inFlags & BEAGLE_FLAG_INVEVEC_STANDARD   ) fprintf(stdout, " INVEVEC_STANDARD"   );
    if (inFlags & BEAGLE_FLAG_INVEVEC_TRANSPOSED ) fprintf(stdout, " INVEVEC_TRANSPOSED" );
    if (inFlags & BEAGLE_FLAG_VECTOR_SSE         ) fprintf(stdout, " VECTOR_SSE"         );
    if (inFlags & BEAGLE_FLAG_VECTOR_AVX         ) fprintf(stdout, " VECTOR_AVX"         );
    if (inFlags & BEAGLE_FLAG_VECTOR_NONE        ) fprintf(stdout, " VECTOR_NONE"        );
    if (inFlags & BEAGLE_FLAG_THREADING_CPP      ) fprintf(stdout, " THREADING_CPP"      );
    if (inFlags & BEAGLE_FLAG_THREADING_OPENMP   ) fprintf(stdout, " THREADING_OPENMP"   );
    if (inFlags & BEAGLE_FLAG_THREADING_NONE     ) fprintf(stdout, " THREADING_NONE"     );
    if (inFlags & BEAGLE_FLAG_PROCESSOR_CPU      ) fprintf(stdout, " PROCESSOR_CPU"      );
    if (inFlags & BEAGLE_FLAG_PROCESSOR_GPU      ) fprintf(stdout, " PROCESSOR_GPU"      );
    if (inFlags & BEAGLE_FLAG_PROCESSOR_FPGA     ) fprintf(stdout, " PROCESSOR_FPGA"     );
    if (inFlags & BEAGLE_FLAG_PROCESSOR_CELL     ) fprintf(stdout, " PROCESSOR_CELL"     );
    if (inFlags & BEAGLE_FLAG_PROCESSOR_PHI      ) fprintf(stdout, " PROCESSOR_PHI"      );
    if (inFlags & BEAGLE_FLAG_PROCESSOR_OTHER    ) fprintf(stdout, " PROCESSOR_OTHER"    );
    if (inFlags & BEAGLE_FLAG_FRAMEWORK_CUDA     ) fprintf(stdout, " FRAMEWORK_CUDA"     );
    if (inFlags & BEAGLE_FLAG_FRAMEWORK_OPENCL   ) fprintf(stdout, " FRAMEWORK_OPENCL"   );
    if (inFlags & BEAGLE_FLAG_FRAMEWORK_CPU      ) fprintf(stdout, " FRAMEWORK_CPU"      );
    if (inFlags & BEAGLE_FLAG_PARALLELOPS_STREAMS) fprintf(stdout, " PARALLELOPS_STREAMS");
    if (inFlags & BEAGLE_FLAG_PARALLELOPS_GRID   ) fprintf(stdout, " PARALLELOPS_GRID"   );
}


void threadWaiting(threadData* tData)
{
    std::unique_lock<std::mutex> l(tData->m, std::defer_lock);
    while (true)
    {
        l.lock();

        // Wait until the queue won't be empty or stop is signaled
        tData->cv.wait(l, [tData] () {
            return (tData->stop || !tData->jobs.empty()); 
            });

        // Stop was signaled, let's exit the thread
        if (tData->stop) { return; }

        // Pop one task from the queue...
        std::packaged_task<void()> j = std::move(tData->jobs.front());
        tData->jobs.pop();

        l.unlock();

        // Execute the task!
        j();
    }
}

void runBeagle(int resource, 
               int stateCount, 
               int ntaxa, 
               int nsites, 
               bool manualScaling, 
               bool autoScaling,
               bool dynamicScaling,
               int rateCategoryCount,
               int nreps,
               bool fullTiming,
               bool requireDoublePrecision,
               bool disableVector,
               bool enableThreads,
               int compactTipCount,
               int randomSeed,
               int rescaleFrequency,
               bool unrooted,
               bool calcderivs,
               bool logscalers,
               int eigenCount,
               bool eigencomplex,
               bool ievectrans,
               bool setmatrix,
               bool opencl,
               int partitionCount,
               bool sitelikes,
               bool newDataPerRep,
               bool randomTree,
               bool rerootTrees,
               bool pectinate,
               bool benchmarklist,
               bool pllTest,
               bool pllSiteRepeats,
               bool pllOnly,
               bool multiRsrc,
               bool postorderTraversal,
               bool newTreePerRep,
               bool newParametersPerRep,
               int  threadCount,
               int* resourceList,
               int  resourceCount,
               bool alignmentFromFile,
               char* treenewick,
               bool clientThreadingEnabled)
{

    int instanceCount = 1;

    std::vector<int> instanceSitesCount;

    if (multiRsrc == true) {
        instanceCount = resourceCount;
    }

    for(int inst=0; inst<instanceCount; inst++) {
        instanceSitesCount.push_back(nsites/instanceCount);
    }

    if (instanceCount > 1) {
        int remainder = nsites % instanceCount;
        int currentInstance = 0;
        while (remainder != 0) {
            instanceSitesCount[currentInstance++]++;
            remainder--;
        }
    }
    
    int edgeCount = ntaxa*2-2;
    int internalCount = ntaxa-1;
    int partialCount = ((ntaxa+internalCount)-compactTipCount)*eigenCount;
    int scaleCount = ((manualScaling || dynamicScaling) ? ntaxa : 0);

    int modelCount = eigenCount * partitionCount;
    
    BeagleInstanceDetails instDetails;
    

    if (benchmarklist) {
        // print version and citation info
        fprintf(stdout, "BEAGLE version %s\n", beagleGetVersion());
        fprintf(stdout, "%s\n", beagleGetCitation());

        long benchmarkFlags = BEAGLE_BENCHFLAG_SCALING_NONE;

        if (manualScaling) {
            if (rescaleFrequency > 1)
                benchmarkFlags = BEAGLE_BENCHFLAG_SCALING_DYNAMIC;
            else
                benchmarkFlags = BEAGLE_BENCHFLAG_SCALING_ALWAYS;
        }

        long preferenceFlags = (enableThreads ? BEAGLE_FLAG_THREADING_CPP : 0);
        long requirementFlags =
        (requireDoublePrecision ? BEAGLE_FLAG_PRECISION_DOUBLE : BEAGLE_FLAG_PRECISION_SINGLE) |
	  (disableVector ? BEAGLE_FLAG_VECTOR_NONE : 0);

        // print resource list
        BeagleBenchmarkedResourceList* rBList;
        rBList = beagleGetBenchmarkedResourceList(
                    ntaxa,
                    compactTipCount,
                    stateCount,
                    nsites,
                    rateCategoryCount,
                    resourceList,
                    resourceCount,
                    preferenceFlags,
                    requirementFlags,
                    eigenCount,
                    partitionCount,
                    calcderivs,
                    benchmarkFlags);

        fprintf(stdout, "Resource benchmarks:\n");
        for (int i = 0; i < rBList->length; i++) {
            fprintf(stdout, "\tResource %i:\n\t\tName : %s\n", i, rBList->list[i].name);
            fprintf(stdout, "\t\tDesc : %s\n", rBList->list[i].description);
            fprintf(stdout, "\t\tSupport Flags:");
            printFlags(rBList->list[i].supportFlags);
            fprintf(stdout, "\n");
            fprintf(stdout, "\t\tRequired Flags:");
            printFlags(rBList->list[i].requiredFlags);
            fprintf(stdout, "\n");
            fprintf(stdout, "\t\tBenchmark Results:\n");
            fprintf(stdout, "\t\t\tNmbr : %d\n", rBList->list[i].number);
            fprintf(stdout, "\t\t\tImpl : %s\n", rBList->list[i].implName);
            fprintf(stdout, "\t\t\tFlags:");
            printFlags(rBList->list[i].benchedFlags);
            fprintf(stdout, "\n");
            fprintf(stdout, "\t\t\tPerf : %.4f ms (%.2fx CPU)\n", rBList->list[i].benchmarkResult, rBList->list[i].performanceRatio);
        }
        fprintf(stdout, "\n");
        std::exit(0);
    }

#ifdef HAVE_PLL
    pll_partition_t * pll_partition;
    pll_operation_t * pll_operations;
    unsigned int * pll_params_indices;

    if (pllTest) {
        int pll_num_params = 4;
        pll_params_indices = (unsigned int *) malloc(pll_num_params * sizeof(unsigned int));
        for (int i = 0; i < pll_num_params; i++) {
            pll_params_indices[i] = 0;
        }

        long pll_attribs = PLL_ATTRIB_ARCH_AVX2;
        if (pllSiteRepeats) {
            pll_attribs |= PLL_ATTRIB_SITE_REPEATS;
        } else if (compactTipCount == ntaxa) {
            pll_attribs |= PLL_ATTRIB_PATTERN_TIP;
        }

        pll_partition = pll_partition_create(ntaxa,
                                       partialCount,           /* clv buffers */
                                       stateCount, /* number of states */
                                       nsites,     /* sequence length */
                                       modelCount,           /* different rate parameters */
                                       edgeCount*modelCount,  /* probability matrices */
                                       rateCategoryCount, /* gamma categories */
                                       scaleCount*eigenCount,           /* scale buffers */
                                       pll_attribs
                                       );          /* attributes */
    }
#endif // HAVE_PLL

    std::vector<int> instances;
#ifdef HAVE_PLL
    if (!pllOnly) {
#endif
    for(int inst=0; inst<instanceCount; inst++) {

        int instanceResource = resource;
        
        if (multiRsrc) {
            instanceResource = resourceList[inst];
        }

        // create an instance of the BEAGLE library
        int instance = beagleCreateInstance(
                    ntaxa,            /**< Number of tip data elements (input) */
                    partialCount, /**< Number of partials buffers to create (input) */
                    compactTipCount,    /**< Number of compact state representation buffers to create (input) */
                    stateCount,       /**< Number of states in the continuous-time Markov chain (input) */
                    instanceSitesCount[inst],           /**< Number of site patterns to be handled by the instance (input) */
                    modelCount,               /**< Number of rate matrix eigen-decomposition buffers to allocate (input) */
                    (calcderivs ? (3*edgeCount*modelCount) : edgeCount*modelCount),/**< Number of rate matrix buffers (input) */
                    rateCategoryCount,/**< Number of rate categories */
                    scaleCount*eigenCount,          /**< scaling buffers */
                    &instanceResource,        /**< List of potential resource on which this instance is allowed (input, NULL implies no restriction */
                    1,                /**< Length of resourceList list (input) */
                    (enableThreads ? BEAGLE_FLAG_THREADING_CPP : 0) |
                    ((multiRsrc && !clientThreadingEnabled) ? BEAGLE_FLAG_COMPUTATION_ASYNCH : 0) |
		    (multiRsrc ? BEAGLE_FLAG_PARALLELOPS_STREAMS : 0),         /**< Bit-flags indicating preferred implementation charactertistics, see BeagleFlags (input) */
                    (disableVector ? BEAGLE_FLAG_VECTOR_NONE : 0) |
                    (opencl ? BEAGLE_FLAG_FRAMEWORK_OPENCL : 0) |
                    (ievectrans ? BEAGLE_FLAG_INVEVEC_TRANSPOSED : BEAGLE_FLAG_INVEVEC_STANDARD) |
                    (logscalers ? BEAGLE_FLAG_SCALERS_LOG : BEAGLE_FLAG_SCALERS_RAW) |
                    (eigencomplex ? BEAGLE_FLAG_EIGEN_COMPLEX : BEAGLE_FLAG_EIGEN_REAL) |
                    (dynamicScaling ? BEAGLE_FLAG_SCALING_DYNAMIC : 0) |
                    (autoScaling ? BEAGLE_FLAG_SCALING_AUTO : 0) |
                    (requireDoublePrecision ? BEAGLE_FLAG_PRECISION_DOUBLE : BEAGLE_FLAG_PRECISION_SINGLE) ,   /**< Bit-flags indicating required implementation characteristics, see BeagleFlags (input) */
                    &instDetails);

        if (instance < 0) {
            fprintf(stderr, "Failed to obtain BEAGLE instance\n\n");
            return;
        } else {
            instances.push_back(instance);

            int rNumber = instDetails.resourceNumber;
            fprintf(stdout, "Using resource %i:\n", rNumber);
            fprintf(stdout, "\tRsrc Name : %s\n",instDetails.resourceName);
            fprintf(stdout, "\tImpl Name : %s\n", instDetails.implName);    
            fprintf(stdout, "\tFlags:");
            printFlags(instDetails.flags);
            fprintf(stdout, "\n\n");

            if (inst+1 < instanceCount) {
                fprintf(stdout, "and\n\n");
            }

            if (threadCount > 1) {
                beagleSetCPUThreadCount(instance, threadCount);
            }

        }
    }
#ifdef HAVE_PLL
    } //if (!pllOnly)
#endif
        
    if (!(instDetails.flags & BEAGLE_FLAG_SCALING_AUTO))
        autoScaling = false;
    
    // set the sequences for each tip using partial likelihood arrays
    gt_srand(randomSeed);   // fix the random seed...
    for(int i=0; i<ntaxa; i++)
    {
        if (compactTipCount == 0 || (i >= (compactTipCount-1) && i != (ntaxa-1))) {
            double* tmpPartials = getRandomTipPartials(nsites, stateCount);
            size_t instanceOffset = 0;
            for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
                if (!pllOnly) {
#endif
                beagleSetTipPartials(instances[inst], i, tmpPartials + instanceOffset);
#ifdef HAVE_PLL
                } //if (!pllOnly)
#endif
                instanceOffset += instanceSitesCount[inst]*stateCount;
            }
#ifdef HAVE_PLL
            if (pllTest) {
                pll_set_tip_clv(pll_partition, i, tmpPartials, 0);
            }
#endif // HAVE_PLL
            free(tmpPartials);
        } else {
            int* tmpStates;
            if (!alignmentFromFile) {
                tmpStates = getRandomTipStates(nsites, stateCount);
            }
#ifdef HAVE_NCL
            else {
                tmpStates = ncl_getAlignmentTipStates(nsites, i);
            }
#endif // HAVE_NCL
            size_t instanceOffset = 0;
            for(int inst=0; inst<instanceCount; inst++) {
#ifdef HAVE_PLL
                if (!pllOnly) {
#endif
                beagleSetTipStates(instances[inst], i, tmpStates + instanceOffset);
#ifdef HAVE_PLL
                } //if (!pllOnly)
#endif
                instanceOffset += instanceSitesCount[inst];
            }
#ifdef HAVE_PLL
            if (pllTest) {
                char* pll_tmp_states = pll_getNucleotideCharStates(tmpStates, nsites);
                pll_set_tip_states(pll_partition, i, pll_map_nt, pll_tmp_states);
                free(pll_tmp_states);
            }
#endif // HAVE_PLL
            free(tmpStates);                
        }
    }


    double* rates = (double*) malloc(rateCategoryCount * sizeof(double));

    
    setNewCategoryRates(partitionCount, rateCategoryCount, instanceCount, instances,
#ifdef HAVE_PLL
                        pllTest, pllOnly, pll_partition,
#endif
                        (double*) rates);
    
    double* patternWeights = (double*) malloc(sizeof(double) * nsites);

    setNewPatternWeights(nsites, instanceCount, instances, instanceSitesCount,
#ifdef HAVE_PLL
                         pllTest, pllOnly, pll_partition,
#endif
                         (double*) patternWeights);

    int* patternPartitions;
    double* partitionLogLs;
    double* partitionD1;
    double* partitionD2;
    if (partitionCount > 1) {
        partitionLogLs = (double*) malloc(sizeof(double) * partitionCount);
        partitionD1 = (double*) malloc(sizeof(double) * partitionCount);
        partitionD2 = (double*) malloc(sizeof(double) * partitionCount);
        patternPartitions = (int*) malloc(sizeof(int) * nsites);
        int partitionSize = nsites/partitionCount;
        for (int i = 0; i < nsites; i++) {
            int sitePartition =  gt_rand()%partitionCount;
            // int sitePartition =  i%partitionCount;
            // int sitePartition = i/partitionSize;
            if (sitePartition > partitionCount - 1)
                sitePartition = partitionCount - 1;
            patternPartitions[i] = sitePartition;
            // printf("patternPartitions[%d] = %d\n", i, patternPartitions[i]);
        }    
        // beagleSetPatternPartitions(instances[0], partitionCount, patternPartitions);
    }

    gt_srand(randomSeed);   // reset the random seed...

    // create base frequency array

	double* freqs = (double*) malloc(stateCount * sizeof(double));
    
    // create an array containing site category weights
	double* weights = (double*) malloc(rateCategoryCount * sizeof(double));

    setNewCategoryWeights(eigenCount, rateCategoryCount, instanceCount, instances,
#ifdef HAVE_PLL
                          pllTest, pllOnly, pll_partition,
#endif
                          (double*) weights);
    
    setNewEigenModels(modelCount, stateCount, (double*) freqs,
                      eigencomplex, ievectrans, setmatrix, eigenCount,
                      instanceCount,
#ifdef HAVE_PLL
                      pllTest, pllOnly, pll_partition,
#endif
                      instances);
    
    // a list of indices and edge lengths
    int* edgeIndices = new int[edgeCount*modelCount];
    int* edgeIndicesD1 = new int[edgeCount*modelCount];
    int* edgeIndicesD2 = new int[edgeCount*modelCount];

#ifdef HAVE_PLL
    unsigned int* pll_edgeIndices;
    if (pllTest) {
        pll_edgeIndices = new unsigned int[edgeCount];
    }
#endif // HAVE_PLL

    for(int i=0; i<edgeCount*modelCount; i++) {
        edgeIndices[i]=i;

#ifdef HAVE_PLL
        if (pllTest) {
            pll_edgeIndices[i] = i;
        }
#endif // HAVE_PLL

        edgeIndicesD1[i]=(edgeCount*modelCount)+i;
        edgeIndicesD2[i]=2*(edgeCount*modelCount)+i;
    }
    
    double* edgeLengths = new double[edgeCount*modelCount];
    for(int i=0; i<edgeCount; i++) {
        edgeLengths[i]=gt_rand() / (double) GT_RAND_MAX;
    }
    
    // create a list of partial likelihood update operations
    // the order is [dest, destScaling, source1, matrix1, source2, matrix2]
    int operationCount = internalCount*modelCount;
    int beagleOpCount = BEAGLE_OP_COUNT;
    if (partitionCount > 1)
        beagleOpCount = BEAGLE_PARTITION_OP_COUNT;
    int* operations = new int[beagleOpCount*operationCount];
    int unpartOpsCount = internalCount*eigenCount;
    int* scalingFactorsIndices = new int[unpartOpsCount]; // internal nodes

#ifdef HAVE_PLL
    if (pllTest) {
        pll_operations = (pll_operation_t *)malloc(unpartOpsCount* sizeof(pll_operation_t));
    }
#endif // HAVE_PLL

    for(int i=0; i<unpartOpsCount; i++){
        int child1Index;
        if (((i % internalCount)*2) < ntaxa)
            child1Index = (i % internalCount)*2;
        else
            child1Index = i*2 - internalCount * (int)(i / internalCount);
        int child2Index;
        if (((i % internalCount)*2+1) < ntaxa)
            child2Index = (i % internalCount)*2+1;
        else
            child2Index = i*2+1 - internalCount * (int)(i / internalCount);

        for (int j=0; j<partitionCount; j++) {
            int op = partitionCount*i + j;
            operations[op*beagleOpCount+0] = ntaxa+i;
            operations[op*beagleOpCount+1] = (dynamicScaling ? i : BEAGLE_OP_NONE);
            operations[op*beagleOpCount+2] = (dynamicScaling ? i : BEAGLE_OP_NONE);
            operations[op*beagleOpCount+3] = child1Index;
            operations[op*beagleOpCount+4] = child1Index + j*edgeCount;
            operations[op*beagleOpCount+5] = child2Index;
            operations[op*beagleOpCount+6] = child2Index + j*edgeCount;
            if (partitionCount > 1) {
                operations[op*beagleOpCount+7] = j;
                operations[op*beagleOpCount+8] = (dynamicScaling ? internalCount : BEAGLE_OP_NONE);
            }

#ifdef HAVE_PLL
            if (pllTest) {
                pll_operations[op].parent_clv_index    = ntaxa+i;
                pll_operations[op].child1_clv_index    = child1Index;
                pll_operations[op].child2_clv_index    = child2Index;
                pll_operations[op].child1_matrix_index = child1Index + j*edgeCount;
                pll_operations[op].child2_matrix_index = child2Index + j*edgeCount;
                pll_operations[op].parent_scaler_index = PLL_SCALE_BUFFER_NONE;
                pll_operations[op].child1_scaler_index = PLL_SCALE_BUFFER_NONE;
                pll_operations[op].child2_scaler_index = PLL_SCALE_BUFFER_NONE;
            }
#endif // HAVE_PLL

            // printf("op %d i %d j %d dest %d c1 %d c2 %d c1m %d c2m %d p %d\n",
            //        op, i, j, ntaxa+i, child1Index, child2Index,
            //        operations[op*beagleOpCount+4], operations[op*beagleOpCount+6], j);
        }

        scalingFactorsIndices[i] = i;

        if (autoScaling)
            scalingFactorsIndices[i] += ntaxa;
    }   

    int* rootIndices = new int[eigenCount * partitionCount];
    int* lastTipIndices = new int[eigenCount * partitionCount];
    int* lastTipIndicesD1 = new int[eigenCount * partitionCount];
    int* lastTipIndicesD2 = new int[eigenCount * partitionCount];
    int* categoryWeightsIndices = new int[eigenCount * partitionCount];
    int* stateFrequencyIndices = new int[eigenCount * partitionCount];
    int* cumulativeScalingFactorIndices = new int[eigenCount * partitionCount];
    int* partitionIndices = new int[partitionCount];
    
    for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
        int pOffset = partitionCount*eigenIndex;

        for (int partitionIndex=0; partitionIndex < partitionCount; partitionIndex++) {
            if (eigenIndex == 0)
                partitionIndices[partitionIndex] = partitionIndex;
            rootIndices[partitionIndex + pOffset] = ntaxa+(internalCount*(eigenIndex+1))-1;//ntaxa*2-2;
            lastTipIndices[partitionIndex + pOffset] = ntaxa-1;
            lastTipIndicesD1[partitionIndex + pOffset] = (ntaxa-1) + (edgeCount*modelCount);
            lastTipIndicesD2[partitionIndex + pOffset] = (ntaxa-1) + 2*(edgeCount*modelCount);
            categoryWeightsIndices[partitionIndex + pOffset] = eigenIndex;
            stateFrequencyIndices[partitionIndex + pOffset] = 0;
            cumulativeScalingFactorIndices[partitionIndex + pOffset] = ((manualScaling || dynamicScaling) ? (scaleCount*eigenCount-1)-eigenCount+eigenIndex+1 : BEAGLE_OP_NONE);
        }

        if (dynamicScaling) {
#ifdef HAVE_PLL
            if (!pllOnly) {
#endif
            beagleResetScaleFactors(instances[0], cumulativeScalingFactorIndices[eigenIndex]);
#ifdef HAVE_PLL
            } //if (!pllOnly)
#endif
        }
    }

    struct timeval time0, time1, time2, time3, time4, time5;
    double bestTimeSetPartitions, bestTimeUpdateTransitionMatrices, bestTimeUpdatePartials, bestTimeAccumulateScaleFactors, bestTimeCalculateRootLogLikelihoods, bestTimeTotal;

    int timePrecision = 4;
    int speedupPrecision = 2;
    int percentPrecision = 2;
    
    double logL = 0.0;
    double deriv1 = 0.0;
    double deriv2 = 0.0;
    
    double previousLogL = 0.0;
    double previousDeriv1 = 0.0;
    double previousDeriv2 = 0.0;

    int* eigenIndices = new int[edgeCount * modelCount];
    int* categoryRateIndices = new int[edgeCount * modelCount];
    for (int eigenIndex=0; eigenIndex < modelCount; eigenIndex++) {
        for(int j=0; j<edgeCount; j++) {
            eigenIndices[eigenIndex*edgeCount + j] = eigenIndex;
            categoryRateIndices[eigenIndex*edgeCount + j] = eigenIndex;
            edgeLengths[eigenIndex*edgeCount + j] = edgeLengths[j];
        }
    }

    gt_srand(randomSeed);   // reset the random seed...

#ifdef HAVE_PLL
    if (!pllOnly) {
#endif

    if ((treenewick || randomTree) && eigenCount==1 && !unrooted) {
        generateNewTree(ntaxa, rerootTrees, pectinate, postorderTraversal, dynamicScaling,
            edgeCount, internalCount, unpartOpsCount, partitionCount, beagleOpCount,
#ifdef HAVE_PLL
            pllTest, pll_operations,
#endif
#ifdef HAVE_NCL
            (newTreePerRep ? NULL : treenewick),
#endif 
            operations);
    }

    int numThreads = 0;
    threadData* threads;
    std::shared_future<void>* futures;
    std::vector<double> threadLogL;

    if (clientThreadingEnabled) {
        numThreads = instanceCount;
        threads = new threadData[numThreads];
        for (int i = 0; i < numThreads; i++) {
            threads[i].t = std::thread(threadWaiting, &threads[i]);
            threadLogL.push_back(0.0);
        }
        futures = new std::shared_future<void>[numThreads];
        if (futures == NULL)
            throw std::bad_alloc();
    }

    auto computeLikelihood = [&] (int i,
                                  double* replicateLogL,
                                  int replicateInstanceCount,
                                  int* replicateInstances,
                                  int* replicateInstanceSitesCount) {

        if (partitionCount > 1 && i==0) { //!(i % rescaleFrequency)) {
            if (beagleSetPatternPartitions(replicateInstances[0], partitionCount, patternPartitions) != BEAGLE_SUCCESS) {
                printf("ERROR: No BEAGLE implementation for beagleSetPatternPartitions\n");
                exit(-1);
            }
        }

        gettimeofday(&time1,NULL);

        if (partitionCount > 1) {
            int totalEdgeCount = edgeCount * modelCount;
            beagleUpdateTransitionMatricesWithMultipleModels(
                                           replicateInstances[0],     // instance
                                           eigenIndices,   // eigenIndex
                                           categoryRateIndices,   // category rate index
                                           edgeIndices,   // probabilityIndices
                                           (calcderivs ? edgeIndicesD1 : NULL), // firstDerivativeIndices
                                           (calcderivs ? edgeIndicesD2 : NULL), // secondDerivativeIndices
                                           edgeLengths,   // edgeLengths
                                           totalEdgeCount);            // count
        } else {
            for (int eigenIndex=0; eigenIndex < modelCount; eigenIndex++) {
                if (!setmatrix) {
                    for(int inst=0; inst<replicateInstanceCount; inst++) {
                        // tell BEAGLE to populate the transition matrices for the above edge lengths
                        beagleUpdateTransitionMatrices(replicateInstances[inst],     // instance
                                                       eigenIndex,             // eigenIndex
                                                       &edgeIndices[eigenIndex*edgeCount],   // probabilityIndices
                                                       (calcderivs ? &edgeIndicesD1[eigenIndex*edgeCount] : NULL), // firstDerivativeIndices
                                                       (calcderivs ? &edgeIndicesD2[eigenIndex*edgeCount] : NULL), // secondDerivativeIndices
                                                       edgeLengths,   // edgeLengths
                                                       edgeCount);            // count
                    }

                } else {
                    double* inMatrix = new double[stateCount*stateCount*rateCategoryCount];
                    for (int matrixIndex=0; matrixIndex < edgeCount; matrixIndex++) {
                        for(int z=0;z<rateCategoryCount;z++){
                            for(int x=0;x<stateCount;x++){
                                for(int y=0;y<stateCount;y++){
                                    inMatrix[z*stateCount*stateCount + x*stateCount + y] = gt_rand() / (double) GT_RAND_MAX;
                                }
                            } 
                        }
                        beagleSetTransitionMatrix(replicateInstances[0], edgeIndices[eigenIndex*edgeCount + matrixIndex], inMatrix, 1);
                        if (calcderivs) {
                            beagleSetTransitionMatrix(replicateInstances[0], edgeIndicesD1[eigenIndex*edgeCount + matrixIndex], inMatrix, 0);
                            beagleSetTransitionMatrix(replicateInstances[0], edgeIndicesD2[eigenIndex*edgeCount + matrixIndex], inMatrix, 0);
                        }
                    }
                }
            }

        }

        // std::cout.setf(std::ios::showpoint);
        // // std::cout.setf(std::ios::floatfield, std::ios::fixed);
        // std::cout.precision(4);
        // unsigned int partialsOps = internalCount * eigenCount;
        // unsigned int flopsPerPartial = (stateCount * 4) - 2 + 1;
        // unsigned long long partialsSize = stateCount * nsites * rateCategoryCount;
        // unsigned long long partialsTotal = partialsSize * partialsOps;
        // unsigned long long flopsTotal = partialsTotal * flopsPerPartial;

        // std::cout << " compute throughput:   ";

        // for (int pRep=0; pRep < 50; pRep++) {
            gettimeofday(&time2, NULL);

            // update the partials
            if (partitionCount > 1) {
                beagleUpdatePartialsByPartition( replicateInstances[0],                   // instance
                                (BeagleOperationByPartition*)operations,     // operations
                                internalCount*eigenCount*partitionCount);    // operationCount
            } else {
                for(int inst=0; inst<replicateInstanceCount; inst++) {
                    beagleUpdatePartials( replicateInstances[inst],      // instance
                                    (BeagleOperation*)operations,     // operations
                                    internalCount*eigenCount,              // operationCount
                                    (dynamicScaling ? internalCount : BEAGLE_OP_NONE));             // cumulative scaling index
                }
            }

            gettimeofday(&time3, NULL);

            // struct timespec ts;
            // ts.tv_sec = 0;
            // ts.tv_nsec = 100000000;
            // nanosleep(&ts, NULL);

            // std::cout << (flopsTotal/getTimeDiff(time2, time3))/1000000.0 << ", ";
        // }
        // std::cout << " GFLOPS " << std::endl<< std::endl;

        // std::cout << " compute throughput:   " << (flopsTotal/getTimeDiff(time2, time3))/1000000.0 << " GFLOPS " << std::endl;


        int scalingFactorsCount = internalCount;
                
        for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
            if (manualScaling && !(i % rescaleFrequency)) {
                beagleResetScaleFactors(replicateInstances[0],
                                        cumulativeScalingFactorIndices[eigenIndex]);
                
                beagleAccumulateScaleFactors(replicateInstances[0],
                                       &scalingFactorsIndices[eigenIndex*internalCount],
                                       scalingFactorsCount,
                                       cumulativeScalingFactorIndices[eigenIndex]);
            } else if (autoScaling) {
                beagleAccumulateScaleFactors(replicateInstances[0], &scalingFactorsIndices[eigenIndex*internalCount], scalingFactorsCount, BEAGLE_OP_NONE);
            }
        }
        
        gettimeofday(&time4, NULL);

        // calculate the site likelihoods at the root node
        if (!unrooted) {
            if (partitionCount > 1) {
                beagleCalculateRootLogLikelihoodsByPartition(
                                            replicateInstances[0],               // instance
                                            rootIndices,// bufferIndices
                                            categoryWeightsIndices,                // weights
                                            stateFrequencyIndices,                 // stateFrequencies
                                            cumulativeScalingFactorIndices,
                                            partitionIndices,
                                            partitionCount,
                                            eigenCount,                      // count
                                            partitionLogLs,
                                            replicateLogL);         // outLogLikelihoods
            } else {
                for(int inst=0; inst<replicateInstanceCount; inst++) {
                    beagleCalculateRootLogLikelihoods(replicateInstances[inst],               // instance
                                                rootIndices,// bufferIndices
                                                categoryWeightsIndices,                // weights
                                                stateFrequencyIndices,                 // stateFrequencies
                                                cumulativeScalingFactorIndices,
                                                eigenCount,                      // count
                                                replicateLogL);         // outLogLikelihoods
                }
                if (multiRsrc && !clientThreadingEnabled) {
                    *replicateLogL = 0.0;
                    double instLogL;
                    for(int inst=0; inst<replicateInstanceCount; inst++) {
                        beagleGetLogLikelihood(inst,
                                               &instLogL);
                        *replicateLogL += instLogL;
                    }
                }
            }
        } else {
            if (partitionCount > 1) {
                beagleCalculateEdgeLogLikelihoodsByPartition(
                                                  replicateInstances[0],
                                                  rootIndices,
                                                  lastTipIndices,
                                                  lastTipIndices,
                                                  (calcderivs ? lastTipIndicesD1 : NULL),
                                                  (calcderivs ? lastTipIndicesD2 : NULL),
                                                  categoryWeightsIndices,
                                                  stateFrequencyIndices,
                                                  cumulativeScalingFactorIndices,
                                                  partitionIndices,
                                                  partitionCount,
                                                  eigenCount,
                                                  partitionLogLs,
                                                  replicateLogL,
                                                  (calcderivs ? partitionD1 : NULL),
                                                  (calcderivs ? &deriv1 : NULL),
                                                  (calcderivs ? partitionD2 : NULL),
                                                  (calcderivs ? &deriv2 : NULL));
            } else {
                for(int inst=0; inst<replicateInstanceCount; inst++) {
                    beagleCalculateEdgeLogLikelihoods(replicateInstances[inst],               // instance
                                                      rootIndices,// bufferIndices
                                                      lastTipIndices,
                                                      lastTipIndices,
                                                      (calcderivs ? lastTipIndicesD1 : NULL),
                                                      (calcderivs ? lastTipIndicesD2 : NULL),
                                                      categoryWeightsIndices,                // weights
                                                      stateFrequencyIndices,                 // stateFrequencies
                                                      cumulativeScalingFactorIndices,
                                                      eigenCount,                      // count
                                                      replicateLogL,    // outLogLikelihood
                                                      (calcderivs ? &deriv1 : NULL),
                                                      (calcderivs ? &deriv2 : NULL));
                }
                if (multiRsrc && !clientThreadingEnabled) {
                    *replicateLogL = 0.0;
                    double instLogL;
                    for(int inst=0; inst<replicateInstanceCount; inst++) {
                        beagleGetLogLikelihood(inst,
                                               &instLogL);
                        *replicateLogL += instLogL;
                    }
                    if (calcderivs) {
                        deriv1 = deriv2 = 0.0;
                        double instDeriv1, instDeriv2;
                        for(int inst=0; inst<replicateInstanceCount; inst++) {
                            beagleGetDerivatives(inst,
                                                 &instDeriv1,
                                                 &instDeriv2);
                            deriv1 += instDeriv1;
                            deriv2 += instDeriv2;
                        }
                    }
                }
            }

        }
    }; // end lambda

//  replicate loop
    for (int i=0; i<nreps; i++){

        if (newDataPerRep) {
            for(int ii=0; ii<ntaxa; ii++)
            {
                if (compactTipCount == 0 || (ii >= (compactTipCount-1) && ii != (ntaxa-1))) {
                    double* tmpPartials = getRandomTipPartials(nsites, stateCount);
                    beagleSetTipPartials(instances[0], ii, tmpPartials);
                    free(tmpPartials);
                } else {
                    int* tmpStates = getRandomTipStates(nsites, stateCount);
                    beagleSetTipStates(instances[0], ii, tmpStates);
                    free(tmpStates);                
                }
            }
        }

        if (newTreePerRep && i > 0 && i != (nreps-1)) {
            generateNewTree(ntaxa, rerootTrees, pectinate, postorderTraversal, dynamicScaling,
                edgeCount, internalCount, unpartOpsCount, partitionCount, beagleOpCount,
#ifdef HAVE_PLL
                false, pll_operations,
#endif
#ifdef HAVE_NCL
                NULL,
#endif 
                operations);

            for(int j=0; j<edgeCount; j++) {
                edgeLengths[j] = gt_rand() / (double) GT_RAND_MAX;
            }
        } else if (newTreePerRep && treenewick && i == (nreps-1)) {
            generateNewTree(ntaxa, rerootTrees, pectinate, postorderTraversal, dynamicScaling,
                edgeCount, internalCount, unpartOpsCount, partitionCount, beagleOpCount,
    #ifdef HAVE_PLL
                pllTest, pll_operations,
    #endif
    #ifdef HAVE_NCL
                treenewick,
    #endif 
                operations);

            for(int j=0; j<edgeCount; j++) {
                edgeLengths[j] = gt_rand() / (double) GT_RAND_MAX;
            }
        }

        if (newParametersPerRep) {
            setNewCategoryRates(partitionCount, rateCategoryCount, instanceCount, instances,
#ifdef HAVE_PLL
                                false, false, pll_partition,
#endif
                                (double*) rates);

            setNewPatternWeights(nsites, instanceCount, instances, instanceSitesCount,
#ifdef HAVE_PLL
                                 false, false, pll_partition,
#endif
                                 (double*) patternWeights);

            setNewCategoryWeights(eigenCount, rateCategoryCount, instanceCount, instances,
#ifdef HAVE_PLL
                                  false, false, pll_partition,
#endif
                                  (double*) weights);
            
            setNewEigenModels(modelCount, stateCount, (double*) freqs,
                              eigencomplex, ievectrans, setmatrix, eigenCount,
                              instanceCount,
#ifdef HAVE_PLL
                              false, false, pll_partition,
#endif
                              instances);
        }


        if (manualScaling && (!(i % rescaleFrequency) || !((i-1) % rescaleFrequency))) {
            for(int j=0; j<operationCount; j++){
                int sIndex = j / partitionCount;
                operations[beagleOpCount*j+1] = (((manualScaling && !(i % rescaleFrequency))) ? sIndex : BEAGLE_OP_NONE);
                operations[beagleOpCount*j+2] = (((manualScaling && (i % rescaleFrequency))) ? sIndex : BEAGLE_OP_NONE);
            }
        }

        if (clientThreadingEnabled) {
            for (int j=0; j<numThreads; j++) {

                std::packaged_task<void()> threadTask(
                    std::bind(computeLikelihood, i, &threadLogL[j], 1,
                        &instances[j], &instanceSitesCount[j]));

                futures[j] = threadTask.get_future();
                threadData* td = &threads[j];

                std::unique_lock<std::mutex> l(td->m);
                td->jobs.push(std::move(threadTask));
                l.unlock();

            }

            logL = 0.0;
        }
        
        // start timing!
        gettimeofday(&time0,NULL);

        if (clientThreadingEnabled) {
            for (int j=0; j<numThreads; j++) {
                threads[j].cv.notify_one();

            }
            for (int j=0; j<numThreads; j++) {
                futures[j].wait();
                logL += threadLogL[j];
            }
        } else {
            computeLikelihood(i, &logL, instanceCount, &instances[0], &instanceSitesCount[0]);
        }

        // end timing!
        gettimeofday(&time5,NULL);
        
        // std::cout.setf(std::ios::showpoint);
        // std::cout.setf(std::ios::floatfield, std::ios::fixed);
        // int timePrecision = 6;
        // int speedupPrecision = 2;
        // int percentPrecision = 2;
        // std::cout << "run " << i << ": ";
        // printTiming(getTimeDiff(time1, time5), timePrecision, resource, cpuTimeTotal, speedupPrecision, 0, 0, 0);
        // fprintf(stdout, "logL = %.5f  ", logL);

            // unsigned int partialsOps = internalCount * eigenCount;
            // unsigned int flopsPerPartial = (stateCount * 4) - 2 + 1;
            // unsigned long long partialsSize = stateCount * nsites * rateCategoryCount;
            // unsigned long long partialsTotal = partialsSize * partialsOps;
            // unsigned long long flopsTotal = partialsTotal * flopsPerPartial;
            // std::cout << " compute throughput:   " << (flopsTotal/getTimeDiff(time2, time3))/1000000.0 << " GFLOPS " << std::endl;
    
        if (i == 0 || getTimeDiff(time0, time5) < bestTimeTotal || (treenewick && i == (nreps-1)))  {
            bestTimeTotal = getTimeDiff(time0, time5);
            bestTimeSetPartitions = getTimeDiff(time0, time1);
            bestTimeUpdateTransitionMatrices = getTimeDiff(time1, time2);
            bestTimeUpdatePartials = getTimeDiff(time2, time3);
            bestTimeAccumulateScaleFactors = getTimeDiff(time3, time4);
            bestTimeCalculateRootLogLikelihoods = getTimeDiff(time4, time5);
        }
        
        if (!(logL - logL == 0.0))
            fprintf(stdout, "error: invalid lnL\n");

        if (!newDataPerRep && !newTreePerRep && !newParametersPerRep) {        
            if (i > 0 && std::abs(logL - previousLogL) > MAX_DIFF)
                fprintf(stdout, "error: large lnL difference between reps\n");
        }
        
        if (calcderivs) {
            if (!(deriv1 - deriv1 == 0.0) || !(deriv2 - deriv2 == 0.0))
                fprintf(stdout, "error: invalid deriv\n");
            
            if (i > 0 && ((std::abs(deriv1 - previousDeriv1) > MAX_DIFF) || (std::abs(deriv2 - previousDeriv2) > MAX_DIFF)) )
                fprintf(stdout, "error: large deriv difference between reps\n");
        }

        previousLogL = logL;
        previousDeriv1 = deriv1;
        previousDeriv2 = deriv2;        
    }

    if (clientThreadingEnabled) {
        // Send stop signal to all threads and join them...
        for (int i = 0; i < numThreads; i++) {
            threadData* td = &threads[i];
            std::unique_lock<std::mutex> l(td->m);
            td->stop = true;
            td->cv.notify_one();
        }

        // Join all the threads
        for (int i = 0; i < numThreads; i++) {
            threadData* td = &threads[i];
            td->t.join();
        }

        delete[] threads;
        delete[] futures;
    }

    if (resource == 0) {
        cpuTimeSetPartitions = bestTimeSetPartitions;
        cpuTimeUpdateTransitionMatrices = bestTimeUpdateTransitionMatrices;
        cpuTimeUpdatePartials = bestTimeUpdatePartials;
        cpuTimeAccumulateScaleFactors = bestTimeAccumulateScaleFactors;
        cpuTimeCalculateRootLogLikelihoods = bestTimeCalculateRootLogLikelihoods;
        cpuTimeTotal = bestTimeTotal;
    }
    
    if (!calcderivs)
        fprintf(stdout, "logL = %.5f \n", logL);
    else
        fprintf(stdout, "logL = %.5f d1 = %.5f d2 = %.5f\n", logL, deriv1, deriv2);

    if (partitionCount > 1) {
        fprintf(stdout, " (");
        for (int p=0; p < partitionCount; p++) {
            fprintf(stdout, "p%d = %.5f", p, partitionLogLs[p]);
            if (p < partitionCount - 1)
                fprintf(stdout, ", ");
        }
        fprintf(stdout, ")\n");
    }
    
    if (calcderivs) {
        if (partitionCount > 1) {
            fprintf(stdout, " (");
            for (int p=0; p < partitionCount; p++) {
                fprintf(stdout, "p%dD1 = %.5f", p, partitionD1[p]);
                if (p < partitionCount - 1)
                    fprintf(stdout, ", ");
            }
            fprintf(stdout, ")\n");
        }
        
        if (partitionCount > 1) {
            fprintf(stdout, " (");
            for (int p=0; p < partitionCount; p++) {
                fprintf(stdout, "p%dD2 = %.5f", p, partitionD2[p]);
                if (p < partitionCount - 1)
                    fprintf(stdout, ", ");
            }
            fprintf(stdout, ")\n");
        }
    }

    if (sitelikes) {
        double* siteLogLs = (double*) malloc(sizeof(double) * nsites);
        beagleGetSiteLogLikelihoods(instances[0], siteLogLs);
        double sumLogL = 0.0;
        fprintf(stdout, "site likelihoods = ");
        for (int i=0; i<nsites; i++) {
            fprintf(stdout, "%.5f \t", siteLogLs[i]);
            sumLogL += siteLogLs[i] * patternWeights[i];
        }
        fprintf(stdout, "\nsumLogL = %.5f\n", sumLogL);
        if (calcderivs) {
            double* siteSecondDerivs = (double*) malloc(sizeof(double) * nsites);
            beagleGetSiteDerivatives(instances[0], siteLogLs, siteSecondDerivs);
            sumLogL = 0.0;
            fprintf(stdout, "site first derivs = ");
            for (int i=0; i<nsites; i++) {
                fprintf(stdout, "%.5f \t", siteLogLs[i]);
                sumLogL += siteLogLs[i] * patternWeights[i];
            }
            fprintf(stdout, "\nsumFirstDerivs = %.5f\n", sumLogL);

            sumLogL = 0.0;
            fprintf(stdout, "site second derivs = ");
            for (int i=0; i<nsites; i++) {
                fprintf(stdout, "%.5f \t", siteSecondDerivs[i]);
                sumLogL += siteSecondDerivs[i] * patternWeights[i];
            }
            fprintf(stdout, "\nsumSecondDerivs = %.5f\n", sumLogL);
            free(siteSecondDerivs);
        }
        free(siteLogLs);
    }

    if (partitionCount > 1) {
        free(patternPartitions);
    }

    std::cout.setf(std::ios::showpoint);
    std::cout.setf(std::ios::floatfield, std::ios::fixed);
    std::cout << "best run: ";
    printTiming(bestTimeTotal, timePrecision, resource, cpuTimeTotal, speedupPrecision, 0, 0, 0);
    if (fullTiming) {
        std::cout << " setPartitions:  ";
        printTiming(bestTimeSetPartitions, timePrecision, resource, cpuTimeSetPartitions, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        std::cout << " transMats:  ";
        printTiming(bestTimeUpdateTransitionMatrices, timePrecision, resource, cpuTimeUpdateTransitionMatrices, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        std::cout << " partials:   ";
        printTiming(bestTimeUpdatePartials, timePrecision, resource, cpuTimeUpdatePartials, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        unsigned int partialsOps = internalCount * eigenCount;
        unsigned int flopsPerPartial = (stateCount * 4) - 2 + 1;
        unsigned int bytesPerPartial = 3 * (requireDoublePrecision ? 8 : 4);
        if (manualScaling) {
            flopsPerPartial++;
            bytesPerPartial += (requireDoublePrecision ? 8 : 4);
        }
        unsigned int matrixBytes = partialsOps * 2 * stateCount*stateCount*rateCategoryCount * (requireDoublePrecision ? 8 : 4);
        unsigned long long partialsSize = stateCount * nsites * rateCategoryCount;
        unsigned long long partialsTotal = partialsSize * partialsOps;
        unsigned long long flopsTotal = partialsTotal * flopsPerPartial;
        std::cout << " partials throughput:   " << (partialsTotal/bestTimeUpdatePartials)/1000.0 << " M partials/second " << std::endl;
        std::cout << " compute throughput:   " << (flopsTotal/bestTimeUpdatePartials)/1000000.0 << " GFLOPS " << std::endl;
        std::cout << " memory bandwidth:   " << (((partialsTotal * bytesPerPartial + matrixBytes)/bestTimeUpdatePartials))/1000000.0 << " GB/s " << std::endl;
        if (manualScaling || autoScaling) {
            std::cout << " accScalers: ";
            printTiming(bestTimeAccumulateScaleFactors, timePrecision, resource, cpuTimeAccumulateScaleFactors, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        }
        std::cout << " rootLnL:    ";
        printTiming(bestTimeCalculateRootLogLikelihoods, timePrecision, resource, cpuTimeCalculateRootLogLikelihoods, speedupPrecision, 1, bestTimeTotal, percentPrecision);

        std::cout << " tree throughput total:   " << (partialsTotal/bestTimeTotal)/1000.0 << " M partials/second " << std::endl;

    }
    std::cout << "\n";
    
    for(int inst=0; inst<instanceCount; inst++) {
        beagleFinalizeInstance(instances[inst]);
    }
#ifdef HAVE_PLL
    } //if (!pllOnly)
#endif

//////////////////////////////////////////////////////////////////////////
// pll test
#ifdef HAVE_PLL
    if (pllTest) {

        double pll_bestTimeSetPartitions, pll_bestTimeUpdateTransitionMatrices, pll_bestTimeUpdatePartials, pll_bestTimeAccumulateScaleFactors, pll_bestTimeCalculateRootLogLikelihoods, pll_bestTimeTotal;

        logL = previousLogL = 0.0;

        gt_srand(randomSeed);   // reset the random seed...

        if ((treenewick || randomTree) && eigenCount==1 && !unrooted) {
            generateNewTree(ntaxa, rerootTrees, pectinate, postorderTraversal, dynamicScaling,
                edgeCount, internalCount, unpartOpsCount, partitionCount, beagleOpCount,
    #ifdef HAVE_PLL
                pllTest, pll_operations,
    #endif
    #ifdef HAVE_NCL
                (newTreePerRep ? NULL : treenewick),
    #endif 
                operations);
        }

        for (int i=0; i<nreps; i++){

            if (newDataPerRep) {
                for(int ii=0; ii<ntaxa; ii++)
                {
                    if (compactTipCount == 0 || (ii >= (compactTipCount-1) && ii != (ntaxa-1))) {
                        double* tmpPartials = getRandomTipPartials(nsites, stateCount);

                        pll_set_tip_clv(pll_partition, i, tmpPartials, 0);

                        free(tmpPartials);
                    } else {
                        int* tmpStates = getRandomTipStates(nsites, stateCount);

                        char* pll_tmp_states = pll_getNucleotideCharStates(tmpStates, nsites);
                        pll_set_tip_states(pll_partition, i, pll_map_nt, pll_tmp_states);
                        free(pll_tmp_states);

                        free(tmpStates);                
                    }
                }
            }


            if (newTreePerRep && randomTree && eigenCount==1 && !unrooted && i > 0 && i != (nreps-1)) {
                generateNewTree(ntaxa, rerootTrees, pectinate, postorderTraversal,
                    dynamicScaling,
                    edgeCount, internalCount, unpartOpsCount, partitionCount, beagleOpCount,
                    pllTest, pll_operations,
#ifdef HAVE_NCL
                    NULL,
#endif 
                    operations);

                for(int j=0; j<edgeCount; j++) {
                    edgeLengths[j] = gt_rand() / (double) GT_RAND_MAX;
                }
            } else if (newTreePerRep && treenewick && i == (nreps-1)) {
                generateNewTree(ntaxa, rerootTrees, pectinate, postorderTraversal, dynamicScaling,
                    edgeCount, internalCount, unpartOpsCount, partitionCount, beagleOpCount,
        #ifdef HAVE_PLL
                    pllTest, pll_operations,
        #endif
        #ifdef HAVE_NCL
                    treenewick,
        #endif 
                    operations);

                for(int j=0; j<edgeCount; j++) {
                    edgeLengths[j] = gt_rand() / (double) GT_RAND_MAX;
                }
            }

            if (newParametersPerRep) {
                setNewCategoryRates(partitionCount, rateCategoryCount, instanceCount, instances,
                                    pllTest, pllOnly, pll_partition,
                                    (double*) rates);

                setNewPatternWeights(nsites, instanceCount, instances, instanceSitesCount,
                                     pllTest, pllOnly, pll_partition,
                                     (double*) patternWeights);

                setNewCategoryWeights(eigenCount, rateCategoryCount, instanceCount, instances,
                                      pllTest, pllOnly, pll_partition,
                                      (double*) weights);
                
                setNewEigenModels(modelCount, stateCount, (double*) freqs,
                                  eigencomplex, ievectrans, setmatrix, eigenCount,
                                  instanceCount,
                                  pllTest, pllOnly, pll_partition,
                                  instances);
            }

            // TODO: pll scaling
            
            gettimeofday(&time0,NULL);

            // TODO: pll partitions

            gettimeofday(&time1,NULL);


            for (int eigenIndex=0; eigenIndex < modelCount; eigenIndex++) {
                // if (!setmatrix) {
                    // tell pll to populate the transition matrices for the above edge lengths
                    pll_update_prob_matrices(pll_partition,
                                             pll_params_indices,
                                             &pll_edgeIndices[eigenIndex*edgeCount],
                                             edgeLengths,
                                             edgeCount);
                // } 
            }
            
            // TODO: pll set matrix

            gettimeofday(&time2, NULL);

            pll_update_partials(pll_partition, pll_operations, internalCount*eigenCount);

            gettimeofday(&time3, NULL);

            // TODO: pll scaling
                    
            gettimeofday(&time4, NULL);

            unsigned int pll_rootIndex = rootIndices[0];
            unsigned int pll_lastTipIndex = lastTipIndices[0];

            // calculate the site likelihoods at the root node
            if (!unrooted) {

                logL = pll_compute_root_loglikelihood(pll_partition,
                                                      pll_rootIndex,
                                                      PLL_SCALE_BUFFER_NONE,
                                                      pll_params_indices,
                                                      NULL);
            } else {

                logL = pll_compute_edge_loglikelihood(pll_partition,
                                                              pll_rootIndex,
                                                              PLL_SCALE_BUFFER_NONE,
                                                              pll_lastTipIndex,
                                                              PLL_SCALE_BUFFER_NONE,
                                                              pll_lastTipIndex,
                                                              pll_params_indices,
                                                              NULL);
            }
            // end timing!
            gettimeofday(&time5,NULL);
            

            if (i == 0 || getTimeDiff(time0, time5) < pll_bestTimeTotal || (treenewick && i == (nreps-1))) {
                pll_bestTimeTotal = getTimeDiff(time0, time5);
                pll_bestTimeSetPartitions = getTimeDiff(time0, time1);
                pll_bestTimeUpdateTransitionMatrices = getTimeDiff(time1, time2);
                pll_bestTimeUpdatePartials = getTimeDiff(time2, time3);
                pll_bestTimeAccumulateScaleFactors = getTimeDiff(time3, time4);
                pll_bestTimeCalculateRootLogLikelihoods = getTimeDiff(time4, time5);
            }
            
            if (!(logL - logL == 0.0))
                fprintf(stdout, "pll error: invalid lnL\n");

            if (!newDataPerRep && !newTreePerRep && !newParametersPerRep) {        
                if (i > 0 && std::abs(logL - previousLogL) > MAX_DIFF)
                    fprintf(stdout, "pll error: large lnL difference between reps\n");
            }
            
            // TODO: pll derivatives

            previousLogL = logL;
        }

        if (resource == 0) {
            cpuTimeSetPartitions = pll_bestTimeSetPartitions;
            cpuTimeUpdateTransitionMatrices = pll_bestTimeUpdateTransitionMatrices;
            cpuTimeUpdatePartials = pll_bestTimeUpdatePartials;
            cpuTimeAccumulateScaleFactors = pll_bestTimeAccumulateScaleFactors;
            cpuTimeCalculateRootLogLikelihoods = pll_bestTimeCalculateRootLogLikelihoods;
            cpuTimeTotal = pll_bestTimeTotal;
        }

        
        fprintf(stdout, "pll logL = %.5f \n", logL);

        //TODO: pll site likelihoods
    
        free(patternWeights);

        std::cout.setf(std::ios::showpoint);
        std::cout.setf(std::ios::floatfield, std::ios::fixed);
        std::cout << "pll best run: ";
        pll_printTiming(pll_bestTimeTotal, bestTimeTotal, timePrecision, 1, cpuTimeTotal, speedupPrecision, 0, 0, 0);
        if (fullTiming) {
            std::cout << " setPartitions:  ";
            printTiming(pll_bestTimeSetPartitions, timePrecision, resource, cpuTimeSetPartitions, speedupPrecision, 1, pll_bestTimeTotal, percentPrecision);
            std::cout << " transMats:  ";
            printTiming(pll_bestTimeUpdateTransitionMatrices, timePrecision, resource, cpuTimeUpdateTransitionMatrices, speedupPrecision, 1, pll_bestTimeTotal, percentPrecision);
            std::cout << " partials:   ";
            printTiming(pll_bestTimeUpdatePartials, timePrecision, resource, cpuTimeUpdatePartials, speedupPrecision, 1, pll_bestTimeTotal, percentPrecision);
            unsigned int partialsOps = internalCount * eigenCount;
            unsigned int flopsPerPartial = (stateCount * 4) - 2 + 1;
            unsigned int bytesPerPartial = 3 * (requireDoublePrecision ? 8 : 4);
            if (manualScaling) {
                flopsPerPartial++;
                bytesPerPartial += (requireDoublePrecision ? 8 : 4);
            }
            unsigned int matrixBytes = partialsOps * 2 * stateCount*stateCount*rateCategoryCount * (requireDoublePrecision ? 8 : 4);
            unsigned long long partialsSize = stateCount * nsites * rateCategoryCount;
            unsigned long long partialsTotal = partialsSize * partialsOps;
            unsigned long long flopsTotal = partialsTotal * flopsPerPartial;
            std::cout << " partials throughput:   " << (partialsTotal/pll_bestTimeUpdatePartials)/1000.0 << " M partials/second " << std::endl;
            std::cout << " compute throughput:   " << (flopsTotal/pll_bestTimeUpdatePartials)/1000000.0 << " GFLOPS " << std::endl;
            std::cout << " memory bandwidth:   " << (((partialsTotal * bytesPerPartial + matrixBytes)/pll_bestTimeUpdatePartials))/1000000.0 << " GB/s " << std::endl;
            if (manualScaling || autoScaling) {
                std::cout << " accScalers: ";
                printTiming(pll_bestTimeAccumulateScaleFactors, timePrecision, resource, cpuTimeAccumulateScaleFactors, speedupPrecision, 1, pll_bestTimeTotal, percentPrecision);
            }
            std::cout << " rootLnL:    ";
            printTiming(pll_bestTimeCalculateRootLogLikelihoods, timePrecision, resource, cpuTimeCalculateRootLogLikelihoods, speedupPrecision, 1, pll_bestTimeTotal, percentPrecision);

            std::cout << " tree throughput total:   " << (partialsTotal/pll_bestTimeTotal)/1000.0 << " M partials/second " << std::endl;

        }
        std::cout << "\n";
        
        delete[] pll_edgeIndices;
        free(pll_operations);
        free(pll_params_indices);
        pll_partition_destroy(pll_partition);
    }
#endif // HAVE_PLL

	free(freqs);
	free(weights);
	free(rates);

    if (multiRsrc) {
        std::exit(0);
    }

}

void printResourceList() {
    // print version and citation info
    fprintf(stdout, "BEAGLE version %s\n", beagleGetVersion());
    fprintf(stdout, "%s\n", beagleGetCitation());     

    // print resource list
    BeagleResourceList* rList;
    rList = beagleGetResourceList();
    fprintf(stdout, "Available resources:\n");
    for (int i = 0; i < rList->length; i++) {
        fprintf(stdout, "\tResource %i:\n\t\tName : %s\n", i, rList->list[i].name);
        fprintf(stdout, "\t\tDesc : %s\n", rList->list[i].description);
        fprintf(stdout, "\t\tFlags:");
        printFlags(rList->list[i].supportFlags);
        fprintf(stdout, "\n");
    }    
    fprintf(stdout, "\n");
    std::exit(0);
}


void helpMessage() {
    std::cerr << "Usage:\n\n";
    std::cerr << "synthetictest [--help] [--resourcelist] [--benchmarklist] [--states <integer>] [--taxa <integer>] [--sites <integer>] [--rates <integer>] [--manualscale] [--autoscale] [--dynamicscale] [--rsrc <integer>] [--reps <integer>] [--doubleprecision] [--disablevector] [--enablethreads] [--compacttips <integer>] [--seed <integer>] [--rescalefrequency <integer>] [--fulltiming] [--unrooted] [--calcderivs] [--logscalers] [--eigencount <integer>] [--eigencomplex] [--ievectrans] [--setmatrix] [--opencl] [--partitions <integer>] [--sitelikes] [--newdata] [--randomtree] [--reroot] [--stdrand] [--pectinate] [--multirsrc] [--postorder] [--newtree] [--newparameters] [--threadcount] [--clientthreads]";
#ifdef HAVE_PLL
    std::cerr << " [--plltest]";
    std::cerr << " [--pllonly]";
    std::cerr << " [--pllrepeats]";
#endif // HAVE_PLL
#ifdef HAVE_NCL
    std::cerr << " [--alignmentdna]";
    std::cerr << " [--compress]";
    std::cerr << " [--tree]";
#endif // HAVE_NCL
    std::cerr << "\n\n";
    std::cerr << "If --help is specified, this usage message is shown\n\n";
    std::cerr << "If --manualscale, --autoscale, or --dynamicscale is specified, BEAGLE will rescale the partials during computation\n\n";
    std::cerr << "If --fulltiming is specified, you will see more detailed timing results (requires BEAGLE_DEBUG_SYNCH defined to report accurate values)\n\n";
    std::exit(0);
}

void interpretCommandLineParameters(int argc, const char* argv[],
                                    int* stateCount,
                                    int* ntaxa,
                                    int* nsites,
                                    bool* manualScaling,
                                    bool* autoScaling,
                                    bool* dynamicScaling,
                                    int* rateCategoryCount,
                                    std::vector<int>* rsrc,
                                    int* nreps,
                                    bool* fullTiming,
                                    bool* requireDoublePrecision,
                                    bool* disableVector,
                                    bool* enableThreads,
                                    int* compactTipCount,
                                    int* randomSeed,
                                    int* rescaleFrequency,
                                    bool* unrooted,
                                    bool* calcderivs,
                                    bool* logscalers,
                                    int* eigenCount,
                                    bool* eigencomplex,
                                    bool* ievectrans,
                                    bool* setmatrix,
                                    bool* opencl,
                                    int*  partitions,
                                    bool* sitelikes,
                                    bool* newDataPerRep,
                                    bool* randomTree,
                                    bool* rerootTrees,
                                    bool* pectinate,
                                    bool* benchmarklist,
                                    bool* pllTest,
                                    bool* pllSiteRepeats,
                                    bool* pllOnly,
                                    bool* multiRsrc,
                                    bool* postorderTraversal,
                                    bool* newTreePerRep,
                                    bool* newParametersPerRep,
                                    int* threadCount,
                                    char** alignmentdna,
                                    bool* compress,
                                    char** treenewick,
                                    bool* clientThreadingEnabled)    {
    bool expecting_stateCount = false;
    bool expecting_ntaxa = false;
    bool expecting_nsites = false;
    bool expecting_rateCategoryCount = false;
    bool expecting_nreps = false;
    bool expecting_rsrc = false;
    bool expecting_compactTipCount = false;
    bool expecting_seed = false;
    bool expecting_rescaleFrequency = false;
    bool expecting_eigenCount = false;
    bool expecting_partitions = false;
    bool expecting_threads = false;
    bool expecting_alignmentdna = false;
    bool expecting_treenewick = false;
    
    for (unsigned i = 1; i < argc; ++i) {
        std::string option = argv[i];
        
        if (expecting_stateCount) {
            *stateCount = (unsigned)atoi(option.c_str());
            expecting_stateCount = false;
        } else if (expecting_ntaxa) {
            *ntaxa = (unsigned)atoi(option.c_str());
            expecting_ntaxa = false;
        } else if (expecting_nsites) {
            *nsites = (unsigned)atoi(option.c_str());
            expecting_nsites = false;
        } else if (expecting_rateCategoryCount) {
            *rateCategoryCount = (unsigned)atoi(option.c_str());
            expecting_rateCategoryCount = false;
        } else if (expecting_rsrc) {
            std::stringstream ss(option);
            int j;
            while (ss >> j) {
                rsrc->push_back(j);
                if (ss.peek() == ',')
                    ss.ignore();
            }
            expecting_rsrc = false;            
        } else if (expecting_nreps) {
            *nreps = (unsigned)atoi(option.c_str());
            expecting_nreps = false;
        } else if (expecting_compactTipCount) {
            *compactTipCount = (unsigned)atoi(option.c_str());
            expecting_compactTipCount = false;
        } else if (expecting_seed) {
            *randomSeed = (unsigned)atoi(option.c_str());
            expecting_seed = false;
        } else if (expecting_rescaleFrequency) {
            *rescaleFrequency = (unsigned)atoi(option.c_str());
            expecting_rescaleFrequency = false;
        } else if (expecting_eigenCount) {
            *eigenCount = (unsigned)atoi(option.c_str());
            expecting_eigenCount = false;
        } else if (expecting_partitions) {
            *partitions = (unsigned)atoi(option.c_str());
            expecting_partitions = false;
        } else if (expecting_threads) {
            *threadCount = (unsigned)atoi(option.c_str());
            expecting_threads = false;
        } else if (expecting_alignmentdna) {
            *alignmentdna = (char*) malloc(sizeof(char) * sizeof(option.c_str()));
            strcpy(*alignmentdna, option.c_str());
            expecting_alignmentdna = false;
        } else if (expecting_treenewick) {
            *treenewick = (char*) malloc(sizeof(char) * sizeof(option.c_str()));
            strcpy(*treenewick, option.c_str());
            expecting_treenewick = false;
        } else if (option == "--help") {
            helpMessage();
        } else if (option == "--resourcelist") {
            printResourceList();
        } else if (option == "--benchmarklist") {
            *benchmarklist = true;
        } else if (option == "--manualscale") {
            *manualScaling = true;
        } else if (option == "--autoscale") {
            *autoScaling = true;
        } else if (option == "--dynamicscale") {
            *dynamicScaling = true;
        } else if (option == "--doubleprecision") {
            *requireDoublePrecision = true;
        } else if (option == "--states") {
            expecting_stateCount = true;
        } else if (option == "--taxa") {
            expecting_ntaxa = true;
        } else if (option == "--sites") {
            expecting_nsites = true;
        } else if (option == "--rates") {
            expecting_rateCategoryCount = true;
        } else if (option == "--rsrc") {
            expecting_rsrc = true;
        } else if (option == "--reps") {
            expecting_nreps = true;
        } else if (option == "--compacttips") {
            expecting_compactTipCount = true;
        } else if (option == "--rescalefrequency") {
            expecting_rescaleFrequency = true;
        } else if (option == "--seed") {
            expecting_seed = true;
        } else if (option == "--fulltiming") {
            *fullTiming = true;
        } else if (option == "--disablevector") {
            *disableVector = true;
        } else if (option == "--enablethreads") {
            *enableThreads = true;
        } else if (option == "--unrooted") {
            *unrooted = true;
        } else if (option == "--calcderivs") {
            *calcderivs = true;
        } else if (option == "--logscalers") {
            *logscalers = true;
        } else if (option == "--eigencount") {
            expecting_eigenCount = true;
        } else if (option == "--eigencomplex") {
            *eigencomplex = true;
        } else if (option == "--ievectrans") {
            *ievectrans = true;
        } else if (option == "--setmatrix") {
            *setmatrix = true;
        } else if (option == "--opencl") {
            *opencl = true;
        } else if (option == "--partitions") {
            expecting_partitions = true;
        } else if (option == "--sitelikes") {
            *sitelikes = true;
        } else if (option == "--newdata") {
            *newDataPerRep = true;
        } else if (option == "--randomtree") {
            *randomTree = true;
        } else if (option == "--stdrand") {
            useStdlibRand = true;
        } else if (option == "--reroot") {
            *rerootTrees = true;
        } else if (option == "--pectinate") {
            *pectinate = true;
#ifdef HAVE_PLL
        } else if (option == "--plltest") {
            *pllTest = true;
        } else if (option == "--pllrepeats") {
            *pllSiteRepeats = true;
        } else if (option == "--pllonly") {
            *pllOnly = true;
            *pllTest = true;
#endif // HAVE_PLL
        } else if (option == "--multirsrc") {
            *multiRsrc = true;
        } else if (option == "--postorder") {
            *postorderTraversal = true;
        } else if (option == "--newtree") {
            *newTreePerRep = true;
        } else if (option == "--newparameters") {
            *newParametersPerRep = true;
        } else if (option == "--threadcount") {
            expecting_threads = true;
#ifdef HAVE_NCL
        } else if (option == "--alignmentdna") {
            expecting_alignmentdna = true;
        } else if (option == "--compress") {
            *compress = true;
        } else if (option == "--tree") {
            expecting_treenewick = true;
#endif // HAVE_NCL
        } else if (option == "--clientthreads") {
            *clientThreadingEnabled = true;
        } else {
            std::string msg("Unknown command line parameter \"");
            msg.append(option);         
            abort(msg.c_str());
        }
    }
    
    if (expecting_stateCount)
        abort("read last command line option without finding value associated with --states");
    
    if (expecting_ntaxa)
        abort("read last command line option without finding value associated with --taxa");
    
    if (expecting_nsites)
        abort("read last command line option without finding value associated with --sites");
    
    if (expecting_rateCategoryCount)
        abort("read last command line option without finding value associated with --rates");

    if (expecting_rsrc)
        abort("read last command line option without finding value associated with --rsrc");
    
    if (expecting_nreps)
        abort("read last command line option without finding value associated with --reps");
    
    if (expecting_seed)
        abort("read last command line option without finding value associated with --seed");
    
    if (expecting_rescaleFrequency)
        abort("read last command line option without finding value associated with --rescalefrequency");

    if (expecting_compactTipCount)
        abort("read last command line option without finding value associated with --compacttips");

    if (expecting_eigenCount)
        abort("read last command line option without finding value associated with --eigencount");

    if (expecting_partitions)
        abort("read last command line option without finding value associated with --partitions");

    if (*stateCount < 2)
        abort("invalid number of states supplied on the command line");
        
    if (*ntaxa < 2)
        abort("invalid number of taxa supplied on the command line");
      
    if (*nsites < 1)
        abort("invalid number of sites supplied on the command line");
    
    if (*rateCategoryCount < 1)
        abort("invalid number of rates supplied on the command line");
        
    if (*nreps < 1)
        abort("invalid number of reps supplied on the command line");

    if (*randomSeed < 1)
        abort("invalid number for seed supplied on the command line");   
        
    if (*manualScaling && *rescaleFrequency < 1)
        abort("invalid number for rescalefrequency supplied on the command line");   
    
    if (*compactTipCount < 0 || *compactTipCount > *ntaxa)
        abort("invalid number for compacttips supplied on the command line");
    
    if (*calcderivs && !(*unrooted))
        abort("calcderivs option requires unrooted tree option");
    
    if (*eigenCount < 1)
        abort("invalid number for eigencount supplied on the command line");
    
    if (*eigencomplex && (*stateCount != 4 || *eigenCount != 1))
        abort("eigencomplex option only works with stateCount=4 and eigenCount=1");

    if (*partitions < 1 || *partitions > *nsites)
        abort("invalid number for partitions supplied on the command line");

    if (*randomTree && (*eigenCount!=1 || *unrooted))
        abort("random tree topology can only be used with eigencount=1 and rooted trees");

    if (*partitions > 1 && *multiRsrc)
        abort("multiple resources cannot be used with partitioning");

    if (*manualScaling && *multiRsrc)
        abort("multiple resources cannot be used with scaling");

    if (*newDataPerRep && *multiRsrc)
        abort("multiple resources cannot be used with new data per replicate");

    if (*setmatrix && *multiRsrc)
        abort("multiple resources cannot be used with arbitrary transition matrix setting");

    if (*sitelikes && *multiRsrc)
        abort("multiple resources cannot be used with site likelihoods output");

    if (*postorderTraversal && (*randomTree==false && treenewick==NULL))
        abort("postorder traversal can only be used with randomtree option");

    if (*newTreePerRep && *randomTree==false)
        abort("new tree per replicate can only be used with randomtree option");

    if (*newTreePerRep && *eigenCount!=1)
        abort("new tree per replicate can only be used with eigencount=1");

    if (*newTreePerRep && *unrooted)
        abort("new tree per replicate can only be used with rooted trees");

    if (*clientThreadingEnabled && *multiRsrc==false)
        abort("client-side threading requires 'multirsrc' setting to be enabled");
}

int main( int argc, const char* argv[] )
{
    // Default values
    int stateCount = 4;
    int ntaxa = 16;
    int nsites = 10000;
    bool manualScaling = false;
    bool autoScaling = false;
    bool dynamicScaling = false;
    bool requireDoublePrecision = false;
    bool disableVector = false;
    bool enableThreads = false;
    bool unrooted = false;
    bool calcderivs = false;
    int compactTipCount = 0;
    int randomSeed = 1;
    int rescaleFrequency = 1;
    bool logscalers = false;
    int eigenCount = 1;
    bool eigencomplex = false;
    bool ievectrans = false;
    bool setmatrix = false;
    bool opencl = false;
    bool sitelikes = false;
    int partitions = 1;
    bool newDataPerRep = false;
    bool randomTree = false;
    bool rerootTrees = false;
    bool pectinate = false;
    bool benchmarklist = false;
    bool pllTest = false;
    bool pllSiteRepeats = false;
    bool pllOnly = false;
    bool multiRsrc = false;
    bool postorderTraversal = false;
    bool newTreePerRep = false;
    bool newParametersPerRep = false;
    int threadCount = 1;
    useStdlibRand = false;
    char* alignmentdna = NULL;
    bool alignmentFromFile = false;
    bool compress = false;
    char* treenewick = NULL;
    bool clientThreadingEnabled = false;

    std::vector<int> rsrc;
    rsrc.push_back(-1);

    int* rsrcList  = NULL;
    int  rsrcCount = 0;

    int nreps = 5;
    bool fullTiming = false;
    
    int rateCategoryCount = 4;
    
    interpretCommandLineParameters(argc, argv, &stateCount, &ntaxa, &nsites, &manualScaling, &autoScaling,
                                   &dynamicScaling, &rateCategoryCount, &rsrc, &nreps, &fullTiming,
                                   &requireDoublePrecision, &disableVector, &enableThreads, &compactTipCount, &randomSeed,
                                   &rescaleFrequency, &unrooted, &calcderivs, &logscalers,
                                   &eigenCount, &eigencomplex, &ievectrans, &setmatrix, &opencl,
                                   &partitions, &sitelikes, &newDataPerRep, &randomTree, &rerootTrees, &pectinate, &benchmarklist, &pllTest, &pllSiteRepeats, &pllOnly, &multiRsrc,
                                   &postorderTraversal, &newTreePerRep, &newParametersPerRep,
                                   &threadCount, &alignmentdna, &compress, &treenewick,
                                   &clientThreadingEnabled);

    if (alignmentdna == NULL) {
        std::cout << "\nSimulating genomic ";
        if (stateCount == 4)
            std::cout << "DNA";
        else
            std::cout << stateCount << "-state data";
        if (partitions > 1) {
            std::cout << " with " << ntaxa << " taxa, " << nsites << " site patterns, and " << partitions << " partitions";
        } else {
            std::cout << " with " << ntaxa << " taxa and " << nsites << " site patterns";
        }
    }
#ifdef HAVE_NCL
    else {
        stateCount = 4;
        ncl_readAlignmentDNA(alignmentdna, &ntaxa, &nsites, compress);
        compactTipCount = ntaxa;
        alignmentFromFile = true;
        free(alignmentdna);
    }
#endif //HAVE_NCL

    if (!benchmarklist)
        std::cout << " (" << nreps << " rep" << (nreps > 1 ? "s" : "");

    std::cout << (manualScaling ? ", manual scaling":(autoScaling ? ", auto scaling":(dynamicScaling ? ", dynamic scaling":"")));

    if (!benchmarklist)
        std::cout << ", random seed " << randomSeed << ")";

    std::cout << "\n\n";

    if (benchmarklist || multiRsrc) {
        rsrcCount =  rsrc.size() - 1;
        if (rsrcCount == 0) {
            rsrcList = NULL;
        } else {
            rsrcList  = &rsrc[1];
        }
    }

    BeagleResourceList* rl = beagleGetResourceList();

    if(rl != NULL){
        for(int i=0; i<rl->length; i++){
            if (rsrc.size() == 1 || std::find(rsrc.begin(), rsrc.end(), i)!=rsrc.end()) {
                runBeagle(i,
                          stateCount,
                          ntaxa,
                          nsites,
                          manualScaling,
                          autoScaling,
                          dynamicScaling,
                          rateCategoryCount,
                          nreps,
                          fullTiming,
                          requireDoublePrecision,
                          disableVector,
                          enableThreads,
                          compactTipCount,
                          randomSeed,
                          rescaleFrequency,
                          unrooted,
                          calcderivs,
                          logscalers,
                          eigenCount,
                          eigencomplex,
                          ievectrans,
                          setmatrix,
                          opencl,
                          partitions,
                          sitelikes,
                          newDataPerRep,
                          randomTree,
                          rerootTrees,
                          pectinate,
                          benchmarklist,
                          pllTest,
                          pllSiteRepeats,
                          pllOnly,
                          multiRsrc,
                          postorderTraversal,
                          newTreePerRep,
                          newParametersPerRep,
                          threadCount,
                          rsrcList,
                          rsrcCount,
                          alignmentFromFile,
                          treenewick,
                          clientThreadingEnabled);
            }
        }
    } else {
        abort("no BEAGLE resources found");
    }

//#ifdef _WIN32
//    std::cout << "\nPress ENTER to exit...\n";
//    fflush( stdout);
//    fflush( stderr);
//    getchar();
//#endif
}