using a parallel.pool.Constant variable in multiple parfor loops
4 次查看(过去 30 天)
显示 更早的评论
I have ClassA as follows
classdef ClassA < handle
properties (Access = public)
myParameter = 'initValue';
end
methods (Access = public)
function obj = ClassA()
end
function [] = changeMyParameter(obj)
obj.myParameter = 'changedValue';
end
end
end
and ClassB as follows
classdef ClassB < handle
properties (Access = public)
myClassAList;
end
methods (Access = public)
function obj = ClassB()
clc
for i = 1 : 20
obj.myClassAList{i} = ClassA();
end
c = parallel.pool.Constant(obj.myClassAList);
parfor i = 1 : 20
c.Value{i}.changeMyParameter();
disp(c.Value{i}.myParameter);
end
disp('do something in the client')
parfor i = 1 : 20
disp(c.Value{i}.myParameter);
end
end
end
end
If I execute ClassB, I expect the disp command to produce the same results in both of the parfor loops, a list of 'changedValue'. This is what happens in the first loop, however, in the second loop I see a list that both have 'changedValue' and 'initValue' elements as well. Why?
0 个评论
采纳的回答
Edric Ellis
2016-11-24
In this case, the parallel.pool.Constant you're creating has a cell array of 20 elements on each worker. In your first parfor loop, each worker gets some of the 20 loop iterates, and modifies those elements of the cell array inside the Constant value. If you modify your parfor loops, hopefully this will help explain what's happening. For the first loop,
parfor i = 1 : 20
t = getCurrentTask();
c.Value{i}.changeMyParameter();
fprintf('Changing element %d on worker %d\n', i, t.ID);
end
This will show you which elements of c.Value are modified on which worker. (Note that each worker has its own independent copy of c.Value).
Then, when you run the second parfor loop, the iterates are not necessarily dispatched to the same workers in the same order, so if you run:
parfor i = 1 : 20
t = getCurrentTask();
fprintf('Checking element %d on worker %d\n', i, t.ID);
disp(c.Value{i}.myParameter);
end
You can see that in some cases, you're checking an element of c.Value on a worker where it wasn't modified.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Parallel Server 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!