summaryrefslogtreecommitdiff
path: root/src/main/ScheduleInterface.c
blob: f58cd1fbc6f4c77a210ca4d0bd7f96283b736a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
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
/*@@
   @file      ScheduleInterface.c
   @date      Thu Sep 16 14:06:21 1999
   @author    Tom Goodale
   @desc
              Routines to interface the main part of Cactus to the schedular.
   @enddesc
   @version   $Id$
 @@*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "cctk_Flesh.h"
#include "cctk_WarnLevel.h"
#include "cctk_Misc.h"

#include "cctk_Schedule.h"
#include "cctki_ScheduleBindings.h"
#include "cctki_Schedule.h"

#include "cctk_Comm.h"
#include "cctk_Sync.h"

#include "cctk_Constants.h"
#include "cctk_Groups.h"
#include "cctk_GroupsOnGH.h"

#include "cctki_FortranWrappers.h"

#include "cctk_Timers.h"

#include "util_Table.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(main_ScheduleInterface_c);


/********************************************************************
 *********************     Local Data Types   ***********************
 ********************************************************************/

typedef enum {sched_none, sched_group, sched_function} iSchedType;
typedef enum {schedpoint_misc, schedpoint_analysis} iSchedPoint;

/* Timer data for a routine in a given schedule bin */
typedef struct t_timer
{
  struct t_timer *next;
  int timer_handle;
  char *schedule_bin;
  int has_been_output;
} t_timer;

typedef struct
{
  /* Static data */
  char *description;

  /*char *thorn; MOVED TO FunctionData */
  char *implementation;

  iSchedType type;

  cFunctionData FunctionData;

  int n_mem_groups;
  int *mem_groups;
  int *timelevels;

  int n_comm_groups;
  int *comm_groups;

  /* Timer data */
  t_timer *timers;

  /* Dynamic data */
  int *CommOnEntry;
  int *StorageOnEntry;

  int done_entry;
  int synchronised;

} t_attribute;

typedef struct
{
  cGH *GH;
  iSchedPoint schedpoint;
  const char *schedule_bin;
  unsigned int n_functions;

  cTimerData *info;
  cTimerData *total_time;
  int print_headers;
  FILE *file;                   /* output file */

  /* Stuff passed in in user calls */

  int (*CallFunction)(void *, cFunctionData *, void *);

} t_sched_data;


/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

static int ScheduleTraverse(const char *where,
                            void *GH,
                            int (*CallFunction)(void *, cFunctionData *, void *));

static t_attribute *CreateAttribute(const char *where,
                                    const char *name,
                                    const char *description,
                                    const char *language,
                                    const char *thorn,
                                    const char *implementation,
                                    int n_mem_groups,
                                    int n_comm_groups,
                                    int n_trigger_groups,
                                    int n_sync_groups,
                                    int n_writes,
                                    int n_reads,
                                    int n_options,
                                    const int *timelevels,
                                    va_list *ap);

static int ParseOptionList(int n_items,
                           t_attribute *attribute,
                           va_list *ap);

static int ParseTagsTable(t_attribute *attribute,
                          va_list *ap);

static int InitialiseOptionList(t_attribute *attribute);

static int ParseOption(t_attribute *attribute,
                       const char *option);

static t_sched_modifier *CreateModifiers(int n_before,
                                         int n_after,
                                         int n_while,
                                         int n_if,
                                         va_list *ap);

int ValidateModifiers(t_sched_modifier *modifier);

static int CreateGroupIndexList(int n_items, int *array, va_list *ap);
static int CreateStringList(int n_items, const char **array, va_list *ap);
static t_sched_modifier *CreateTypedModifier(t_sched_modifier *modifier,
                                             const char *type,
                                             int n_items,
                                             va_list *ap);
static cFunctionType TranslateFunctionType(const char *where);

static int SchedulePrint(const char *where);

static int CCTKi_SchedulePrintEntry(t_attribute *attribute, t_sched_data *data);
static int CCTKi_SchedulePrintExit(t_attribute *attribute, t_sched_data *data);
static int CCTKi_SchedulePrintWhile(int n_whiles,
                                    char **whiles,
                                    t_attribute *attribute,
                                    t_sched_data *data,
                                    int first);
static int CCTKi_SchedulePrintIf(int n_if,
                                 char **ifs,
                                 t_attribute *attribute,
                                 t_sched_data *data);
static int CCTKi_SchedulePrintFunction(void *function, t_attribute *attribute, t_sched_data *data);

static int CCTKi_ScheduleCallEntry(t_attribute *attribute, t_sched_data *data);
static int CCTKi_ScheduleCallExit(t_attribute *attribute, t_sched_data *data);
static int CCTKi_ScheduleCallWhile(int n_whiles,
                                   char **whiles,
                                   t_attribute *attribute,
                                   t_sched_data *data,
                                   int first);
static int CCTKi_ScheduleCallIf(int n_ifs,
                                char **ifs,
                                t_attribute *attribute,
                                t_sched_data *data);
static int CCTKi_ScheduleCallFunction(void *function,
                                      t_attribute *attribute,
                                      t_sched_data *data);

static int SchedulePrintTimes(const char *where, t_sched_data *data);
static int CCTKi_SchedulePrintTimesFunction(void *function,
                                            t_attribute *attribute,
                                            t_sched_data *data);
static void CCTKi_SchedulePrintTimerInfo(cTimerData *info,
                                         cTimerData *total_time,
                                         const char *where,
                                         const char *description,
                                         FILE *file);
static void CCTKi_SchedulePrintTimerHeaders(cTimerData *info,
                                            FILE *file);
static int CCTKi_ScheduleResetTimerOutputFlag(void *function,
                                              t_attribute *attribute,
                                              t_sched_data *data);
static void PrintDelimiterLine (char delimiter,
                                const cTimerData *timer,
                                FILE *file);


/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

/* FIXME: these should be put in a header somewhere */

int CCTKi_TriggerSaysGo(cGH *GH, int variable);
int CCTKi_TriggerAction(void *GH, int variable);


/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/


static int indent_level = 0;

static int n_scheduled_comm_groups = 0;
static int *scheduled_comm_groups = NULL;

static int n_scheduled_storage_groups = 0;
static int *scheduled_storage_groups = NULL;
static int *scheduled_storage_groups_timelevels = NULL;

static cTimerData *timerinfo = NULL;
static int total_timer = -1;

static const cFunctionData *current_scheduled_function = NULL;


/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    CCTK_CallFunction
   @date       Thu Jan 27 11:29:47 2000
   @author     Tom Goodale
   @desc
   Calls a function depending upon the data passed in the the
   fdata structure.
   @enddesc
   @calls

   @var     function
   @vdesc   pointer to function
   @vtype   void *
   @vio     in
   @endvar
   @var     fdata
   @vdesc   data about the function
   @vtype   cFunctionData *
   @vio     in
   @endvar
   @var     data
   @vdesc   Data to be passed to the function
   @vtype   void *
   @vio     inout
   @endvar

   @returntype int
   @returndesc
   0 - didn't synchronise
   @endreturndesc
@@*/
int CCTK_CallFunction(void *function,
                      cFunctionData *fdata,
                      void *data)
{
  void (*standardfunc)(void *);

  int (*noargsfunc)(void);

  int (*oneargfunc)(void *);

  if(current_scheduled_function != NULL)
  {
    CCTK_VWarn(CCTK_WARN_PICKY, __LINE__, __FILE__, "Cactus",
               "CCTK_CallFunction: recursive call, calling "
               "'%s: %s::%s' while within '%s: %s::%s'",
               fdata->where, fdata->thorn, fdata->routine,
               current_scheduled_function->where,
               current_scheduled_function->thorn,
               current_scheduled_function->routine);
  }
  current_scheduled_function = fdata;

  switch(fdata->type)
  {
    case FunctionNoArgs:
      noargsfunc = (int (*)(void))function;
      noargsfunc();
      break;
    case FunctionOneArg:
      oneargfunc = (int (*)(void *))function;
      oneargfunc(data);
      break;
    case FunctionStandard:
      switch(fdata->language)
      {
        case LangC:
          standardfunc = (void (*)(void *))function;
          standardfunc(data);
          break;
        case LangFortran:
          fdata->FortranCaller(data, function);
          break;
        case LangNone:
          /* this should never happen */
          /* fall through */
        default :
          CCTK_Warn(1,__LINE__,__FILE__,"Cactus",
                    "CCTK_CallFunction: Unknown language.");
      }
      break;
    default :
      CCTK_Warn(1,__LINE__,__FILE__,"Cactus",
                "CCTK_CallFunction: Unknown function type.");
  }

  current_scheduled_function = NULL;

  /* Return 0, meaning didn't synchronise */
  return 0;
}

 /*@@
   @routine    CCTK_ScheduleQueryCurrentFunction
   @date       Fri Apr 20 08:57:49 PDT 2012
   @author     Roland Haas
   @desc
   Returns the cFunctionData of the function currenlty executing via
   CCTK_CallFunction.
   @enddesc
   @calls

   @var     GH
   @vdesc   GH data
   @vtype   const cGH *
   @vio     in
   @endvar

   @returntype cFunctionData *
   @returndesc
   Data about the function. NULL if no function is currently executing via
   CCTK_CallFunction.
   @endreturndesc
@@*/
const cFunctionData *CCTK_ScheduleQueryCurrentFunction(const cGH * CCTK_ATTRIBUTE_UNUSED GH)
{
  return current_scheduled_function;
}

