mat2grp.c
/* Convert an input matrix file into a grouped file
 * with bpg basis functions per group per dimension. */
Convert an input matrix file into a grouped file
with bpg basis functions per group per dimension.
Convert an input matrix file into a grouped file with bpg basis functions per group per dimension .
int mat2grp (FILE *matfile, FILE *grpfile, int bpg) {
	int msize[3], grphdr[5], bpgvol, rrow;
	long i, grpslab, matslab;
	cplx *grpvals, *slabvals;
	bpgvol = bpg * bpg * bpg;
	fread (msize, sizeof(int), 3, matfile);
	grphdr[0] = 0;
	grphdr[1] = GEDIV(msize[0], bpg);
	grphdr[2] = GEDIV(msize[1], bpg);
	grphdr[3] = GEDIV(msize[2], bpg);
	grphdr[4] = bpg;
	fwrite (grphdr, sizeof(int), 5, grpfile);
	matslab = msize[0] * msize[1];
	grpslab = grphdr[1] * grphdr[2] * bpgvol;
	grpvals = malloc (grpslab * sizeof(cplx));
	slabvals = malloc (bpg * matslab * sizeof(cplx));
	for (i = 0; i < msize[2]; i += bpg) {
		rrow = MIN(bpg, MAX(0, msize[2] - i));
		fread (slabvals, sizeof(cplx), rrow * matslab, matfile);
		memset (grpvals, 0, grpslab * sizeof(cplx));
#pragma omp parallel default(shared)
{
		long l, j, k;
		int gidx[3], bidx[3];
#pragma omp for
		for (j = 0; j < grpslab; ++j) {
			l = j / bpgvol;
			k = j % bpgvol;
			GRID(gidx, l, grphdr[1], grphdr[2]);
			gidx[0] *= bpg;
			gidx[1] *= bpg;
			GRID(bidx, k, bpg, bpg);
			bidx[0] += gidx[0];
			bidx[1] += gidx[1];
			if (bidx[0] >= msize[0] || bidx[1] >= msize[1]
					|| bidx[2] >= rrow) continue;
			k = bidx[0] + msize[0] * (bidx[1] + msize[1] * bidx[2]);
			grpvals[j] = slabvals[k];
		}
}
		fwrite (grpvals, sizeof(cplx), grpslab, grpfile);
	}
	free (grpvals);
	free (slabvals);
	return grphdr[1] * grphdr[2] * grphdr[3];
}
int mat2grp ( FILE * matfile , FILE * grpfile , int bpg ) { int msize [ 3 ] , grphdr [ 5 ] , bpgvol , rrow ; long i , grpslab , matslab ; cplx * grpvals , * slabvals ; bpgvol = bpg * bpg * bpg ; fread ( msize , sizeof ( int ) , 3 , matfile ) ; grphdr [ 0 ] = 0 ; grphdr [ 1 ] = GEDIV ( msize [ 0 ] , bpg ) ; grphdr [ 2 ] = GEDIV ( msize [ 1 ] , bpg ) ; grphdr [ 3 ] = GEDIV ( msize [ 2 ] , bpg ) ; grphdr [ 4 ] = bpg ; fwrite ( grphdr , sizeof ( int ) , 5 , grpfile ) ; matslab = msize [ 0 ] * msize [ 1 ] ; grpslab = grphdr [ 1 ] * grphdr [ 2 ] * bpgvol ; grpvals = malloc ( grpslab * sizeof ( cplx ) ) ; slabvals = malloc ( bpg * matslab * sizeof ( cplx ) ) ; for ( i = 0 ; i < msize [ 2 ] ; i += bpg ) { rrow = MIN ( bpg , MAX ( 0 , msize [ 2 ] - i ) ) ; fread ( slabvals , sizeof ( cplx ) , rrow * matslab , matfile ) ; memset ( grpvals , 0 , grpslab * sizeof ( cplx ) ) ; #pragma  omp parallel default(shared) { long l , j , k ; int gidx [ 3 ] , bidx [ 3 ] ; #pragma  omp for for ( j = 0 ; j < grpslab ; ++ j ) { l = j / bpgvol ; k = j % bpgvol ; GRID ( gidx , l , grphdr [ 1 ] , grphdr [ 2 ] ) ; gidx [ 0 ] *= bpg ; gidx [ 1 ] *= bpg ; GRID ( bidx , k , bpg , bpg ) ; bidx [ 0 ] += gidx [ 0 ] ; bidx [ 1 ] += gidx [ 1 ] ; if ( bidx [ 0 ] >= msize [ 0 ] || bidx [ 1 ] >= msize [ 1 ] || bidx [ 2 ] >= rrow ) continue ; k = bidx [ 0 ] + msize [ 0 ] * ( bidx [ 1 ] + msize [ 1 ] * bidx [ 2 ] ) ; grpvals [ j ] = slabvals [ k ] ; } } fwrite ( grpvals , sizeof ( cplx ) , grpslab , grpfile ) ; } free ( grpvals ) ; free ( slabvals ) ; return grphdr [ 1 ] * grphdr [ 2 ] * grphdr [ 3 ] ; }
Convert an input matrix file into a grouped file
 with bpg basis functions per group per dimension.
Convert an input matrix file into a grouped file with bpg basis functions per group per dimension .
/* Read the matrix size. */ /* Set and write the group header. */ /* Read a slab of data. Watch not to run off the end. */ /* Blank the slab of groups. */ /* Now build each of the groups in the slab. */ /* Pull out the group index and the element index. */ /* Find the index of the starting basis of the group. */ /* Find the index of the basis in the slab. */ /* Skip values off the end of the matrix. */ /* Copy the value into position. */ /* Write the slab of groups. */