#!/usr/bin/perl -w
# plot-benchmaster
#
# Copyright (c) 2000 Robert G. Brown <rgb@phy.duke.edu>
# Licensed according to GPL v2b (see file "Copying" in this distribution).
#
# $Id: log-plot-benchmaster,v 1.6 2004/12/16 21:37:02 rgb Exp $
#

 use Getopt::Std;	# Single character options are plenty.

#========================================================================
# Set defaults for variables
# We'll assume that 100 runs are enough for decent statistics in general
#========================================================================
 $hostname = `hostname`;
 chop($hostname);
 $samples = 10;
 $begin_size = 1;	# Remember that double precision is 2^3 = 8 bytes
 $log2_max = 23;	# 2^{23+3} is 32 MB, corrected for double and single.
 $floatflag = 0;	# This probably doesn't do anything.  Love the "probably".
 $rand_flag = "";
 $verbose = 0;
 $statsonly = 0;
 $testid = 0;

#========================================================================
# Now we parse the CL with getopt standard (single character)
#========================================================================
# Option list
 getopts('b:dhl:n:rs:t:v:');

# Assignments
 if($opt_b) {$begin_size = $opt_b;}
 if($opt_l) {$log2_max = $opt_l;}
 if($opt_h) {$opt_h=0;Usage(); exit(1);}
 if($opt_n) {$samples = $opt_n;}
 if($opt_r) {$opt_r = 0;$rand_flag = "-r";}
 if($opt_s) {$statsonly = 1;$hostname = $opt_s;}
 if($opt_t) {$testid = $opt_t;}
 if($opt_v) {$verbose = $opt_v;}

 if($floatflag) { $log2_max++; }	# floats are only four bytes, need one more.

# If leftovers, punt with error message and Usage()
 $ARGC = @ARGV;
 if($ARGC) {
   Usage("Arguments left over -- incorrect number or type of arguments");
   exit(1);
 }

 $end_size = 2**($log2_max + $floatflag);
 if($verbose) {
   print<<EOF;
log-plot-benchmaster beginning at $begin_size, ending at $end_size, increment
in powers of two (plus odd neighbor), accumulating $samples samples.
EOF
 }

 # float rate
 $size = $begin_size;
 while($size < $end_size){
   if($verbose){
     print "executing benchmaster -t $testid -n $samples -s $size $rand_flag -q\n";
   }
   if($size == $begin_size){
     $rateok = system("benchmaster -t $testid -n $samples -s $size $rand_flag");
   } else {
     $rateok = system("benchmaster -t $testid -n $samples -s $size $rand_flag -q");
   }
   $size_odd = $size + 1;
   if($verbose){
     print "executing benchmaster -t $testid -n $samples -s $size_odd $rand_flag -q\n";
   }
   $rateok = system("benchmaster -t $testid -n $samples -s $size_odd $rand_flag -q");
   # double the size to build a log curve in powers of 2 x (4 or 8)
   $size *= 2;
 }

exit 0;

sub Usage {

 my $message = shift;
 if($message) {print STDERR "Error: $message\n";}
 print STDERR "
Usage:
  log-plot-benchmaster [-t test] [-b begin_size] [-l log2_max] [-r]
                    [-h] [-n samples] [-v]

  -b sets the beginning vector size (for vector tests, best a
     power of 2)
  -l sets the maximum power of 2 times this size to be tested
     (the vector size is doubled each pass for l passes)
  -n sets the number of independent samples to be evaluated at each size
  -r turns on shuffled order for those vector tests that support it.

NOTE WELL!  This is beta software under development and various things may
not work correctly or at all.  This script in particular isn't fully
debugged and will probably do silly things for some values of -t, and the
stats routine is NOT smart enough to recognize which test it is resumming
(it will just crunch through the output files in results, which are replaced
by each new test).  USE CAUTION, and REPORT BUGS (preferrably with fixes:-)
to rgb\@phy.duke.edu.
\n";

 exit;
}