/*@@
   @routine    CCTKi_ScheduleFunction
   @date       Thu Sep 16 18:19:01 1999
   @author     Tom Goodale
   @desc
   Schedules a function.
   @enddesc
   @calls

   @var     function
   @vdesc   function to be scheduled
   @vtype   void *
   @vio     in
   @endvar
   @var     name
   @vdesc   working name of function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     thorn
   @vdesc   name of thorn providing function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     implementation
   @vdesc   name of implementation thorn belongs to
   @vtype   const char *
   @vio     in
   @endvar
   @var     description
   @vdesc   desciption of function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     where
   @vdesc   where to schedule the function
   @vtype   const char *
   @vio     in
   @endvar
   @var     language
   @vdesc   language of function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     n_mem_groups
   @vdesc   Number of groups needing memory switched on during this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_comm_groups
   @vdesc   Number of groups needing communication switched on during this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_trigger_groups
   @vdesc   Number of groups to trigger this function on
   @vtype   int
   @vio     in
   @endvar
   @var     n_sync_groups
   @vdesc   Number of groups needing synchronisation after this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_writes
   @vdesc   Number of writes clauses
   @vtype   int
   @vio     in
   @endvar
   @var     n_reads
   @vdesc   Number of reads clauses
   @vtype   int
   @vio     in
   @endvar
   @var     n_options
   @vdesc   Number of options for this schedule block
   @vtype   int
   @vio     in
   @endvar
   @var     n_before
   @vdesc   Number of functions/groups to schedule before
   @vtype   int
   @vio     in
   @endvar
   @var     n_after
   @vdesc   Number of functions/groups to schedule after
   @vtype   int
   @vio     in
   @endvar
   @var     n_while
   @vdesc   Number of vars to schedule while
   @vtype   int
   @vio     in
   @endvar
   @var     n_if
   @vdesc   Number of vars to schedule if
   @vtype   int
   @vio     in
   @endvar
   @var     timelevels
   @vdesc   The number of timelevels of the storage groups to enable.
   @vtype   const int *
   @vio     in
   @var     ...
   @vdesc   remaining options
   @vtype   multiple const char *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   Return val of DoScheduleFunction or
   -1 - memory failure
   @endreturndesc
@@*/
int CCTKi_ScheduleFunction(void *function,
                           const char *name,
                           const char *thorn,
                           const char *implementation,
                           const char *description,
                           const char *where,
                           const char *language,
                           int n_mem_groups,
                           int n_comm_groups,
                           int n_trigger_groups,
                           int n_sync_groups,
                           int n_writes,
                           int n_reads,
                           int n_options,
                           int n_before,
                           int n_after,
                           int n_while,
                           int n_if,
                           const int *timelevels,
                           ...
                           )
{
  int retcode;
  t_attribute *attribute;
  t_sched_modifier *modifier;
  va_list ap;

  va_start(ap, timelevels);

  attribute = CreateAttribute(where,name,description, language, thorn, implementation,
                              n_mem_groups, n_comm_groups, n_trigger_groups,
                              n_sync_groups, n_writes, n_reads,
                              n_options, timelevels, &ap);
  modifier  = CreateModifiers(n_before, n_after, n_while, n_if, &ap);

  va_end(ap);

  ValidateModifiers(modifier);

  if(attribute && (modifier || (n_before == 0 && n_after == 0 &&
                                n_while == 0 && n_if == 0)))
  {
    attribute->FunctionData.type = TranslateFunctionType(where);

    retcode = CCTKi_DoScheduleFunction(where, name, function, modifier, (void *)attribute);

    if(retcode == -2)
    {
      CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                  "Tried to schedule duplicate item '%s' from thorn '%s' in '%s'",
                  name, thorn, where);
    }
#ifdef DEBUG
    fprintf(stderr, "Scheduled %s at %s\n", name, where);
#endif
  }
  else
  {
    fprintf(stderr, "Internal error: Failed to schedule %s at %s!!!\n", name, where);
    exit(2);
    retcode = -1;
  }

  return retcode;
}

/*@@
   @routine    CCTKi_ScheduleGroup
   @date       Thu Sep 16 18:19:18 1999
   @author     Tom Goodale
   @desc
   Schedules a group.
   @enddesc
   @calls

   @var     realname
   @vdesc   real name of group to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     name
   @vdesc   working name of group to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     thorn
   @vdesc   name of thorn providing group to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     implementation
   @vdesc   name of implementation group belongs to
   @vtype   const char *
   @vio     in
   @endvar
   @var     description
   @vdesc   desciption of group to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     where
   @vdesc   where to schedule the group
   @vtype   const char *
   @vio     in
   @endvar
   @var     n_mem_groups
   @vdesc   Number of groups needing memory switched on during this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_comm_groups
   @vdesc   Number of groups needing communication switched on during this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_trigger_groups
   @vdesc   Number of groups to trigger this function on
   @vtype   int
   @vio     in
   @endvar
   @var     n_sync_groups
   @vdesc   Number of groups needing synchronisation after this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_writes
   @vdesc   Number of writes clauses
   @vtype   int
   @vio     in
   @endvar
   @var     n_reads
   @vdesc   Number of reads clauses
   @vtype   int
   @vio     in
   @endvar
   @var     n_options
   @vdesc   Number of options for this schedule block
   @vtype   int
   @vio     in
   @endvar
   @var     n_before
   @vdesc   Number of functions/groups to schedule before
   @vtype   int
   @vio     in
   @endvar
   @var     n_after
   @vdesc   Number of functions/groups to schedule after
   @vtype   int
   @vio     in
   @endvar
   @var     n_while
   @vdesc   Number of vars to schedule while
   @vtype   int
   @vio     in
   @endvar
   @var     n_if
   @vdesc   Number of vars to schedule if
   @vtype   int
   @vio     in
   @endvar
   @var     timelevels
   @vdesc   The number of timelevels of the storage groups to enable.
   @vtype   const int *
   @var     ...
   @vdesc   remaining options
   @vtype   multiple const char *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   Return val of DoScheduleGroup or
   -1 - memory failure
   @endreturndesc
@@*/
int CCTKi_ScheduleGroup(const char *realname,
                        const char *name,
                        const char *thorn,
                        const char *implementation,
                        const char *description,
                        const char *where,
                        int n_mem_groups,
                        int n_comm_groups,
                        int n_trigger_groups,
                        int n_sync_groups,
                        int n_writes,
                        int n_reads,
                        int n_options,
                        int n_before,
                        int n_after,
                        int n_while,
                        int n_if,
                        const int *timelevels,
                        ...
                        )
{
  int retcode;
  t_attribute *attribute;
  t_sched_modifier *modifier;
  va_list ap;

  va_start(ap, timelevels);

  attribute = CreateAttribute(where,name,description, NULL, thorn, implementation,
                              n_mem_groups, n_comm_groups, n_trigger_groups,
                              n_sync_groups, n_writes, n_reads,
                              n_options, timelevels, &ap);
  modifier  = CreateModifiers(n_before, n_after, n_while, n_if, &ap);

  va_end(ap);

  ValidateModifiers(modifier);

  if(attribute && (modifier || (n_before == 0 && n_after == 0 &&
                                n_writes == 0 && n_reads == 0 &&
                                n_while == 0 && n_if == 0)))
  {
    retcode = CCTKi_DoScheduleGroup(where, name, realname, modifier, (void *)attribute);

    if(retcode == -2)
    {

      CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                  "Tried to schedule duplicate item '%s' from thorn '%s' in '%s'",
                  name, thorn, where);
    }

#ifdef DEBUG
    fprintf(stderr, "Scheduled %s at %s\n", name, where);
#endif
  }
  else
  {
#ifdef DEBUG
    fprintf(stderr, "Failed to schedule %s at %s!!!\n", name, where);
#endif
    retcode = -1;
  }

  return retcode;

}


/*@@
   @routine    CCTKi_ScheduleGroupStorage
   @date       Fri Sep 17 18:55:59 1999
   @author     Tom Goodale
   @desc
   Schedules a group for storage when a GH is created.
   @enddesc
   @calls

   @var     group
   @vdesc   group name
   @vtype   const char *
   @vio     in
   @endvar
   @var     timelevels
   @vdesc   The number of timelevels of this group to enable.
   @vtype   int
   @vio     in

   @returntype int
   @returndesc
   Group index or
   -1 - memory failure
   @endreturndesc
@@*/
int CCTKi_ScheduleGroupStorage(const char *group, int timelevels)
{
  int *temp;
  int *temp2;

  temp = realloc(scheduled_storage_groups,
                 (n_scheduled_storage_groups+1) * sizeof(int));
  temp2 = realloc(scheduled_storage_groups_timelevels,
                  (n_scheduled_storage_groups+1) * sizeof(int));

  if(temp && temp2)
  {
    temp[n_scheduled_storage_groups++] = CCTK_GroupIndex(group);
    scheduled_storage_groups = temp;
    scheduled_storage_groups_timelevels = temp2;

    scheduled_storage_groups_timelevels[n_scheduled_storage_groups-1] = timelevels;
  }

  return (temp && temp2  ? temp[n_scheduled_storage_groups-1] : -1);
}


