| 1 | package Parallel::Mpich::MPD; | ||
| 2 | |||
| 3 | 12 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 12 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 12 | ▶ | use File::Temp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 12 | ▶ | use IO::All; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 12 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 12 | ▶ | use Time::HiRes qw( usleep); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 12 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 12 | ▶ | use Parallel::Mpich::MPD::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Parallel::Mpich::MPD::Job; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Parallel::Mpich::MPD - Mpich MPD wrapper | ||
| 16 | |||
| 17 | =item I<$VERSION> | ||
| 18 | |||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | our $VERSION = '0.9.3'; | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | use Parallel::Mpich::MPD; | ||
| 26 | |||
| 27 | # VERBOSE LEVEL | ||
| 28 | #$Parallel::Mpich::MPD::Common::WARN=1; | ||
| 29 | #$Parallel::Mpich::MPD::Common::DEBUG=1; | ||
| 30 | |||
| 31 | #CHECK ENV | ||
| 32 | Parallel::Mpich::MPD::Common::env_Hostsfile(/path/to/machinesfile); | ||
| 33 | Parallel::Mpich::MPD::Common::env_MpichHome(/path/to/mpdhome); | ||
| 34 | Parallel::Mpich::MPD::Common::env_Check(); | ||
| 35 | |||
| 36 | #CHECK MPD AND NETWORK | ||
| 37 | my %hostsup; | ||
| 38 | my %hostsdown; | ||
| 39 | my %info=Parallel::Mpich::MPD::info(); #check mpd master | ||
| 40 | print Dumper(\%info) | ||
| 41 | %hostsup= Parallel::Mpich::MPD::Common::checkHosts(hostsdown => \%hostsdown ); #check ping and ssh on machines | ||
| 42 | %hostsup= Parallel::Mpich::MPD::check( reboot =>1:0, hostsdown=>\%hostsdown); #check mpds instances and try to repair | ||
| 43 | ... | ||
| 44 | |||
| 45 | # USE MPD | ||
| 46 | Parallel::Mpich::MPD::boot(); #start mpd instances defined by default machinesfile | ||
| 47 | my $alias1=Parallel::Mpich::MPD::makealias(); | ||
| 48 | if Parallel::Mpich::MPD::createJob(cmd => $cmd, params => $parms, $machinesfile => $hostsfile, alias => $alias1)){ | ||
| 49 | my $job=Parallel::Mpich::MPD::findJob(jobalias => $alias, getone => 1); | ||
| 50 | $job->sig_kill() if defined $job; | ||
| 51 | } | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | This I<Parallel::Mpich::MPD>, a wrapper module for MPICH2 Process Management toolkit from L<http://www-unix.mcs.anl.gov/mpi/mpich2/>. | ||
| 56 | The wrapper include the following tools: basic configuration, mpdcheck, mpdboot, mpdcleanup, mpdtrace, | ||
| 57 | mpdringtest, mpdallexit, mpiexec, mpdsigjob and mpdlistjobs. | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item boot(hosts => @hosts, machinesfile => $machines, checkOnly => 1|0, output => \$output) | ||
| 62 | |||
| 63 | starts a set of mpd's on a list of machines. boot try to verify that the hosts in the host | ||
| 64 | file are up before attempting start mpds on any of them. | ||
| 65 | |||
| 66 | =item rebootHost(host => $hostname) | ||
| 67 | |||
| 68 | restart mpd on the specified host. rebootHost will kill old mpds before restarting a new one. | ||
| 69 | The killed MPDS are filtered by specific port and host. | ||
| 70 | |||
| 71 | =item check(machinesfile => $file, hostsup => \%hosts, hostsdown => \%hostsdown , reboot => 1) | ||
| 72 | |||
| 73 | Check if MPD master and nodes are well up. If MPD master is down it try to ping and ssh machines. | ||
| 74 | If you use the option reboot, check will try to restart mpd on specified machines or to reboot the master. | ||
| 75 | |||
| 76 | =item info( ) | ||
| 77 | |||
| 78 | return an %info of the master with the following keys (master, hostname, port) | ||
| 79 | |||
| 80 | =item validateMachinesfile(machinefiles => $filename) | ||
| 81 | |||
| 82 | check with mpdtrace if all machines specified by filename are up. If not, a temporary file is | ||
| 83 | created with the resized machinesfile | ||
| 84 | |||
| 85 | =item shutdown( ) | ||
| 86 | |||
| 87 | causes all mpds in the ring to exit | ||
| 88 | |||
| 89 | =item createJob({cmd => $cmd , machinesfile=> $filename, [params => $params], [ncpu => $ncpu], [alias => $alias]) | ||
| 90 | |||
| 91 | start a new job with the command line and his params. It return true if ok. | ||
| 92 | WARNING ncpu could be redefined if mpdtrace return à small hosts list | ||
| 93 | |||
| 94 | Example: | ||
| 95 | |||
| 96 | Parallel::Mpich::MPD::createJob(cmd => $cmd, params => $parms, ncpu => '3', alias => 'job1'); | ||
| 97 | |||
| 98 | =item listJobs([mpdlistjobs_contents=>$str]) | ||
| 99 | |||
| 100 | Return an Parallel::Mpich::MPD::Job array for all available jobs | ||
| 101 | If mpdlistjobs_contents argument is present, the code will not call mpdlistjobs but | ||
| 102 | take the parameter as a fake results of this command | ||
| 103 | |||
| 104 | =item findJob([%criteria][, return=>(getone|host2pidlist)) | ||
| 105 | |||
| 106 | find a job from crtiteria. It return a Job instance or undef for no match | ||
| 107 | |||
| 108 | =over 4 | ||
| 109 | |||
| 110 | =item Criteria can be of | ||
| 111 | |||
| 112 | =item username=>'somename' or username=>\@arrayOfNames | ||
| 113 | |||
| 114 | =item jobid=>'somename' or jobid=>\@arrayOfJobid | ||
| 115 | |||
| 116 | =item jobalias=>'somename' or jobalias=>\@arrayOfJobalias | ||
| 117 | |||
| 118 | To set an array of names; | ||
| 119 | |||
| 120 | $criteria{psid} [&& $criteria{rank}] You can select psid from the specified rank. | ||
| 121 | $criteria{reloadlist} force the call of listjobs | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head4 return value | ||
| 126 | |||
| 127 | By default (no return=>... argument), returned value will be a hash (or a hash ref, depending on context), {jobid1=>$job1, jobid2=>job2,...} | ||
| 128 | |||
| 129 | =over 4 | ||
| 130 | |||
| 131 | =item return=>'getone' | ||
| 132 | |||
| 133 | Will force to return the one job matching, or undef if none or error if many. | ||
| 134 | |||
| 135 | =item return=>'host2pidlist' | ||
| 136 | |||
| 137 | return a hash (or a ref to this hash, depending on context), host=>\@pidlist | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =head4 Examples | ||
| 142 | |||
| 143 | =begin verbatim | ||
| 144 | |||
| 145 | Parallel::Mpich::MPD::findJob(alias => 'olivier1', return=>'getone')->sendSig(); | ||
| 146 | my %kobs=Parallel::Mpich::MPD::findJob(alias => ['olivier1', 'olivier2']); | ||
| 147 | my $refjobs=Parallel::Mpich::MPD::findJob(alias => ['olivier1', 'olivier2']); | ||
| 148 | my $job=Parallel::Mpich::MPD::findJob(jobid => '1@linux02_32996', return=>'getone'); | ||
| 149 | |||
| 150 | =end verbatim | ||
| 151 | |||
| 152 | =item trace([hosts => %hosts], long => 1) | ||
| 153 | |||
| 154 | Lists the hostname of each of the mpds in the ring | ||
| 155 | return true if ok | ||
| 156 | |||
| 157 | [long=1] shows full hostnames and listening ports and ifhn | ||
| 158 | |||
| 159 | =item makealias( ) | ||
| 160 | |||
| 161 | "handle-" + PID + RAND(100) + Instance COUNTER++ | ||
| 162 | |||
| 163 | return a uniq string alias | ||
| 164 | |||
| 165 | =item clean([hosts => %hosts] [killcmd=>"cmd"]) | ||
| 166 | |||
| 167 | Removes the Unix socket on local (the default) and remote machines | ||
| 168 | This is useful in case the mpd crashed badly and did not remove it, which it normally does | ||
| 169 | [hosts => %hosts] use specified hosts , | ||
| 170 | [$cleancmd="cmd"] user defined kill command | ||
| 171 | |||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Olivier Evalet, Alexandre Masselot, C<< <alexandre.masselot at genebio.com> >> | ||
| 176 | |||
| 177 | =head1 BUGS | ||
| 178 | |||
| 179 | Please report any bugs or feature requests to | ||
| 180 | C<bug-parallel-mpich-mpd at rt.cpan.org>, or through the web interface at | ||
| 181 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parallel-Mpich-MPD>. | ||
| 182 | I will be notified, and then you'll automatically be notified of progress on | ||
| 183 | your bug as I make changes. | ||
| 184 | |||
| 185 | =head1 SUPPORT | ||
| 186 | |||
| 187 | You can find documentation for this module with the perldoc command. | ||
| 188 | |||
| 189 | perldoc Parallel::Mpich::MPD | ||
| 190 | |||
| 191 | You can also look for information at: | ||
| 192 | |||
| 193 | =over 4 | ||
| 194 | |||
| 195 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 196 | |||
| 197 | L<http://annocpan.org/dist/Parallel-Mpich-MPD> | ||
| 198 | |||
| 199 | =item * CPAN Ratings | ||
| 200 | |||
| 201 | L<http://cpanratings.perl.org/d/Parallel-Mpich-MPD> | ||
| 202 | |||
| 203 | =item * RT: CPAN's request tracker | ||
| 204 | |||
| 205 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parallel-Mpich-MPD> | ||
| 206 | |||
| 207 | =item * Search CPAN | ||
| 208 | |||
| 209 | L<http://search.cpan.org/dist/Parallel-Mpich-MPD> | ||
| 210 | |||
| 211 | =back | ||
| 212 | |||
| 213 | =head1 ACKNOWLEDGEMENTS | ||
| 214 | |||
| 215 | =head1 COPYRIGHT & LICENSE | ||
| 216 | |||
| 217 | Copyright 2006 Olivier Evalet, Alexandre Masselot, all rights reserved. | ||
| 218 | |||
| 219 | This program is free software; you can redistribute it and/or modify it | ||
| 220 | under the same terms as Perl itself. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | require Exporter; | ||
| 225 | our $cached_all_jobs={}; | ||
| 226 | our (@ISA, @EXPORT, @EXPORT_OK); | ||
| 227 | @ISA = qw(Exporter); | ||
| 228 | |||
| 229 | |||
| 230 | @EXPORT = qw(); | ||
| 231 | @EXPORT_OK = (); | ||
| 232 | |||
| 233 | |||
| 234 | #sub new{ | ||
| 235 | # my $pkg=shift; | ||
| 236 | # my $this={}; | ||
| 237 | # env_Init() unless defined(%env); | ||
| 238 | # bless $this, $pkg; | ||
| 239 | # return $this; | ||
| 240 | #} | ||
| 241 | |||
| 242 | # hosts => @hosts, machinesfile => $machines, checkOnly => 1|0,check => \$output | ||
| 243 | sub boot{ | ||
| 244 | my %params=@_; | ||
| 245 | env_Init(); | ||
| 246 | my $cmdparms=""; #only one instance of mpd should run | ||
| 247 | my $usermachinesfile; | ||
| 248 | $usermachinesfile.="$params{machinesfile}" if defined $params{machinesfile}; | ||
| 249 | $usermachinesfile.=Parallel::Mpich::MPD::Common::__param_buildHost(@{$params{hosts}}) if defined $params{hosts}; | ||
| 250 | |||
| 251 | # if (defined $env{info}{user}){ | ||
| 252 | # $cmdparms.=" -u $env{info}{user}"; | ||
| 253 | # } | ||
| 254 | |||
| 255 | if (defined $env{conf}{rpc}){ | ||
| 256 | $cmdparms.=" -r $env{conf}{rpc}"; | ||
| 257 | } | ||
| 258 | if (defined $usermachinesfile){ | ||
| 259 | $cmdparms.=" -f $usermachinesfile"; | ||
| 260 | $cmdparms.=" -n ".Parallel::Mpich::MPD::Common::nbHostInMachinefile($usermachinesfile); | ||
| 261 | }elsif (defined $env{conf}{mpd}{hostsfile}){ | ||
| 262 | my ($ncpu,$stripfile)=Parallel::Mpich::MPD::Common::stripMachinefile($env{conf}{mpd}{hostsfile}); | ||
| 263 | $cmdparms.=" -f $stripfile "; | ||
| 264 | $cmdparms.=" -n $ncpu "; | ||
| 265 | } | ||
| 266 | # if (defined $env{conf}{mpiexec}{ncpu}){ | ||
| 267 | # $cmdparms.=" -n ".($env{conf}{mpiexec}{ncpu}+1); | ||
| 268 | # } | ||
| 269 | if (defined($params{checkOnly})){ | ||
| 270 | $cmdparms.=" --chkuponly --verbose "; | ||
| 271 | }elsif (defined($params{verbose})){ | ||
| 272 | $cmdparms.=" --verbose "; | ||
| 273 | } | ||
| 274 | |||
| 275 | my %execparms=(cmd => commandPath('mpdboot')." $cmdparms"); | ||
| 276 | if (defined($params{output})){ | ||
| 277 | $execparms{stdout}=$params{output} | ||
| 278 | } | ||
| 279 | my $ret=Parallel::Mpich::MPD::Common::__exec(%execparms); | ||
| 280 | |||
| 281 | |||
| 282 | return $ret==0; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub info{ | ||
| 286 | #genebio-95_59850 (192.168.173.95) | ||
| 287 | my %params=@_; | ||
| 288 | env_Init(); | ||
| 289 | my $stdout=""; | ||
| 290 | my $stderr=""; | ||
| 291 | my $jobret=Parallel::Mpich::MPD::Common::__exec(cmd => commandPath('mpdtrace')." -l",stdout => \$stdout, stderr=>\$stderr); | ||
| 292 | my @hosts=split /\n/, $stdout; | ||
| 293 | my %info=(); | ||
| 294 | if ($jobret==0){ | ||
| 295 | $info{master}=$hosts[0]; | ||
| 296 | my $tmp=$info{master}; | ||
| 297 | $tmp=~ s/(\S+)_(\d+)/$info{hostname}=$1;$info{port}=$2/e; | ||
| 298 | print "DEBUG:MPD::info:". Dumper(\%info) if $Parallel::Mpich::MPD::Common::DEBUG==1; | ||
| 299 | } | ||
| 300 | return %info; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | sub rebootHost{ | ||
| 305 | my %params=@_; | ||
| 306 | my $stdout; | ||
| 307 | env_Init(); | ||
| 308 | |||
| 309 | my %mpdinfo=(); | ||
| 310 | unless (defined($params{host})){ | ||
| 311 | print STDERR "ERROR:rebootHost(1): you should define an hostname where to start mpd \n"; | ||
| 312 | return %mpdinfo; | ||
| 313 | } | ||
| 314 | |||
| 315 | %mpdinfo=info(); | ||
| 316 | |||
| 317 | if((my $c=%mpdinfo) eq "0"){ | ||
| 318 | print STDERR "ERROR:rebootHost(2): MPD seems dead, restart the mpd system \n"; | ||
| 319 | return undef; | ||
| 320 | } | ||
| 321 | |||
| 322 | #TODO make this better ;) | ||
| 323 | # pkill -f mpd.py | ||
| 324 | my $kill; | ||
| 325 | my $cmd; | ||
| 326 | if (system("pkill --help &>/dev/null")){ | ||
| 327 | $kill="pkill -U $env{info}{user} -f mpd.py"; | ||
| 328 | $cmd="ssh $params{'host'} 'pkill -U $env{info}{user} -f mpd.py'"; | ||
| 329 | }else{ | ||
| 330 | $kill="ps -U $env{info}{user} -o pid,command|grep -e \"$mpdinfo{hostname} -p $mpdinfo{port}\" |grep -v grep |cut -d \" \" -f2"; | ||
| 331 | $cmd="ssh $params{'host'} '$kill|xargs kill 2>/dev/null'"; | ||
| 332 | } | ||
| 333 | my $ret=system $cmd; | ||
| 334 | |||
| 335 | $cmd="ssh $params{'host'} ". commandPath('mpd')." -h $mpdinfo{hostname} -p $mpdinfo{port} --ncpus=1 -e -d"; | ||
| 336 | |||
| 337 | my $res=Parallel::Mpich::MPD::Common::__exec(cmd => $cmd, stdout => \$stdout); | ||
| 338 | |||
| 339 | if ($res!=0){ | ||
| 340 | print STDERR "ERROR:rebootHost(3): Could not start mpd on host $params{host} \n"; | ||
| 341 | return undef; | ||
| 342 | } | ||
| 343 | |||
| 344 | print "INFO:restart mpd on $params{'host'}:$mpdinfo{port} \n" if $Parallel::Mpich::MPD::Common::WARN==1; | ||
| 345 | |||
| 346 | #check mpdtrace to detect the new host | ||
| 347 | # my %hosts; | ||
| 348 | # my $res=trace(hosts => \%hosts); | ||
| 349 | # return (defined($hosts{$params{host}}) && $hosts{$params{host}}==1)?1:undef; | ||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | # TODO check should also check on each nodes that: | ||
| 354 | # $HOME/.mpd.conf owner and attr=600 | ||
| 355 | # check DNS hostname for master and nodes | ||
| 356 | |||
| 357 | # use case check | ||
| 358 | # ================= | ||
| 359 | |||
| 360 | # 1) unplug network wire | ||
| 361 | # 2) some mpd died correctly | ||
| 362 | # 3) some mpd died badly | ||
| 363 | # 4) all nodes are dead | ||
| 364 | |||
| 365 | # | ||
| 366 | # | ||
| 367 | # machinesfile => $file, hostsup => \%hosts, hostsdown => \%hostsdown , reboot => 1 | ||
| 368 | sub check{ | ||
| 369 | my %params=@_; | ||
| 370 | env_Init(); | ||
| 371 | my $out=""; | ||
| 372 | my %hosts=(); | ||
| 373 | my $should_resize; | ||
| 374 | |||
| 375 | my ($ncpu,$stripfile)=Parallel::Mpich::MPD::Common::stripMachinefile($env{conf}{mpd}{hostsfile}) unless defined $params{machinesfile}; | ||
| 376 | |||
| 377 | my $machinesfile=(defined $params{machinesfile})?$params{machinesfile}:$stripfile; | ||
| 378 | #trace hosts | ||
| 379 | |||
| 380 | #1) check mpdtrace hosts | ||
| 381 | my $res=trace(hosts => \%hosts); | ||
| 382 | |||
| 383 | my $machines=io($machinesfile)->slurp; | ||
| 384 | my %hostsdown; | ||
| 385 | if ($res){ | ||
| 386 | #compare hosts result with the input machinesfile | ||
| 387 | my %hostsup; | ||
| 388 | |||
| 389 | foreach (split /[\n\r]/, $machines){ | ||
| 390 | next unless /\S/; | ||
| 391 | next if /#.*$/; | ||
| 392 | s/([^.]+).*$/$1/; | ||
| 393 | if (defined($hosts{$_}) && $hosts{$_}==1){ | ||
| 394 | $hostsup{$_}=1; | ||
| 395 | }else{ | ||
| 396 | $should_resize=1; | ||
| 397 | if (defined $params{reboot} && $params{reboot}==1){ | ||
| 398 | $hostsdown{$_}=1 unless(defined(rebootHost(host =>"$_"))); | ||
| 399 | }else{ | ||
| 400 | $hostsdown{$_}=1; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } | ||
| 404 | # here the status is wrong, cluster should be resized | ||
| 405 | if (defined $should_resize && defined $params{reboot} && $params{reboot}==1){ | ||
| 406 | %hostsup=check(machinesfile => $machinesfile); | ||
| 407 | } | ||
| 408 | %{$params{hostsup}}=%hostsup if defined $params{hostsup}; | ||
| 409 | %{$params{hostsdown}}=%hostsdown if defined $params{hostsdown}; | ||
| 410 | |||
| 411 | return %hostsup ; | ||
| 412 | |||
| 413 | |||
| 414 | }else{ | ||
| 415 | foreach (split /[\n\r]/, $machines){ | ||
| 416 | next unless /\S/; | ||
| 417 | next if /#.*$/; | ||
| 418 | s/([^.]+).*$/$1/; | ||
| 419 | $hostsdown{$_}=1; | ||
| 420 | } | ||
| 421 | %{$params{hostsdown}}=%hostsdown if defined $params{hostsdown}; | ||
| 422 | |||
| 423 | print "ERROR:MPD::check(): MPD seems down, it's a really bad news!\n" if ($Parallel::Mpich::MPD::Common::WARN == 1); | ||
| 424 | if (defined $params{reboot} && $params{reboot}==1){ | ||
| 425 | print "INFO: Clean all MPD.\n"; | ||
| 426 | clean(pkill=>1); | ||
| 427 | print "INFO: trying to restart MPD.\n"; | ||
| 428 | boot(); | ||
| 429 | return check(machinesfile => $machinesfile, hostsup=>$params{hostsup}, hostsdown=>$params{hostsdown}); | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | #2) check vailable hosts (ping + ssh publicKey) if mpdtrace return FALSE | ||
| 434 | checkHosts(machinesfile => $machinesfile, hostsup => \%hosts); | ||
| 435 | unless (keys %hosts){ | ||
| 436 | # | ||
| 437 | # FATAL ERROR: the cluster is dead | ||
| 438 | # | ||
| 439 | die "ERROR: your cluster is dead! All Hosts defined in $machinesfile are down."; | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | #3) should clean and restart mpd ? | ||
| 444 | |||
| 445 | |||
| 446 | return %hosts=(); | ||
| 447 | } | ||
| 448 | |||
| 449 | |||
| 450 | |||
| 451 | |||
| 452 | |||
| 453 | |||
| 454 | #use a cached jobs to force the update on each getJobs call!!! | ||
| 455 | sub __jobsFactory{ | ||
| 456 | |||
| 457 | my $myjobs=shift; | ||
| 458 | my $host={}; | ||
| 459 | my @allentries; | ||
| 460 | my $job;#={infos => {jobid=>''}}; | ||
| 461 | |||
| 462 | $cached_all_jobs={}; | ||
| 463 | |||
| 464 | #map result to hash | ||
| 465 | foreach (split/\n\n/, $myjobs){ | ||
| 466 | my %h; | ||
| 467 | s/(\S+)[\s=]+(\S*)?$/$h{$1}=$2/emg; | ||
| 468 | push @allentries, \%h; | ||
| 469 | } | ||
| 470 | |||
| 471 | #order job | ||
| 472 | foreach (@allentries){ | ||
| 473 | my $data=$_; | ||
| 474 | $job=$cached_all_jobs->{$data->{jobid}}||=Parallel::Mpich::MPD::Job->new(jobid=>$data->{jobid}); | ||
| 475 | # if (!$job || ($data->{jobid} ne $job->jobid)){ | ||
| 476 | # $job=P; | ||
| 477 | # =$job; | ||
| 478 | # $job->jobid($data->{jobid}); | ||
| 479 | $job->jobalias($data->{jobalias}); | ||
| 480 | $job->username($data->{username}); | ||
| 481 | # } | ||
| 482 | $host={}; | ||
| 483 | $host->{host} =$data->{host}; | ||
| 484 | $host->{pid} =$data->{pid}; | ||
| 485 | $host->{sid} =$data->{sid}; | ||
| 486 | $host->{rank} =$data->{rank}; | ||
| 487 | $host->{pgm} =$data->{pgm}; | ||
| 488 | $job->hosts_push($host); | ||
| 489 | } | ||
| 490 | |||
| 491 | print STDERR (values %{$cached_all_jobs}) if ($Parallel::Mpich::MPD::Common::DEBUG == 1); | ||
| 492 | return values %{$cached_all_jobs}; | ||
| 493 | } | ||
| 494 | |||
| 495 | |||
| 496 | |||
| 497 | sub validateMachinesfile{ | ||
| 498 | my %params=@_; | ||
| 499 | my $trace=""; | ||
| 500 | my %hosts=(); | ||
| 501 | return undef unless defined $params{machinesfile}; | ||
| 502 | my $retfile=$params{machinesfile}; | ||
| 503 | |||
| 504 | |||
| 505 | if ( defined($params{machinesfile}) && -e $params{machinesfile}){ | ||
| 506 | #1) check mpdtrace hosts | ||
| 507 | my $res=trace(hosts => \%hosts); | ||
| 508 | |||
| 509 | if ($res){ | ||
| 510 | #compare hosts result with the input machinesfile | ||
| 511 | my $machines=io($params{machinesfile})->slurp; | ||
| 512 | print "DEBUG:validateMachinesfile(1) : machines=$machines\n\n" if ($Parallel::Mpich::MPD::Common::DEBUG == 1); | ||
| 513 | |||
| 514 | my $fhosts = new File::Temp(UNLINK=>0, TEMPLATE => File::Spec->tmpdir."/$TMP_MPD_PREFIX-machines-XXXX"); | ||
| 515 | foreach (split /[\n\r]/, $machines){ | ||
| 516 | next unless /\S/; | ||
| 517 | next if /#.*$/; | ||
| 518 | s/([^.]+).*$/$1/; | ||
| 519 | |||
| 520 | if (defined($hosts{$_}) && $hosts{$_}==1 ){ | ||
| 521 | print $fhosts $_."\n"; | ||
| 522 | print "validated machines : ".$_."\n" if ($Parallel::Mpich::MPD::Common::DEBUG == 1); | ||
| 523 | }else{ | ||
| 524 | $retfile=$fhosts->filename; | ||
| 525 | print STDERR "WARNING: node $_ defined on machinesfile [".$params{machinesfile}."] is not available\n"; | ||
| 526 | } | ||
| 527 | } | ||
| 528 | }else{ | ||
| 529 | print STDERR "ERROR:validateMachinesfile(1): fatal mpd error \n"; | ||
| 530 | } | ||
| 531 | }else{ | ||
| 532 | print STDERR "ERROR:validateMachinesfile(2): could not open file : ".$params{machinesfile}."\n"; | ||
| 533 | } | ||
| 534 | |||
| 535 | return $retfile; | ||
| 536 | } | ||
| 537 | |||
| 538 | |||
| 539 | sub isJobRegistered{ | ||
| 540 | my ($alias)=@_; | ||
| 541 | # we don't know if job is register without alias | ||
| 542 | return 1 unless defined $alias; | ||
| 543 | if(system(commandPath('mpdlistjobs')." 2>/dev/null|grep -qe \"alias.*$alias\"")){ | ||
| 544 | return 0; | ||
| 545 | }else{ | ||
| 546 | print STDERR "INFO: job $alias is registered \n" if ($Parallel::Mpich::MPD::Common::WARN == 1); | ||
| 547 | return 1; | ||
| 548 | } | ||
| 549 | } | ||
| 550 | |||
| 551 | sub waitJobRegistration{ | ||
| 552 | my ($alias)=@_; | ||
| 553 | my $TIMEOUT=0; | ||
| 554 | do{ | ||
| 555 | usleep (400*1000); | ||
| 556 | $TIMEOUT+=400; | ||
| 557 | }while(!isJobRegistered($alias) && $TIMEOUT<10000); | ||
| 558 | return 1; | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | |||
| 563 | # TODO check that the machine file contains booted hosts | ||
| 564 | # FIXME create job should wait until job is correctly registered by mpd. | ||
| 565 | sub createJob{ | ||
| 566 | my %params=@_; | ||
| 567 | env_Init(); | ||
| 568 | my $spawn = 'yes'; | ||
| 569 | $spawn=$params{spawn} if exists $params{spawn}; | ||
| 570 | my $_out; | ||
| 571 | my $_err; | ||
| 572 | my $_pid=""; | ||
| 573 | |||
| 574 | my $mpiexecArgs=""; | ||
| 575 | $mpiexecArgs.=" -a $params{alias}" if defined($params{alias}); | ||
| 576 | |||
| 577 | my $inputfile=(defined $params{machinesfile})?$params{machinesfile}:Parallel::Mpich::MPD::Common::env_Hostsfile(); | ||
| 578 | my $machinesfile=validateMachinesfile(machinesfile => $inputfile); | ||
| 579 | $mpiexecArgs.=" -machinefile ".$machinesfile; | ||
| 580 | |||
| 581 | $mpiexecArgs.="-u $params{user}" if defined($params{user}); | ||
| 582 | |||
| 583 | #TODO much more here see mpiexec -hosts | ||
| 584 | #$mpiexecArgs.=" -n $params{ncpu}" if defined($params{ncpu}); | ||
| 585 | |||
| 586 | if(defined $params{ncpu} && "$inputfile" eq "$machinesfile"){ | ||
| 587 | $mpiexecArgs.=" -n $params{ncpu}"; | ||
| 588 | }else{ | ||
| 589 | my $n=Parallel::Mpich::MPD::Common::nbHostInMachinefile($machinesfile); | ||
| 590 | $mpiexecArgs.=" -n $n"; | ||
| 591 | } | ||
| 592 | $mpiexecArgs.=" -env LD_ASSUME_KERNEL ".$ENV{LD_ASSUME_KERNEL} if ($ENV{LD_ASSUME_KERNEL}); | ||
| 593 | |||
| 594 | if (! defined($params{cmd})){ | ||
| 595 | carp "ERROR: key cmd not available"; | ||
| 596 | return undef; | ||
| 597 | } | ||
| 598 | # hosts info | ||
| 599 | my $cmdparms="";#.="-f ".Parallel::Mpich::MPD::Common::__param_buildHost(@hosts) if defined(@hosts); | ||
| 600 | my $cmd=commandPath('mpiexec')." $mpiexecArgs $params{cmd} $params{params}"; | ||
| 601 | print STDERR "hostfile=".$machinesfile."\n"; | ||
| 602 | |||
| 603 | |||
| 604 | my %args=(cmd => $cmd); | ||
| 605 | $args{params}=\$params{params} if (defined($params{params}) ); | ||
| 606 | $args{spawn}=\$params{spawn} if (defined($params{spawn}) ); | ||
| 607 | $args{stdout}=$params{stdout} if (defined($params{stdout}) ); | ||
| 608 | $args{stderr}=$params{stderr} if (defined($params{stderr}) ); | ||
| 609 | $args{pid}=$params{pid} if (defined($params{pid}) ); | ||
| 610 | |||
| 611 | my $ret=Parallel::Mpich::MPD::Common::__exec(%args); | ||
| 612 | waitJobRegistration($params{alias}) if defined($params{spawn}) && defined($params{alias}); | ||
| 613 | |||
| 614 | |||
| 615 | return $ret==0; | ||
| 616 | } | ||
| 617 | |||
| 618 | sub listJobs{ | ||
| 619 | my %hparams=@_; | ||
| 620 | env_Init(); | ||
| 621 | my $stdout=""; | ||
| 622 | my $stderr; | ||
| 623 | if(defined ($hparams{mpdlistjobs_contents})){ | ||
| 624 | $stdout=$hparams{mpdlistjobs_contents}; | ||
| 625 | }else{ | ||
| 626 | my $res=Parallel::Mpich::MPD::Common::__exec(cmd => commandPath('mpdlistjobs'), stdout => \$stdout); | ||
| 627 | # my $cmd=commandPath('mpdlistjobs'); | ||
| 628 | # $stdout=`$cmd`; | ||
| 629 | print "DEBUG: mpdlistjobs stdout= $stdout \n" if ($Parallel::Mpich::MPD::Common::DEBUG == 1); | ||
| 630 | if ($res!=0){ | ||
| 631 | carp "error executing line mpdlistjobs command\n"; | ||
| 632 | return undef; | ||
| 633 | } | ||
| 634 | if ($stdout eq ""){ | ||
| 635 | return undef; | ||
| 636 | } | ||
| 637 | } | ||
| 638 | |||
| 639 | |||
| 640 | return __jobsFactory($stdout); | ||
| 641 | } | ||
| 642 | |||
| 643 | |||
| 644 | |||
| 645 | |||
| 646 | #find a job from crtiteria. It return an Hash of Jobs instance or null for no match | ||
| 647 | #params | ||
| 648 | # $criteria{pid} [ && $criteria{rank}] | ||
| 649 | # $criteria{jobalias} | ||
| 650 | # $criteria{username} | ||
| 651 | # $criteria{reloadlist} | ||
| 652 | # $criteria{getone} return the first occurrence | ||
| 653 | |||
| 654 | sub findJob{ | ||
| 655 | my %criteria=@_; | ||
| 656 | #Transform criteria from array or scalar into a hash | ||
| 657 | foreach my $k(qw(jobalias username jobid)) { | ||
| 658 | next unless defined $criteria{$k}; | ||
| 659 | if(ref($criteria{$k}) eq 'ARRAY'){ | ||
| 660 | # print STDERR "k=[$k]\n"; | ||
| 661 | my %h; | ||
| 662 | foreach(@{$criteria{$k}}){ | ||
| 663 | # print STDERR "\$_=[$_]\n"; | ||
| 664 | $h{$_}=1; | ||
| 665 | } | ||
| 666 | $criteria{$k}=\%h; | ||
| 667 | }else{ | ||
| 668 | #scalar, make it array | ||
| 669 | $criteria{$k}={$criteria{$k}=>1}; | ||
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | #force to reload the list | ||
| 674 | listJobs() if defined($criteria{reloadlist}); | ||
| 675 | |||
| 676 | # # je ne comprend pas pourquoi $jobs->{$job->jobid} est un tableau!!! | ||
| 677 | # undef my $jobs; | ||
| 678 | # foreach my $job (values %{$cached_all_jobs}){ | ||
| 679 | # if ($job->equals(%criteria)){ | ||
| 680 | # #WRONG: returns the first one! | ||
| 681 | # return $job if (defined($criteria{getone})); | ||
| 682 | |||
| 683 | # if (defined($criteria{jobalias})) { | ||
| 684 | # push @{$jobs->{$job->jobid}}, $job; | ||
| 685 | |||
| 686 | # }elsif (defined($criteria{username})) { | ||
| 687 | # push @{$jobs->{$job->jobid}}, $job; | ||
| 688 | # }elsif (defined($criteria{jobid})) { | ||
| 689 | # push @{$jobs->{$job->jobid}},$job; | ||
| 690 | # }elsif (defined($criteria{pid})) { | ||
| 691 | # push @{$jobs->{$job->jobid}}, $job; | ||
| 692 | # } | ||
| 693 | # } | ||
| 694 | # } | ||
| 695 | my %retjobs; | ||
| 696 | foreach my $j (values %{$cached_all_jobs}){ | ||
| 697 | if((defined($j->jobid) && $criteria{jobid}{$j->jobid}) || | ||
| 698 | (defined($j->jobalias) && $criteria{jobalias}{$j->jobalias}) || | ||
| 699 | (defined($j->username) && $criteria{username}{$j->username})){ | ||
| 700 | $retjobs{$j->jobid}=$j; | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | if ($criteria{return} && $criteria{return} eq 'getone'){ | ||
| 705 | #no match!! | ||
| 706 | return undef unless %retjobs; | ||
| 707 | |||
| 708 | my $sz=keys %retjobs; | ||
| 709 | print __PACKAGE__."::findjobs() ".Dumper \%retjobs if $Parallel::Mpich::MPD::Common::WARN; | ||
| 710 | |||
| 711 | if($sz==1){ | ||
| 712 | #OK | ||
| 713 | return (values %retjobs)[0]; | ||
| 714 | }else{ | ||
| 715 | #too many matches | ||
| 716 | die "too many matches ($sz) for criteria | ||
| 717 | jobid=> (".($criteria{jobid} || join('|', keys %{$criteria{jobid}})).") | ||
| 718 | jobalias=> (".($criteria{jobalias} || join('|', keys %{$criteria{jobalias}})).") | ||
| 719 | username=>(".($criteria{username} || join('|', keys %{$criteria{username}})).") | ||
| 720 | "; | ||
| 721 | } | ||
| 722 | }elsif ($criteria{return} && $criteria{return} eq 'host2pidlist'){ | ||
| 723 | my %rethash; | ||
| 724 | foreach (values %retjobs){ | ||
| 725 | my @h=$_->hosts(); | ||
| 726 | foreach (@h){ | ||
| 727 | $rethash{$_->{host}}{$_->{pid}}=$_->{pgm}; | ||
| 728 | } | ||
| 729 | } | ||
| 730 | return wantarray?%rethash:\%rethash; | ||
| 731 | }else{ | ||
| 732 | return wantarray?%retjobs:\%retjobs; | ||
| 733 | } | ||
| 734 | } | ||
| 735 | |||
| 736 | |||
| 737 | sub trace{ | ||
| 738 | my %params=@_; | ||
| 739 | env_Init(); | ||
| 740 | my $stdout=""; | ||
| 741 | my $jobret=Parallel::Mpich::MPD::Common::__exec(cmd => commandPath('mpdtrace'),stdout => \$stdout); | ||
| 742 | if (defined($params{hosts}) && $jobret==0){ | ||
| 743 | my %tmp=%{$params{hosts}}; | ||
| 744 | foreach (split /\n/, $stdout){ | ||
| 745 | $tmp{$_}=1; | ||
| 746 | } | ||
| 747 | %{$params{hosts}}=%tmp; | ||
| 748 | } | ||
| 749 | return $jobret==0; | ||
| 750 | } | ||
| 751 | |||
| 752 | |||
| 753 | # | ||
| 754 | # clean | ||
| 755 | # Removes the Unix socket on local (the default) and remote machines | ||
| 756 | # This is useful in case the mpd crashed badly and did not remove it, which it normally does | ||
| 757 | # params | ||
| 758 | # [hosts => %hosts] pkill=>1 | ||
| 759 | sub clean{ | ||
| 760 | my %params=@_; | ||
| 761 | env_Init(); | ||
| 762 | my $cmdparms; | ||
| 763 | if (defined $params{hosts}){ | ||
| 764 | $cmdparms.=" -f ".Paralle l::Mpich::MPD::Common::__param_buildHost(@{$params{hosts}}) ; | ||
| 765 | }else{ | ||
| 766 | $cmdparms.=" -f ".Parallel::Mpich::MPD::Common::stripMachinefile($env{conf}{mpd}{hostsfile}); | ||
| 767 | } | ||
| 768 | $cmdparms.=" -k \"pkill -U $env{info}{user} -f mpd.py\"" if defined $params{pkill}; | ||
| 769 | |||
| 770 | my $cmd=commandPath('mpdcleanup')."$cmdparms 2>/dev/null"; | ||
| 771 | # print STDERR $cmd."\n"; | ||
| 772 | return system ($cmd); | ||
| 773 | } | ||
| 774 | |||
| 775 | sub shutdown{ | ||
| 776 | my $stdout=""; | ||
| 777 | my $stderr=""; | ||
| 778 | env_Init(); | ||
| 779 | my $ret2=Parallel::Mpich::MPD::Common::__exec(cmd => commandPath('mpdallexit')); | ||
| 780 | $ret2=clean() if $ret2; | ||
| 781 | return $ret2==0; | ||
| 782 | } | ||
| 783 | |||
| 784 | our $aliasCounter=0; | ||
| 785 | sub makealias{ | ||
| 786 | |||
| 787 | # alias is hablde + PID + RAND(100) + COUNTER++ | ||
| 788 | # inside a thread the aliascounter is a copy ! | ||
| 789 | return "handle-$$-".int(rand(100))."-".($aliasCounter++); | ||
| 790 | } | ||
| 791 | |||
| 792 | |||
| 793 | 1; # End of Parallel::Mpich::MPD | ||