#!/usr/bin/perl -w

################################################################################
##
## Copyright (c) 2007 Intel Corporation 
## All rights reserved. 
##
## Redistribution and use in source and binary forms, with or without 
## modification, are permitted provided that the following conditions are met: 
##
## * Redistributions of source code must retain the above copyright notice, 
## this list of conditions and the following disclaimer. 
## * Redistributions in binary form must reproduce the above copyright notice, 
## this list of conditions and the following disclaimer in the documentation 
## and/or other materials provided with the distribution. 
## * Neither name of Intel Corporation nor the names of its contributors 
## may be used to endorse or promote products derived from this software 
## without specific prior written permission.
## 
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
## PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
## OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
## NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################

# use strict;

print "\nIntel Cluster OpenMP Latency Measurement\n";

no warnings 'uninitialized';

if ($ARGV[0] ne "") {
    print <<EOD;

    This script measures the latency of messages sent from the first process
    mentioned in the initialization file to the second process, and back again.  
    The initialization file must be in the current working directory or findable 
    in the KMP_CLUSTER_PATH, and it must specify at least 2 processes.

    The Intel(R) compiler setup script (iccvars.csh) must have been already run
    for this script to work.
EOD
exit;
}

$pgm = <<"ENDPGM";
#include <stdlib.h>
void foo()
{
   char * f = malloc(10);
   free(f);
}

int main()
{
   #pragma omp parallel
   {
     foo();
   }
}
ENDPGM

if (!(-e "kmp_cluster.ini")) {
    die "Error - You must have a kmp_cluster.ini file in the current directory\n";
}

open(INI, "kmp_cluster.ini");
$_ = <INI>;
if (!(m/--hostlist=/) && !(m/--hostfile=/)) {
    die "Error - Your kmp_cluster.ini file must specify at least 2 hosts\n";
}
if (m/--hostlist=/) {
    s/.*--hostlist=([a-zA-Z._\-0-9,]+).*/$1/;
    $hosts=$_;
}
if (m/--hostfile=/) {
    s/.*--hostlist=([a-zA-Z_.0-9]+).*/$1/;
    $hostfile=$_;
    open(HFILE, "$hostfile");
    $hosts="";
    while(<HFILE>) {
	$hosts = "$hosts,$_";
    }
}
close(INI);

print "\nFor kmp_cluster.ini:\n\n";
system "cat kmp_cluster.ini";

print "\nMeasuring latency for hosts: $hosts\n";

$fileroot = "$$.latency";
$srcfile = "$fileroot.c";
$exefile = "$fileroot.exe";

open(PGM, ">$srcfile");

print PGM $pgm;

system "icc -cluster-openmp-profile $srcfile -o $exefile > /dev/null";

if (!(-e $exefile)) {
    die "Unable to compile - you probably need to run the compiler setup script\n";
}

system "$exefile";

if (!(-e "guide.gvs")) {
    die "Something is wrong - getlatency program did not produce guide.gvs file\n";
}

system "grep network guide.gvs";

system "rm -f $srcfile $exefile"