/*@@
   @routine    CCTKi_ScheduleGroupComm
   @date       Fri Sep 17 18:55:59 1999
   @author     Tom Goodale
   @desc
   Schedules a group for communication when a GH is created.
   @enddesc
   @calls

   @var     group
   @vdesc   group name
   @vtype   const char *
   @vio     in
   @endvar
   @returntype int
   @returndesc
   Group index or
   -1 - memory failure
   @endreturndesc
@@*/
int CCTKi_ScheduleGroupComm(const char *group)
{
  int *temp;

  temp = realloc(scheduled_comm_groups,
                 (n_scheduled_comm_groups+1) * sizeof(int));
  if(temp)
  {
    temp[n_scheduled_comm_groups++] = CCTK_GroupIndex(group);
    scheduled_comm_groups = temp;
  }

  return (temp ? temp[n_scheduled_comm_groups-1] : -1);
}


 /*@@
   @routine    CCTK_ScheduleTraverse
   @date       Tue Apr  4 08:05:27 2000
   @author     Tom Goodale
   @desc
   Traverses a schedule point, and its entry and exit points if necessary.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar
   @var     GH
   @vdesc   GH data
   @vtype   void *
   @vio     inout
   @endvar
   @var     CallFunction
   @vdesc   Function called to call a function
   @vtype   int (*)(void *, cFubctionData, void *)
   @vio     in
   @vcomment
   Set to NULL to use the default
   @endvar

   @returntype int
   @returndesc
   0 - success
   1 - memory failure
   @endreturndesc
@@*/
int CCTK_ScheduleTraverse(const char *where,
                          void *GH,
                          int (*CallFunction)(void *, cFunctionData *, void *))
{
  int retcode;

  int special;
  const char *current;

  static char *current_point = NULL;
  static unsigned int current_length = 0;
  char *temp;

  special=0;

  /* Special entry points have $ in them */
  for(current=where; *current; current++)
  {
    if(*current == '$')
    {
      special = 1;
      break;
    }
  }

  retcode = 0;

  if(special)
  {
    ScheduleTraverse(where, GH, CallFunction);
  }
  else
  {
    if(current_length < strlen(where) + 7)
    {
      current_length = strlen(where)+7;

      temp = realloc(current_point, current_length);

      if(temp)
      {
        current_point = temp;
      }
      else
      {
        retcode = 1;
      }
    }
    if(retcode == 0)
    {
      sprintf(current_point, "%s$ENTRY", where);
      ScheduleTraverse(current_point, GH, CallFunction);

      ScheduleTraverse(where, GH, CallFunction);

      sprintf(current_point, "%s$EXIT", where);
      ScheduleTraverse(current_point, GH, CallFunction);
    }
  }

  return retcode;
}


/*@@
   @routine    CCTKi_ScheduleGHInit
   @date       Fri Sep 17 21:25:13 1999
   @author     Tom Goodale
   @desc
   Does any scheduling stuff setup which requires a GH.
   @enddesc
   @calls

   @var     GH
   @vdesc   GH data
   @vtype   void *
   @vio     inout
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
int CCTKi_ScheduleGHInit(void *GH)
{
  int i;


  /* create and start the CCTK total timer */
  total_timer = CCTK_TimerCreate ("CCTK total time");
  if (total_timer >= 0)
  {
    CCTK_TimerStartI (total_timer);
  }
  else
  {
    CCTK_VWarn (1, __LINE__, __FILE__, "Cactus",
                "Couldn't create CCTK total timer. "
                "No timing information will be available.");
  }

  if (n_scheduled_storage_groups>0)
  {
    CCTK_GroupStorageIncrease(GH,
                              n_scheduled_storage_groups,
                              scheduled_storage_groups,
                              scheduled_storage_groups_timelevels,
                              NULL);
  }

  for(i = 0; i < n_scheduled_comm_groups; i++)
  {
    CCTK_EnableGroupCommI(GH,scheduled_comm_groups[i]);
  }

  return 0;
}

/*@@
   @routine    CCTK_SchedulePrint
   @date       Fri Sep 17 21:52:44 1999
   @author     Tom Goodale
   @desc
   Prints out the schedule info.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
#define PUTS(x) printf("%*s%s\n", indent_level, "", x)
int CCTK_SchedulePrint(const char *where)
{
  if(!where)
  {
    indent_level = 2;
    PUTS("if (recover initial data)");
    PUTS("  Recover parameters");
    PUTS("endif");
    putchar ('\n');
    PUTS("Startup routines");
    PUTS("  [CCTK_STARTUP]");
    SchedulePrint("CCTK_STARTUP");
    putchar ('\n');
    PUTS("Startup routines which need an existing grid hierarchy");
    PUTS("  [CCTK_WRAGH]");
    SchedulePrint("CCTK_WRAGH");
    PUTS("Parameter checking routines");
    PUTS("  [CCTK_PARAMCHECK]");
    SchedulePrint("CCTK_PARAMCHECK");
    putchar ('\n');
    PUTS("Initialisation");
    PUTS("  if (NOT (recover initial data AND recovery_mode is 'strict'))");
    indent_level += 2;
    PUTS("  [CCTK_PREREGRIDINITIAL]");
    SchedulePrint("CCTK_PREREGRIDINITIAL$ENTRY");
    SchedulePrint("CCTK_PREREGRIDINITIAL");
    SchedulePrint("CCTK_PREREGRIDINITIAL$EXIT");
    PUTS("  Set up grid hierarchy");
    PUTS("  [CCTK_POSTREGRIDINITIAL]");
    SchedulePrint("CCTK_POSTREGRIDINITIAL$ENTRY");
    SchedulePrint("CCTK_POSTREGRIDINITIAL");
    SchedulePrint("CCTK_POSTREGRIDINITIAL$EXIT");
    PUTS("  [CCTK_BASEGRID]");
    SchedulePrint("CCTK_BASEGRID$ENTRY");
    SchedulePrint("CCTK_BASEGRID");
    SchedulePrint("CCTK_BASEGRID$EXIT");
    PUTS("  [CCTK_INITIAL]");
    SchedulePrint("CCTK_INITIAL$ENTRY");
    SchedulePrint("CCTK_INITIAL");
    SchedulePrint("CCTK_INITIAL$EXIT");
    PUTS("  [CCTK_POSTINITIAL]");
    SchedulePrint("CCTK_POSTINITIAL$ENTRY");
    SchedulePrint("CCTK_POSTINITIAL");
    SchedulePrint("CCTK_POSTINITIAL$EXIT");
    PUTS("  Initialise finer grids recursively");
    PUTS("  Restrict from finer grids");
    PUTS("  [CCTK_POSTRESTRICTINITIAL]");
    SchedulePrint("CCTK_POSTRESTRICTINITIAL$ENTRY");
    SchedulePrint("CCTK_POSTRESTRICTINITIAL");
    SchedulePrint("CCTK_POSTRESTRICTINITIAL$EXIT");
    PUTS("  [CCTK_POSTPOSTINITIAL]");
    SchedulePrint("CCTK_POSTPOSTINITIAL$ENTRY");
    SchedulePrint("CCTK_POSTPOSTINITIAL");
    SchedulePrint("CCTK_POSTPOSTINITIAL$EXIT");
    PUTS("  [CCTK_POSTSTEP]");
    SchedulePrint("CCTK_POSTSTEP$ENTRY");
    SchedulePrint("CCTK_POSTSTEP");
    SchedulePrint("CCTK_POSTSTEP$EXIT");
    PUTS("endif");
    PUTS("if (recover initial data)");
    PUTS("  [CCTK_BASEGRID]");
    SchedulePrint("CCTK_BASEGRID$ENTRY");
    SchedulePrint("CCTK_BASEGRID");
    SchedulePrint("CCTK_BASEGRID$EXIT");
    PUTS("  [CCTK_RECOVER_VARIABLES]");
    SchedulePrint("CCTK_RECOVER_VARIABLES");
    PUTS("  [CCTK_POST_RECOVER_VARIABLES]");
    SchedulePrint("CCTK_POST_RECOVER_VARIABLES");
    PUTS("endif");
    PUTS("if (checkpoint initial data)");
    PUTS("  [CCTK_CPINITIAL]");
    SchedulePrint("CCTK_CPINITIAL");
    PUTS("endif");
    PUTS("if (analysis)");
    PUTS("  [CCTK_ANALYSIS]");
    SchedulePrint("CCTK_ANALYSIS$ENTRY");
    SchedulePrint("CCTK_ANALYSIS");
    SchedulePrint("CCTK_ANALYSIS$EXIT");
    indent_level -=2;
    PUTS("endif");
    PUTS("Output grid variables");
    putchar ('\n');
    PUTS("do loop over timesteps");
    PUTS("  [CCTK_PREREGRID]");
    SchedulePrint("CCTK_PREREGRID$ENTRY");
    SchedulePrint("CCTK_PREREGRID");
    SchedulePrint("CCTK_PREREGRID$EXIT");
    PUTS("  Change grid hierarchy");
    PUTS("  [CCTK_POSTREGRID]");
    SchedulePrint("CCTK_POSTREGRID$ENTRY");
    SchedulePrint("CCTK_POSTREGRID");
    SchedulePrint("CCTK_POSTREGRID$EXIT");
    PUTS("  Rotate timelevels");
    PUTS("  iteration = iteration+1");
    PUTS("  t = t+dt");
    PUTS("  [CCTK_PRESTEP]");
    SchedulePrint("CCTK_PRESTEP$ENTRY");
    SchedulePrint("CCTK_PRESTEP");
    SchedulePrint("CCTK_PRESTEP$EXIT");
    PUTS("  [CCTK_EVOL]");
    SchedulePrint("CCTK_EVOL$ENTRY");
    SchedulePrint("CCTK_EVOL");
    SchedulePrint("CCTK_EVOL$EXIT");
    PUTS("  Evolve finer grids recursively");
    PUTS("  Restrict from finer grids");
    PUTS("  [CCTK_POSTRESTRICT]");
    SchedulePrint("CCTK_POSTRESTRICT$ENTRY");
    SchedulePrint("CCTK_POSTRESTRICT");
    SchedulePrint("CCTK_POSTRESTRICT$EXIT");
    PUTS("  [CCTK_POSTSTEP]");
    SchedulePrint("CCTK_POSTSTEP$ENTRY");
    SchedulePrint("CCTK_POSTSTEP");
    SchedulePrint("CCTK_POSTSTEP$EXIT");
    PUTS("if (checkpoint)");
    PUTS("  [CCTK_CHECKPOINT]");
    SchedulePrint("CCTK_CHECKPOINT");
    PUTS("endif");
    PUTS("if (analysis)");
    PUTS("  [CCTK_ANALYSIS]");
    SchedulePrint("CCTK_ANALYSIS$ENTRY");
    SchedulePrint("CCTK_ANALYSIS");
    SchedulePrint("CCTK_ANALYSIS$EXIT");
    PUTS("endif");
    PUTS("Output grid variables");
    PUTS("enddo");
    putchar ('\n');
    PUTS("Termination routines");
    PUTS("  [CCTK_TERMINATE]");
    SchedulePrint("CCTK_TERMINATE");
    putchar ('\n');
    PUTS("Shutdown routines");
    PUTS("  [CCTK_SHUTDOWN]");
    SchedulePrint("CCTK_SHUTDOWN");
    putchar ('\n');
    PUTS("Routines run after changing the grid hierarchy:");
    PUTS("  [CCTK_POSTREGRID]");
    SchedulePrint("CCTK_POSTREGRID");
  }
  else
  {
    SchedulePrint(where);
  }

  return (0);
}

/*@@
   @routine    CCTK_SchedulePrintTimes
   @date       2007-01-16
   @author     Erik Schnetter
   @desc
   Prints out the schedule timings.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
int CCTK_SchedulePrintTimes(const char *where)
{
  return CCTK_SchedulePrintTimesToFile(where, stdout);
}

/*@@
   @routine    CCTK_SchedulePrintTimesToFile
   @date       Fri Sep 17 21:52:44 1999
   @author     Tom Goodale
   @desc
   Prints out the schedule timings to a file.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar

   @var     file
   @vdesc   Output file, must be open for writing
   @vtype   FILE *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
int CCTK_SchedulePrintTimesToFile(const char *where, FILE *file)
{
  t_sched_data data;

  if(!timerinfo)
  {
    timerinfo = CCTK_TimerCreateData();
  }

  data.GH = NULL;
  data.schedule_bin = where;
  data.schedpoint = schedpoint_misc;
  data.print_headers = 1;
  data.file = file;
  data.info = timerinfo;
  data.total_time = CCTK_TimerCreateData();

  if(!where)
  {
    SchedulePrintTimes("CCTK_STARTUP", &data);
    SchedulePrintTimes("CCTK_WRAGH", &data);
    SchedulePrintTimes("CCTK_PARAMCHECK", &data);
    SchedulePrintTimes("CCTK_PREREGRIDINITIAL$ENTRY", &data);
    SchedulePrintTimes("CCTK_PREREGRIDINITIAL", &data);
    SchedulePrintTimes("CCTK_PREREGRIDINITIAL$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTREGRIDINITIAL$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTREGRIDINITIAL", &data);
    SchedulePrintTimes("CCTK_POSTREGRIDINITIAL$EXIT", &data);
    SchedulePrintTimes("CCTK_BASEGRID$ENTRY", &data);
    SchedulePrintTimes("CCTK_BASEGRID", &data);
    SchedulePrintTimes("CCTK_BASEGRID$EXIT", &data);
    SchedulePrintTimes("CCTK_INITIAL$ENTRY", &data);
    SchedulePrintTimes("CCTK_INITIAL", &data);
    SchedulePrintTimes("CCTK_INITIAL$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTINITIAL$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTINITIAL", &data);
    SchedulePrintTimes("CCTK_POSTINITIAL$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTRESTRICTINITIAL$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTRESTRICTINITIAL", &data);
    SchedulePrintTimes("CCTK_POSTRESTRICTINITIAL$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTPOSTINITIAL$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTPOSTINITIAL", &data);
    SchedulePrintTimes("CCTK_POSTPOSTINITIAL$EXIT", &data);
    SchedulePrintTimes("CCTK_RECOVER_VARIABLES", &data);
    SchedulePrintTimes("CCTK_POST_RECOVER_VARIABLES", &data);
    SchedulePrintTimes("CCTK_CPINITIAL", &data);
    SchedulePrintTimes("CCTK_PREREGRID$ENTRY", &data);
    SchedulePrintTimes("CCTK_PREREGRID", &data);
    SchedulePrintTimes("CCTK_PREREGRID$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTREGRID$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTREGRID", &data);
    SchedulePrintTimes("CCTK_POSTREGRID$EXIT", &data);
    SchedulePrintTimes("CCTK_PRESTEP$ENTRY", &data);
    SchedulePrintTimes("CCTK_PRESTEP", &data);
    SchedulePrintTimes("CCTK_PRESTEP$EXIT", &data);
    SchedulePrintTimes("CCTK_EVOL$ENTRY", &data);
    SchedulePrintTimes("CCTK_EVOL", &data);
    SchedulePrintTimes("CCTK_EVOL$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTRESTRICT$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTRESTRICT", &data);
    SchedulePrintTimes("CCTK_POSTRESTRICT$EXIT", &data);
    SchedulePrintTimes("CCTK_POSTSTEP$ENTRY", &data);
    SchedulePrintTimes("CCTK_POSTSTEP", &data);
    SchedulePrintTimes("CCTK_POSTSTEP$EXIT", &data);
    SchedulePrintTimes("CCTK_CHECKPOINT", &data);
    SchedulePrintTimes("CCTK_ANALYSIS$ENTRY", &data);
    SchedulePrintTimes("CCTK_ANALYSIS", &data);
    SchedulePrintTimes("CCTK_ANALYSIS$EXIT", &data);
    SchedulePrintTimes("CCTK_TERMINATE", &data);
    SchedulePrintTimes("CCTK_SHUTDOWN", &data);
  }
  else
  {
    SchedulePrintTimes(where, &data);
  }

  CCTK_TimerDestroyData(data.total_time);

  /* also print total time at the bottom */
  if (total_timer >= 0)
  {
    int total_timer_running = CCTK_TimerIsRunningI(total_timer);
    if (total_timer_running)
    {
      CCTK_TimerStopI (total_timer);
    }
    CCTK_TimerI (total_timer, timerinfo);
    CCTKi_SchedulePrintTimerInfo
      (timerinfo, NULL, "", "Total time for simulation", file);

    /* just in case this is not at termination yet ... */
    if (total_timer_running)
    {
      CCTK_TimerStartI (total_timer);
    }
  }

  return 0;
}

