Actual source code: ex151.c

petsc-3.3-p6 2013-02-11
  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:   incorrect permutation (disable this op for now).
  9:    - mpibaij:  weird error indicating that it can't handle rectangular matrices (disable this op for now).
 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,sviewer;
 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:   /* The column index set must currently be  */
 46:   ISCreateGeneral(PETSC_COMM_SELF,7,ixcol,PETSC_USE_POINTER,&iscol);
 47:   ISSetPermutation(isrow);
 48:   ISSetPermutation(iscol);

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

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

 63:   PetscViewerASCIIPrintf(viewer,"Row permutation\n");
 64:   ISView(isrow,viewer);
 65:   PetscViewerASCIIPrintf(viewer,"Column permutation\n");
 66:   PetscViewerGetSingleton(viewer,&sviewer);
 67:   ISView(iscol,sviewer);
 68:   PetscViewerRestoreSingleton(viewer,&sviewer);

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

 76:   PetscFinalize();
 77:   return 0;
 78: }