Can Win32 :: OLE be used in parallel to run multiple instances of Matlab?

When Parallel::Loops combines with Win32::OLE to create multiple instances of Matlab, if we use

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

in each cycle, the program crashes with an unhandled exception.

If you drop my to $ML , then it works, but only one instance is running at any time.

+1
source share
4 answers

I'm not sure what the advantage of creating multiple COM MATLAB servers in a parallel loop is.

By default, the server is created in shared mode, that is, it is shared by all instances. The MATLAB mechanism is available to the user as single-threaded, so in your case all client calculations are run sequentially, and not in parallel.

Fortunately, you create the MATLAB COM server in a special mode:

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

Learn more about this page for more information.

PS: I know very little Perl :)

+1
source

You really need to show the full code.

If you install my $ML inside the anonymous routine passed to Parallel::Loops , then you only set the value in the child process and the value will not be available to the parent.

It is not clear why you are using Matlab processes, but they will not be saved after the death of their parent, which is one of the child processes that are launched by your main program.

You can try declaring my @matlabs outside the loop, and then do

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

inside the loop. But if you have several persistent Matlab processes, then why not just run a simple for loop?

0
source

Parellel :: Loop is tested without working with Win32 :: OLE to run multiple instances of Matlab, but Parellel :: Forkmanager works using the "only" trick from Amro and the key from http://www.perlmonks.org/bare/?node_id = 894058 for the error "CoInitialize is not called":

before using the loop:

 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;'); 

Purpose of using OLE instesd:

 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 job, but we are honored. Using Perl to achieve the parallellized effects of the Matlab score, we can keep the entire available processor busy performing tasks assigned before the parellel cycle and collect / combine the results after this cycle.

0
source

With further experiment, it was discovered that the Parallel :: Loop error and the Win32 :: OLE (0.1709) 0x800706be error: "Remote procedure call error or free pool when using Parellel :: ForkManager can both be avoided by the sentence 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 warning silence. All code should be included inside the parallel loop, and this is the working version:

  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;'); 
0
source

Source: https://habr.com/ru/post/892896/


All Articles