MPI_Init();

global nx ny nz nzb ZeRo mpitag

comm    = MPI_Comm_Load("NEWORLD");
nprocs  = MPI_Comm_size(comm);
me      = MPI_Comm_rank(comm);


src = me; src_up = me+1; src_dn = me-1;
dst = me; dst_up = me+1; dst_dn = me-1;


nx      = 128;                               %# grid points
ny      = 128;
nz      = 128;
nzb     = nz/nprocs;


ZeRo    = zeros(nx,ny,nzb+2);

mpitag = sort(primes(1000),'descend');

for i = 1:100

if me == 0
    utot = randn(nx,ny,nz);

    for i = 1:nprocs-1

        u = utot(:,:,nzb*i+1:nzb*(i+1)); MPI_Send(u,i,i*mpitag(27),comm);

    end
     u = ZeRo; u(:,:,2:nzb+1) = utot(:,:,1:nzb);

else

    u = ZeRo; [u(:,:,2:nzb+1)] = MPI_Recv(0,me*mpitag(27),comm);

end
end

MPI_Finalize;

disp('SUCCESS');
