MATLAB: Parfor with COMSOL object

comsol livelinkparfor

Dear Concerned,
I have been reading through parallel processing toolbox lately to integrate COMSOL with matlab parallel processing and i am new to parallel processing with matlab aswell.
However i am struggling with the below problem.
In a parfor loop in am forced to load a COMSOL model object like the one below: "model = mphload('LIB_9AH_1D.mph');"
parfor i = 1:2
comsolPort=[2036 2037];
t = getCurrentTask();
labit=t.ID;
mphstart(comsolPort(labit))
import('com.comsol.model.*');
import('com.comsol.model.util.*');
model = mphload('LIB_9AH_1D.mph');
end
Later i want to refer the 'model' object outside the parfor loop. I am doing this to invoke two COMSOL process in parallel to work with matlab.
However i get an error saying "Temporary variable model is used after the parfor loop,but its value is not available after the loop"
How to make a object value available after the parfor loop?. Any ways to approach this problem will be really helpful.
Matlab version and details:
MATLAB Version: 8.2.0.701 (R2013b) MATLAB License Number: 595731 Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)

Best Answer

Outputs from a parfor loop need to be either sliced outputs, or reductions. In this case, a sliced output is probably most appropriate. You could put the model into a cell array:
parfor i = 1:2
...
model{i} = ...;
end
Related Question