parfor does not propagate changes to handle class - matlab

In R2018b, consider the following toy class:
classdef MyObj < handle
properties
use_parallel = true
test_value = NaN
end
methods
function myMethod(obj)
% Call one of the nested functions below:
if all([obj.use_parallel])
parallel();
disp('Parallel (inside myMethod):')
[obj.test_value]
else
sequential();
disp('Sequential (inside myMethod):')
[obj.test_value]
end
% Sequentially assign some values
function sequential()
for ii = 1:numel(obj)
obj(ii).test_value = ii; end
end
% Assign some values in parallel
function parallel()
parfor ii = 1:numel(obj)
set_value(obj(ii),labindex());
obj_copy(ii) = obj(ii);
end
obj = obj_copy;
end
end
end
end
% parfor requires subfunction (and not nested function):
function set_value(obj,index)
obj.test_value = index;
end
Though this question closely resembles this and this question, their underlying problem was essentially some variant of the limitation outlined in the documentation:
You can send handle objects as inputs to the body of a parfor-loop. However, any changes made to handle objects on the workers during loop iterations are not automatically propagated back to the client. That is, changes made inside the loop are not automatically reflected after the loop
However, as far as I can see, the toy class above is compliant with the parfor slicing rules as well as these particulars regarding handle classes. In my understanding, it should therefore correctly copy the modified obj back to myMethod's workspace.
However, running the following:
clc
% Assign sequentially
M(3) = MyObj();
[M.use_parallel] = deal(false);
M.myMethod();
disp('Sequential (outside class):')
[M.test_value]
disp(' ')
% Assign in parallel
N(3) = MyObj();
[N.use_parallel] = deal(true);
N.myMethod();
disp('Parallel (outside class):')
[N.test_value]
gives on my parpool of 6 workers:
Sequential (inside myMethod):
ans =
1 2 3 % <- OK
Sequential (outside class):
ans =
1 2 3 % <- OK. Nothing unexpected
Parallel (inside myMethod):
ans =
1 1 1 % <- OK, apparently, lab 1 did everything
Parallel (outside class):
ans =
NaN NaN NaN % <- hmmm...changes did not propagate
This means the obj.test_value gets correctly assigned, and the modified obj is indeed correctly copied into myMethod's workspace. Yet somehow, this modified obj is a different entity than the obj before modification, because the changes do not propagate higher up the stack...
Changing the parallel() function to a subfunction (instead of a nested function) and explicitly passing the obj parameter around, does not affect this outcome.
Sooooo...what's going on here?

I can reduce the problem to this:
classdef MyObj < matlab.mixin.Copyable
properties
test_value = NaN
end
methods
function myMethod(obj)
obj = obj.copy();
obj.test_value = rand;
disp('Inside method:')
obj.test_value
end
end
end
Removing nested functions, subfunctions and parfor from the equation. Running the code (after obvious changes) above results in:
Inside method:
ans =
4.8089e-01 % <- obj re-initialized OK
Outside class:
ans =
NaN % <- but this does NOT modify the handle class!
That means it's a language "feature" I wasn't aware of! Apparently, methods can modify existing objects, but not redefine them completely. I'll try digging in the documentation to find out some more about this, but this poses a whole new problem: how to copy the changes made by parfor back into the object?
TL;DR:
function myMethod(obj)
% ↓ new object ↓ old object
obj = obj.copy();
Cris was right; though the new and old objects have identical names, they are different things—they shadow each other. Any changes made to the new obj will not modify the old obj.

Related

Looping a Function in Matlab

