2014-02-11 52 views

回答

0

倍频支持通过是多线程库在Linux上多线程。请参阅Get GNU Octave to work with a multicore processor. (Multithreading) Atlas通过Cyqwin支持Windows http://math-atlas.sourceforge.net/faq.html#where我在Gentoo Linux上使用过这些库,并且在运行单个进程时速度有所提高。这些库自动在多个处理器之间划分一些操作。这当然不是最佳的解决方案,因为它只适用于处理非常大的矩阵。在Windows上运行也可能很困难。我知道我在Linux上遇到了一些问题。调整库时,不要使用多个作业进行编译(请参阅编译器文档)。它每次尝试都失败了。

更好的解决办法是,如果你可以把你的问题分成更小的问题,这些问题是相互独立的,这样它们的执行顺序并不重要。我写了一个小Perl脚本来分解多个对Octave的调用,因为Octave没有创建线程的本地方式。由于您在Windows上,除非您使用Cyqwin,否则需要进行一些调整。

#!/usr/bin/perl -wT 

$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin"; 
use strict; 
use POSIX qw(setsid :sys_wait_h); 
use Time::Piece; 
use Time::Local; 

#------------------- GLOBALS ----------------------- 

my $go_me = 0; 
my $kid = 0; 
my $kid_pid = 0; 
my $num_children = 0; 

#------------- SUBROUTINES ---------------------- 

sub Interrupt 
{ $go_me = 0; } 

sub Interrupt_Die 
{ 
    $go_me = 0; 
    print localtime . " > $$ - Dieing: @_ $!\n"; 
    exit(0); 
} 

sub Child_Is_Done 
{ $num_children++; } 

#------------------ PROCESS OVERHEAD ------------------------- 

if($#ARGV != 1) 
{ 
    print "Usage: octbatch /path/filename [concurrency number]\n"; 
    exit(0); 
} 

my $stdout_file = ""; 

if($ENV{HOME} =~ m/^(\/home\/\w+)$/) 
{ 
    $stdout_file = "$1" . '/octbatch.log'; 
} 
else 
{ 
    print "Invalid value for $ENV{HOME}\nDon't run as root.\n"; 
    exit(0); 
} 

open STDIN,'/dev/null' or die "Can't read /dev/null: $!"; 
open STDOUT,'>',$stdout_file or die "Can't write to $stdout_file: $!"; 
open(STDERR, ">&STDOUT") or die "Can't write to $stdout_file: $!"; 
defined(my $pid = fork) or die "Can't fork: $!"; 
exit if $pid; 
setsid or die "Can't start a new session: $!"; 
my $parent = "$$"; 
print "Parent process is: $parent\n"; 
$go_me = 1; 
print localtime . " > $$ - Starting.\n"; 

#----------------------- MAIN CYCLE --------------------------------- 

my $filename = $ARGV[0]; 
unless(open(COMMANDFILE, "<$filename")) 
{ die "Could not open $filename $!\n"; } 
$num_children = $ARGV[1]; 
$SIG{'CHLD'} = 'Child_Is_Done'; 

while($go_me and my $line = <COMMANDFILE>) 
{ 
    if($num_children <= 0 and $kid_pid) 
    { 
     my $child_pid = -1; 
     $child_pid = wait(); 
     if(WIFEXITED($?)) 
     { print localtime . " > $$ - Process $child_pid exited\n"; } 
    } 

    chomp($line); 
    print localtime . " > $$ - Running $line\n"; 
    $num_children--; 
    Interrupt_Die("Can't fork!") unless defined($kid_pid = fork()); 

    if($kid_pid) 
    { 
     print localtime . " > $$ - I am the parent.\n"; 
     $SIG{'CHLD'} = 'Child_Is_Done'; 
    } 
    else 
    { 
     print localtime . " > $$ - I am the child.\n"; 
     $go_me = 0; 
     $kid = 1; 
     print localtime . " > $$ - Running $line\n"; 

     if($line =~ m/^(octave ([^;\n\r]+))$/) 
     { 
     $line = "$1"; 
     system("$line"); 
     } 
     else 
     { 
     print localtime . " > $$ - Invalid line. Skipping.\n"; 
     } 
    } 
} 

if($kid) 
{ 
    print localtime . " > $$ - Process $$ exited\n"; 
    exit(0); 
} 
else 
{ 
    my $child_pid = -1; 

    do 
    { 
     $child_pid = waitpid(-1,0); 
     if(WIFEXITED($?)) 
     { print localtime . " > $$ - Process $child_pid exited\n"; } 
    } while($child_pid > 0); 

    print localtime . " > $$ - Stopping: $!\n"; 
    exit(0); 
} 
相关问题