MATLAB Invalid Simulink object name, parfor and load_system - matlab

I try to run a model in a loop using parfor command.
So I wrote the following code:
% control_model.m
warning('off', 'Simulink:Engine:UsingDefaultMaxStepSize');
apool = gcp('nocreate');
if isempty(apool)
apool = parpool('local');
end
load_system('mymodel');
tic
parfor w=1:10
warning('off', 'Simulink:Engine:UsingDefaultMaxStepSize');
w_str=num2str(w);
set_param('mymodel/mysystem','sys', ['tf(',w_str,',[1 ',w_str,'])'] )
sim('mymodel',[],[])
drawnow
end
toc
close_system('mymodel',0);
% delete(poolobj)
Then I will get the following error:
Error using control_model (line 11)
Invalid Simulink object name: mymodel/mysystem
If I run it again, I get the same error (Although using for instead of parfor solves this error).
But if I put a load_system('mymodel'); just after parfor and run it once, the problem is fixed. And even if I remove load_system command, the error is not shown for the next times anymore.
I am interested in knowing what is happening behind the scene and why the first load_system does not solve the problem even if I run the program so many times. while the one in parfor fix the problem even if being removed in the next time calling the script?

I think you need to load the model on the worker (see sim in parfor with Normal Mode in the documentation for more details), so your load_system needs to be within the parfor loop:
tic
parfor w=1:10
load_system('mymodel');
warning('off', 'Simulink:Engine:UsingDefaultMaxStepSize');
w_str=num2str(w);
set_param('mymodel/mysystem','sys', ['tf(',w_str,',[1 ',w_str,'])'] )
sim('mymodel',[],[])
drawnow
end
toc

Related

Modify The Following Matlab Parfor Loop

In the following parfor loop, Matlab says the variable 'sf' cannot be classified. However, the way it is defined inside the innermost loop doesn't seem to affect parfor. Could you tell me the issue and show me how this code snippet should be modified?
parfor ii=1:1:10000
for jj=1:200
for kk=1:80
sf{kk}=fit([kk*dKy;(kk+1)*dKy],[result{kk}(ii);result{kk+1}(ii)],'exp1','lower',[kk*dKy,result{kk}(ii)]);
fun=#(t) sf{kk}(t).*cos(Ky(kk).*t);
result2{ii}(jj)=0;
result2{ii}(jj)=result2{ii}(jj)+integral(fun,Ky(kk),Ky(kk+1),'ArrayValued',true)/(2*pi);
end
end
end

MATLAB parfor error: function or variable?

The following code:
matlabpool('open','local',2)
parfor i=1:5
proc = System.Diagnostics.Process;
end
results in an error:
Error: MATLAB cannot determine whether "System" refers to a function or variable.
However, when I execute the parfor loop again (after the error), it runs through! I found a couple of similar questions, but I wasn't able to implement the suggested solutions.
MATLAB parfor - cannot determine whether "ModelUtil" refers to a function or variable?
MATLAB using parfor (parallel computing toolbox) and custom packages with +
I can't wrap my mind around why the loop runs the second time. If I then call
matlabpool close
and execute the whole script again, the error appears once again. So it only happens the first time after the pool was initiated. Any ideas?
This is because any variable or function you use in a parfor loop must be defined explicitly in the code at parsing time. If there is any ambiguity, Matlab opt to throw an error rather than messing up by assuming.
Just define an anonymous function that create the object you want before the parfor loop, then you can use it as will within the parfor loop.
This run fine on my machine (Matlab R2013a):
getSystemProcess = #() System.Diagnostics.Process ;
parfor i=1:5
proc = getSystemProcess();
end
Read this Matlab chapter for more informations on how the variable/function names are interpreted in a parfor loop: Unambiguous Variable Names

Getting error in parfor but not in for-loop in matlab

I am trying the matlab parallel processing and during this I tried to apply this thing to my code. Below is the code.
matlabpool open 2
pop = create_population(match_matrix,PopSize);
ftns = zeros(PopSize,1);
parfor i=1:PopSize
ftns(i) = get_fitness(pop{i});
end
matlabpool close
The error I am getting is following
Error using parallel_function (line 589)
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error stack:
SWIFTga>(parfor body) at 127
Error in SWIFTga (line 126)
parfor i=1:PopSize
I am getting no errors if I replace the parfor with for. Please suggest what may be going wrong....
I tried so much and at last found the problem. Actually I was accessing global variables in the function called inside parfor body. From the matlab documentation found that we cant access global variables in parfor as there is synchronization issues. I has to change the code from global variables to argument passing. Thanks all for those who put some time into my question.

will same variable name cause conflict in parfor

I have a basic code like this :
parfor i=1:8
[t,y]=ode15s(#rate,tspan,cin,options,i); % the option i is evaluated in the rate function
figure(1)
subplot(3,3,i+1)
plot(t,y)
hold on
end
Will any conflict arise because the variable name y is same in all iterations ?
No, every worker has its unique namespace.
However, a worker cannot open figures that display on the client (thanks for the reminder, #Edric), so everything after the call to ode15s will not produce any useful result.
To move the plotting outside the parfor loop, you can do the following (there are more efficient solutions, this one will work for sure):
tCell = cell(8,1);
yCell = cell(8,1);
parfor i=1:8
[tCell{i},yCell{i}]=ode15s(#rate,tspan,cin,options,i); % the option i is evaluated in the rate function
end
figure(1)
for i=1:8
subplot(3,3,i+1)
plot(tCell{i},yCell{i})
hold on
end
Following on from #Jonas' answer, just a note to point out that if you're using R2013b or later, and you wish to display graphics while waiting for your parallel computations to complete, you could use PARFEVAL, like in this example: http://www.mathworks.co.uk/help/distcomp/examples/parfeval-blackjack.html .

Error using parallel_function (matlabpool & parfor)

I want to read a large amount of files, process each of them and save the results for each of them in a .mat file. The processing of each file is independent from the others, so I'd like to try using parfor. I have written the following Matlab script file:
load filelist
obj = package.name.SomeObject();
matlabpool local 5
parfor i=1:length(filelist)
result = obj.compute(filelist{i});
[~, name, ~] = fileparts(filelist{i});
save(['~/path/' name], 'result');
end % file loop
matlabpool close
When I try to run it on my computer, a Matlab pool is intialized (connected to 5 workers), but then the following error message occurs:
Error using parallel_function (line 589)
Undefined function or variable "cleaner".
Error in readfiles (line 14)
parfor i=1:length(filelist)
Error in run (line 64)
evalin('caller', [script ';']);
Do you know where the problem could be?
I don't know why (there might be a connection to this issue), but the problem was solved by enclosing the code inside a function and calling that function (instead of calling the script file by run script.m). It also required creating a parsave function (see explanation here).