/* ------------------------------------------------------ purpose -- to read an Perm object from a formatted file return value -- 1 if success, 0 if failure created -- 96jan05, cca ------------------------------------------------------ */ int Perm_readFromFormattedFile ( Perm *perm, FILE *fp ) { int i, isPresent, j, rc, size ; int itemp[2] ; /* --------------- check the input --------------- */ if ( perm == NULL || fp == NULL ) { fprintf(stderr, "\n error in Perm_readFromFormattedFile(%p,%p)" "\n bad input\n", perm, fp) ; return(0) ; } /* --------------------- clear the data fields --------------------- */ Perm_clearData(perm) ; /* ----------------------------------------------------- read in the two scalar parameters: isPresent and size ----------------------------------------------------- */ if ( (rc = IVfscanf(fp, 2, itemp)) != 2 ) { fprintf(stderr, "\n error in Perm_readFromFormattedFile(%p,%p)" "\n %d items of %d read\n", perm, fp, rc, 2) ; return(0) ; } isPresent = itemp[0] ; size = itemp[1] ; if ( isPresent < 1 || isPresent > 3 || size <= 0 ) { fprintf(stderr, "\n error in Perm_readFromFormattedFile(%p,%p)" "\n isPresent = %d, size = %d", perm, fp, isPresent, size) ; return(0) ; } /* --------------------- initialize the object --------------------- */ Perm_initWithTypeAndSize(perm, isPresent, size) ; if ( isPresent == 2 || isPresent == 3 ) { /* ----------------------------------------- read in the old-to-new permutation vector ----------------------------------------- */ int *oldToNew = perm->oldToNew ; if ( (rc = IVfscanf(fp, size, oldToNew)) != size ) { fprintf(stderr, "\n error in Perm_readFromFormattedFile(%p,%p)" "\n %d items of %d read\n", perm, fp, rc, size) ; exit(-1) ; } for ( i = 0 ; i < size ; i++ ) { if ( oldToNew[i] == size ) { for ( j = 0 ; j < size ; j++ ) { oldToNew[j]-- ; } break ; } } } if ( isPresent == 1 || isPresent == 3 ) { /* ----------------------------------------- read in the new-to-old permutation vector ----------------------------------------- */ int *newToOld = perm->newToOld ; if ( (rc = IVfscanf(fp, size, newToOld)) != size ) { fprintf(stderr, "\n error in Perm_readFromFormattedFile(%p,%p)" "\n %d items of %d read\n", perm, fp, rc, size) ; exit(-1) ; } for ( i = 0 ; i < size ; i++ ) { if ( newToOld[i] == size ) { for ( j = 0 ; j < size ; j++ ) { newToOld[j]-- ; } break ; } } } /* ---------------------------- check the permutation object ---------------------------- */ if ( Perm_checkPerm(perm) != 1 ) { fprintf(stderr, "\n fatal error in Perm_readFromFormattedFile(%p,%p)" "\n permutation is not valid\n", perm, fp) ; exit(-1) ; } return(1) ; }
/*--------------------------------------------------------------------*/ int main ( int argc, char *argv[] ) /* ----------------------------------------------- generate a nested dissection permutation object for a n1 x n2 x n3 regular grid. created -- 96feb01, cca ----------------------------------------------- */ { Perm *perm ; FILE *msgFile ; int j, msglvl, n1, n2, n3, nvtx ; int *newToOld, *oldToNew, *temp ; if ( argc != 7 ) { fprintf(stdout, "\n\n usage : %s msglvl msgFile n1 n2 n3 permFile" "\n generate separators" "\n msglvl -- message level" "\n msgFile -- message file" "\n n1 -- number of points in first direction" "\n n2 -- number of points in second direction" "\n n3 -- number of points in third direction" "\n permFile -- file to contain the Perm structure" "\n", argv[0]) ; return(1) ; } msglvl = atoi(argv[1]) ; if ( strcmp(argv[2], "stdout") == 0 ) { msgFile = stdout ; } else if ( (msgFile = fopen(argv[2], "a")) == NULL ) { fprintf(stderr, "\n error in test4, file %s, line %d" "\n unable to open file %s", __FILE__, __LINE__, argv[2]) ; exit(-1) ; } n1 = atoi(argv[3]) ; n2 = atoi(argv[4]) ; n3 = atoi(argv[5]) ; /* ----------------------------- create the permutation object ----------------------------- */ nvtx = n1 * n2 * n3 ; perm = Perm_new() ; Perm_initWithTypeAndSize(perm, 3, nvtx) ; newToOld = perm->newToOld ; oldToNew = perm->oldToNew ; /* --------------------- call the nd procedure --------------------- */ mkNDperm(n1, n2, n3, newToOld, 0, n1-1, 0, n2-1, 0, n3-1) ; /* ----------------------------------------- fill in the new-to-old permutation vector ----------------------------------------- */ for ( j = 0 ; j < nvtx ; j++ ) { perm->oldToNew[perm->newToOld[j]] = j ; } if ( msglvl > 2 ) { Perm_writeForHumanEye(perm, msgFile) ; } /* ---------------------------------------- write the permutation vector to its file ---------------------------------------- */ if ( strcmp("none", argv[6]) != 0 ) { Perm_writeToFile(perm, argv[6]) ; } /* ------------------------ free the working storage ------------------------ */ Perm_free(perm) ; return(1) ; }
/* ------------------------------------------------ given a permutation and a vector to map vertices into compressed vertices, create and return a permutation object for the compressed vertices. created -- 96may02, cca ------------------------------------------------ */ Perm * Perm_compress ( Perm *perm, IV *eqmapIV ) { int n, N, v, vcomp, vnew ; int *eqmap, *head, *link, *newToOld, *oldToNew, *vals ; Perm *perm2 ; /* --------------- check the input --------------- */ if ( perm == NULL || (n = perm->size) <= 0 || eqmapIV == NULL || n != IV_size(eqmapIV) || (eqmap = IV_entries(eqmapIV)) == NULL ) { fprintf(stderr, "\n fatal error in Perm_compress(%p,%p)" "\n bad input\n", perm, eqmapIV) ; if ( perm != NULL ) { Perm_writeStats(perm, stderr) ; } if ( eqmapIV != NULL ) { IV_writeStats(eqmapIV, stderr) ; } spoolesFatal(); } n = perm->size ; if ( (oldToNew = perm->oldToNew) == NULL ) { Perm_fillOldToNew(perm) ; oldToNew = perm->oldToNew ; } if ( (newToOld = perm->newToOld) == NULL ) { Perm_fillNewToOld(perm) ; newToOld = perm->newToOld ; } /* --------------------------------- create the new permutation object --------------------------------- */ N = 1 + IVmax(n, eqmap, &v) ; perm2 = Perm_new() ; Perm_initWithTypeAndSize(perm2, 3, N) ; /* -------------------------------------------- get the head/link structure for the vertices -------------------------------------------- */ head = IVinit(N, -1) ; link = IVinit(n, -1) ; for ( v = 0 ; v < n ; v++ ) { vcomp = eqmap[v] ; link[v] = head[vcomp] ; head[vcomp] = v ; } /* --------------------------- get the two vectors to sort --------------------------- */ IVramp(N, perm2->newToOld, 0, 1) ; vals = IVinit(N, -1) ; for ( vcomp = 0 ; vcomp < N ; vcomp++ ) { v = head[vcomp] ; vnew = perm->oldToNew[v] ; for ( v = link[v] ; v != -1 ; v = link[v] ) { if ( vnew > perm->oldToNew[v] ) { vnew = perm->oldToNew[v] ; } } vals[vcomp] = vnew ; } IV2qsortUp(N, vals, perm2->newToOld) ; for ( vcomp = 0 ; vcomp < N ; vcomp++ ) { perm2->oldToNew[perm2->newToOld[vcomp]] = vcomp ; } /* --------------------- free the working data --------------------- */ IVfree(head) ; IVfree(link) ; IVfree(vals) ; return(perm2) ; }