| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# farms out jobs to a list of machines, finding available CPU |
| 4 |
# |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use IO::Handle; |
| 8 |
#--------------------------------------------------------- |
| 9 |
# check args |
| 10 |
#--------------------------------------------------------- |
| 11 |
if ($#ARGV < 1 ) { |
| 12 |
die "Usage: $0 nfilesperplot file1 [file2 file3 ... filen] \n also with a file called input \n"; |
| 13 |
} |
| 14 |
#--------------------------------------------------------- |
| 15 |
# set farming method options are ssh, xgrid |
| 16 |
#--------------------------------------------------------- |
| 17 |
#my $farmusing='ssh'; |
| 18 |
my $farmusing='none'; |
| 19 |
#my $farmusing='xgrid'; |
| 20 |
my $delay=3; |
| 21 |
#--------------------------------------------------------- |
| 22 |
# set name and location of the supersphplot executable |
| 23 |
#--------------------------------------------------------- |
| 24 |
my $home=`cd; pwd -P`; |
| 25 |
chomp ($home); |
| 26 |
my $exe="$home/splash/bin/ssplash -readpix chf"; |
| 27 |
my $pwd = `pwd`; |
| 28 |
chomp($pwd); |
| 29 |
#--------------------------------------------------------- |
| 30 |
# set other default options |
| 31 |
#--------------------------------------------------------- |
| 32 |
my $inputfile='input'; |
| 33 |
my $pgplotfile='pgplot'; |
| 34 |
my $pgplotdev='none'; # doesn't matter |
| 35 |
my $pgplotdir= $ENV{'PGPLOT_DIR'}; # get this from the environment |
| 36 |
my $ldpath= $ENV{'LD_LIBRARY_PATH'}; # get this from the environment |
| 37 |
my $run; |
| 38 |
#--------------------------------------------------------- |
| 39 |
# get filenames and nfilesperplot from command line |
| 40 |
#--------------------------------------------------------- |
| 41 |
my ($nfilesperplot,@files) = @ARGV; |
| 42 |
#--------------------------------------------------------- |
| 43 |
# work out how many files should be sent as command line arguments to each run |
| 44 |
#--------------------------------------------------------- |
| 45 |
if ($nfilesperplot < 1) { die "error: nfilesperplot must be > 0\n" }; |
| 46 |
my $nfiles=$#files + 1; |
| 47 |
my $nruns=$nfiles/$nfilesperplot; |
| 48 |
print "nfiles = $nfiles; nruns = $nruns\n"; |
| 49 |
#--------------------------------------------------------- |
| 50 |
# get generic input from input file |
| 51 |
#--------------------------------------------------------- |
| 52 |
my @inputs = `cat $inputfile` or die "error: input file does not exist\n"; |
| 53 |
#open my $fh,'<',$inputfile or die "error: input file does not exist\n"; |
| 54 |
my $line; |
| 55 |
my $nline = 0; |
| 56 |
my $devline = 0; |
| 57 |
foreach $line (@inputs) { |
| 58 |
if ( $line =~ m|(/.+)\s| ) { |
| 59 |
$pgplotdev = $1; |
| 60 |
$devline = $nline; |
| 61 |
} |
| 62 |
$nline = $nline + 1; |
| 63 |
} |
| 64 |
#close $fh; |
| 65 |
|
| 66 |
#--------------------------------------------------------- |
| 67 |
# work out what device we are writing to and therefore |
| 68 |
# what the filenames should be |
| 69 |
#--------------------------------------------------------- |
| 70 |
|
| 71 |
print "PGPLOT device = $pgplotdev $inputs[$devline]\n"; |
| 72 |
if ( $pgplotdev eq '/xw' or $pgplotdev eq '/aqt' ) { |
| 73 |
die "must choose a non-interactive PGPLOT device\n"; |
| 74 |
} |
| 75 |
#--------------------------------------------------------- |
| 76 |
# work out the filename extension from the device name |
| 77 |
#--------------------------------------------------------- |
| 78 |
my ($ext) = $pgplotdev =~ m|/(.+)|; |
| 79 |
#print "extension = $ext \n"; |
| 80 |
|
| 81 |
#--------------------------------------------------------- |
| 82 |
# farm out the jobs |
| 83 |
#--------------------------------------------------------- |
| 84 |
my $filestart = 0; |
| 85 |
my $fileend = $filestart + $nfilesperplot; |
| 86 |
my $n = 0; |
| 87 |
my $tempdir = 'temp'; |
| 88 |
system "mkdir $tempdir" || die("error creating temporary directory $tempdir"); |
| 89 |
for ($run=1;$run<=$nruns;$run++) { |
| 90 |
my @inputstemp = @inputs; |
| 91 |
my $num = sprintf("%04d",$run); |
| 92 |
my $pgplotfile = "pgplot_$num.$ext"; |
| 93 |
#my $pgplotfile = "pgplot.$ext\_$run"; |
| 94 |
$inputstemp[$devline] = "$pgplotfile$pgplotdev\n"; |
| 95 |
print "------------ run $run : $pgplotfile ------------\n"; |
| 96 |
my @argsn=@files[$filestart..$fileend-1]; |
| 97 |
#---write the input file |
| 98 |
open(INPUTF,"> $tempdir/input$run") || die("can't write temporary files"); |
| 99 |
foreach $line (@inputstemp) { |
| 100 |
chomp($line); |
| 101 |
print INPUTF "$line \n"; |
| 102 |
} |
| 103 |
close(INPUTF); |
| 104 |
#---write the executable script--- |
| 105 |
my $outfile = "$tempdir/run$run.output"; |
| 106 |
open(RUNSCR,"> $tempdir/run$run.csh") || die("can't write run script"); |
| 107 |
print RUNSCR "#!/bin/tcsh \n"; |
| 108 |
print RUNSCR "setenv PGPLOT_DIR $pgplotdir \n"; |
| 109 |
# print RUNSCR "source $home/.cshrc \n"; |
| 110 |
# print RUNSCR "setenv LD_LIBRARY_PATH $ldpath \n"; |
| 111 |
print RUNSCR "setenv F_UFMTENDIAN 'big;little:168' \n"; |
| 112 |
print RUNSCR "setenv PGPLOT_FONT $home/pgplot/grfont.dat_big \n"; |
| 113 |
print RUNSCR "cd $pwd \n"; |
| 114 |
print RUNSCR "$exe @argsn < $tempdir/input$run >& $outfile \n"; |
| 115 |
close(RUNSCR); |
| 116 |
system "chmod a+x ./$tempdir/run$run.csh"; |
| 117 |
if ( -e "$outfile" ) { system "rm $outfile" }; |
| 118 |
my $commandline = "./$tempdir/run$run.csh"; |
| 119 |
if ( -s $pgplotfile ) { |
| 120 |
print "skipping $pgplotfile which already exists \n"; |
| 121 |
} else { |
| 122 |
if ($farmusing eq 'ssh') { |
| 123 |
farmjob_ssh( $commandline, $n ); |
| 124 |
sleep $delay; |
| 125 |
} elsif ($farmusing eq 'xgrid') { |
| 126 |
farmjob_xgrid( $commandline ); |
| 127 |
} elsif ($farmusing eq 'none') { |
| 128 |
print "no job farmed: using farming method \'none\' \n"; |
| 129 |
} else { |
| 130 |
die "unknown farming method \n"; |
| 131 |
} |
| 132 |
} |
| 133 |
#--------------------------------- |
| 134 |
$filestart = $fileend; |
| 135 |
$fileend = $filestart + $nfilesperplot; |
| 136 |
} |
| 137 |
exit; |
| 138 |
|
| 139 |
#--------------------------------------------------------- |
| 140 |
# subroutine to farm job via Apple's Xgrid |
| 141 |
#--------------------------------------------------------- |
| 142 |
|
| 143 |
sub farmjob_xgrid { |
| 144 |
my $commandline=shift; |
| 145 |
my $xgridauth = "-hostname cytosine.ex.ac.uk -auth Kerberos -gid 2"; |
| 146 |
my $jobid = `xgrid $xgridauth -job submit $commandline` || die "xgrid not found \n"; |
| 147 |
($jobid) = $jobid =~ m/jobIdentifier\s+=\s+(\d+);/; # \s matches spaces (+ = at least one) \d decimals |
| 148 |
print "farmed via xgrid: job id = $jobid \n"; |
| 149 |
system "echo echo deleting... >> cleanxgrid; echo xgrid $xgridauth -job delete -id $jobid >> cleanxgrid"; |
| 150 |
system "echo echo getting results... >> getresultsxgrid; echo xgrid $xgridauth -job results -id $jobid >> getresultsxgrid"; |
| 151 |
sleep 1; # avoid multiple rapid-fire requests to xgrid server |
| 152 |
} |
| 153 |
|
| 154 |
#--------------------------------------------------------- |
| 155 |
# subroutine to farm jobs via simple ssh commands |
| 156 |
# to a list of machines |
| 157 |
#--------------------------------------------------------- |
| 158 |
|
| 159 |
sub farmjob_ssh { |
| 160 |
my ($commandline,$n)=@_; |
| 161 |
my (@machines) = `cat machinelist` or die "ERROR: for ssh version must list machines in file machinelist \n"; |
| 162 |
my $nmachines = $#machines; |
| 163 |
if ( $nmachines < 1 ) { |
| 164 |
die "ERROR: no machines specified in file machinelist \n"; |
| 165 |
} |
| 166 |
my $pwd = `pwd`; |
| 167 |
chomp($pwd); |
| 168 |
|
| 169 |
my $njobsrun = 0; |
| 170 |
my $loadavmax = 0.5; |
| 171 |
my $bs = '\\'; |
| 172 |
|
| 173 |
# loop through all available machines looking for spare CPU |
| 174 |
while ($njobsrun < 1) { |
| 175 |
#--set which machine to try |
| 176 |
if ($n > $nmachines) { |
| 177 |
print "waiting for machines to become free \n"; |
| 178 |
sleep 30; |
| 179 |
$n = 0; |
| 180 |
}; |
| 181 |
my $machine = $machines[$n]; |
| 182 |
chomp($machine); |
| 183 |
|
| 184 |
my $loadav = 0.0; |
| 185 |
print "trying $machine: "; |
| 186 |
#--get load average for this machine using uptime command |
| 187 |
if ($loadavmax > 0.0) { |
| 188 |
my $uptime=`ssh $machine uptime`; |
| 189 |
($loadav) = $uptime =~ m/load ave.+:\s(\d+\.\d+)./; |
| 190 |
print " load average last 1 minute = $loadav "; |
| 191 |
} |
| 192 |
#--set job running if not busy |
| 193 |
if ( $loadav < $loadavmax ) { |
| 194 |
print " ...OK \n"; |
| 195 |
$njobsrun = $njobsrun + 1; |
| 196 |
# run the job |
| 197 |
print "running $commandline on machine $machine at nice +19\n"; |
| 198 |
system "ssh -f $machine 'cd $pwd; nice +19 $commandline < /dev/null >& /dev/null &'"; |
| 199 |
} else { |
| 200 |
print " ...too busy \n"; |
| 201 |
} |
| 202 |
$n = $n + 1; |
| 203 |
} |
| 204 |
$_[1] = $n; |
| 205 |
} |