1 2 static char help[] = "Partition tiny grid.\n\n"; 3 4 /* 5 Include "petscmat.h" so that we can use matrices. Note that this file 6 automatically includes: 7 petscsys.h - base PETSc routines petscvec.h - vectors 8 petscmat.h - matrices 9 petscis.h - index sets 10 petscviewer.h - viewers 11 */ 12 #include <petscmat.h> 13 14 int main(int argc,char **args) 15 { 16 Mat A,At; 17 PetscMPIInt rank,size; 18 PetscInt *ia,*ja,row; 19 MatPartitioning part; 20 IS is,isn; 21 PetscBool equal; 22 23 PetscFunctionBeginUser; 24 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 25 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 26 PetscCheck(size == 4,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Must run with 4 processors"); 27 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 28 29 PetscCall(PetscMalloc1(5,&ia)); 30 PetscCall(PetscMalloc1(16,&ja)); 31 if (rank == 0) { 32 ja[0] = 1; ja[1] = 4; ja[2] = 0; ja[3] = 2; ja[4] = 5; ja[5] = 1; ja[6] = 3; ja[7] = 6; 33 ja[8] = 2; ja[9] = 7; 34 ia[0] = 0; ia[1] = 2; ia[2] = 5; ia[3] = 8; ia[4] = 10; 35 } else if (rank == 1) { 36 ja[0] = 0; ja[1] = 5; ja[2] = 8; ja[3] = 1; ja[4] = 4; ja[5] = 6; ja[6] = 9; ja[7] = 2; 37 ja[8] = 5; ja[9] = 7; ja[10] = 10; ja[11] = 3; ja[12] = 6; ja[13] = 11; 38 ia[0] = 0; ia[1] = 3; ia[2] = 7; ia[3] = 11; ia[4] = 14; 39 } else if (rank == 2) { 40 ja[0] = 4; ja[1] = 9; ja[2] = 12; ja[3] = 5; ja[4] = 8; ja[5] = 10; ja[6] = 13; ja[7] = 6; 41 ja[8] = 9; ja[9] = 11; ja[10] = 14; ja[11] = 7; ja[12] = 10; ja[13] = 15; 42 ia[0] = 0; ia[1] = 3; ia[2] = 7; ia[3] = 11; ia[4] = 14; 43 } else { 44 ja[0] = 8; ja[1] = 13; ja[2] = 9; ja[3] = 12; ja[4] = 14; ja[5] = 10; ja[6] = 13; ja[7] = 15; 45 ja[8] = 11; ja[9] = 14; 46 ia[0] = 0; ia[1] = 2; ia[2] = 5; ia[3] = 8; ia[4] = 10; 47 } 48 49 PetscCall(MatCreateMPIAdj(PETSC_COMM_WORLD,4,16,ia,ja,NULL,&A)); 50 PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD)); 51 52 /* Create the same matrix but using MatSetValues() */ 53 PetscCall(MatCreate(PETSC_COMM_WORLD,&At)); 54 PetscCall(MatSetSizes(At,4,4,16,16)); 55 PetscCall(MatSetType(At,MATMPIADJ)); 56 for (PetscInt i=0; i<4; i++) { 57 row = i + 4*rank; 58 PetscCall(MatSetValues(At,1,&row,ia[i+1]-ia[i],ja + ia[i],NULL,INSERT_VALUES)); 59 } 60 PetscCall(MatAssemblyBegin(At,MAT_FINAL_ASSEMBLY)); 61 PetscCall(MatAssemblyEnd(At,MAT_FINAL_ASSEMBLY)); 62 PetscCall(MatEqual(A,At,&equal)); 63 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Matrices are not equal that should be equal"); 64 PetscCall(MatDestroy(&At)); 65 66 /* 67 Partition the graph of the matrix 68 */ 69 PetscCall(MatPartitioningCreate(PETSC_COMM_WORLD,&part)); 70 PetscCall(MatPartitioningSetAdjacency(part,A)); 71 PetscCall(MatPartitioningSetFromOptions(part)); 72 /* get new processor owner number of each vertex */ 73 PetscCall(MatPartitioningApply(part,&is)); 74 /* get new global number of each old global number */ 75 PetscCall(ISPartitioningToNumbering(is,&isn)); 76 PetscCall(ISView(isn,PETSC_VIEWER_STDOUT_WORLD)); 77 PetscCall(ISDestroy(&is)); 78 79 PetscCall(ISDestroy(&isn)); 80 PetscCall(MatPartitioningDestroy(&part)); 81 82 /* 83 Free work space. All PETSc objects should be destroyed when they 84 are no longer needed. 85 */ 86 PetscCall(MatDestroy(&A)); 87 88 PetscCall(PetscFinalize()); 89 return 0; 90 } 91 /* 92 test: 93 requires: parmetis 94 args: -mat_view 95 nsize: 4 96 */ 97