Actual source code: ex193.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  1: /*
  2:  * ex193.c
  3:  *
  4:  *  Created on: Jul 29, 2015
  5:  *      Author: Fande Kong fdkong.jd@gmail.com
  6:  */
  7: /*
  8:  * An example demonstrates how to use hierarchical partitioning approach
  9:  */

 11: #include <petscmat.h>

 13: static char help[] = "Illustrates use of hierarchical partitioning.\n";

 17: int main(int argc,char **args)
 18: {
 19:   Mat             A;                      /* matrix */
 20:   PetscInt        m,n;                    /* mesh dimensions in x- and y- directions */
 21:   PetscInt        i,j,Ii,J,Istart,Iend;
 22:   PetscErrorCode  ierr;
 23:   PetscMPIInt     size;
 24:   PetscScalar     v;
 25:   MatPartitioning part;
 26:   IS              coarseparts,fineparts;
 27:   IS              is,isn,isrows;
 28:   MPI_Comm        comm;

 30:   PetscInitialize(&argc,&args,(char*)0,help);
 31:   comm = PETSC_COMM_WORLD;
 32:   MPI_Comm_size(comm,&size);
 33:   PetscOptionsBegin(comm,NULL,"ex193","hierarchical partitioning");
 34:   m = 15;
 35:   PetscOptionsInt("-M","Number of mesh points in the x-direction","partitioning",m,&m,NULL);
 36:   n = 15;
 37:   PetscOptionsInt("-N","Number of mesh points in the y-direction","partitioning",n,&n,NULL);
 38:   PetscOptionsEnd();

 40:   /*
 41:      Assemble the matrix for the five point stencil (finite difference), YET AGAIN
 42:   */
 43:   MatCreate(comm,&A);
 44:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 45:   MatSetFromOptions(A);
 46:   MatSetUp(A);
 47:   MatGetOwnershipRange(A,&Istart,&Iend);
 48:   for (Ii=Istart; Ii<Iend; Ii++) {
 49:     v = -1.0; i = Ii/n; j = Ii - i*n;
 50:     if (i>0)   {J = Ii - n; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 51:     if (i<m-1) {J = Ii + n; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 52:     if (j>0)   {J = Ii - 1; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 53:     if (j<n-1) {J = Ii + 1; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 54:     v = 4.0; MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 55:   }
 56:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 57:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 58:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 59:   /*
 60:    Partition the graph of the matrix
 61:   */
 62:   MatPartitioningCreate(comm,&part);
 63:   MatPartitioningSetAdjacency(part,A);
 64:   MatPartitioningSetType(part,MATPARTITIONINGHIERARCH);
 65:   MatPartitioningHierarchicalSetNcoarseparts(part,2);
 66:   MatPartitioningHierarchicalSetNfineparts(part,4);
 67:   MatPartitioningSetFromOptions(part);
 68:   /* get new processor owner number of each vertex */
 69:   MatPartitioningApply(part,&is);
 70:   /* coarse parts */
 71:   MatPartitioningHierarchicalGetCoarseparts(part,&coarseparts);
 72:   ISView(coarseparts,PETSC_VIEWER_STDOUT_WORLD);
 73:   /* fine parts */
 74:   MatPartitioningHierarchicalGetFineparts(part,&fineparts);
 75:   ISView(fineparts,PETSC_VIEWER_STDOUT_WORLD);
 76:   /* partitioning */
 77:   ISView(is,PETSC_VIEWER_STDOUT_WORLD);
 78:   /* get new global number of each old global number */
 79:   ISPartitioningToNumbering(is,&isn);
 80:   ISView(isn,PETSC_VIEWER_STDOUT_WORLD);
 81:   ISBuildTwoSided(is,&isrows);
 82:   ISView(isrows,PETSC_VIEWER_STDOUT_WORLD);
 83:   ISDestroy(&is);
 84:   ISDestroy(&coarseparts);
 85:   ISDestroy(&fineparts);
 86:   ISDestroy(&isrows);
 87:   ISDestroy(&isn);
 88:   MatPartitioningDestroy(&part);
 89:   MatDestroy(&A);
 90:   PetscFinalize();
 91:   return 0;
 92: }