summaryrefslogtreecommitdiff
path: root/lib/sbin/CreateScheduleBindings.pl
blob: 18657189b1c07f02fa45f8fd218f2ed50483c44e (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
#/*@@
#  @file      CreateScheduleBindings.pl
#  @date      Thu Sep 16 23:30:21 1999
#  @author    Tom Goodale
#  @desc
#             New schedule stuff.  Should be renamed !!!
#  @enddesc
#  @version   $Header$
#@@*/

#/*@@
#  @routine    CreateScheduleBindings
#  @date       Fri Sep 17 14:16:23 1999
#  @author     Tom Goodale
#  @desc
#              Creates the schedule bindings.
#  @enddesc
#@@*/
sub CreateScheduleBindings
{
  my($bindings_dir, $rhinterface_db, $rhschedule_db) = @_;
  my($start_dir);
  my($thorn);
  my($file_list);
  my($rsbuffer);

  if(! -d $bindings_dir)
  {
    mkdir($bindings_dir, 0755) || die "Unable to create $bindings_dir";
  }
  $start_dir = `pwd`;

  chdir $bindings_dir;

  if(! -d 'Schedule')
  {
    mkdir('Schedule', 0755) || die 'Unable to create Schedule directory';
  }

  if(! -d 'include')
  {
    mkdir('include', 0755) || die 'Unable to create include directory';
  }

  chdir 'Schedule';

  $file_list = '';

  foreach $thorn (sort split(" ", $rhinterface_db->{"THORNS"}))
  {
    $rsbuffer = &ScheduleCreateFile($thorn, $rhinterface_db, $rhschedule_db);
    &WriteFile("Schedule$thorn.c",\$rsbuffer);
    $file_list .= " Schedule$thorn.c";

    $rsbuffer = &ScheduleCreateInterfaceFile($thorn, $rhinterface_db, $rhschedule_db);
    &WriteFile("../include/${thorn}/cctk_ScheduleFunctions.h",\$rsbuffer);
    if($thorn eq "Cactus") {
        &WriteFile("../include/CactusBindings/cctk_ScheduleFunctions.h",\$rsbuffer);
    }
  }

  $rsbuffer = &ScheduleCreateBindings($rhinterface_db, $rhschedule_db);
  &WriteFile("BindingsSchedule.c", \$rsbuffer);
  $file_list .= " BindingsSchedule.c";

  $rsbuffer = &ParameterRecoveryCreateBindings($rhinterface_db, $rhschedule_db);
  &WriteFile("BindingsParameterRecovery.c", \$rsbuffer);
  $file_list .= " BindingsParameterRecovery.c";

  $line = "SRCS = $file_list\n";
  &WriteFile("make.code.defn", \$line);

  chdir "$start_dir";
}

#/*@@
#  @routine    ScheduleCreateInterfaceFile
#  @date       2006-05-11
#  @author     Erik Schnetter
#  @desc
#              Create a string containing all the data which should go
#              into a schedule interface file.
#  @enddesc
#@@*/
sub ScheduleCreateInterfaceFile
{
  my ($thorn, $rhinterface_db, $rhschedule_db) = @_;
  
  # Map groups to one of their block numbers (groups may have several
  # block numbers)
  my %group_block = ();
  
  for (my $block = 0;
       $block < $rhschedule_db->{"\U$thorn\E N_BLOCKS"};
       ++ $block)
  {
      if ($rhschedule_db->{"\U$thorn\E BLOCK_$block TYPE"} eq "GROUP")
      {
          my $group = $rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"};
          $group_block{$group} = $block;
      }
  }
  
  
  
  # Process each schedule block
  
  my @data = ();
  push (@data, "#include \"$thorn/cctk_Arguments.h\"");
  
  for (my $block = 0;
       $block < $rhschedule_db->{"\U$thorn\E N_BLOCKS"};
       ++ $block)
  {
      if ($rhschedule_db->{"\U$thorn\E BLOCK_$block TYPE"} eq "FUNCTION")
      {
          my ($language, $function);
          if ($rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"} =~ m:^\s*C\s*$:i)
          {
              $language = 'C';
              $function = $rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"};
          }
          elsif ($rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"} =~ m:^\s*(F|F77|FORTRAN|F90)\s*$:i)
          {
              $language = 'Fortran';
              $function = "CCTK_FNAME(".$rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"} .")";
          }
          
          my $where = $rhschedule_db->{"\U$thorn\E BLOCK_$block WHERE"};
          # Find one outermost enclosing group iteratively (there may
          # be serveral outermost enclosing groups)
          my %been_there;       # avoid cycles
          while (! $been_there{$where} && defined $group_block{$where})
          {
              $been_there{$where} = defined;
              my $block1 = $group_block{$where};
              $where = $rhschedule_db->{"\U$thorn\E BLOCK_$block1 WHERE"};
          }
          my $is_special =
              $where eq 'CCTK_STARTUP' ||
              $where eq 'CCTK_RECOVER_PARAMETERS' ||
              $where eq 'CCTK_SHUTDOWN';
          
          push (@data, '');
          if ($language eq 'C')
          {
              push (@data, '#ifdef CCODE');
              push (@data, '#ifdef __cplusplus');
              push (@data, 'extern "C"');
              push (@data, '#endif');
              if ($is_special)
              {
                  push (@data, "int $function (void);")
              }
              else
              {
                  push (@data, "void $function (CCTK_ARGUMENTS);")
              }
              push (@data, '#endif /* CCODE */');
          }
          elsif ($language eq 'Fortran')
          {
# Can't use CCTK_FNAME in header files (which is implicit in $function)
#              push (@data, '#ifdef CCODE');
#              push (@data, '#ifdef __cplusplus');
#              push (@data, 'extern "C"');
#              push (@data, '#endif');
#              if ($is_special)
#              {
#                  push (@data, "CCTK_FCALL int $function (void);")
#              }
#              else
#              {
#                  push (@data, "CCTK_FCALL void $function (CCTK_FARGUMENTS);")
#              }
#              push (@data, '#endif /* CCODE */');
# Can't declare functions in Fortran at file scope
#              push (@data, '#ifdef FCODE');
#              push (@data, '#ifdef F90CODE');
#              push (@data, '      interface');
#              if ($is_special)
#              {
#                  push (@data, "      integer function $function ()");
#                  push (@data, '         implicit none');
#                  push (@data, "      end function $function");
#              }
#              else
#              {
#                  push (@data, "      subroutine $function (CCTK_ARGUMENTS)");
#                  push (@data, '         implicit none');
#                  push (@data, '         DECLARE_CCTK_ARGUMENTS');
#                  push (@data, "      end subroutine $function");
#              }
#              push (@data, '      end interface');
#              push (@data, '#else /* ! F90CODE */');
#              push (@data, "      external $function");
#              if ($is_special)
#              {
#                  push (@data, "      integer $function");
#              }
#              push (@data, '#endif /* F90CODE */');
#              push (@data, '#endif /* FCODE */');
          }
          
      }
  }
  
  push(@data, "\n");   # workaround for perl 5.004_04 to add a trailing newline
  
  return join ("\n", @data);
}



#/*@@
#  @routine    ScheduleCreateFile
#  @date       Fri Sep 17 17:34:26 1999
#  @author     Tom Goodale
#  @desc
#              Creates a string containing all the data
#              which should go into a schedule file.
#  @enddesc
#@@*/
sub ScheduleCreateFile
{
  my($thorn, $rhinterface_db, $rhschedule_db) = @_;

  my($implementation);
  my($buffer, $prototypes);
  my($recovery_buffer, $recovery_prototypes);
  my($block, $block_buffer, $block_prototype);
  my($statement, $statement_buffer, $statement_prototype);
  my(@data);

  $implementation = $rhinterface_db->{"\U$thorn\E IMPLEMENTS"};

  $buffer = $recovery_buffer = $rhschedule_db->{"\U$thorn\E FILE"};

  # Process each schedule block
  for($block = 0 ; $block < $rhschedule_db->{"\U$thorn\E N_BLOCKS"}; $block++)
  {
    if ($rhschedule_db->{"\U$thorn\E BLOCK_$block WHERE"} !~ /RECOVER_PARAMETERS/)
    {
      ($block_buffer, $block_prototype) = &ScheduleBlock($thorn, $implementation, $block,
                                                         $rhinterface_db, $rhschedule_db);
      $buffer =~ s:\@BLOCK\@$block:$block_buffer:;
      $recovery_buffer =~ s:\@BLOCK\@$block::;
      $prototypes .= "$block_prototype";
    }
    else
    {
      $block_buffer = $rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"};
      if($rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"} !~ m:^\s*C\s*$:i )
      {
        $block_buffer = "CCTK_FNAME ($block_buffer)";
      }

      $recovery_buffer =~ s:\@BLOCK\@$block:result =  $block_buffer();:;
      $buffer =~ s:\@BLOCK\@$block::;
      $recovery_prototypes .= "extern int $block_buffer(void);\n";
    }
  }

  # Process each schedule statement
  for($statement = 0 ; $statement < $rhschedule_db->{"\U$thorn\E N_STATEMENTS"}; $statement++)
  {
    ($statement_buffer, $statement_prototype) = &ScheduleStatement($thorn, $implementation, $statement,
                                                                   $rhinterface_db, $rhschedule_db);
    $buffer =~ s:\@STATEMENT\@$statement:$statement_buffer:;
    $recovery_buffer =~ s:\@STATEMENT\@$statement::;

    $prototypes .= $statement_prototype;
  }

  # Actually create the file contents string
  @data = ();
  push(@data, '/*@@');
  push(@data, "   \@file       Schedule$thorn.c");
  push(@data, '   @author     Automatically generated by CreateScheduleBindings.pl');
  push(@data, '   @desc');
  push(@data, '               Creates the schedule and parameter recovery bindings ');
  push(@data, "               for thorn $thorn");
  push(@data, '   @enddesc');
  push(@data, '@@*/');
  push(@data, '');
  #push(@data, "#define THORN_IS_$thorn");
  #push(@data, "#define CCTK_THORNSTRING \"$thorn\"");
  push(@data, '');
  push(@data, "#include \"$thorn/cctk.h\"");
  push(@data, "#include \"$thorn/CParameters.h\"");
  push(@data, '#include "cctki_ScheduleBindings.h"');
  push(@data, "#include \"$thorn/cctk_ScheduleFunctions.h\"");
  push(@data, '');
  #push(@data, '/* prototypes for schedule bindings functions to be registered */');
  #push(@data, '/* Note that this is a cheat, we just need a function pointer. */');
  #push(@data, $prototypes);
  #push(@data, '');
  push(@data, '/* Prototypes for Fortran schedule bindings functions to be registered */');
  push(@data, '/* Note that this is a cheat, we just need a function pointer. */');
  push(@data, join "\n", (grep /CCTK_FNAME/, (split "\n", $prototypes)));
  push(@data, '');
  push(@data, "void CCTKi_BindingsSchedule_$thorn(void);");
  push(@data, "void CCTKi_BindingsSchedule_$thorn(void)");
  push(@data, '{');
  push(@data, '  DECLARE_CCTK_PARAMETERS');

  # filter out empty lines
  foreach $line (split ("\n", $buffer))
  {
    push(@data, $line) if ($line);
  }

  push(@data, '}');

  push(@data, '');
  push(@data, '/*@@');
  push(@data, "  \@routine    CCTKi_BindingsParameterRecovery_$thorn");
  push(@data, '  @author     Automatically generated by CreateScheduleBindings.pl');
  push(@data, '  @desc');
  push(@data, "              Creates the parameter recovery bindings for thorn $thorn");
  push(@data, '  @enddesc');
  push(@data, '@@*/');
  push(@data, '');
  if (defined ($recovery_prototypes))
  {
    push(@data, '/* prototypes of parameter recovery functions to be registered. */');
    push(@data, "$recovery_prototypes");
  }
  push(@data, "int CCTKi_BindingsParameterRecovery_$thorn(void);");
  push(@data, "int CCTKi_BindingsParameterRecovery_$thorn(void)");
  push(@data, '{');
  if (defined ($recovery_prototypes))
  {
    push(@data, '  DECLARE_CCTK_PARAMETERS');
    push(@data, '  int result = 0;');
    push(@data, '');

    # filter out empty lines
    foreach $line (split ("\n", $recovery_buffer))
    {
      push(@data, $line) if ($line);
    }

    push(@data, '');
    push(@data, '  return (result);');
  }
  else
  {
    push(@data, '  /* this thorn doesn\'t define any parameter recovery routines */');
    push(@data, '  return (0);');
  }
  push(@data, '}');
  push(@data, "\n");   # workaround for perl 5.004_04 to add a trailing newline

  return join ("\n", @data);
}


#/*@@
#  @routine    ScheduleCreateBindings
#  @date       Fri Sep 17 18:17:13 1999
#  @author     Tom Goodale
#  @desc
#  Creates a string containing all the data which should go into the master
#  schedule bindings file.
#  @enddesc
#@@*/
sub ScheduleCreateBindings
{
  my($rhinterface_db, $rhschedule_db) = @_;
  my(@data);

  @data = ();
  push(@data, '/*@@');
  push(@data, '   @file    BindingsSchedule.c');
  push(@data, '   @author  Automatically generated by CreateScheduleBindings.pl');
  push(@data, '   @desc');
  push(@data, '            Calls all the thorn schedule bindings file if the thorns are active.');
  push(@data, '   @enddesc');
  push(@data, '  @@*/');
  push(@data, '');

  push(@data, '#include "cctk_ActiveThorns.h"');
  push(@data, '');

  push(@data, '/* Prototypes for functions to be registered. */');
  foreach $thorn (sort split(' ', $rhinterface_db->{"THORNS"}))
  {
    push(@data, "void CCTKi_BindingsSchedule_$thorn(void);");
  }

  push(@data, '');
  push(@data, 'int CCTKi_BindingsScheduleInitialise(void);');
  push(@data, 'int CCTKi_BindingsScheduleInitialise(void)');
  push(@data, '{');
  foreach $thorn (sort split(' ', $rhinterface_db->{"THORNS"}))
  {
    push(@data, "  if(CCTK_IsThornActive(\"$thorn\"))");
    push(@data, '  {');
    push(@data, "    CCTKi_BindingsSchedule_$thorn();");
    push(@data, '  }');
  }
  push(@data, '  return 0;');
  push(@data, '}');
  push(@data, "\n");   # workaround for perl 5.004_04 to add a trailing newline

  return join ("\n", @data);
}


#/*@@
#  @routine    ParameterRecoveryCreateBindings
#  @date       Tue Apr 18 13:17:13 2000
#  @author     Gabrielle Allen
#  @desc
#  Creates a string containing all the data which should go into the master
#  parameter recovery bindings file.
#  @enddesc
#@@*/
sub ParameterRecoveryCreateBindings
{
  my($rhinterface_db, $rhschedule_db) = @_;
  my(@data);

  @data = ();
  push(@data, '/*@@');
  push(@data, '   @file       BindingsParameterRecovery.c');
  push(@data, '   @author     Automatically generated by CreateScheduleBindings.pl');
  push(@data, '   @desc');
  push(@data, '               Calls all the thorn parameter recovery bindings file if the thorns are active.');
  push(@data, '   @enddesc');
  push(@data, '  @@*/');
  push(@data, '');

  push(@data, '#include "cctk_ActiveThorns.h"');
  push(@data, '');
  push(@data, '/* Prototypes for functions to be registered. */');

  foreach $thorn (sort split(' ', $rhinterface_db->{"THORNS"}))
  {
    push(@data, "int CCTKi_BindingsParameterRecovery_$thorn(void);");
  }

  push(@data, 'int CCTKi_BindingsParameterRecoveryInitialise(void);');
  push(@data, '');
  push(@data, 'int CCTKi_BindingsParameterRecoveryInitialise(void)');
  push(@data, '{');
  push(@data, '  int result;');
  push(@data, '  int retval = 0;');
  push(@data, '');
  push(@data, '  do');
  push(@data, '  {');
  foreach $thorn (sort split(' ', $rhinterface_db->{"THORNS"}))
  {
    push(@data, "    if(CCTK_IsThornActive(\"$thorn\"))");
    push(@data, '    {');
    push(@data, "      result = CCTKi_BindingsParameterRecovery_$thorn();");
    push(@data, '      if (result != 0)');
    push(@data, '        retval = result;');
    push(@data, '      if (retval > 0)');
    push(@data, '        break;');
    push(@data, '    }');
  }
  push(@data, '  } while (0);');
  push(@data, '');
  push(@data, '  return retval;');
  push(@data, '}');
  push(@data, "\n");   # workaround for perl 5.004_04 to add a trailing newline

  return join ("\n", @data);
}


#/*@@
#  @routine    ScheduleBlock
#  @date       Fri Sep 17 17:37:59 1999
#  @author     Tom Goodale
#  @desc
#  Creates the code for a given schedule block
#  @enddesc
#@@*/
sub ScheduleBlock
{
  my($thorn, $implementation, $block, $rhinterface_db, $rhschedule_db) = @_;

  my($buffer, $prototype);
  my($indent, $language, $function);
  my($mem_groups);
  my($tlist);
  my($comm_groups);
  my($unused_comm_groups);
  my($trigger_groups);
  my($sync_groups);
  my(@options);
  my($tags);
  my(@before_list);
  my(@after_list);
  my(@writes_list);
  my(@reads_list);
  my(@while_list);
  my(@if_list);

  # Extract group and routine information from the databases
  ($mem_groups, $tlist) = &ScheduleSelectGroups($thorn, $implementation,
                                                $rhschedule_db->{"\U$thorn\E BLOCK_$block STOR"},
                                                $rhinterface_db);

  &ScheduleValidateTimeLevels($thorn, $implementation, $mem_groups,$tlist, $rhinterface_db);


  ($unused_comm_groups) = &ScheduleSelectGroups($thorn, $implementation,
                                                $rhschedule_db->{"\U$thorn\E BLOCK_$block COMM"},
                                                $rhinterface_db);
  if (@$unused_comm_groups)
  {
    print "No need to switch on Communication in $thorn\n";
    print "Communication automatically assigned for variables with storage\n";
  }

  $comm_groups = [];

  ($trigger_groups) = &ScheduleSelectGroups($thorn, $implementation,
                                            $rhschedule_db->{"\U$thorn\E BLOCK_$block TRIG"},
                                            $rhinterface_db);

  ($sync_groups) = &ScheduleSelectGroups($thorn, $implementation,
                                         $rhschedule_db->{"\U$thorn\E BLOCK_$block SYNC"},
                                         $rhinterface_db);

  @options = split(/,/, $rhschedule_db->{"\U$thorn\E BLOCK_$block OPTIONS"});
  $tags = $rhschedule_db->{"\U$thorn\E BLOCK_$block TAGS"};
  $tags = '' if ! defined $tags;

  @before_list = &ScheduleSelectRoutines($thorn, $implementation,
                                         $rhschedule_db->{"\U$thorn\E BLOCK_$block BEFORE"},
                                         $rhschedule_db);

  @after_list = &ScheduleSelectRoutines($thorn, $implementation,
                                        $rhschedule_db->{"\U$thorn\E BLOCK_$block AFTER"},
                                        $rhschedule_db);

  @writes_list = &ScheduleSelectRoutines($thorn, $implementation,
                                         $rhschedule_db->{"\U$thorn\E BLOCK_$block WRITES"},
                                         $rhschedule_db);

  @reads_list = &ScheduleSelectRoutines($thorn, $implementation,
                                        $rhschedule_db->{"\U$thorn\E BLOCK_$block READS"},
                                        $rhschedule_db);

  @while_list = &ScheduleSelectVars($thorn, $implementation,
                                    $rhschedule_db->{"\U$thorn\E BLOCK_$block WHILE"},
                                    $rhinterface_db);

  @if_list = &ScheduleSelectVars($thorn, $implementation,
                                 $rhschedule_db->{"\U$thorn\E BLOCK_$block IF"},
                                 $rhinterface_db);


  # Create a block so that we can have local variables.
  $buffer = "  {\n";

  # Create the timelevel array
  $buffer .= '    int cctkschedulei_tlevelarray[] = {';

  foreach $i (@$tlist)
  {
    $buffer .= "$i,";
  }

  # Add a final number to make sure we have at least one number in array
  # Can also be used to detect end of array.

  $buffer .= "0};\n";

  # add check on number of timelevels in case we were using a parameter
  for($i=0; $i < @$tlist; $i++)
  {
    $buffer .= "    if(!($$tlist[$i] >= 0 && $$tlist[$i]  <= CCTK_MaxTimeLevels(\"$$mem_groups[$i]\")))\n";
    $buffer .= "        CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,\n";
    $buffer .= "                   \"Tried to schedule %d timelevels for group '%s' in schedule.ccl.\\n\"\n";
    $buffer .= "                   \"Value must be between 0 and %d (inclusive)\",\n";
    $buffer .= "                   $$tlist[$i], \"$$mem_groups[$i]\", CCTK_MaxTimeLevels(\"$$mem_groups[$i]\"));\n";
    $buffer .= "\n";
  }

  # Start writing out the data
  if($rhschedule_db->{"\U$thorn\E BLOCK_$block TYPE"} eq "GROUP")
  {
    $prototype = '';
    $buffer .= '    CCTKi_ScheduleGroup("' . $rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"} . "\",\n";
    $indent  = '                        ';
    $language = '';
  }
  elsif($rhschedule_db->{"\U$thorn\E BLOCK_$block TYPE"} eq "FUNCTION")
  {
    if($rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"} =~ m:^\s*C\s*$:i )
    {
      $language = 'C';
      $function = $rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"};
    }
    elsif($rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"} =~ m:^\s*(F|F77|FORTRAN|F90)\s*$:i )
    {
      $language = 'Fortran';
      $function = "CCTK_FNAME(".$rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"} .")";
    }
    else
    {
      if (!$rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"})
      {
        $mess = "Language not specified in schedule block: " .$rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"} ." in thorn: $thorn";
        &CST_error(0,$mess,'',__LINE__,__FILE__);
        return ('', '');
      }
      else
      { 
        $mess = 'Unknown language ' .$rhschedule_db->{"\U$thorn\E BLOCK_$block LANG"} ." in schedule block: ".$rhschedule_db->{"\U$thorn\E BLOCK_$block NAME"} ." in thorn: $thorn";
        &CST_error(0,$mess,'',__LINE__,__FILE__);
        return ('', '');
      }
    }
    $prototype = "extern int $function(void);\n";
    $buffer .= "    CCTKi_ScheduleFunction((void *)$function,\n";
    $indent  = '                           ';
    $buffer .= $indent;
  }
  else
  {
    $mess = 'Internal error: Unknown schedule block type ' . $rhschedule_db->{"\U$thorn\E BLOCK_$block TYPE"};
    &CST_error(0,$mess,'',__LINE__,__FILE__);
    return ('', '');
  }

  $buffer .= '"' . $rhschedule_db->{"\U$thorn\E BLOCK_$block AS"} . "\",\n";
  $buffer .= "$indent\"$thorn\",\n";
  $buffer .= "$indent\"$implementation\",\n";
  $buffer .= "$indent\"" . $rhschedule_db->{"\U$thorn\E BLOCK_$block DESCRIPTION"} . "\",\n";
  $buffer .= "$indent\"" . $rhschedule_db->{"\U$thorn\E BLOCK_$block WHERE"} . "\",\n";
  if($language ne '')
  {
    $buffer .= "$indent\"$language\",\n";
  }

  $buffer .= $indent . scalar(@$mem_groups) . ",  /* Number of STORAGE  groups   */\n";
  $buffer .= $indent . scalar(@$comm_groups) . ",  /* Number of COMM     groups   */\n";
  $buffer .= $indent . scalar(@$trigger_groups) . ",  /* Number of TRIGGERS groups   */\n";
### TR 22 Jan 2004: disabled the check for triggers of ANALYSIS routines
###                 as the absence of triggers now means to schedule
###                 such routines unconditionally
#  if (!scalar(@$trigger_groups) && $rhschedule_db->{"\U$thorn\E BLOCK_$block WHERE"} eq "CCTK_ANALYSIS")
#  {
#    $mess = "Schedule error: Scheduling at ANALYSIS in $thorn with no TRIGGERS\n";
#    $help = 'Functions or function groups scheduled in the ANALYSIS bin require TRIGGERS to be set.';
#    $help .= 'Triggers are grid variables or grid variable group names which are examined by ';
#    $help .= 'IO methods to decide whether of not execution should happen.';
#    &CST_error(0,$mess,$help,__LINE__,__FILE__);
#  }
  $buffer .= $indent . scalar(@$sync_groups)  . ", /* Number of SYNC     groups    */\n";
  $buffer .= $indent . scalar(@writes_list)   . ", /* Number of WRITES clauses     */\n";
  $buffer .= $indent . scalar(@reads_list)    . ", /* Number of READS clauses      */\n";
  $buffer .= $indent . scalar(@options)       . ", /* Number of Options            */\n";
  $buffer .= $indent . scalar(@before_list)   . ", /* Number of BEFORE   routines  */\n";
  $buffer .= $indent . scalar(@after_list)    . ", /* Number of AFTER    routines  */\n";
  $buffer .= $indent . scalar(@while_list)    . ", /* Number of WHILE    variables */\n";
  $buffer .= $indent . scalar(@if_list)       . ", /* Number of IF       variables */\n";
  $buffer .= $indent . "cctkschedulei_tlevelarray  /* Array of timelevel data for storage groups */";

  foreach $item (@$mem_groups, @$comm_groups, @$trigger_groups, @$sync_groups,
                 @writes_list, @reads_list, @options)
  {
    $buffer .= ",\n$indent\"$item\"";
  }

  $buffer .= ",\n$indent\"$tags\"";

  foreach $item (@before_list, @after_list, @while_list, @if_list)
  {
    $buffer .= ",\n$indent\"$item\"";
  }

  $buffer .= ");\n\n";

  # Close this block.
  $buffer .= "  }\n";

  return ($buffer, $prototype);
}

#/*@@
#  @routine    ScheduleStatement
#  @date       Fri Sep 17 17:38:30 1999
#  @author     Tom Goodale
#  @desc
#  Creates the code for a given schedule statement
#  @enddesc
#@@*/
sub ScheduleStatement
{
  my($thorn, $implementation, $statement, $rhinterface_db, $rhschedule_db) = @_;

  my($buffer, $prototype);
  my($groups);
  my($misc);

  # Extract the groups.
  ($groups,$misc) = &ScheduleSelectGroups($thorn, $implementation,
                                          $rhschedule_db->{"\U$thorn\E STATEMENT_$statement GROUPS"},
                                          $rhinterface_db);


  if($rhschedule_db->{"\U$thorn\E STATEMENT_$statement TYPE"} eq "STOR")
  {
    &ScheduleValidateTimeLevels($thorn, $implementation, $groups,$misc, $rhinterface_db);
    $function = "CCTKi_ScheduleGroupStorage(";
    $prototype = "";

    my $i;

    # add check on number of timelevels in case we were using a parameter
    for($i=0; $i < @$groups; $i++)
    {
      $buffer .= "  if(!($$misc[$i] >= 0 && $$misc[$i]  <= CCTK_MaxTimeLevels(\"$$groups[$i]\")))\n";
      $buffer .= "      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,\n";
      $buffer .= "                 \"Tried to schedule %d timelevels for group '%s' in schedule.ccl.\\n\"\n";
      $buffer .= "                 \"Value must be between 0 and %d (inclusive)\",\n";
      $buffer .= "                 $$misc[$i], \"$$groups[$i]\", CCTK_MaxTimeLevels(\"$$groups[$i]\"));\n";
      $buffer .= "\n";
    }

    for($i=0; $i < @$groups; $i++)
    {
      my $group     = $$groups[$i];
      my $timelevel = $$misc[$i];

      $buffer .= "  $function\"$group\",$timelevel);\n"
    }
  }
  elsif($rhschedule_db->{"\U$thorn\E STATEMENT_$statement TYPE"} eq "COMM")
  {
    print "No need to switch on Communication in $thorn\n";
    print "Communication automatically assigned for variables with storage\n";
  }
  else
  {

    $mess = "Unknown statement type '" .$rhschedule_db{"\U$thorn\E STATEMENT_$statement TYPE"};
    &CST_error(0,$mess,"",__LINE__,__FILE__);
    return ("", "");
  }

  return ($buffer, $prototype);
}


#/*@@
#  @routine    ScheduleSelectGroups
#  @date       Fri Sep 17 17:38:53 1999
#  @author     Tom Goodale
#  @desc
#  Parses a list of variable groups and selects valid ones.
#  @enddesc
#@@*/
sub ScheduleSelectGroups
{
  my($thorn, $implementation, $group_list, $rhinterface_db) = @_;
  my(@groups);
  my(@temp_list);
  my($group);
  my($other_imp, $other_thorn, $foundit, $block);
  my($misc,@misc_list);

  @temp_list = split(/[,\s\n]+/, $group_list);

  foreach $group (@temp_list)
  {
    next if($group =~ m:^\s*$:);

    $other_imp = "";

#    print "DEBUG: $thorn,$implementation - group was $group ";

    # Strip off extra miscellaneous info on group- i.e [...] bit
    $group =~ m/^([^[]+)(\[([^]]*)\])?$/;

    $group = $1;

    my $misc = $3;

    if(! defined $misc)
    {
      $misc = "";
    }

#    print ", group now is $group with misc '$misc'\n"; #DEBUG

    if($group =~ m/^(.+)::(.+)$/)
    {
      $other_imp=$1;
      $group = $2;

#      print "DEBUG: $thorn, $other_imp, $group, $misc\n";

      if(($other_imp !~ m:^\s*$thorn\s*$:i) && ($other_imp !~ m:^\s*$implementation\s*$:i))
      {
        # The name has been given completely specified but it isn't this thorn.

        if($rhinterface_db->{"IMPLEMENTATION \U$implementation\E ANCESTORS"} =~ m:\b$other_imp\b:i)
        {
          $block = "PUBLIC";
        }
        elsif($rhinterface_db->{"IMPLEMENTATION \U$implementation\E FRIENDS"} =~ m:\b$other_imp\b:i)
        {
          $block = "PROTECTED";
        }
        else
        {
          $mess = "Schedule error: Thorn $thorn - group $other_imp\:\:$group doesn't exist.";
          $help = "Check thorn $thorn inherits from implementation $other_imp";
          &CST_error(0,$mess,$help,__LINE__,__FILE__);
          next;
        }

        $rhinterface_db->{"IMPLEMENTATION \U$other_imp\E THORNS"} =~ m:(\w+):;
        $other_thorn = $1;

        if($rhinterface_db->{"\U$other_thorn\E $block GROUPS"} =~ m:\b$group\b:i)
        {
          push(@groups, "$other_imp\::$group");
          push(@misc_list, $misc);
          next;
        }
        else
        {
          $mess = "Schedule error: Thorn $thorn - group $other_imp\:\:$group doesn't exist.\n";
          &CST_error(0,$mess,"",__LINE__,__FILE__);
          next;
        }
      }
    }

    if($rhinterface_db->{"\U$thorn\E PRIVATE GROUPS"} =~ m:\b$group\b:i)
    {
      push(@groups, "$thorn\::$group");
      push(@misc_list, $misc);
    }
    elsif($rhinterface_db->{"\U$thorn\E PROTECTED GROUPS"} =~ m:\b$group\b:i)
    {
      push(@groups, "$implementation\::$group");
      push(@misc_list, $misc);
    }
    elsif($rhinterface_db->{"\U$thorn\E PUBLIC GROUPS"} =~ m:\b$group\b:i)
    {
      push(@groups, "$implementation\::$group");
      push(@misc_list, $misc);
    }
    elsif($other_imp eq "")
    {
      $foundit = 0;
      # Check ancestors and friends
      foreach $other_imp (split(" ", $rhinterface_db->{"IMPLEMENTATION \U$implementation\E ANCESTORS"}))
      {
        $rhinterface_db->{"IMPLEMENTATION \U$other_imp\E THORNS"} =~ m:(\w+):;
        $other_thorn = $1;

        if($rhinterface_db->{"\U$other_thorn\E PUBLIC GROUPS"} =~ m:\b$group\b:i)
        {
          push(@groups, "$other_imp\::$group");
          push(@misc_list, $misc);
          $foundit = 1;
          last;
        }
      }
      if(! $foundit)
      {
        foreach $other_imp (split(" ", $rhinterface_db->{"IMPLEMENTATION \U$implementation\E FRIENDS"}))
        {
          $rhinterface_db->{"IMPLEMENTATION \U$other_imp\E THORNS"} =~ m:(\w+):;
          $other_thorn = $1;

          if($rhinterface_db->{"\U$other_thorn\E PROTECTED GROUPS"} =~ m:\b$group\b:i)
          {
            push(@groups, "$other_imp\::$group");
            push(@misc_list, $misc);
            $foundit = 1;
            last;
          }
        }
      }
      if(! $foundit)
      {
        $mess = "Schedule error: Thorn $thorn - group $group doesn't exist.";
        $help = "Check $group really is in thorn $thorn. Groups from other thorns ";
        $help .= "need to be specified using <implementation>\:\:$group and ";
        $help .= "<implementation> must be inherited by your thorn.";
        &CST_error(0,$mess,$help,__LINE__,__FILE__);

      }
    }
    else
    {
      $mess = "Schedule error: Thorn $thorn - group $group doesn't exist.";
      &CST_error(0,$mess,"",__LINE__,__FILE__);

    }
  }

  return (\@groups,\@misc_list);
}

#/*@@
#  @routine    ScheduleSelectRoutines
#  @date       Fri Sep 17 17:39:29 1999
#  @author     Tom Goodale
#  @desc
#  Parses a list of schedule routines/groups.
#  FIXME - should validate
#  @enddesc
#@@*/
sub ScheduleSelectRoutines
{
  my($thorn, $implementation, $routine_list, $rhschedule_db) = @_;
  my(@routines);
  my(@temp_list);
  my($routine);

  @temp_list = split(/[,\s\n]+/, $routine_list);

  foreach $routine (@temp_list)
  {
    next if($routine =~ m:^\s*$:);

    push(@routines, $routine);

  }

  return @routines;
}

#/*@@
#  @routine    ScheduleSelectVars
#  @date       Fri Sep 17 17:39:58 1999
#  @author     Tom Goodale
#  @desc
#  Parses a list of variables
#  FIXME - should validate
#  @enddesc
#@@*/
sub ScheduleSelectVars
{
  my($thorn, $implementation, $var_list, $rhinterface_db) = @_;
  my(@vars);
  my(@temp_list);
  my($var);

  @temp_list = split(/[,\s\n]+/, $var_list);

  foreach $var (@temp_list)
  {
    next if($var =~ m:^\s*$:);

    push(@vars, $var);
  }

  return @vars;
}

#/*@@
#  @routine    ScheduleValidateTimeLevels
#  @date       Tue Apr 16 15:22:02 2002
#  @author     Tom Goodale
#  @desc
#  Validate the timelevel specifiers for a group list.
#  @enddesc
#@@*/
sub ScheduleValidateTimeLevels
{
  my($thorn, $implementation, $groups,$timelevels_list, $rhinterface_db) = @_;

  my $i;

  my $return_code;

  $return_code = 0;

  for($i = 0; $i < @$groups; $i++)
  {
    my $group = $$groups[$i];
    my $timelevels = $$timelevels_list[$i];

#    print "DEBUG: validate $thorn,$implementation,$group,$timelevels\n";

    if($timelevels !~ /^[[:alpha:]_][[:word:]]*$/ && $timelevels !~ /^\d*$/)
    {
      &CST_error(0,"Invalid timelevel specifier '$timelevels' in schedule.ccl of thorn '$thorn'","",__LINE__,__FILE__);
      $return_code++;
      next;
    }

    $group =~ m/^(.+)::(.+)$/;

    my $imp = $1;
    my $groupname = $2;

    my $allowed_timelevels;

    if($1 =~ /^$thorn$/i)
    {
      $allowed_timelevels =  $rhinterface_db->{"\U$thorn GROUP $groupname TIMELEVELS\E"};
    }
    else
    {
      @thornlist = split(" ",$rhinterface_db->{"IMPLEMENTATION \U$imp\E THORNS"});

      $allowed_timelevels =  $rhinterface_db->{"\U$thornlist[0] GROUP $groupname TIMELEVELS\E"};
    }

    # If the maximum number of timelevels is 1, the timelevel specification can be empty.
    if($timelevels eq "" && $allowed_timelevels == 1)
    {
      $$timelevels_list[$i] = 1;
      next;
    }
    elsif($timelevels eq "")
    {
      &CST_error(0,"Timelevel specifier missed for group '$group' in schedule.ccl of thorn '$thorn'\n".
                   "This group has more than one timelevel, so a timelevel specifier is mandatory.\n" .
                   "You may specify a maximum of $allowed_timelevels timelevels\n" .
                   "e.g. try $group\[$allowed_timelevels]\n" .
                   "Note that you should only activate the number of timelevels necessary\n" .
                   "for your scheme, which may be less than this maximum."
                 ,"",__LINE__,__FILE__);
      $return_code++;
    }
    elsif($timelevels =~ /^[[:alpha:]_][[:word:]]*$/ || ($timelevels > 0 && $timelevels  <= $allowed_timelevels))
    {
      next;
    }
    else
    {
      if($allowed_timelevels > 1)
      {
        &CST_error(0,"Tried to schedule $timelevels timelevels for group '$group' in schedule.ccl of thorn '$thorn'\n" .
                     "Value must be between 1 and $allowed_timelevels (inclusive)","",__LINE__,__FILE__);
      }
      else
      {
        &CST_error(0,"Tried to schedule $timelevels timelevels for group '$group' in schedule.ccl of thorn '$thorn'\n" .
                     "This variable has one timelevel only","",__LINE__,__FILE__);
      }
      $return_code++;
    }
  }

  return $return_code;
}

1;