如果处理器0上的断言失败,则运行mpirun的python脚本不会停止

时间:2015-10-14 08:27:59

标签: python assert mpi4py

我有一个python脚本,其中包含一组并行执行的操作,其中包含库mpi4py。 在操作结束时,具有等级0的处理器执行断言测试。如果断言失败,则进程应该停止并且程序终止。但是,该程序并没有退出,我猜这是因为其他处理器正在持有。如果断言失败,如何使程序结束执行? 我使用如下命令运行:

mpirun -np 10 python myscript.py 

然后我在代码中有一行代码:

if rank ==0:
    assert mytest()==0

1 个答案:

答案 0 :(得分:0)

而不是断言,你应该中止。

https://planet.scipy.org/docs/apiref/mpi4py.MPI.Comm-class.html#Abort

#!/usr/bin/perl

use strict;
use warnings;

use File::stat;
use Getopt::Long;

my $user      = $ENV{USER} // 'USER NOT SET';
my $home_path = "/abc/site/home/$user";
my $auth_path = "$home_path/.userauthentication";
my $machine   = 'c16991';
my $pgMachine = 'lc0140';

# How you error-out in Perl: just die
die ( "-F- .userauthentication file must be created in $home_path\n"
    . "-I- .userauthentication file format: <emailaddress> <unix pwd>.\n"
    . "-I- Please make sure $auth_path permission is set to 000\n  "
    )
    unless -e $auth_path
    ;

# stat does permissions for Perl. 
my $perms = stat( $auth_path )->mode & 0777;

if ( $perms ) { # non zero
    my $permstr = sprintf "%3.3o", $perms;
    die "-F- $auth_path permission is set to: $permstr";
}

# switch processing already baked-in.
GetOptions ( 'block=s' => \$DBB1
           , 'tag=s'   => \$tag
           , 'local=s' => \$local 
           ) 
    or die "Invalid option specified"
    ;

die '-F- Please enter tag value to proceed!'
    if ( $local = 'y' and not $tag )
    ;
# $logFile is undefined in your script.
open ( my $lh, '<', $logFile ) 
    or die "Could not open $logFile!"
    ;
open ( my $out, '>', "/tmp/transpose_$$.pl" ) 
    or die "Could not open transpose_$$ file!"
    ;

# No need for grep or sed.
while ( <$lh> ) { 
    #next unless s/.*Username: //; # grep + sed
    #s/;.*//; # sed 

    # better yet, this does it all:
    next unless my ( $cap ) = m/\bUsername:\s([^;]+)/;

    # Don't do this. 
    # There should be a better way than outputing another perl script.
    say {$out} "chomp $cap;"; 
    # Do you need to quote what you captured?
    # say {$out} "chomp '$cap';";

}
close $lh;