Perl问题在并行化后打印嵌套循环输出

时间:2014-10-03 13:14:29

标签: perl

我正在尝试打印the following code的输出,但是它有问题,有些行没有5列,有些值丢失,混合,......

./ script.pl文件5 0.00 0.021

#!/usr/bin/perl

use warnings;
use strict;

use Parallel::ForkManager;

my $outputfilename = $ARGV[0];
my $CPU_count      = $ARGV[1];
my $start          = $ARGV[2];
my $end            = $ARGV[3];


my $fork_manager = Parallel::ForkManager->new($CPU_count);

for ( my $k1 = $start; $k1 < $end; $k1 += 0.001 ) {

    $fork_manager->start and next;

    for ( my $k2 = $start; $k2 < $end; $k2 += 0.01 ) {
        for ( my $k3 = $start; $k3 < $end; $k3 += 0.001 ) {
            for ( my $k4 = $start; $k4 < $end; $k4 += 0.001 ) {
                for ( my $k5 = $start; $k5 < $end; $k5 += 0.001 ) {

open my $F1, '>',$outputfilename . "_" . $k1 . $k2 . $k3 . $k4 . $k5  or die $!;
         print  $F1 ("k1: $k1\tk2: $k2\tk3: $k3\tk4: $k4\tk5: $k5\n");

                }
            }
        }
    }
    $fork_manager->finish;
}
$fork_manager->wait_all_children;

输出错误

...
k1: 0.002   k2: 0.00    k3: 0.00    k4: 0.016   k5: 0.006
k1: 0.002   k00 k2: 0.00    k3: 0.001   k4: 0.012   k5: 0.003
k1: 0.003   k2: 0.00    k3: 0.012   k4: 0.013   k5: 0.001
k1: 0.003   k2: 0.00    k3: 0.012   0.011
....

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

您的叉子正在竞相写入文件。根据您对输出的要求,您应该在战略要点上放置独占文件锁。

在下面的示例中,至少线条不会混淆。

 use Fcntl qw(:flock);

 # ...
 flock($F1, LOCK_EX) or die $!;
 print $F1 ("k1: ",$k1,"\t","k2: ",$k2,"\t","k3: ",$k3,"\t","k4: ",$k4,"\t","k5: ",$k5,"\n");
 flock($F1, LOCK_UN);

答案 1 :(得分:2)

for ( my $k1 = $start; $k1 < $end; $k1 += 0.001 ) {

    $fork_manager->start and next;
    open ( my $output, ">", $outputfilename."_".$k1 ) or die $!;
    for ( my $k2 = $start; $k2 < $end; $k2 += 0.01 ) {
        for ( my $k3 = $start; $k3 < $end; $k3 += 0.001 ) {
            for ( my $k4 = $start; $k4 < $end; $k4 += 0.001 ) {
                for ( my $k5 = $start; $k5 < $end; $k5 += 0.001 ) {
                    print  {$output} ("k1: $k1\tk2: $k2\tk3: $k3\tk4: $k4\tk5: $k5\n");
                }
            }
        }
    }
    close ( $output );
    $fork_manager->finish;
}
$fork_manager->wait_all_children;


foreach ( my $filename, glob ( "$outputfilename_*" ) ) {
     #stick together the results of something. 
}

每个fork最终会得到一个文件 - 所以从0..1开始你将获得1000.但是每个fork都有一个活动的输出文件句柄,所以没有竞争条件。