Code covered by the BSD License  

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

Demo5_MPI.m
%% Simple MPI Example

% Sending Data from Lab1 to Lab2 and Lab4 using labSend
% Lab1 and Lab2 Receive Data from Lab1 using labReceive
%
% This is running in an spmd block but the code inside the block
% could run in a paralleljob or in pmode. 

clear all

%% Open up a matlabpool

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

%% Running MPI commands

source = 1;
destination = [2, 4];

spmd
    if labindex == source

        % Send some data from source lab
        testData = 1000;
        labSend(testData, destination);
        
    elseif any(labindex == destination)

        % Receive on destination lab
        recvdata = labReceive(source);
        fprintf('I am lab %d out of %d \n', labindex, numlabs);
        disp(recvdata)
        
    end
end

%% Access data on the client 

disp(recvdata)

%% Close the matlabpool 

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

Contact us at files@mathworks.com