When Parallel::Loops
is combined with Win32::OLE
to create multiple instance of Matlab, if we use
my $ML = Win32::OLE->new('Matlab.Application') or die "Cannot start MATLAB"
in each loop, the program crashes showing an unhandled exception.
If drop the my
before $ML
then it works, but at any time only one instance is running.
With further experiment, it is found that the error of Parallel::Loop and the error of Win32::OLE(0.1709) error 0x800706be: "The remote procedure call failed or Free to wrong pool when using Parellel::ForkManager can be both avoided by a suggestion from http://www-01.ibm.com/support/docview.wss?uid=swg21261292 and http://search.cpan.org/~gsar/libwin32-0.191/OLE/lib/Win32/OLE/TPJ.pod about silence the warning. All the code need to be included inside the parallel loop, and this are the working version:
I'm not sure what is the benefit of creating multiple MATLAB COM servers in a parallel loop.
By default the server is created in shared mode, ie shared by all instances. The MATLAB engine is exposed to the user as single-threaded, so in your case all the clients computations are run in serial, not in parallel.
Fortunately you create MATLAB COM server in dedicated mode:
See this page for more info
PS: I know very little Perl :)
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:
and inside the loop use:
The purpose of use OLE instesd of just using:
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.
You really should show your full code.
If you are setting
my $ML
inside the anonymous subroutine passed toParallel::Loops
then you are setting the value in only a child process and the value will no be available to the parent.It is unclear what you are using the
Matlab
processes for, but they will not persist after the death of their parent, which is one of the child processes that your main program starts.You could try declaring
my @matlabs
outside the loop, and then doingwithin the loop. But if you a number of persistent
Matlab
processes then why not just run a simplefor
loop?