/*
 * shift function for multiple paths
 */
/*
 * "in"  is a list of "m" arrays of length "nx" packets to read
 * "out" is a list of "m" arrays of length "nx" packets to write
 * "inchan"  is a list of "m" input channels for the arrays
 * "outchan"  is a list of "m" output channels for the arrays
 */
#include "msgtyp.h"
#define MAXM 5
mshift(in,inchan,out,outchan,nx,m)
short **in, **out;
int *inchan, *outchan, nx,m;
{
 static int node,proc,host,dim;
 int srcpr,typepr,cflagr;
 int srcpw,typepw,cflagw;
 int rcw, rcr, i, ii, j, jj;
 extern int nwrite(), nread(), ntime(), nparity();
 int nb, ns; int endtime, oddpar, iam0;
 short *inptr[MAXM], *outptr[MAXM];

 whoami(&node,&proc,&host,&dim);
 iam0 = (node==0) ? 1 : 0 ;
 oddpar = nparity(node);
 nb = PKTLEN;
 ns = nb/sizeof(short);

 for(i=0; i<m; i++) {
	inptr[i] = in[i];
	outptr[i] = out[i];
	}

 if(oddpar&0) { /* if my node parity is odd, read first and then write */

 jj = -1;
 for(i=0; i<nx; i++) {
     if(++jj == m) jj=0;
     for(ii=0; ii<m; ii++) {
	 /* jj=ii; */
	 srcpr = inchan[jj]; typepr = DATA; cflagr = 0;
	 rcr = nread (inptr[jj],nb,&srcpr,&typepr,&cflagr);
	 if(rcr < 0) report_err(rcr);
	 srcpw = outchan[jj];  typepw = DATA; cflagw = 0;
	 rcw = nwrite(outptr[jj],nb,srcpw,typepw,&cflagw);
	 if(rcw < 0) report_err(rcw);
	 outptr[jj] += ns;
	 /* for(j=0; j<ns; j++) inptr[jj][j] = cptr[j]; */
	 inptr[jj] += ns;
	 if(++jj == m) jj=0;
	 }
     }
} else { /* if my node parity is even, write first and then read */

     for(ii=0; ii<m; ii++) { /* write out first m packets */
	 srcpw = outchan[ii];  typepw = DATA; cflagw = 0;
	 rcw = nwrite(outptr[ii],nb,srcpw,typepw,&cflagw);
	 if(rcw < 0) report_err(rcw);
	 outptr[ii] += ns;
	 }

     jj = iam0-1;
     for(i=1; i<nx; i++) { /* now do read/write scramble for the rest */
	 if(++jj == m) jj=0;
	 for(ii=0; ii<m; ii++) { 
	     /* jj=ii+iam0; if(jj==m)jj=0; */
	     srcpr = inchan[jj]; typepr = DATA; cflagr = 0;
	     rcr = nread (inptr[jj],nb,&srcpr,&typepr,&cflagr);
	     if(rcr < 0) report_err(rcr);

	     srcpw = outchan[jj];  typepw = DATA; cflagw = 0;
	     rcw = nwrite(outptr[jj],nb,srcpw,typepw,&cflagw);
	     if(rcw < 0) report_err(rcw);
	     outptr[jj] += ns;

	     /* for(j=0; j<ns; j++) inptr[jj][j] = cptr[j]; */
	     inptr[jj] += ns;
	     if(++jj == m) jj=0;
	    }
        }

     if(++jj == m) jj=0;
     for(ii=0; ii<m; ii++) { /* read last m packets */
	 /* jj=ii+iam0; if(jj==m) jj=0; */
	 srcpr = inchan[jj];  typepr = DATA; cflagr = 0;
	 rcr = nread(inptr[jj],nb,&srcpr,&typepr,&cflagr);
	 if(rcr < 0) report_err(rcr);

	 /* for(j=0; j<ns; j++) inptr[jj][j] = cptr[j]; */
	 inptr[jj] += ns;
	 if(++jj == m) jj=0;
	 }
    }
}