total newbie here. I'm having problems looping a function that I've created. I'm having some problems copying the code over but I'll give a general idea of it:
function[X]=Test(A,B,C,D)
other parts of the code
.
.
.
X = linsolve(K,L)
end
where K,L are other matrices I derived from the 4 variables A,B,C,D
The problem is whenever I execute the function Test(1,2,3,4), I can only get one answer out. I'm trying to loop this process for one variable, keep the other 3 variables constant.
For example, I want to get answers for A = 1:10, while B = 2, C = 3, D = 4
I've tried the following method and they did not work:
Function[X] = Test(A,B,C,D)
for A = 1:10
other parts of the code...
X=linsolve(K,L)
end
Whenever I keyed in the command Test(1,2,3,4), it only gave me the output of Test(10,2,3,4)
Then I read somewhere that you have to call the function from somewhere else, so I edited the Test function to be Function[X] = Test(B,C,D) and left A out where it can be assigned in another script eg:
global A
for A = 1:10
Test(A,2,3,4)
end
But this gives an error as well, as Test function requires A to be defined. As such I'm a little lost and can't seem to find any information on how can this be done. Would appreciate all the help I can get.
Cheers guys
I think this is what you're looking for:
A=1:10; B=2; C=3; D=4;
%Do pre-allocation for X according to the dimensions of your output
for iter = 1:length(A)
X(:,:,iter)= Test(A(iter),B,C,D);
end
X
where
function [X]=Test(A,B,C,D)
%other parts of the code
X = linsolve(K,L)
end
Try this:
function X = Test(A,B,C,D)
% allocate output (it is faster than changing the size in every loop)
X = {};
% loop for each position in A
for i = 1:numel(A);
%in the other parts you have to use A(i) instead of just A
... other parts of code
%overwrite the value in X at position i
X{i} = linsolve(K,L);
end
end
and run it with Test(1:10,2,3,4)
To answer what went wrong before:
When you loop with 'for A=1:10' you overwrite the A that was passed to the function (so the function will ignore the A that you passed it) and in each loop you overwrite the X calculated in the previous loop (that is why you can only see the answer for A=10).
The second try should work if you have created a file named Test.m with the function X = (A,B,C,D) as the first code in the file. Although the global assignment is unnecessary. In fact I would strongly recommend you not to use global variables as it gets very messy very fast.

How does function work regarding to the memory usage?

When you are using function in MATLAB you have just the output of the function in the work space and all the other variables that maybe created or used in the body of that function are not shown. I am wondering how does function work? Does it clear all other variables from memory and just save the output?
function acts like a small, isolated programming environment. At the front end you insert your input (e.g. variables, strings, name-value pairs etc). After the function has finished, only the output is available, discarding all temporarily created variables.
function [SUM] = MySum(A)
for ii = 1:length(A)-1
SUM(ii) = A(ii)+A(ii+1);
kk(ii) = ii;
end
end
>> A=1:10
>> MySum(A)
This code just adds two consecutive values for the input array A. Note that the iteration number, stored in kk, is not output and is thus discarded after the function has completed. In MATLAB kk(ii) = ii; will be underlined orange, since it 'might be unused'.
Say you want to also retain kk, just add it to the function outputs:
function [SUM,kk] = MySum(A)
and keep the rest the same.
If you have large variables that you only use up to a certain point and wish them not clogging up your memory whilst the function is running, use clear for that:
function [SUM] = MySum(A)
for ii = 1:length(A)-1
SUM(ii) = A(ii)+A(ii+1);
kk(ii) = ii;
end
clear kk
end

MATLAB Return value of first non-empty argument (built-in COALESCE function)

