Code covered by the BSD License  

Highlights from
Enhancing e-Infrastructures with Advanced Technical Computing: Parallel MATLAB® on the Grid

Demo2_spmd.m
%% 
clear all

%% open up a matlabpool

if matlabpool('size') == 0
    matlabpool open 2
else
    warning('MATLAB:poolOpen', 'matlabpool already open')
end

%% serial part of code

n1 = 50;
n2 = 100;

%% simple spmd block

spmd
    a = rand(n1,n1);
    display(size(a))
end

%% spmd with distributed arrays

spmd
    a = rand(n2,n2,codistributor); % uses default distribution
    display(size(localPart(a)));
end

%% spmd with distributed arrays
% creating a distributed array from local parts

spmd
    % load or create data
    L = rand(n1,n1);
    % create distributed array
    D = codistributed(L);  % use default distribution
    % print sizes of arrays
    display(size(D));
    display(size(localPart(D)));
end

%% Parallel Math
% Data stays on workers between spmd blocks

spmd
    % perform matrix operations
    E = D*D'
    F = D.*D
    % use overloaded functions
    X = fft(D)
    Y = svd(D)
    % retrieve local part
    L = localPart(D);
end

%% Can access the data by referencing into it, now copies to client

display(L{1})

%% Close the matlabpool 

if matlabpool('size') ~= 0
    matlabpool close
else
    warning('MATLAB:poolClose', 'matlabpool already closed')
end

Contact us at files@mathworks.com