/*@@
   @routine    CCTK_TranslateLanguage
   @date       Thu Sep 16 18:18:31 1999
   @author     Tom Goodale
   @desc
   Translates a language string into an internal enum.
   @enddesc
   @calls

   @var     sval
   @vdesc   Language
   @vtype   const char *
   @vio     in
   @endvar

   @returntype cLanguage
   @returndesc
   The language
   @endreturndesc
@@*/
cLanguage CCTK_TranslateLanguage(const char *sval)
{
  cLanguage retcode;

  if(CCTK_Equals(sval, "C"))
  {
    retcode = LangC;
  }
  else if(CCTK_Equals(sval, "Fortran"))
  {
    retcode = LangFortran;
  }
  else
  {
    fprintf(stderr, "Unknown language %s\n", sval);
    retcode = LangNone;
  }

  return retcode;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

/*@@
   @routine    ScheduleTraverse
   @date       Fri Sep 17 21:52:44 1999
   @author     Tom Goodale
   @desc
   Traverses the given schedule point.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar
   @var     GH
   @vdesc   GH data
   @vtype   void *
   @vio     inout
   @endvar
   @var     CallFunction
   @vdesc   Function called to call a function
   @vtype   int (*)(void *, cFubctionData, void *)
   @vio     in
   @vcomment
   Set to NULL to use the default
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/

static int ScheduleTraverse(const char *where,
                            void *GH,
                            int (*CallFunction)(void *, cFunctionData *, void *))
{
  t_sched_data data;
  int (*calling_function)(void *, t_attribute *, t_sched_data *);

  data.GH = (cGH *)GH;
  data.CallFunction = CallFunction ? CallFunction : CCTK_CallFunction;
  data.schedule_bin = where;
  data.schedpoint = CCTK_Equals(data.schedule_bin, "CCTK_ANALYSIS") ?
                    schedpoint_analysis : schedpoint_misc;
  calling_function = CCTKi_ScheduleCallFunction;

  CCTKi_DoScheduleTraverse(where,
     (int (*)(void *, void *))                    CCTKi_ScheduleCallEntry,
     (int (*)(void *, void *))                    CCTKi_ScheduleCallExit,
     (int (*)(int, char **, void *, void *, int)) CCTKi_ScheduleCallWhile,
     (int (*)(int, char **, void *, void *))      CCTKi_ScheduleCallIf,
     (int (*)(void *, void *, void *))            calling_function,
     (void *)&data);

  return 0;
}

 /*@@
   @routine    CreateAttribute
   @date       Thu Sep 16 18:22:48 1999
   @author     Tom Goodale
   @desc
   Creates an attribute structure for a schedule item.
   @enddesc
   @calls

   @var     description
   @vdesc   desciption of function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     language
   @vdesc   language of function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     thorn
   @vdesc   name of thorn providing function to be scheduled
   @vtype   const char *
   @vio     in
   @endvar
   @var     implementation
   @vdesc   name of implementation thorn belongs to
   @vtype   const char *
   @vio     in
   @endvar
   @var     n_mem_groups
   @vdesc   Number of groups needing memory switched on during this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_comm_groups
   @vdesc   Number of groups needing communication switched on during this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_trigger_groups
   @vdesc   Number of groups to trigger this function on
   @vtype   int
   @vio     in
   @endvar
   @var     n_sync_groups
   @vdesc   Number of groups needing synchronisation after this function
   @vtype   int
   @vio     in
   @endvar
   @var     n_writes
   @vdesc   Number of writes clauses
   @vtype   int
   @vio     in
   @endvar
   @var     n_reads
   @vdesc   Number of reads clauses
   @vtype   int
   @vio     in
   @endvar
   @var     n_options
   @vdesc   Number of options for this schedule block
   @vtype   int
   @vio     in
   @endvar
   @var     timelevels
   @vdesc   The number of timelevels of the storage groups to enable.
   @vtype   const int *
   @var     ap
   @vdesc   options
   @vtype   va_list of multiple const char *
   @vio     inout
   @vcomment
   This should have as many items as the sum of the above n_* options
   @endvar

   @returntype t_attribute
   @returndesc
   The attribute
   @endreturndesc
@@*/
static t_attribute *CreateAttribute(const char *where,
                                    const char *name,
                                    const char *description,
                                    const char *language,
                                    const char *thorn,
                                    const char *implementation,
                                    int n_mem_groups,
                                    int n_comm_groups,
                                    int n_trigger_groups,
                                    int n_sync_groups,
                                    int n_writes,
                                    int n_reads,
                                    int n_options,
                                    const int *timelevels,
                                    va_list *ap)
{
  t_attribute *this;
  int i;

  this = calloc(1, sizeof(t_attribute));

  if(this)
  {
    this->FunctionData.where = malloc((strlen(where)+1)*sizeof(char));
    this->FunctionData.routine = malloc((strlen(name)+1)*sizeof(char));
    this->description    = malloc((strlen(description)+1)*sizeof(char));
    this->FunctionData.thorn = malloc((strlen(thorn)+1)*sizeof(char));
    this->implementation = malloc((strlen(implementation)+1)*sizeof(char));
    if (n_mem_groups > 0)
    {
      this->mem_groups     = malloc(n_mem_groups*sizeof(int));
      this->timelevels     = malloc(n_mem_groups*sizeof(int));
      this->StorageOnEntry = malloc(n_mem_groups*sizeof(int));
    }
    if (n_trigger_groups > 0)
    {
      this->FunctionData.TriggerGroups = malloc(n_trigger_groups*sizeof(int));
    }
    if (n_sync_groups > 0)
    {
      this->FunctionData.SyncGroups = malloc(n_sync_groups*sizeof(int));
    }
    if (n_writes > 0)
    {
      this->FunctionData.WritesClauses = malloc(n_writes*sizeof(char*));
    }
    if (n_reads > 0)
    {
      this->FunctionData.ReadsClauses = malloc(n_reads*sizeof(char*));
    }
    if (n_comm_groups > 0)
    {
      this->comm_groups    = malloc(n_comm_groups*sizeof(int));
      this->CommOnEntry    = malloc(n_comm_groups*sizeof(int));
    }

    if(this->FunctionData.where &&
       this->FunctionData.routine &&
       this->description     &&
       this->FunctionData.thorn  &&
       this->implementation  &&
       (this->mem_groups || n_mem_groups==0)         &&
       (this->timelevels || n_mem_groups==0)         &&
       (this->StorageOnEntry || n_mem_groups==0)         &&
       (this->comm_groups || n_comm_groups==0)       &&
       (this->FunctionData.TriggerGroups || n_trigger_groups==0) &&
       (this->FunctionData.SyncGroups || n_sync_groups==0) &&
       (this->FunctionData.WritesClauses || n_writes==0) &&
       (this->FunctionData.ReadsClauses || n_reads==0))
    {
      strcpy(this->FunctionData.where,where);
      strcpy(this->FunctionData.routine,name);
      strcpy(this->description,    description);
      strcpy(this->FunctionData.thorn, thorn);
      strcpy(this->implementation, implementation);

      if(language)
      {
        this->type = sched_function;
        this->FunctionData.language = CCTK_TranslateLanguage(language);
        this->FunctionData.FortranCaller = (int (*)(cGH *,void *))CCTKi_FortranWrapper(thorn);
      }
      else
      {
        this->type = sched_group;
      }

      /* Create the lists of indices of groups we're interested in. */
      CreateGroupIndexList(n_mem_groups,     this->mem_groups, ap);
      CreateGroupIndexList(n_comm_groups,    this->comm_groups, ap);
      CreateGroupIndexList(n_trigger_groups, this->FunctionData.TriggerGroups, ap);
      CreateGroupIndexList(n_sync_groups,    this->FunctionData.SyncGroups, ap);
      CreateStringList    (n_writes,       this->FunctionData.WritesClauses, ap);
      CreateStringList    (n_reads,       this->FunctionData.ReadsClauses, ap);

      for(i=0; i< n_mem_groups; i++)
      {
        this->timelevels[i] = timelevels[i];
      }

      /* Check the miscellaneous options */

      InitialiseOptionList(this);
      ParseOptionList(n_options, this, ap);

      /* Check the tags */
      ParseTagsTable(this, ap);

      this->n_mem_groups     = n_mem_groups;
      this->n_comm_groups    = n_comm_groups;
      this->FunctionData.n_TriggerGroups = n_trigger_groups;
      this->FunctionData.n_SyncGroups = n_sync_groups;
      this->FunctionData.n_WritesClauses = n_writes;
      this->FunctionData.n_ReadsClauses = n_reads;

      this->timers = NULL;
    }
    else
    {
      free(this->FunctionData.where);
      free(this->FunctionData.routine);
      free(this->description);
      free(this->comm_groups);
      free(this->FunctionData.TriggerGroups);
      free(this->FunctionData.SyncGroups);
      free(this->FunctionData.WritesClauses);
      free(this->FunctionData.ReadsClauses);
      free(this);
      this = NULL;
    }
  }

  return this;
}

/*@@
   @routine    CreateModifier
   @date       Thu Sep 16 18:23:13 1999
   @author     Tom Goodale
   @desc
   Creates a schedule modifier list.
   @enddesc
   @calls

   @var     n_before
   @vdesc   Number of functions/groups to schedule before
   @vtype   int
   @vio     in
   @endvar
   @var     n_after
   @vdesc   Number of functions/groups to schedule after
   @vtype   int
   @vio     in
   @endvar
   @var     n_while
   @vdesc   Number of vars to schedule while
   @vtype   int
   @vio     in
   @endvar
   @var     n_if
   @vdesc   Number of vars to schedule if
   @vtype   int
   @vio     in
   @endvar
   @var     ap
   @vdesc   options
   @vtype   va_list of multiple const char *
   @vio     inout
   @vcomment
   This should have as many items as the sum of the above n_* options
   @endvar

   @returntype t_sched_modifier *
   @returndesc
   the schedule modifier
   @endreturndesc
@@*/
static t_sched_modifier *CreateModifiers(int n_before,
                                         int n_after,
                                         int n_while,
                                         int n_if,
                                         va_list *ap)
{
  t_sched_modifier *modifier;

  modifier = CreateTypedModifier(NULL, "before", n_before, ap);
  modifier = CreateTypedModifier(modifier, "after", n_after, ap);
  modifier = CreateTypedModifier(modifier, "while", n_while, ap);
  modifier = CreateTypedModifier(modifier, "if", n_if, ap);

  return modifier;
}

/*@@
   @routine    ValidateModifier
   @date       Sat Apr 14 18:28:13 2001
   @author     Gabrielle Allen
   @desc
   Validates a schedule modifier list. At the moment just check that
   the while and if modifiers use a CCTK_INT grid variable.
   @enddesc
   @calls

   @var     modifier
   @vdesc
   @vtype   t_sched_modifier *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   Negative if modifier not valid, zero if modifier is valid.
   @endreturndesc
@@*/
int ValidateModifiers(t_sched_modifier *modifier)
{
  int retval = 0;
  int vindex;
  int type;

  for (;modifier;modifier=modifier->next)
  {
    if (modifier->type != sched_while ||
        modifier->type != sched_if)
    {
      continue;
    }
    vindex = CCTK_VarIndex(modifier->argument);
    if (vindex < 0)
    {
      CCTK_VWarn(0,__LINE__,__FILE__,"Cactus",
                 "While qualifier %s could not be parsed as fully "
                 "specified grid variable name (e.g. MyThorn::MyVar",
                 modifier->argument);
      retval = -1;
    }
    else
    {
      type = CCTK_VarTypeI(vindex);
      if (type != CCTK_VARIABLE_INT)
      {
        if (modifier->type == sched_while)
        {
          CCTK_VWarn(0,__LINE__,__FILE__,"Cactus",
                     "While qualifier %s is not a CCTK_INT grid variable",
                     modifier->argument);
        }
        else if (modifier->type == sched_if)
        {
          CCTK_VWarn(0,__LINE__,__FILE__,"Cactus",
                     "If qualifier %s is not a CCTK_INT grid variable",
                     modifier->argument);
        }
        retval = -1;
      }
    }
  }
  return retval;
}

/*@@
   @routine    CreateGroupIndexList
   @date       Fri Sep 17 21:51:51 1999
   @author     Tom Goodale
   @desc
   Gets the next n_items group names from the variable argument list
   and converts them to indices.
   @enddesc
   @calls

   @var     n_items
   @vdesc   number of items on the list
   @vtype   int
   @vio     in
   @endvar
   @var     array
   @vdesc   array of indices
   @vtype   int *
   @vio     out
   @endvar
   @var     ap
   @vdesc   argument list
   @vtype   va_list of const char *
   @vio     inout
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
static int CreateGroupIndexList(int n_items, int *array, va_list *ap)
{
  int i;
  const char *item;

  for(i=0; i < n_items; i++)
  {
    item = va_arg(*ap, const char *);

    array[i] = CCTK_GroupIndex(item);
  }

  return 0;
}


/*@@
   @routine    CreateStringList
   @date       2007-05-24
   @author     Erik Schnetter
   @desc
   Gets the next n_items group names from the variable argument list
   and converts them to strings.
   @enddesc
   @calls

   @var     n_items
   @vdesc   number of items on the list
   @vtype   int
   @vio     in
   @endvar
   @var     array
   @vdesc   array of strings
   @vtype   char **
   @vio     out
   @endvar
   @var     ap
   @vdesc   argument list
   @vtype   va_list of const char *
   @vio     inout
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
static int CreateStringList(int n_items, const char **array, va_list *ap)
{
  int i;
  const char *item;

  for(i=0; i < n_items; i++)
  {
    item = va_arg(*ap, const char *);

    array[i] = strdup(item);
  }

  return 0;
}


 /*@@
   @routine    ParseOptionList
   @date       Thu Jan 27 20:26:42 2000
   @author     Tom Goodale
   @desc
   Extracts the list of miscellaneous options in a schedule
   group definition.
   @enddesc
   @calls

   @var     n_items
   @vdesc   number of items on the list
   @vtype   int
   @vio     in
   @endvar
   @var     attribute
   @vdesc   attribute list
   @vtype   t_attribute *
   @vio     inout
   @endvar
   @var     ap
   @vdesc   argument list
   @vtype   va_list of const char *
   @vio     inout
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
static int ParseOptionList(int n_items,
                           t_attribute *attribute,
                           va_list *ap)
{
  int i;
  const char *item;

  for(i=0; i < n_items; i++)
  {
    item = va_arg(*ap, const char *);

    ParseOption(attribute, item);
  }

  return 0;
}

 /*@@
   @routine    InitialiseOptionList
   @date       Thu Jan 27 20:36:54 2000
   @author     Tom Goodale
   @desc
   Initialises the miscellaneous option list for a schedule group.
   @enddesc
   @calls

   @var     attribute
   @vdesc   option attribute
   @vtype   t_attribute *
   @vio     out
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
static int InitialiseOptionList(t_attribute *attribute)
{
  attribute->FunctionData.meta = 0;
  attribute->FunctionData.meta_early = 0;
  attribute->FunctionData.meta_late = 0;
  attribute->FunctionData.global = 0;
  attribute->FunctionData.global_early = 0;
  attribute->FunctionData.global_late = 0;
  attribute->FunctionData.level = 0;
  attribute->FunctionData.singlemap = 0;
  attribute->FunctionData.local = 0;

  attribute->FunctionData.loop_meta = 0;
  attribute->FunctionData.loop_global = 0;
  attribute->FunctionData.loop_level = 0;
  attribute->FunctionData.loop_singlemap = 0;
  attribute->FunctionData.loop_local = 0;

  return 0;
}

 /*@@
   @routine    ParseOption
   @date       Thu Jan 27 20:29:36 2000
   @author     Tom Goodale
   @desc
   Parses an individual option to a schedule group.
   @enddesc
   @calls

   @var     attribute
   @vdesc   option attribute
   @vtype   t_attribute *
   @vio     out
   @endvar
   @var     option
   @vdesc   Option
   @vtype   const char *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
static int ParseOption(t_attribute *attribute,
                       const char *option)
{
  if(CCTK_Equals(option, "META"))
  {
    attribute->FunctionData.meta = 1;
  }
  else if(CCTK_Equals(option, "META-EARLY"))
  {
    attribute->FunctionData.meta_early = 1;
  }
  else if(CCTK_Equals(option, "META-LATE"))
  {
    attribute->FunctionData.meta_late = 1;
  }
  else if(CCTK_Equals(option, "GLOBAL"))
  {
    attribute->FunctionData.global = 1;
  }
  else if(CCTK_Equals(option, "GLOBAL-EARLY"))
  {
    attribute->FunctionData.global_early = 1;
  }
  else if(CCTK_Equals(option, "GLOBAL-LATE"))
  {
    attribute->FunctionData.global_late = 1;
  }
  else if(CCTK_Equals(option, "LEVEL"))
  {
    attribute->FunctionData.level = 1;
  }
  else if(CCTK_Equals(option, "SINGLEMAP"))
  {
    attribute->FunctionData.singlemap = 1;
  }
  else if(CCTK_Equals(option, "LOCAL"))
  {
    attribute->FunctionData.local = 1;
  }
  else if(CCTK_Equals(option, "LOOP-META"))
  {
    attribute->FunctionData.loop_meta = 1;
  }
  else if(CCTK_Equals(option, "LOOP-GLOBAL"))
  {
    attribute->FunctionData.loop_global = 1;
  }
  else if(CCTK_Equals(option, "LOOP-LEVEL"))
  {
    attribute->FunctionData.loop_level = 1;
  }
  else if(CCTK_Equals(option, "LOOP-SINGLEMAP"))
  {
    attribute->FunctionData.loop_singlemap = 1;
  }
  else if(CCTK_Equals(option, "LOOP-LOCAL"))
  {
    attribute->FunctionData.loop_local = 1;
  }
  else
  {
    CCTK_VWarn(1,__LINE__,__FILE__,"Cactus",
               "ParseOption: Unknown option \"%s\" for schedule item %s::%s (scheduled at %s)",
               option,
               attribute->FunctionData.thorn, attribute->FunctionData.routine,
               attribute->FunctionData.where);
  }

  return 0;
}


 /*@@
   @routine    ParseTagsTable
   @date       2006-08-03
   @author     Erik Schnetter
   @desc
   Extract the tags table in a schedule group definition.
   @enddesc
   @calls

   @var     attribute
   @vdesc   attribute list
   @vtype   t_attribute *
   @vio     inout
   @endvar
   @var     ap
   @vdesc   argument list
   @vtype   va_list of const char *
   @vio     inout
   @endvar

   @returntype int
   @returndesc
   0 - success
   @endreturndesc
@@*/
static int ParseTagsTable(t_attribute *attribute,
                          va_list *ap)
{
  const char *item;
  int table;

  item = va_arg(*ap, const char *);
  table = Util_TableCreateFromString (item);
  if (table < 0)
  {
    return table;
  }
  attribute->FunctionData.tags = table;

  return 0;
}


/*@@
   @routine    CreateTypedModifier
   @date       Fri Sep 17 21:50:59 1999
   @author     Tom Goodale
   @desc
   Adds the next n_items items from the variable argument list
   onto the modifer.
   @enddesc
   @calls

   @var     modifier
   @vdesc   base schedule modifier
   @vtype   t_sched_modifier
   @vio     inout
   @vcomment
   This is a list which gets expanded by this function
   @endvar
   @var     type
   @vdesc   modifier type
   @vtype   const char *
   @vio     in
   @vcomment
   before, after, while, if
   @endvar
   @var     n_items
   @vdesc   Number of items on list
   @vtype   int
   @vio     in
   @endvar
   @var     ap
   @vdesc   argument list
   @vtype   va_list of const char *
   @vio     inout
   @endvar

   @returntype t_sched_modifier *
   @returndesc
   modifier list
   @endreturndesc

@@*/
static t_sched_modifier *CreateTypedModifier(t_sched_modifier *modifier,
                                             const char *type,
                                             int n_items,
                                             va_list *ap)
{
  int i;
  const char *item;

  for(i=0; i < n_items; i++)
  {
    item = va_arg(*ap, const char *);

    modifier = CCTKi_ScheduleAddModifier(modifier, type, item);
  }

  return modifier;
}

/*@@
   @routine    TranslateFunctionType
   @date       Mon Jan 24 16:52:06 2000
   @author     Tom Goodale
   @desc
   Translates a string saying what schedule point
   a function is registered at into the appropriate
   function type.
   @enddesc
   @calls

   @var     where
   @vdesc   schedule point
   @vtype   const char *
   @vio     in
   @endvar

   @returntype cFunctionType
   @returndesc
   The function type
   @endreturndesc
@@*/
static cFunctionType TranslateFunctionType(const char *where)
{
  cFunctionType retcode;

  int special;
  const char *current;

  special = 0;

  /* Special entry points have $ in them */
  for(current=where; *current; current++)
  {
    if(*current == '$')
    {
      special = 1;
      break;
    }
  }

  if(special)
  {
    retcode = FunctionOneArg;
  }
  else if(CCTK_Equals(where, "CCTK_STARTUP"))
  {
    retcode = FunctionNoArgs;
  }
  else if(CCTK_Equals(where, "CCTK_SHUTDOWN"))
  {
    retcode = FunctionNoArgs;
  }
  else
  {
    retcode = FunctionStandard;
  }

  return retcode;
}

/*@@
   @routine    SchedulePrint
   @date       Sun Sep 19 13:31:23 1999
   @author     Tom Goodale
   @desc
   Traverses the schedule data for a particular entry point and
   prints out the data.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   return code of DoScheduleTraverse or
   0 - where is NULL
   @endreturndesc
@@*/
static int SchedulePrint(const char *where)
{
  int retcode;
  t_sched_data data;

  data.GH = NULL;
  data.schedule_bin = where;
  data.schedpoint = schedpoint_misc;

  if(where)
  {
    retcode = CCTKi_DoScheduleTraverse(where,
       (int (*)(void *, void *))                    CCTKi_SchedulePrintEntry,
       (int (*)(void *, void *))                    CCTKi_SchedulePrintExit,
       (int (*)(int, char **, void *, void *, int)) CCTKi_SchedulePrintWhile,
       (int (*)(int, char **, void *, void *))      CCTKi_SchedulePrintIf,
       (int (*)(void *, void *, void *))            CCTKi_SchedulePrintFunction,
       (void *)&data);
  }
  else
  {
    retcode = 0;
  }

  return retcode;
}

/*@@
   @routine    SchedulePrintTimes
   @date       Fri Oct 22 12:35:06 1999
   @author     Tom Goodale
   @desc
   Prints the times for a particular schedule entry point.
   @enddesc
   @calls

   @var     where
   @vdesc   Schedule point
   @vtype   const char *
   @vio     in
   @endvar
   @var     data
   @vdesc   schedule data
   @vtype   t_sched_data
   @vio     in
   @endvar
   @var     file
   @vdesc   Output file, must be open for writing
   @vtype   FILE *
   @vio     in
   @endvar

   @returntype int
   @returndesc
   return code of DoScheduleTraverse or
   0 - where is NULL
   @endreturndesc
@@*/
static int SchedulePrintTimes(const char *where, t_sched_data *data)
{
  int i;
  int retcode;
  char *description;

  if(where)
  {
    data->schedule_bin = where;
    memset (data->total_time->vals, 0,
            data->total_time->n_vals * sizeof (data->total_time->vals[0]));

    retcode = CCTKi_DoScheduleTraverse(where, NULL, NULL, NULL, NULL,
       (int (*)(void *, void *, void *)) CCTKi_ScheduleResetTimerOutputFlag,
       NULL);

    data->n_functions = 0;
    retcode = CCTKi_DoScheduleTraverse(where, NULL, NULL, NULL, NULL,
       (int (*)(void *, void *, void *)) CCTKi_SchedulePrintTimesFunction,
       (void *)data);

    /* print total time for this schedule bin
       if any routines have been called in it */
    if (retcode >= 0 && data->n_functions > 0)
    {
      for (i = 0; i < data->total_time->n_vals; i++)
      {
        data->total_time->vals[i].type = data->info->vals[i].type;
        data->total_time->vals[i].units = data->info->vals[i].units;
        data->total_time->vals[i].heading = data->info->vals[i].heading;
      }
      description = malloc (strlen (where) + 16);
      sprintf (description, "Total time for %s", where);
      CCTKi_SchedulePrintTimerInfo(data->total_time, NULL, "", description,
                                   data->file);
      free (description);
    }
  }
  else
  {
    retcode = 0;
  }

  return retcode;
}

/********************************************************************
 *********************     Printing Routines   **********************
 ********************************************************************/


 /*@@
   @routine    CCTKi_SchedulePrintEntry
   @date       Sun Sep 19 13:31:23 1999
   @author     Tom Goodale
   @desc
   Routine called on entry to a group when traversing for printing.
   @enddesc
   @calls

   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - schedule item is inactive
   1 - schedule item is active
   @endreturndesc
@@*/
static int CCTKi_SchedulePrintEntry(t_attribute *attribute,
                                    t_sched_data *data)
{
  /* prevent compiler warnings about unused parameters */
  data = data;

  indent_level += 2;

  if (attribute && attribute->type == sched_group)
  {
    printf("%*s %s: %s\n", indent_level + 5, "GROUP",
           attribute->FunctionData.routine,attribute->description);
  }

  return 1;
}

/*@@
   @routine    CCTKi_SchedulePrintExit
   @date       Sun Sep 19 13:31:23 1999
   @author     Tom Goodale
   @desc
   Routine called on exit to a group when traversing for printing.
   @enddesc
   @calls

   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   1 - this has no meaning
   @endreturndesc
@@*/
static int CCTKi_SchedulePrintExit(t_attribute *attribute,
                                   t_sched_data *data)
{
  /* prevent compiler warnings about unused parameters */
  data       = data;
  attribute  = attribute;

  indent_level -= 2;

  return 1;
}

/*@@
   @routine    CCTKi_SchedulePrintWhile
   @date       Sun Sep 19 13:31:23 1999
   @author     Tom Goodale
   @desc
   Routine called for while of a group when traversing for printing.
   @enddesc
   @calls

   @var     n_whiles
   @vdesc   number of while statements
   @vtype   int
   @vio     in
   @endvar
   @var     whiles
   @vdesc   while statements
   @vtype   char **
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar
   @var     first
   @vdesc   flag - is this the first time we are checking while on this schedule item
   @vtype   int
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - schedule item is inactive
   1 - schedule item is active
   @endreturndesc
@@*/
static int CCTKi_SchedulePrintWhile(int n_whiles,
                                    char **whiles,
                                    t_attribute *attribute,
                                    t_sched_data *data,
                                    int first)
{
  int i;

  /* prevent compiler warnings about unused parameters */
  attribute = attribute;
  data = data;

  if(first)
  {
    printf("%*s", indent_level + 2 + 7, "while (");

    for(i = 0; i < n_whiles; i++)
    {
      if(i > 0)
      {
        printf(" && ");
      }

      printf("%s", whiles[i]);
    }
    printf(")\n");
    indent_level += 2;
  }
  else
  {
    indent_level -= 2;
    printf("%*s\n", indent_level + 9, "end while");
  }

  return first;
}

/*@@
   @routine    CCTKi_SchedulePrintIf
   @date       Dec 27, 2005
   @author     Erik Schnetter
   @desc
   Routine called for if of a group when traversing for printing.
   @enddesc
   @calls

   @var     n_ifs
   @vdesc   number of if statements
   @vtype   int
   @vio     in
   @endvar
   @var     ifs
   @vdesc   if statements
   @vtype   char **
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - schedule item is inactive
   1 - schedule item is active
   @endreturndesc
@@*/
static int CCTKi_SchedulePrintIf(int n_ifs,
                                 char **ifs,
                                 t_attribute *attribute,
                                 t_sched_data *data)
{
  int i;

  /* prevent compiler warnings about unused parameters */
  attribute = attribute;
  data = data;

  printf("%*s", indent_level + 2 + 7, "if (");

  for(i = 0; i < n_ifs; i++)
  {
    if(i > 0)
    {
      printf(" && ");
    }

    printf("%s", ifs[i]);
  }
  printf(")\n");

  return 1;
}

/*@@
   @routine    CCTKi_SchedulePrintFunction
   @date       Sun Sep 19 13:36:25 1999
   @author     Tom Goodale
   @desc
   Function which actually prints out data about a group or a function.
   @enddesc
   @calls

   @var     function
   @vdesc   the function to be called
   @vtype   void *
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   1 - this has no meaning
   @endreturndesc
@@*/
static int CCTKi_SchedulePrintFunction(void *function,
                                       t_attribute *attribute,
                                       t_sched_data *data)
{
  const char* mode;
  const char* loop_mode;
  /* prevent compiler warnings about unused parameters */
  function = function;
  data = data;

  if (attribute->FunctionData.meta ||
      attribute->FunctionData.meta_early ||
      attribute->FunctionData.meta_late)
  {
    mode = "[meta] ";
  }
  else if (attribute->FunctionData.global ||
           attribute->FunctionData.global_early ||
           attribute->FunctionData.global_late)
  {
    mode = "[global] ";
  }
  else if (attribute->FunctionData.level)
  {
    mode = "[level] ";
  }
  else if (attribute->FunctionData.singlemap)
  {
    mode = "[singlemap] ";
  }
  else if (attribute->FunctionData.local)
  {
    mode = "[local] ";
  }
  else
  {
    mode = "";
  }

  if (attribute->FunctionData.loop_meta)
  {
    loop_mode = "[loop-meta] ";
  }
  else if (attribute->FunctionData.loop_global)
  {
    loop_mode = "[loop-global] ";
  }
  else if (attribute->FunctionData.loop_level)
  {
    loop_mode = "[loop-level] ";
  }
  else if (attribute->FunctionData.loop_singlemap)
  {
    loop_mode = "[loop-singlemap] ";
  }
  else if (attribute->FunctionData.loop_local)
  {
    loop_mode = "[loop-local] ";
  }
  else
  {
    loop_mode = "";
  }

  if (indent_level > 0)
  {
    printf ("%*s", indent_level, " ");
  }
  printf("%s::%s: %s%s%s\n",
         attribute->FunctionData.thorn, attribute->FunctionData.routine,
         mode, loop_mode,
         attribute->description);

  return 1;
}


/********************************************************************
 *********************     Calling Routines   ***********************
 ********************************************************************/


 /*@@
   @routine    CCTKi_ScheduleCallEntry
   @date       Sun Sep 19 13:24:06 1999
   @author     Tom Goodale
   @desc
   Routine called when a schedule group is entered.
   @enddesc
   @calls

   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - schedule item is inactive
   1 - schedule item is active
   @endreturndesc
@@*/
static int CCTKi_ScheduleCallEntry(t_attribute *attribute,
                                   t_sched_data *data)
{
  int i;
  int indx;
  int last;
  int go;

  if(attribute)
  {
    if(data->schedpoint == schedpoint_analysis)
    {
      /* In analysis, so check triggers */
      if(attribute->FunctionData.n_TriggerGroups == 0)
      {
        go = 1;
      }
      else
      {
        /* Check if it is now being triggered */
        go = 0;
        for (i = 0; i < attribute->FunctionData.n_TriggerGroups ; i++)
        {
          indx = CCTK_FirstVarIndexI(attribute->FunctionData.TriggerGroups[i]);
          last  = indx + CCTK_NumVarsInGroupI(attribute->FunctionData.TriggerGroups[i]) -1;
          for(; indx <= last ; indx++)
          {
            go = go || CCTKi_TriggerSaysGo(data->GH, indx);
          }
        }
      }
    }
    else
    {
      go = 1;
    }

    if(go)
    {
      /* Switch on storage for groups */
/*       for(i = 0; i < attribute->n_mem_groups; i++) */
/*       { */
/*         attribute->StorageOnEntry[i] = CCTK_EnableGroupStorageI(data->GH,attribute->mem_groups[i]); */
/*       } */

      if(attribute->n_mem_groups > 0)
      {
        CCTK_GroupStorageIncrease(data->GH,
                                  attribute->n_mem_groups,
                                  attribute->mem_groups,
                                  attribute->timelevels,
                                  attribute->StorageOnEntry);
      }

      /* Switch on communication for groups. */
      for(i = 0; i < attribute->n_comm_groups; i++)
      {
        attribute->CommOnEntry[i] = CCTK_EnableGroupCommI(data->GH,attribute->comm_groups[i]);
      }
    }

    /* Initialise the synchronised flag. */
    attribute->synchronised = 0;

    /* Remember if we have switched on storage and comm or not. */
    attribute->done_entry = go;
  }
  else
  {
    go = 1;
  }

  return go;
}

/*@@
   @routine    CCTKi_ScheduleCallExit
   @date       Sun Sep 19 13:25:24 1999
   @author     Tom Goodale
   @desc
   Routine called on exit from a schedule group.
   @enddesc
   @calls

   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   1 - this has no meaning
   @endreturndesc
@@*/
static int CCTKi_ScheduleCallExit(t_attribute *attribute,
                                  t_sched_data *data)
{
  int i;
  int vindex;
  int last;

  /* Only do this if the entry routine did stuff. */
  if(attribute && attribute->done_entry)
  {

    /* Synchronise variable groups associated with this schedule group. */
    if(attribute->FunctionData.n_SyncGroups > 0 && ! attribute->synchronised)
    {
      CCTK_SyncGroupsI(data->GH,
                       attribute->FunctionData.n_SyncGroups,
                       attribute->FunctionData.SyncGroups);
      attribute->synchronised = 0;
    }

    if(data->schedpoint == schedpoint_analysis)
    {
      /* In analysis, so do any trigger actions. */
      for (i = 0; i < attribute->FunctionData.n_TriggerGroups ; i++)
      {
        vindex = CCTK_FirstVarIndexI(attribute->FunctionData.TriggerGroups[i]);
        last  = vindex + CCTK_NumVarsInGroupI(attribute->FunctionData.TriggerGroups[i]) - 1;
        for(; vindex <= last ; vindex++)
        {
          CCTKi_TriggerAction(data->GH, vindex);
        }
      }
    }

    /* Switch off communication if it was done in entry. */
    for(i = 0; i < attribute->n_comm_groups; i++)
    {
      if(!attribute->CommOnEntry[i])
      {
        CCTK_DisableGroupCommI(data->GH,attribute->comm_groups[i]);
      }
    }

    /* Switch off storage if it was switched on in entry. */
/*     for(i = 0; i < attribute->n_mem_groups; i++) */
/*     { */
/*       if(!attribute->StorageOnEntry[i]) */
/*       { */
/*         CCTK_DisableGroupStorageI(data->GH,attribute->mem_groups[i]); */
/*       } */
/*     } */

    if(attribute->n_mem_groups > 0)
    {
      CCTK_GroupStorageDecrease(data->GH,
                                attribute->n_mem_groups,
                                attribute->mem_groups,
                                attribute->StorageOnEntry,
                                NULL);
    }
  }

  return 1;
}

/*@@
   @routine    CCTKi_ScheduleCallWhile
   @date       Sun Sep 19 13:27:53 1999
   @author     Tom Goodale
   @desc
   Routine called to check variables to see if a group or function should be executed.
   @enddesc
   @calls

   @var     n_whiles
   @vdesc   number of while statements
   @vtype   int
   @vio     in
   @endvar
   @var     whiles
   @vdesc   while statements
   @vtype   char **
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar
   @var     first
   @vdesc   flag - is this the first time we are checking while on this schedule item
   @vtype   int
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - schedule item is inactive
   1 - schedule item is active
   @endreturndesc
@@*/
static int CCTKi_ScheduleCallWhile(int n_whiles,
                                   char **whiles,
                                   t_attribute *attribute,
                                   t_sched_data *data,
                                   int first)
{
  int i;
  int retcode;

  /* prevent compiler warnings about unused parameters */
  attribute = attribute;
  first = first;

  retcode = 1;

  /* FIXME - should do a lot of validation either here or on registration */
  for(i = 0; i < n_whiles; i++)
  {
    retcode = retcode && *((CCTK_INT *)CCTK_VarDataPtr(data->GH, 0, whiles[i]));
  }

  return retcode;
}

/*@@
   @routine    CCTKi_ScheduleCallIf
   @date       Dec 2007, 2005
   @author     Erik Schnetter
   @desc
   Routine called to check variables to see if a group or function should be executed.
   @enddesc
   @calls

   @var     n_ifs
   @vdesc   number of if statements
   @vtype   int
   @vio     in
   @endvar
   @var     ifs
   @vdesc   if statements
   @vtype   char **
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   0 - schedule item is inactive
   1 - schedule item is active
   @endreturndesc
@@*/
static int CCTKi_ScheduleCallIf(int n_ifs,
                                char **ifs,
                                t_attribute *attribute,
                                t_sched_data *data)
{
  int i;
  int retcode;

  /* prevent compiler warnings about unused parameters */
  attribute = attribute;

  retcode = 1;

  /* FIXME - should do a lot of validation either here or on registration */
  for(i = 0; i < n_ifs; i++)
  {
    retcode = retcode && *((CCTK_INT *)CCTK_VarDataPtr(data->GH, 0, ifs[i]));
  }

  return retcode;
}

/*@@
   @routine    CCTKi_ScheduleCallFunction
   @date       Sun Sep 19 13:29:14 1999
   @author     Tom Goodale
   @desc
   The routine which actually calls a function.
   @enddesc
   @calls

   @var     function
   @vdesc   the function to be called
   @vtype   void *
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   1 - this has no meaning
   @endreturndesc
@@*/
static int CCTKi_ScheduleCallFunction(void *function,
                                      t_attribute *attribute,
                                      t_sched_data *data)
{
  /* find the timer for this function and this schedule bin */
  t_timer *timer = attribute->timers;
  while (timer && strcmp(timer->schedule_bin, data->schedule_bin))
  {
    timer = timer->next;
  }

  /* create the timer if it doesn't exist yet */
  if (! timer)
  {
    /* build a unique name for this timer */
    const char *thorn = attribute->FunctionData.thorn;
    const char *name = attribute->FunctionData.routine;
    const char *where = data->schedule_bin;
    char *timername = malloc (strlen (thorn) + strlen (name) +
                              strlen (where) + 20);
    if (! timername)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, "Cactus",
                  "Could not allocate memory for timer name");
    }
    else
    {
      static int timernum = 0;
      sprintf (timername, "[%04d] %s: %s in %s", timernum++, thorn, name,where);
      const int timer_handle = CCTK_TimerCreate(timername);
      if (timer_handle < 0)
      {
        CCTK_VWarn (1, __LINE__, __FILE__, "Cactus",
                    "Could not create timer with name '%s'", timername);
      }
      else
      {
        timer = malloc (sizeof (*timer));
        if (! timer)
        {
          CCTK_VWarn (1, __LINE__, __FILE__, "Cactus",
                      "Could not allocate memory for timer with name '%s'",
                      timername);
          CCTK_TimerDestroy (timername);
        }
        else
        {
          timer->timer_handle = timer_handle;
          timer->schedule_bin = strdup (where);
          timer->next = attribute->timers;
          attribute->timers = timer;
        }
      }
      free (timername);
    }
  }

  if (timer)
  {
    CCTK_TimerStartI(timer->timer_handle);
  }

  /* Use whatever has been chosen as the calling function for this
   * function.
   */
  attribute->synchronised = data->CallFunction(function, &(attribute->FunctionData), data->GH);

  if (timer)
  {
    CCTK_TimerStopI(timer->timer_handle);
  }

  return 1;
}