Is there something inbuilt in MATLAB which functions similar to the SQL COALESCE function. I want that function to return the first 'existing' value from all the arguments.
For example,
clear A B; C=10; COALESCE(A,B,C)
should return value of C (because A and B are unassigned/don't exist).
I know it would be very easy to code, and I am just being lazy here. But, I would be surprised if MATLAB doesn't have a similar function.
As far as I know there is no built-in function for that. But you can easily write your own.
Note that it is not possible in Matlab to pass a variable that has not been defined prior of using it. Therefore your proposed call clear A B; C=10; COALESCE(A,B,C) is invalid and will throw an error. Instead we can define an empty variable using var=[].
The following code creates two empty variables A, B and and assigns C=10. Inside the function coalesce we assume at the beginning that all variables are empty. In the for-loop we return the first non-empty variable. In the version without for-loop we get the index of the first non-zero element and then return the corresponding content of the cell if a non-zero element exists.
If you want the function to be accessible from everywhere within Matlab, see the documentation here.
function testcoalesce
A = [];
B = [];
C = 10;
COALESCE(A,B)
COALESCE(A,B,C)
end
% with for-loop (much faster)
function out = COALESCE(varargin)
out = [];
for i = 1:length(varargin)
if ~isempty(varargin{i})
out = varargin{i};
return;
end
end
end
% without for-loop (slower)
function out = COALESCE(varargin)
out = [];
ind = find(cellfun('isempty', varargin)==0, 1);
if ~isempty(ind);
out = varargin{ind};
end
end
The output is as expected:
ans =
[]
ans =
10
Timing the two functions showed, that the first solution using the for-loop is approximately 48% faster than the function without loop.
(10 samples, 1'000'000 iterations, 3 variables & 20 variables)

How to access variables in the properties block of a Matlab System Object?

I am working on a simple System Object in Matlab/Simulink.
It looks like this :
classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates
% correlateHeader
%
% This template includes the minimum set of functions required
% to define a System object with discrete state.
properties
Header
%nrOfBitsInPreviousStep=0;
s=100;
d=zeros(1,s);
end
properties (DiscreteState)
end
properties (Access = private)
max_nr_of_packets=20;
max_packet_length_in_bytes=300;
current_packet=1;
% Pre-computed constants.
% h = commsrc.pn('GenPoly', [9 5 0],'NumBitsOut', 8,'InitialStates',ones(1,9));
% data=logical(zeros(max_nr_of_packets,max_packet_length_in_bytes*8));
end
methods (Access = protected)
function setupImpl(obj,u)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
end
function [maxCorr]= stepImpl(obj,u)
eml.extrinsic('num2str');
coder.extrinsic('sprintf');
% Implement algorithm. Calculate y as a function of
% input u and discrete states.
%y = size(u,1);
symbols=sign(u);
c=abs(conv(flipud(obj.Header),[symbols; symbols]));
maxCorr=max(c);
% maxCorr
if(maxCorr==36)
idx36=find(c(1:size(symbols,1))==36);
disp('header(s) detected at the following location(s) in bytes:');
disp(sprintf('%15.4f \n',idx36/8));
nrOfSymbols=size(symbols,1);
disp(['out of nr. of total symbols: ' num2str(nrOfSymbols)]);
disp('------');
% maxCorr
end
% y=obj.pBufferIdx;
end
function resetImpl(obj)
% Initialize discrete-state properties.
end
function varargout = isOutputFixedSizeImpl(~)
varargout = {true};
end
function varargout = getOutputSizeImpl(obj)
varargout = {[1 1]};
end
end
end
However when I compile/run it I get the following error:
The System object name 'realtime_header_detectorSO' specified in MATLAB System block 'freqScanningRT/Sync and
Find Header/detect header' is invalid.
Caused by:
Undefined function or variable 's'.
However (!) the following code compiles and runs just fine :
classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates
% correlateHeader
%
% This template includes the minimum set of functions required
% to define a System object with discrete state.
properties
Header
%nrOfBitsInPreviousStep=0;
s=100;
% d=zeros(1,s);
end
properties (DiscreteState)
end
properties (Access = private)
max_nr_of_packets=20;
max_packet_length_in_bytes=300;
current_packet=1;
% Pre-computed constants.
% h = commsrc.pn('GenPoly', [9 5 0],'NumBitsOut', 8,'InitialStates',ones(1,9));
% data=logical(zeros(max_nr_of_packets,max_packet_length_in_bytes*8));
end
methods (Access = protected)
function setupImpl(obj,u)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
end
function [maxCorr]= stepImpl(obj,u)
eml.extrinsic('num2str');
coder.extrinsic('sprintf');
% Implement algorithm. Calculate y as a function of
% input u and discrete states.
%y = size(u,1);
disp(obj.s);
symbols=sign(u);
c=abs(conv(flipud(obj.Header),[symbols; symbols]));
maxCorr=max(c);
% maxCorr
if(maxCorr==36)
idx36=find(c(1:size(symbols,1))==36);
disp('header(s) detected at the following location(s) in bytes:');
disp(sprintf('%15.4f \n',idx36/8));
nrOfSymbols=size(symbols,1);
disp(['out of nr. of total symbols: ' num2str(nrOfSymbols)]);
disp('------');
% maxCorr
end
% y=obj.pBufferIdx;
end
function resetImpl(obj)
% Initialize discrete-state properties.
end
function varargout = isOutputFixedSizeImpl(~)
varargout = {true};
end
function varargout = getOutputSizeImpl(obj)
varargout = {[1 1]};
end
end
end
So I can access s in the stepImpl(obj,u) as obj.s but I cannot access s inside the properties block, where it is defined !
Now this is confusing.
Is there way to access s inside the properties block ?
The problem is that I have to use the properties block because if I try this :
function setupImpl(obj,u)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
d=zeros(1,obj.s);
end
then I get :
Error due to multiple causes.
Caused by:
Problem creating simulation target MEX-file for model 'freqScanningRT'.
Simulink detected an error
'Computed maximum size is not bounded.
Static memory allocation requires all sizes to be bounded.
The computed size is [1 x :?].'.
The error occurred for MATLAB System block 'freqScanningRT/Sync and Find Header/detect header'. See line
34, column 15 in file
'path/realtime_header_detectorSO.m'.
The error was detected during code generation phase.
Start code generation report.
To prevent this error, use one of the following:
* Modify the System object to avoid code that does not support code generation.
* Change 'Simulate using' parameter to 'Interpreted Execution'.
Any idea how to refer to variables in the properties blocks ?
There must be a way to do this.
You should be able to use
properties
s = 100;
d = zeros(1,100);
end
right? If you already have the 100 as a default for s, you should also be able to provide this as part of the default for d.
I'm guessing that you're trying to avoid doing that because you feel uncomfortable repeating the "100". But I'd also guess that really, "100" is some sort of magic number that your system depends on; so really, you should try to pull it out as a constant in any case, whether it's repeated or not.
So in that case, you might improve things with
properties (Constant)
MYCONSTANT = 100;
end
properties
% Reference Constant class properties with the class name
s = realtime_header_detectorSO.MYCONSTANT;
d = zeros(1, realtime_header_detectorSO.MYCONSTANT);
end
You're not going to be able to do what you're originally trying to do - it's not possible to reference the name of one property within the property definition block when defining another property (even though you can perfectly well reference it within a method). I guess I understand why you find that confusing - but to clear up your confusion, note that you have no guarantee over the order in which MATLAB instantiates default values for properties. If it tried to create d before it had created s, it would obviously fail.
You can avoid this problem all together by initializing the properties in the constructor.

parfor and handle classes

I have a handle class:
classdef A<handle
properties
a;
end
methods
function obj = A(a)
obj.a=a;
end
end
end
And I have a cell array of A objects:
arr={};
for i=1:3
arr{i}=A(i);
end
What I would like to do is pass that cell array to a parfor loop so that each object's value will change:
parfor i=1:3
arr{i}.a=i*5;
end
However, this code does not change arr at all. Indeed, here it states that
Changes made to handle classes on the workers during loop iterations are not automatically propagated to the client.
How can I overcome this?
An interesting question; I actually never encountered this problem. It's always good to know everything about parfor limitations, so I did some googlin' and came up with this:
I have received an answer to this issue from technical support.
Apparently the Mathworks regard it as a 'feature' that changes to
objects are not returned - though I can't see that it's a very useful
feature. Anyway, the way to get modified class properties returned
from a parfor loop is to make an explicit change which can be
recognised by parfor. Here are two examples which work for the above
example object:
parfor n = 1:num
exArray(n).data = n:n+5;
end
or
parfor n = 1:num
temp = exArray(n);
setData(temp,n:n+5);
exArray(n) = temp;
end
Actually, if you change any object property it also seems to work. So
for example this also works, if there is a second property data2 which
is set explicitly, both data and data2 are correctly returned:
parfor n = 1:num
setData(exArray(n),n:n+5);
exArray(n).data2 = n:n+5;
end
Where the example object is given by
classdef Example < handle
properties
data
end
methods
function obj = Example(data)
obj.data = data;
end
function setData(obj,data)
obj.data = data;
end
function data = getData(obj)
data = obj.data;
end
end
end
and the array is initialized simply as
% Initialise array of objects
for n = 1:num
exArray(n) = Example(zeros(1,6));
end