Actual source code: ex151.c

petsc-3.13.6 2020-09-29
Report Typos and Errors
  1: static char help[] = "Tests MatPermute() in parallel.\n\n";
  2: /* Results:
  3:    Sequential:
  4:    - seqaij:   correct permutation
  5:    - seqbaij:  permutation not supported for this MATTYPE
  6:    - seqsbaij: permutation not supported for this MATTYPE
  7:    Parallel:
  8:    - mpiaij:   correct permutation
  9:    - mpibaij:  correct permutation
 10:    - mpisbaij: permutation not supported for this MATTYPE
 11:  */

 13:  #include <petscmat.h>

 15: int main(int argc,char **argv)
 16: {
 17:   const struct {PetscInt i,j; PetscScalar v;} entries[] = {{0,3,1.},{1,2,2.},{2,1,3.},{2,5,4.},{3,0,5.},{3,6,6.},{4,1,7.},{4,4,8.}};
 18:   const PetscInt ixrow[5]                               = {4,2,1,0,3},ixcol[7] = {5,3,6,1,2,0,4};
 19:   Mat            A,B;
 21:   PetscInt       i,rstart,rend,cstart,cend;
 22:   IS             isrow,iscol;
 23:   PetscViewer    viewer;
 24:   PetscBool      view_sparse;

 26:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 27:   /* ------- Assemble matrix, --------- */
 28:   MatCreate(PETSC_COMM_WORLD,&A);
 29:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,5,7);
 30:   MatSetFromOptions(A);
 31:   MatSetUp(A);
 32:   MatGetOwnershipRange(A,&rstart,&rend);
 33:   MatGetOwnershipRangeColumn(A,&cstart,&cend);

 35:   for (i=0; i<(PetscInt)(sizeof(entries)/sizeof(entries[0])); i++) {
 36:     MatSetValue(A,entries[i].i,entries[i].j,entries[i].v,INSERT_VALUES);
 37:   }
 38:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 39:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 41:   /* ------ Prepare index sets ------ */
 42:   ISCreateGeneral(PETSC_COMM_WORLD,rend-rstart,ixrow+rstart,PETSC_USE_POINTER,&isrow);
 43:   ISCreateGeneral(PETSC_COMM_WORLD,cend-cstart,ixcol+cstart,PETSC_USE_POINTER,&iscol);
 44:   ISSetPermutation(isrow);
 45:   ISSetPermutation(iscol);

 47:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 48:   view_sparse = PETSC_FALSE;
 49:   PetscOptionsGetBool(NULL,NULL, "-view_sparse", &view_sparse, NULL);
 50:   if (!view_sparse) {
 51:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_DENSE);
 52:   }
 53:   PetscViewerASCIIPrintf(viewer,"Original matrix\n");
 54:   MatView(A,viewer);

 56:   MatPermute(A,isrow,iscol,&B);
 57:   PetscViewerASCIIPrintf(viewer,"Permuted matrix\n");
 58:   MatView(B,viewer);

 60:   if (!view_sparse) {
 61:     PetscViewerPopFormat(viewer);
 62:   }
 63:   PetscViewerASCIIPrintf(viewer,"Row permutation\n");
 64:   ISView(isrow,viewer);
 65:   PetscViewerASCIIPrintf(viewer,"Column permutation\n");
 66:   ISView(iscol,viewer);

 68:   /* Free data structures */
 69:   ISDestroy(&isrow);
 70:   ISDestroy(&iscol);
 71:   MatDestroy(&A);
 72:   MatDestroy(&B);

 74:   PetscFinalize();
 75:   return ierr;
 76: }



 80: /*TEST

 82:    build:
 83:       requires: !complex

 85:    test:
 86:       args: -view_sparse

 88:    test:
 89:       suffix: 2
 90:       nsize: 2
 91:       args: -view_sparse

 93:    test:
 94:       suffix: 2b
 95:       nsize: 2
 96:       args: -mat_type baij -view_sparse

 98:    test:
 99:       suffix: 3
100:       nsize: 3
101:       args: -view_sparse

103:    test:
104:       suffix: 3b
105:       nsize: 3
106:       args: -mat_type baij -view_sparse

108: TEST*/