summaryrefslogtreecommitdiff
path: root/lib/sbin/SchedLatex.pl
blob: ba67e8073ad9ebe2cddc1e655ebf13ec26d5faee (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
#!/usr/bin/perl -s

use strict;
use vars qw($h $help $cctk_home $thornlist $outdir $verbose $debug $directory $document_type);

#/*@@
#  @file      SchedLatex.pl
#  @date      Sun Mar  3 19:05:41 CET 2002
#  @author    Ian Kelley
#  @desc 
#  This program will take as input a thornlist, and outputs a latex table    
#  that contains the information in the thorns' schedule.ccl file(s).  This latex
#  table can then be used as a stand-alone document, or as a section of a large
#  "ThornGuide"         
#  @enddesc 
#  @version 
#@@*/

#########################
# ->> SchedLatex.pl <<- #
#########################################################################
#                      Standard HELP Function                           #
#########################################################################
if ($h || $help) {
   print "--> SchedLatex.pl <--\n";
   print "   This program will take as input a thornlist, and outputs a latex table that contains the information in the thorns' schedule.ccl file(s).  This latex table can then be used as a stand-alone document, or as a section of a larger 'ThornGuide'";
   print "Options:\n";
   print "\t-thornlist=     : (req) list specific thorns to process\n";
   print "\t-directory=     : (opt) dir. of arrangements (default arrangements/)\n";
   print "\t-outdir=        : (opt) directory to dump output files, default is .\n";
   print "\t-document_type= : (opt) 'document' or 'section'\n";
   print "\n";
   print "\t-debug          : (opt) prints thorn name on each schedule \n";
   print "\t-verbose        : (opt) gives verbose output to screen\n";
   print "\t-h/-help        : (opt) this screen\n";
   print "\nExample:\n";
   print "\t\$ perl -s /lib/sbin/SchedLatex.pl -thornlist=WaveToyC.th -outdir=/tmp/ -document_type=document\n";
   exit 0;
}

#########################################################################
#                                                                       #
#  This program will take as input a thorn, and output a latex table    #
#  that contains the information in that thorns schedule.ccl file.      #
#                                                                       #
#########################################################################

##############
# REQUIRE(S) #
##############
$cctk_home .= '/' if (($cctk_home !~ /\/$/) && defined $cctk_home);

my $sbin_dir = "${cctk_home}lib/sbin";
require "$sbin_dir/ScheduleParser.pl";
require "$sbin_dir/CSTUtils.pl";

# common procedures used to create the thornguide(s)
require "$sbin_dir/ThornUtils.pm";

# for reading of the thornlist routine: %thorns = &ReadThornlist($thornlist)
require "$sbin_dir/MakeUtils.pl";

##################
# INITIALIZATION #
##################
my $TABLE_WIDTH   ||= "160";
$document_type    ||= "section";

my $start_directory = `pwd`;
chomp ($start_directory);

# set some variables in ThornUtils(.pm) namespace
$ThornUtils::cctk_home          = $cctk_home;
$ThornUtils::start_directory    = $start_directory;
$ThornUtils::verbose            = $verbose;
$ThornUtils::debug              = $debug;

my $ofh;
my %arrangements;
my %thorns;
my %schedule_database;
my %pathsToThorns;
my %arrangements_database;

my @listOfThorns;

my %var_mapping = (
	'STOR' => 'Storage',
	'LANG' => 'Language',
	'AFTER'=> 'After',
	'TRIG' => 'Triggers',
	'SYNC' => 'Sync',
);

# get/setup the output directory and the arrangements directory
$outdir                 = ThornUtils::SetupOutputDirectory($outdir);
my $arrangements_dir       = ThornUtils::GetArrangementsDir($directory);

# determine thornlist, create one if one doesn't exist
if (defined $thornlist) {
   # provided by MakeUtils.pl, returns a hash with a list of the thorns in our thornlist
   %thorns       = &ReadThornlist($thornlist);
   @listOfThorns = sort keys %thorns; 
} else {
   # we don't have a thornlist, go find all thorns in arrangements directory
   @listOfThorns = ThornUtils::CreateThornlist($arrangements_dir);
}

# this will return us a hash with keys as thorn names, and values as absolute paths to the
# thorn's directory param.ccl can be located in that path.
#   We need this information to create a schedule database using create_schedule_database
#
# We are not doing ''one'' call to schedule_database as we easily could, because there is NO WAY
# (currently) to distinguish between two identical thorns in different arrangements.  So we
# would get stuff from Alphathorns/IOHDF5 in CactusBase/IOHDF5, or/and visa-versa. 
ThornUtils::ClassifyThorns(\%arrangements, @listOfThorns);

# lets go through, one arrangement at a time
foreach my $arrangement (sort keys %arrangements) 
{
   print "\n$arrangement" if ($debug);

   # now each thorn in the given arrangement
   foreach my $thorn (@{$arrangements{$arrangement}}) 
   {
      print "\n\t$thorn" if ($debug);

      # get the path for this individual thorn
      %pathsToThorns = ThornUtils::GetThornPaths(["$arrangement/$thorn"], $arrangements_dir, "schedule.ccl");

      # we are selecting STDOUT so that any junk from the &create_schedule_database won't get in our output
      my $filehandle = select(STDOUT);
      %schedule_database = &create_schedule_database(%pathsToThorns);
      select($filehandle);

      $arrangements_database{$arrangement}->{$thorn} = &ReadScheduleDatabase(\%schedule_database);
   }
}

# just dump out the data-structure if we are in debug mode, don't create any files 
if ($debug) { 
   ThornUtils::Dump(\%arrangements_database);
   print "\n";
   exit 0;
} else {
   ThornUtils::ProcessAllArrangements(\%arrangements_database);
}

print "\nFinished.\n";

#########################################################################
#                 END OF MAIN SECTION OF THE PROGRAM                    # 
#########################################################################

#########################################################################
#                     BEGINNING OF SUB-ROUTINES                         #
#########################################################################

#/*@@
#  @routine   ReadLatexDatabase
#  @date      Sun Mar  3 01:54:37 CET 2002
#  @author    Ian Kelley
#  @desc 
#   Calls schedule_parser.pl, which will read in the schedule.ccl file  
#   from a single thorn and return all data as a %hash table, which we  
#   will then parse to put into our own %hash tables named according to 
#   the variable names.                                                 
#                                                                       
#   %(variable_name)  : any number of hashes created with their names   
#                       being the variable names, they then have $keys  
#                       (descriptions) with $values (well, values)      
#                          (e.g.) $name{"default"} = "Cactus";          
#  @enddesc 
#  @version 
#@@*/
sub ReadScheduleDatabase
{
  my (%schedule_database) = %{$_[0]};
  my ($name, $block, $var, $conditionals) = "";

  my %thorn;

  foreach (sort keys %schedule_database)
  {
     print STDERR "\n[$_] --> [$schedule_database{$_}]" if ($debug || $verbose);

     if (/^(.*?)\s(.*?)\s(.*)$/) 
     {
        ($name, $block, $var) = ($1, $2, $3); 
        chomp($schedule_database{$_});

        $thorn{$block}->{$var} = $schedule_database{$_};

        $thorn{$block}->{"CONDITIONAL"} = "0";
        $thorn{$block}->{"THORN"}       = $name;
     } else {
        print STDERR "\n\"$_ --> $schedule_database{$_}\"" if ($verbose || $verbose);
     }
   } #-- foreach %schedule_database

   # conditional blocks/statements
   $conditionals = $schedule_database{"$name FILE"};
   foreach my $key (sort keys %thorn) 
   {
      next if ($key !~ /(BLOCK|STATEMENT)\_(\d+)/);

      my $b = "\@$1\@$2";

      # try to figure out if stuff is conditional storage
      while ($conditionals =~ /\b(if|else)\b.*?\{\s*?(.*?(?:.*?\{.*?[^\{].*?\}.*?)?(?:.*?\{.*?[^\{].*?\}.*?)?(?:.*?\{.*?[^\{].*?\}.*?)?(?:.*?\{.*?[^\{].*?\}.*?)?(?:.*?\{.*?[^\{].*?\}.*?)?.*?)\}/imgs) 
      {
         my $if = $2;
         if ($if =~ /\Q$b\E/) {
            $thorn{$key}->{"CONDITIONAL"} = 1;
         }
      }
   }   

   return \%thorn;
}

#/*@@
#  @routine   ProcessOneThorn
#  @date      Sun Mar  3 01:54:37 CET 2002
#  @author    Ian Kelley
#  @desc 
#
#  @enddesc 
#  @version 
#@@*/
sub ProcessOneThorn 
{
   # get the thorn hash
   my (%thorn)      = %{$_[0]};
   my $arrangement  = $_[1];
   my $thorn        = $_[2];
 
   my $ofh = ThornUtils::StartDocument("schedule", $thorn, $outdir, $arrangement, "Schedule", $document_type);

   # go print out all the good stuff for any given thorn
   &CreateLatexTable(\%thorn, "$arrangement/$thorn");

   ThornUtils::EndDocument($ofh, $document_type);
}

#/*@@
#  @routine   CreateLatexTable                                                     
#  @date      Sun Mar  3 01:54:37 CET 2002
#  @author    Ian Kelley
#  @desc 
#    Takes whatever table element is currently reffered to by $table    
#    and prints it out into a LaTeX table.  Only nifty things it curr.  
#    does is NOT print ranges for BOOLEAN and SHARED elements.          
#  @enddesc 
#  @version 
#@@*/
sub CreateLatexTable
{
   my %thorn      = %{$_[0]};
   my $thorn_name = $_[1];

   my $printgridtitle = 1;
   my @conditional_statements;
   my @always_statements;
   my %aliases;
   my $len;

   # categorize the storage types for STATEMENTS into conditional and always on
   foreach my $key (sort keys %thorn) 
   {
      next if ($key !~ /^STATEMENT/);

      if ($thorn{$key}->{"TYPE"} eq "STOR") 
      {
         if ($thorn{$key}->{"CONDITIONAL"} == 1) {
            push @conditional_statements, split/,/, $thorn{$key}->{"GROUPS"};
         } elsif ($thorn{$key}->{"CONDITIONAL"} == 0) {
            push @always_statements, split/,/, $thorn{$key}->{"GROUPS"};
         }
      }
   }

   # which storage type has the most elements?
   $len = @conditional_statements > @always_statements ? @conditional_statements : @always_statements;

   # print blurb about what this page is about
   print "\n\n\\noindent This section lists all the variables which are assigned storage by thorn ". ThornUtils::CleanForLatex($thorn_name) . ".  Storage can either last for the duration of the run ({\\bf Always} means that if this thorn is activated storage will be assigned, {\\bf Conditional} means that if this thorn is activated storage will be assigned for the duration of the run if some condition is met), or can be turned on for the duration of a schedule function.\n\n";

   # print out storage allocation at the top
   print "\n\\subsection\*\{Storage\}";
   if (@conditional_statements > 0 || @always_statements > 0) 
   {
      print "\n\n\\hspace\{5mm\}\n\n \\begin\{tabular*\}\{${TABLE_WIDTH}mm\}\{ll\} \n";
      print @always_statements > 0 ? "\n\{\\bf Always:\}" : "~";
      print "& ";
      print @conditional_statements > 0 ? "\{\\bf Conditional:\} \\\\ \n"  : " ~ \\\\ \n";
 
      for (my $i = 0; $i <= $len; $i++) 
      {
         print $always_statements[$i] !~ /^$/ ? ThornUtils::CleanForLatex($always_statements[$i]) : "~";
         print " & ";
         print $conditional_statements[$i] !~ /^$/ ? ThornUtils::CleanForLatex($conditional_statements[$i]) : "~";
         print "\\\\ \n";
      } 
      print "\\end\{tabular*\} \n\n";
   } else {
       print "NONE";
   }

   # print out each scheduled block
   foreach my $block (sort keys %thorn) 
   {
      next if ($block !~ /^BLOCK/);
 
      # print the title, but only once
      if ($printgridtitle) {
         print "\n\\subsection\*\{Scheduled Functions\}";
         $printgridtitle = 0;
      }
 
      print "\n\\vspace\{5mm\}\n";
      print "\n\\noindent \{\\bf " .     ThornUtils::CleanForLatex($thorn{$block}->{"WHERE"}) . "\} ";
      print $thorn{$block}->{"CONDITIONAL"} ? "  (conditional) \n" : "\n";
      print "\n\\hspace\{5mm\} " .       ThornUtils::ToLower(ThornUtils::CleanForLatex($thorn{$block}->{"NAME"})) . " \n";
      print "\n\\hspace\{5mm\}\{\\it " . ThornUtils::ToLower(ThornUtils::CleanForLatex($thorn{$block}->{"DESCRIPTION"})) . " \} \n\n";
      print "\n\\hspace\{5mm\}\n\n \\begin\{tabular*\}\{${TABLE_WIDTH}mm\}\{cll\} \n";
 
      $aliases{$thorn{$block}->{"NAME"}} = $thorn{$block}->{"AS"};
 
      # Clean up the hash before we go iterate through and print out the rest
      delete $thorn{$block}->{"CONDITIONAL"};
      delete $thorn{$block}->{"WHERE"};
      delete $thorn{$block}->{"DESCRIPTION"};
      delete $thorn{$block}->{"AS"};
      delete $thorn{$block}->{"NAME"};
      delete $thorn{$block}->{"THORN"};

      # go print out the rest of the key/value pairs
      foreach my $group_key (sort keys %{$thorn{$block}}) {
         &OutputVar($group_key, $thorn{$block}->{$group_key});
      } # foreach sort keys %{$thorn{$group}}
 
      print "\\end\{tabular*\} \n\n";
   } # foreach %blocks
 
   # delete aliases where they key equals the value
   foreach my $key (sort keys %aliases) {
      if ($key eq $aliases{$key}) {
         delete $aliases{$key};
      }
   }
 
   # print out any Aliased functions in a table
   if (scalar(keys %aliases) > 0) 
   {
      print "\n\\subsection\*\{Aliased Functions\}";
      print "\n\n\\hspace\{5mm\}\n\n \\begin\{tabular*\}\{${TABLE_WIDTH}mm\}\{ll\} \n";
      print "\n\{\\bf Alias Name:\} ~~~~~~~ & \{\\bf Function Name:\} \\\\ \n";
 
      foreach my $key (sort keys %aliases) {
         print ThornUtils::CleanForLatex($key) ." & ". ThornUtils::CleanForLatex($aliases{$key}) ." \\\\ \n";
      }
      print "\\end\{tabular*\} \n\n";
   }

   print "\n\n\\vspace\{5mm\}"; 
}

#/*@@
#  @routine   OutputVar
#  @date      Sun Mar  3 01:54:37 CET 2002
#  @author    Ian Kelley
#  @desc 
#     Prints out a var and description for a table element
#
#  @enddesc 
#  @version 
#@@*/
sub OutputVar 
{
   my $description = shift;
   my $value       = shift;   
 
   # print out the different storage, we split it up, because it can get long
   return if ($value =~ /^$/);

   $description = defined $var_mapping{$description} ? $var_mapping{$description} : $description;
   $description = ThornUtils::Translate($description);

   # go through and print out the values, split them onto new lines if their
   # are multiple entries separated by comas
   if (my @temp = split/,/, $value)  
   {
      print "~ & ${description}:  & "; 
 
      my $fp = 1;
      foreach my $t (@temp) 
      {
         $t = ThornUtils::ToLower(ThornUtils::CleanForLatex($t));
 
         if ($fp) {
            print "$t \\\\ \n";
            $fp = 0;
         } else {
            print "~& ~ &" . $t . "\\\\ \n";
         }
      }
   } else {
      print "~ & ${description}:  &" . ThornUtils::ToLower(ThornUtils::CleanForLatex($value)) . "\\\\ \n";
   }
}