问题描述
我正在研究一个数学模型,该模型使用XFOIL生成的数据,XFOIL是一种流行的航空航天工具,用于查找机翼的升力和阻力系数.
I'm working on a mathematical model that uses data generated from XFOIL, a popular aerospace tool used to find the lift and drag coefficients on airfoils.
我有一个Perl脚本,该脚本使用不同的输入参数反复调用XFOIL以生成所需的数据.我需要XFOIL运行5600次,每次运行大约100秒,大约需要6.5天才能完成.
I have a Perl script that calls XFOIL repeatedly with different input parameters to generate the data I need. I need XFOIL to run 5,600 times, at around 100 seconds per run, soabout 6.5 days to complete.
我有一台四核计算机,但是我作为程序员的经验有限,而且我实际上只知道如何使用基本的Perl.
I have a quad-core machine, but my experience as a programmer is limited, and I really only know how to use basic Perl.
我想一次在自己的核心上运行四个XFOIL实例.像这样:
I would like to run four instances of XFOIL at a time, all on their own core. Something like this:
while ( 1 ) {
for ( i = 1..4 ) {
if ( ! exists XFOIL_instance(i) ) {
start_new_XFOIL_instance(i, input_parameter_list);
}
}
}
因此,当我们可以使用新的输入参数列表启动新实例时,程序将一直检查(或最好是休眠)直到XFOIL实例可用.
So the program is checking (or preferably sleeping) until an XFOIL instance is free, when we can start a new instance with the new input parameter list.
推荐答案
尝试 Parallel :: ForkManager .它是一个模块,为分叉此类过程提供了简单的界面.
Try Parallel::ForkManager. It's a module that provides a simple interface for forking off processes like this.
下面是一些示例代码:
#!/usr/bin/perl
use strict;
use warnings;
use Parallel::ForkManager;
my @input_parameter_list =
map { join '_', ('param', $_) }
( 1 .. 15 );
my $n_processes = 4;
my $pm = Parallel::ForkManager->new( $n_processes );
for my $i ( 1 .. $n_processes ) {
$pm->start and next;
my $count = 0;
foreach my $param_set (@input_parameter_list) {
$count++;
if ( ( $count % $i ) == 0 ) {
if ( !output_exists($param_set) ) {
start_new_XFOIL_instance($param_set);
}
}
}
$pm->finish;
}
$pm->wait_all_children;
sub output_exists {
my $param_set = shift;
return ( -f "$param_set.out" );
}
sub start_new_XFOIL_instance {
my $param_set = shift;
print "starting XFOIL instance with parameters $param_set!\n";
sleep( 5 );
touch( "$param_set.out" );
print "finished run with parameters $param_set!\n";
}
sub touch {
my $fn = shift;
open FILE, ">$fn" or die $!;
close FILE or die $!;
}
您将需要为start_new_XFOIL_instance和output_exists函数提供自己的实现,并且还需要定义自己的参数集以传递给XFOIL.
You'll need to supply your own implementations for the start_new_XFOIL_instance and the output_exists functions, and you'll also want to define your own sets of parameters to pass to XFOIL.
这篇关于如何使我的Perl脚本对子进程使用多个内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!