%
% This version uses spmd, with a separate RNG for each worker in
% the pool.
%
% It's simple, and it's repeatable for a fixed number of workers
%

pool = parpool(2)

% not really clear whether this next loop is needed

spmd
  RandStream.setGlobalStream( ...
  RandStream.create('mrg32k3a','NumStreams',numlabs,'StreamIndices',labindex)); 
end

% Create a Composite object
z=Composite();

for pass = 1:2
    
spmd
  z = randn(1,1);
  disp(z);
  sum2 = gplus(z^2);
end
% the variable c records all the values from each worker

c = [z{:}];
disp(c);

sum1 = sum(c.^2);
disp(sum1);

disp(sum2{1});

end

delete(pool)
