Make TcGenDeriv warning-free
[ghc-hetmet.git] / utils / parallel / SN.pl
1 #!/usr/local/bin/perl
2 #                                      (C) Hans Wolfgang Loidl, November 1995
3 #############################################################################
4 # Time-stamp: <Sun Nov  5 1995 00:23:45 Stardate: [-31]6545.08 hwloidl>
5 #
6 # Usage: SN [options] <gr-file>
7 #
8 # Create a summary of spark names that occur in gr-file (only END events in 
9 # gr-file are necessary). Creates a gnuplot impulses graph (spark names by 
10 # number of threads) as summary.
11 #
12 # Options:
13 #  -h        ... help; print this text.
14 #  -v        ... verbose mode.
15 #
16 #############################################################################
17
18 $gran_dir = $ENV{'GRANDIR'};
19 if ( $gran_dir eq "" ) {
20     print STDERR "SN: Warning: Env variable GRANDIR is undefined\n";
21 }
22
23 push(@INC, $gran_dir, $gran_dir . "/bin");
24 # print STDERR "INC: " . join(':',@INC) . "\n";
25
26 require "getopts.pl";
27 require "par-aux.pl";
28 require "stats.pl";
29
30 &Getopts('hv');  
31
32 do process_options();
33
34 if ( $opt_v ) { do print_verbose_message(); }
35
36 # ---------------------------------------------------------------------------
37 # Init
38 # ---------------------------------------------------------------------------
39
40 chop($date = `date`);
41 chop($stardate = `stardate`);
42
43 open (IN,"<$input") || die "$!: $input";
44 $n = 0;
45 $is_end=0;
46 while (<IN>) {
47     $is_end = 1 if /END\s+(\w+).*SN\s+(\d+).*RT\s*(\d+)/;
48     next unless $is_end;
49     $n++;
50     $sn = $2;
51     $rt = $3;
52     #$sn_dec = hex($sn);
53     $num_sns{$sn}++;
54     $rts_sns{$sn} += $rt;
55     #do inc ($sn_dec);
56     $is_end=0;
57 }
58 close (IN);
59
60 @sorted_keys=sort {$a<=>$b} keys(%num_sns);
61 #$max_val=&list_max(@sorted_keys);
62
63 open (SUM,">$summary") || die "$!: $summary";
64
65 print SUM "# Generated by SN at $date $stardate\n";
66 print SUM "# Input file: $input\n";
67 print SUM "#" . "-"x77 . "\n";
68 print SUM "Total number of threads: $n\n";
69 print SUM "# Format: SN: Spark Site N: Number of threads AVG: average RT\n";
70 # . "RTS: Sum of RTs ";
71
72 foreach $k (@sorted_keys) {
73     $num = $num_sns{$k};
74     $rts = $rts_sns{$k};
75     $avg = $rts/$num;
76     #print SUM "SN: $k \tN: $num \tRTS: $rts \tAVG: $avg\n";
77     print SUM "$k \t$num \t$avg\n";
78 }
79 close (SUM);
80
81 open (OUT,">$output") || die "$!: $output";
82 print OUT "# Generated by SN at $date $stardate\n";
83 print OUT "# Input file: $input\n";
84 print OUT "#" . "-"x77 . "\n";
85
86 $max_val=0;
87 foreach $k (@sorted_keys) {
88     $num = $num_sns{$k};
89     $max_val = $num      if $num > $max_val;
90     print OUT "$k\t$num\n";
91 }
92 close (OUT);
93
94 do write_gp($gp_file,$ps_file); 
95
96 print "Gnu plotting figures ...\n";
97 system "gnuplot $gp_file";
98
99 print "Extending thickness of impulses ...\n";
100 $ext_size = 100;
101 $gray = 0.3;
102 do gp_ext($ps_file);
103
104 exit (0);
105
106 # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
107
108 sub inc {
109     local ($sn) = @_;
110     local (@k);
111     
112     @k = keys(%num_sns);
113     if ( &is_elem($sn, @k) ) { 
114         $num_sns{$sn}++;
115     } else {
116         $num_sns{$sn} = 1;
117     }
118 }
119
120 # ----------------------------------------------------------------------------
121
122 sub is_elem { 
123     local ($x,@list) = @_;
124     local ($found);
125
126     for ($found = 0, $y = shift(@list); 
127          $#list == -1 || $found;  
128          $found = ($x == $y), $y = shift(@list)) {}
129
130     return ($found);
131 }
132
133 # ----------------------------------------------------------------------------
134
135 # -----------------------------------------------------------------------------
136
137 sub process_options {
138
139     if ( $opt_h ) {                      
140         open(ME,$0) || die "Can't open myself ($0): $!\n";
141         $n = 0;
142         while (<ME>) {
143             last if $_ =~ /^$/;
144             print $_;
145             $n++;
146         }
147         close(ME);
148         exit ;
149     }
150     
151     if ( $opt_s ) {
152         $opt_s =~ s/[\(\)\[\]]//g;
153         @sparks = split(/[,;. ]+/, $opt_s);
154     } else {
155         @sparks = ( 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15);
156     }
157
158     if ( $#ARGV != 0 ) {
159         print "Usage: $0 [options] <gr-file>\n;";
160         print "Use -h option to get details\n";
161         exit 1;
162     }
163
164     $input = $ARGV[0];
165     ($ps_file = $input) =~ s/\.gr/-SN.ps/;
166     ($gp_file = $input) =~ s/\.gr/-SN.gp/;
167     ($summary = $input) =~ s/\.gr/-SN.sn/;
168
169     #($basename = $gr_file) =~ s/\.gr//;
170     #$rts_file = $basename . ".rts";        # "RTS";
171     #$gran_file = "g.ps"; # $basename . ".ps";
172     #$rts_file = $gr_file;
173     #$rts_file =~ s/\.gr/.rts/g;
174
175     if ( $opt_o ) {
176         $output = $opt_o;
177     } else {
178         ($output = $input) =~ s/\.gr/-SN.dat/;
179     }
180
181     if ( $opt_e ) {
182         $ext_size = $opt_e; 
183     } else {
184         $ext_size = 100; 
185     }
186     
187     if ( $opt_i ) {
188         $gray = $opt_i; 
189     } else {
190         $gray = 0; 
191     }
192 }
193
194 # -----------------------------------------------------------------------------
195
196 sub print_verbose_message {
197     print "Input: $input \tOutput: $output\n";
198 }
199
200 # -----------------------------------------------------------------------------
201
202 # ToDo: Takes these from global module:
203
204 # ----------------------------------------------------------------------------
205
206 sub gp_ext {
207     local (@file_names) = @_;
208     local ($file_name);
209     local ($ps_file_name);
210     local ($prg);
211
212     #$prg = system "which gp-ext-imp";
213     #print "  Using script $prg for impuls extension\n";
214     $prg = $ENV{GRANDIR} ? $ENV{GRANDIR} . "/bin/gp-ext-imp" 
215                          : $ENV{HOME} . "/bin/gp-ext-imp" ;
216     if ( $opt_v ) {
217         print "    (using script $prg)\n";
218     }
219
220     foreach $file_name (@file_names) {
221         $ps_file_name = $file_name; # NB change to orig !!!!&dat2ps_name($file_name);
222         system "$prg -w $ext_size -g $gray " . 
223                $ps_file_name  . " " . 
224                $ps_file_name . "2" ;
225         system "mv " . $ps_file_name . "2 " . $ps_file_name;
226     }
227 }
228
229 # ----------------------------------------------------------------------------
230
231 sub write_gp { 
232     local ($gp_file,$ps_file) = @_;
233     local ($str);
234
235     $xsize = 1;
236     $ysize = 1;
237     $xlabel = "Spark sites";
238     $ylabel = "Number of threads";
239     $xstart = &list_min(@sorted_keys);
240     $xend = &list_max(@sorted_keys);
241     $ymax = $max_val; 
242     $xtics = ""; "(" . join(',',@sorted_keys) . ")\n";
243     $in_file = $output;
244     $out_file = $ps_file;
245
246     open (GP,">$gp_file") || die "$!: $gp_file";
247     print GP "set term postscript \"Roman\" 20\n";
248
249     # identical to the part in write_gp_record of RTS2gran
250
251     $str = "set size " . $xsize . "," . $ysize . "\n" .
252            "set xlabel \"" . $xlabel . "\"\n" .
253            "set ylabel \"" . $ylabel . "\"\n" .
254            ($xstart eq "" ? "" 
255                           : "set xrange [" . int($xstart) .":" . int($xend) . "]\n") .
256            ($opt_Y ? 
257               ("set yrange [" . (index($logaxes,"y") != -1 ? 1 : 0) . ":$opt_Y]\n") :
258               ($ymax eq "" ? "" 
259                         : "set yrange [" . (index($logaxes,"y") != -1 ? 1 : 0) . 
260                           ":" . &list_max(2,int($ymax+$ymax/5)) . "]\n")) .
261            ($xtics ne "" ? "set xtics $xtics" : "") . 
262            "set tics out\n" .
263            "set border\n" .
264            ( $nPEs!=0 ? "set title \"$nPEs PEs\"\n" : "" ) .
265            "set nokey \n" .
266            "set nozeroaxis\n" .
267            "set format xy \"%8.8g\"\n" .
268            (index($logaxes,"x") != -1 ? 
269                "set logscale x\n" : 
270                "set nologscale x\n") .
271            (index($logaxes,"y") != -1 ? 
272                "set logscale y\n" : 
273                "set nologscale y\n") .
274            "set output \"" . $out_file . "\"\n" .
275            "plot \"" . $in_file . "\" with impulses\n\n";
276     print GP $str;
277     close (GP);
278 }
279
280 # ----------------------------------------------------------------------------