/* $Header: f772Darray_to_mat.m,v 1.2 91/08/23 16:51:01 chu mp310d $

                    Copyright (c) 1991
                    MasPar Computer Corporation
                    All Rights Reserved

    This program has been licensed subject to restrictions
    on reproduction, use and disclosure and this notice
    does not imply that the program or the related ideas
    or algorithms are in the public domain.

	example to blockIn a fe f77 double precision array fe_a(ny,nx)
	into mat in mplabb
*/

#include <mpl.h>

void f772Darray_to_mat(fe_a,be_a,ny,nx) 
register int ny;			/* # of rows of 2-D matrix fe_a */
int nx;					/* # of columns of 2-D matrix fe_a */
double *fe_a;				/* 2-D matrix fe_a */
register plural double *be_a;		/* mpl memory layers to hold fe_a */

{
   int nbxa;				/* # of blocks in x-direction - 1 */
   int nbya;				/* # of blocks in y-direction - 1 */
   int nxpa;				/* the remainder of x-dimension */
   int nypa;				/* the remainder of y-dimension */
   
   register int i, j, k;		/* for loops index */

   nbxa = nx>>lnxproc;			/* nbxa = nx / nxproc */
   nbya = ny>>lnyproc;			/* nbya = ny / nyproc */
   nxpa = nx - (nbxa<<lnxproc);		/* nxpa = nx % nxproc */
   nypa = ny - (nbya<<lnyproc);		/* nypa = ny % nyproc */

   for ( j=0; j<nbya; j++ ) {		/* for each whole block in PE y-axis */
      for ( i=0; i<nbxa; i++ ) {	/* for each whole block in PE x-axis */
         for ( k=0; k<nxproc; k++ )	/* fill each column in the PE grid */
	         blockIn(&fe_a[((i<<lnxproc)+k)*ny+(j<<lnyproc)],
	                 be_a,k,0,1,nyproc,sizeof(double));
         be_a++;			/* go to next PE memory layer */
      }
      if ( nxpa > 0 ) {		/* if a partial block in x after whole blocks */
         for ( k=0; k<nxpa; k++ )	/* partially fill each col in PE grid */
	         blockIn(&fe_a[((nbxa<<lnxproc)+k)*ny+(j<<lnyproc)],
	                 be_a,k,0,1,nyproc,sizeof(double));
         be_a++;			/* go to next PE memory layer */
      }
   }

   if ( nypa > 0 ) {			/* if row of partial blocks in y */
      for ( i=0; i<nbxa; i++ ) {	/* for each block in x */
         for ( k=0; k<nxproc; k++ )	/* fill remaining columns in PE grid */
	         blockIn(&fe_a[((i<<lnxproc)+k)*ny+(nbya<<lnyproc)],
	                 be_a,k,0,1,nypa,sizeof(double));
         be_a++;			/* go to next PE memory layer */
      }
      if ( nxpa > 0 ) {			/* if partial block in x */
         for ( k=0; k<nxpa; k++ )	/* partially fill rem. col in PE grid */
	         blockIn(&fe_a[((nbxa<<lnxproc)+k)*ny+(nbya<<lnyproc)],
	                 be_a,k,0,1,nypa,sizeof(double));
      }
   }
}
