1

Parallel::LoopsWin32::OLE创建多个Matlab实例时,如果我们使用

my $ML = Win32::OLE->new('Matlab.Application') or die "Cannot start MATLAB"

在每个循环中,程序都会崩溃并显示未处理的异常。

如果放弃my之前,$ML那么它可以工作,但在任何时候只有一个实例在运行。

4

4 回答 4

1

我不确定在并行循环中创建多个 MATLAB COM 服务器有什么好处。

默认情况下,服务器以共享模式创建,即由所有实例共享。MATLAB 引擎作为单线程向用户公开,因此在您的情况下,所有客户端计算都是串行运行的,而不是并行运行的。

幸运的是,您在专用模式下创建了 MATLAB COM 服务器:

Win32::OLE->new('Matlab.Application.single')

请参阅此页面了解更多信息

PS:我对 Perl 知之甚少 :)

于 2013-04-26T20:43:21.597 回答
0

Parellel::Loop are tested not working with Win32::OLE to start multiple instance of Matlab, but Parellel::Forkmanager works by using the "single" trick from Amro and clue from http://www.perlmonks.org/bare/?node_id=894058 for an error of "CoInitialize has not been called":

before the loop use:

use Win32::OLE; # qw(EVENTS); #Win32::OLE(0.1709) error 0x800401f0: "CoInitialize has not been called"  
Win32::OLE->Initialize();  

and inside the loop use:

my $ML = Win32::OLE->new('Matlab.Application.single') or die "Cannot start MATLAB";  
$ML->{'Visible'}=0;  
$ML->Execute('try;cd \''.$wkdir.'\';'.$executable.' '.$file.' '.$countfile.';catch;end;quit;');  

The purpose of use OLE instesd of just using:

system('matlab -automation -wait -r "try;cd \''.$wkdir.'\';'.$executable.' '.$file.' '.$countfile.';catch;end;quit;');  

is to hide the Matlab window, we just want it to do the work but we get the honor. Using Perl to achieve the parallellized matlab parfor effects we can keep all the available CPU busy doing tasks allocated before the parellel loop and collect/combine the results after that loop.

于 2013-04-28T04:05:04.313 回答
0

你真的应该展示你的完整代码。

如果您my $ML 传递给的匿名子例程中进行设置,Parallel::Loops那么您仅在进程中设置该值,并且该值对父进程不可用。

目前尚不清楚您将这些Matlab进程用于什么目的,但它们在其父进程(这是您的主程序启动的子进程之一)死后不会持续存在。

您可以尝试my @matlabs在循环外声明,然后执行

push @matlabs, Win32::OLE->new('Matlab.Application')

循环内。但是,如果您有许多持久 Matlab进程,那么为什么不运行一个简单的for循环呢?

于 2013-04-26T20:33:25.470 回答
0

进一步实验,发现Parallel::Loop的错误和Win32::OLE(0.1709) error 0x800706be: "The remote procedure call failed or Free to wrong pool when using Parellel::ForkManager的错误都可以避免根据http://www-01.ibm.com/support/docview.wss?uid=swg21261292http://search.cpan.org/~gsar/libwin32-0.191/OLE/lib/Win32/OLE的建议/TPJ.pod关于使警告静音。所有代码都需要包含在并行循环中,这是工作版本:

    require Win32::OLE;
    import Win32::OLE;
    Win32::OLE->Initialize();
    no warnings qw(once); 
    $Win32::OLE::Warn = 0;
    my $ML = Win32::OLE->new('Matlab.Application.single') or die "Cannot start MATLAB";
    $ML->{'Visible'}=0;
    $ML->Execute('try;cd \''.$wkdir.'\';'.$executable.' '.$file.' '.$countfile.';catch;end;quit;');  
于 2013-04-29T16:07:01.123 回答