%% spmdex1.m
format compact
%%
parpool
%%
% Make better, start with: 
spmd
    x=labindex
end    
And some similar.
%{
>> help gop
 gop   Global operation
    RES = gop(FUN,X) is the reduction via the function handle FUN of
    the variant arrays X from each lab. The result is replicated on all labs.
    The function Z=FUN(X,Y) should accept two arguments X and Y of the same
    type and produce a result Z of that type, ...
    Returns the same result on each target lab,
    unless target lab specified:
 
    RES = gop(FUN, X, LABTARGET) reduces the variant arrays X using FUN,
    and places the result on the lab indicated by LABTARGET.  RES will be
    equal to [] on all other labs.
 
    Example 1
%}
    spmd
      x = labindex;
      gop(@plus,x)    % the sum of the x's from each lab, same as gplus(x)
      gop(@max,x)     % the max of the x's from each lab.
      gop(@horzcat,x) % the horizontal concatenation of the x's.
    end



format compact
spmd
    x=labindex;
    s=gplus(x);    % Sum over labs
    S=gop(@sum,x); % ----- " -----  gop: global operation
    m=gop(@max,x); % max over labs
end  
x 
%%
s
%%
S
%%
m
%%
[x{1} x{2}]
[x{:}]
%%
[s{:}]  
m{1}
%%
% parpool(4)


% Also, gop, gplus, and gcat allow us to specify a single lab to which the function output should be returned, and they return an empty vector on the other labs.
% spmd
%  s = gplus(x, 1);   % Return result (sum) in s{1} only
% end
% [s{1},s{2},s{3},s{4}]
% ans =
%    10