/********************************************************************
 ****************     Timer Printing Routines   *********************
 ********************************************************************/

/*@@
   @routine    CCTKi_SchedulePrintTimesFunction
   @date       Fri Oct 22 12:26:26 1999
   @author     Tom Goodale
   @desc
   Function which actually prints out data about a group or a function.
   @enddesc
   @calls

   @var     function
   @vdesc   the function to be called
   @vtype   void *
   @vio     in
   @endvar
   @var     attribute
   @vdesc   schedule item attributes
   @vtype   t_attribute *
   @vio     in
   @endvar
   @var     data
   @vdesc   data associated with schedule item
   @vtype   t_sched_data
   @vio     in
   @endvar

   @returntype int
   @returndesc
   1 - this has no meaning
   @endreturndesc
@@*/
static int CCTKi_SchedulePrintTimesFunction(void *function,
                                            t_attribute *attribute,
                                            t_sched_data *data)
{
  /* prevent compiler warnings about unused parameters */
  function = function;

  /* find the timer for this function and this schedule bin */
  t_timer *timer = attribute->timers;
  while (timer && strcmp(timer->schedule_bin, data->schedule_bin))
  {
    timer = timer->next;
  }
  if (timer && ! timer->has_been_output)
  {
    timer->has_been_output = 1;
    CCTK_TimerI(timer->timer_handle, data->info);

    if(data->print_headers)
    {
      CCTKi_SchedulePrintTimerHeaders(data->info, data->file);

      data->print_headers = 0;
    }

    CCTKi_SchedulePrintTimerInfo(data->info, data->total_time,
                                 attribute->FunctionData.thorn,
                                 attribute->description,
                                 data->file);
    data->n_functions++;
  }

  return 1;
}

