#!/usr/bin/perl -w

#   /*        Fast GEMM routine for Alpha 21164/21264      */
#   /*         on  Linux, Digital UNIX                     */
#   /*        by Kazushige Goto <goto@statabo.rim.or.jp>   */

$maxmem = 230;   # MB unit

@program  = ("bms", "bmd", "bmc", "bmz");
@unitsize = (4, 8, 8, 16);

sub calc{
    local $size     = $_[0];
    local $ld       = $_[1];
    local $category;
    local $i;
    local ($times, $flops, $maxflops, $minflops, $totalflops);

    printf STDERR "%5d ", $ld;

    $times = 2;
    if ($size <= 800){ $times = 3;};
    if ($size <= 400){ $times = 4;};

    for ($category = 0; $category < 4; $category++){

	$memsize = (($size * $ld * 3 * $unitsize[$category]) >> 20);
	if ($memsize <= $maxmem){
	    $maxflops   = 0;
	    $minflops   = 99999999999;
	    $totalflops = 0;
	    for($i=1; $i<= $times; $i++){
		if (($category >> 1) == 0){
		    $flops = `$program[$category] $size $ld 0 2.0 1.0 1`;
		} else {
		    $flops = `$program[$category] $size $ld 0 2.0 3.0 1.0 0.0 1`;
		}
		$totalflops += $flops;
		if ($flops < $minflops) { $minflops = $flops;}
		if ($flops > $maxflops) { $maxflops = $flops;}
	    }
#	    if ($times <= 3){
#		printf STDERR "%8.3f ", $totalflops / $times;
#	    } else {
#		$totalflops -= $maxflops;
#		$totalflops -= $minflops;
#		printf STDERR "%8.3f ", $totalflops / ($times - 2);
#	    }
		printf STDERR "%8.3f ", $maxflops;

	} else {
	    print STDERR "Skipped  ";
	}
    }
    printf STDERR "\n";
}

`make clean`;
`make SIMPLE=-DSIMPLE bms bmd bmc bmz`;

for($ld = 100; $ld < 4000; $ld += 100){
    &calc($ld, $ld);
}
