#!/usr/bin/perl
#
# SPEEDUP COMPUTATION 2 (Averages)
#
# Andrew J. Pounds, Ph.D.
# Departmentis of Chemistry and Computer Science
# Mercer University
# Spring 2026
#
# Given the number of processors in column 1
# (starting with 1 processor) and the runtime in 
# column 2 this Perl script will compute the 
# values for the speedup curve.
#
# usage is ./speedup2.pl datafile
#
#
use strict;
use warnings;

my $filename = $ARGV[0];

open my $fh, '<', $filename or die "Cannot open $filename: $!";

my @processors;
my @runtimes;

my $iterations=10;

while ( my $line = <$fh> ) {
# print $line;
    chomp($line);
    my @row = split( " ", $line );
#           print $row[0] . " " . $row[4] . "\n"; 
    push(@processors,$row[0]);
    push(@runtimes, $row[4]);

}

# Now prepare to compute the average of $iterations

my @avgprocessors;
my @avgruntimes;
my $procsum = 0;
my $runtimesum = 0;

for ( my $k=0; $k<scalar(@processors)/$iterations; $k++ ) {
    my $procsum = 0;
    my $runtimesum = 0;
    for ( my $i=$k*$iterations; $i<$k*$iterations+$iterations; $i++ ){
        $procsum = $procsum + $processors[$i];
        $runtimesum = $runtimesum + $runtimes[$i];
    }
    push(@avgprocessors, $procsum/$iterations);
    push(@avgruntimes, $runtimesum/$iterations);
}

# Now print the results

print "Procs" . "\t" . "Average Megaflops ( " . $iterations . " Iterations )\n"; 
for ( my $i=0; $i<scalar(@avgprocessors); $i++ ) {
    print $avgprocessors[$i] . "\t" . $avgruntimes[$i] . "\n";
}

print "\n";

print "Procs" . "\t" . "Average Speedup ( " . $iterations . " Iterations )\n"; 
for ( my $i=0; $i<scalar(@avgprocessors); $i++ ) {
    print $avgprocessors[$i] . "\t" . $avgruntimes[$i]/$avgruntimes[0] . "\n";
}


