Meffy
Meffy

Reputation: 1

Parallel execution of COM instances in Matlab

I'm trying to accelerate our test environment by using the ParralelToolbox of Mathworks. However I am unable to start several Matlab instances in parallel (up to now we run our tests sequentially and each one is starting a new Matlab instance via an ActX server). So when I run the following code below

ML=ver('Matlab');
ML_Path=matlabroot;
ML_Ver=ML.Version;
parfor i = 1:3
    NewMatlab = actxserver(['matlab.application.single.',ML_Ver])
    Answer = NewMatlab.Feval('test',1);
    NewMatlab.Quit;
    NewMatlab.release;
end

the Matlab instances are called sequentially (test is just a very simple script that sums up a few numbers).

However if I start a new Matlab via command line

ML=ver('Matlab');
ML_Path=matlabroot;
ML_Ver=ML.Version;
parfor i = 1:3
    dos('matlab -nodesktop -minimize -wait -batch "test"');
end

it works. I see that these two methods are quite different in the handling of starting Matlab, but the first approach would be

Upvotes: 0

Views: 144

Answers (1)

Edric
Edric

Reputation: 25140

If you want each iteration of your test to run in a completely separate MATLAB instance, you could use the batch function, like this:

for i = 1:3
    j(i) = batch(@test, nOut, {argsIn...});
end
% Later, collect results
for i = 1:3
    wait(j(i)), fetchOutputs(j(i))
end

Or, you could simply use parfor directly

parpool() % If necessary
parfor i = 1:3
    out{i} = test(...)
end

(You only need to call parpool if no pool is currently open, and you have your preferences set so that a pool is not automatically created when you hit the parfor).

Upvotes: 1

Related Questions