static void CCTKi_SchedulePrintTimerInfo(cTimerData *timer,
                                         cTimerData *total_time,
                                         const char *where,
                                         const char *description,
                                         FILE *file)
{
  int i, j;


  /* print delimiter line */
  if (*where == 0)
  {
    PrintDelimiterLine ('-', timer, file);
  }

  /* print the timer description */
  fprintf (file, "%-16.16s| %-40.40s", where, description);

  /* print the actual timer values */
  for (i = 0; i < timer->n_vals; i++)
  {
    j = strlen (timer->vals[i].heading) + strlen (timer->vals[i].units) + 3;

    fprintf (file, "| %*.8f ", j, timer->vals[i].seconds);
    if (total_time)
    {
      total_time->vals[i].seconds += timer->vals[i].seconds;
    }
  }

  fprintf (file, "\n");

  /* print delimiter line */
  if (*where == 0)
  {
    PrintDelimiterLine ('=', timer, file);
  }
}


static void CCTKi_SchedulePrintTimerHeaders (cTimerData *timer, FILE *file)
{
  int i;


  PrintDelimiterLine ('=', timer, file);

  fprintf (file,
           "%-16.16s| %-40.40s", "Thorn", "Scheduled routine in time bin");
  for (i = 0; i < timer->n_vals; i++)
  {
    fprintf (file, "| %s [%s] ", timer->vals[i].heading, timer->vals[i].units);
  }
  fprintf (file, "\n");

  PrintDelimiterLine ('=', timer, file);
}


static int CCTKi_ScheduleResetTimerOutputFlag(void *function,
                                              t_attribute *attribute,
                                              t_sched_data *data)
{
  /* prevent compiler warnings about unused parameters */
  function = function;
  data = data;

  /* find the timer for this function and this schedule bin */
  t_timer *timer = attribute->timers;
  while (timer)
  {
    timer->has_been_output = 0;
    timer = timer->next;
  }

  return 1;
}


static void PrintDelimiterLine (char delimiter,
                                const cTimerData *timer,
                                FILE *file)
{
  int i, len;


  len = 58;
  for (i = 0; i < timer->n_vals; i++)
  {
    len += strlen (timer->vals[i].heading) + strlen (timer->vals[i].units) + 6;
  }
  for (i = 0; i < len; i++)
  {
    fprintf (file, "%c", delimiter);
  }
  fprintf (file, "\n");
}