Actual source code: ex193.c
petsc-3.14.6 2021-03-30
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";
15: int main(int argc,char **args)
16: {
17: Mat A; /* matrix */
18: PetscInt m,n; /* mesh dimensions in x- and y- directions */
19: PetscInt i,j,Ii,J,Istart,Iend;
20: PetscErrorCode ierr;
21: PetscMPIInt size;
22: PetscScalar v;
23: MatPartitioning part;
24: IS coarseparts,fineparts;
25: IS is,isn,isrows;
26: MPI_Comm comm;
28: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
29: comm = PETSC_COMM_WORLD;
30: MPI_Comm_size(comm,&size);
31: PetscOptionsBegin(comm,NULL,"ex193","hierarchical partitioning");
32: m = 15;
33: PetscOptionsInt("-M","Number of mesh points in the x-direction","partitioning",m,&m,NULL);
34: n = 15;
35: PetscOptionsInt("-N","Number of mesh points in the y-direction","partitioning",n,&n,NULL);
36: PetscOptionsEnd();
38: /*
39: Assemble the matrix for the five point stencil (finite difference), YET AGAIN
40: */
41: MatCreate(comm,&A);
42: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
43: MatSetFromOptions(A);
44: MatSetUp(A);
45: MatGetOwnershipRange(A,&Istart,&Iend);
46: for (Ii=Istart; Ii<Iend; Ii++) {
47: v = -1.0; i = Ii/n; j = Ii - i*n;
48: if (i>0) {J = Ii - n; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
49: if (i<m-1) {J = Ii + n; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
50: if (j>0) {J = Ii - 1; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
51: if (j<n-1) {J = Ii + 1; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
52: v = 4.0; MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
53: }
54: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
55: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
56: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
57: /*
58: Partition the graph of the matrix
59: */
60: MatPartitioningCreate(comm,&part);
61: MatPartitioningSetAdjacency(part,A);
62: MatPartitioningSetType(part,MATPARTITIONINGHIERARCH);
63: MatPartitioningHierarchicalSetNcoarseparts(part,2);
64: MatPartitioningHierarchicalSetNfineparts(part,4);
65: MatPartitioningSetFromOptions(part);
66: /* get new processor owner number of each vertex */
67: MatPartitioningApply(part,&is);
68: /* coarse parts */
69: MatPartitioningHierarchicalGetCoarseparts(part,&coarseparts);
70: ISView(coarseparts,PETSC_VIEWER_STDOUT_WORLD);
71: /* fine parts */
72: MatPartitioningHierarchicalGetFineparts(part,&fineparts);
73: ISView(fineparts,PETSC_VIEWER_STDOUT_WORLD);
74: /* partitioning */
75: ISView(is,PETSC_VIEWER_STDOUT_WORLD);
76: /* get new global number of each old global number */
77: ISPartitioningToNumbering(is,&isn);
78: ISView(isn,PETSC_VIEWER_STDOUT_WORLD);
79: ISBuildTwoSided(is,NULL,&isrows);
80: ISView(isrows,PETSC_VIEWER_STDOUT_WORLD);
81: ISDestroy(&is);
82: ISDestroy(&coarseparts);
83: ISDestroy(&fineparts);
84: ISDestroy(&isrows);
85: ISDestroy(&isn);
86: MatPartitioningDestroy(&part);
87: MatDestroy(&A);
88: PetscFinalize();
89: return ierr;
90: }
95: /*TEST
97: test:
98: nsize: 4
99: args: -mat_partitioning_hierarchical_Nfineparts 2
100: requires: parmetis
101: TODO: cannot run because parmetis does reproduce across all machines, probably due to nonportable random number generator
103: TEST*/