Actual source code: ex151.c

petsc-3.4.5 2014-06-29
  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:  */
 12: #include <petscmat.h>

 16: int main(int argc,char **argv)
 17: {
 18:   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.}};
 19:   const PetscInt ixrow[5]                               = {4,2,1,3,0},ixcol[7] = {5,3,6,1,2,0,4};
 20:   Mat            A,B;
 22:   PetscInt       i,rstart,rend,cstart,cend;
 23:   IS             isrow,iscol;
 24:   PetscViewer    viewer;
 25:   PetscBool      view_sparse;

 27:   PetscInitialize(&argc,&argv,(char*)0,help);

 29:   /* ------- Assemble matrix, --------- */
 30:   MatCreate(PETSC_COMM_WORLD,&A);
 31:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,5,7);
 32:   MatSetFromOptions(A);
 33:   MatSetUp(A);
 34:   MatGetOwnershipRange(A,&rstart,&rend);
 35:   MatGetOwnershipRangeColumn(A,&cstart,&cend);

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

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

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

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

 62:   PetscViewerASCIIPrintf(viewer,"Row permutation\n");
 63:   ISView(isrow,viewer);
 64:   PetscViewerASCIIPrintf(viewer,"Column permutation\n");
 65:   ISView(iscol,viewer);

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

 73:   PetscFinalize();
 74:   return 0;
 75: }