1 static char help[] = "Tests MatPermute() for a square matrix in parallel.\n\n"; 2 #include <petscmat.h> 3 /* Results: 4 Sequential: 5 - seqaij: correct permutation 6 - seqbaij: permutation not supported for this MATTYPE 7 - seqsbaij: permutation not supported for this MATTYPE 8 Parallel: 9 - mpiaij: incorrect permutation (disable this op for now) 10 - seqbaij: correct permutation, but all manner of memory leaks 11 (disable this op for now, because it appears broken for nonsquare matrices; see ex151) 12 - seqsbaij: permutation not supported for this MATTYPE 13 14 */ 15 int main(int argc, char **argv) { 16 const struct { 17 PetscInt i, j; 18 PetscScalar v; 19 } entries[] = { 20 {0, 3, 1.}, 21 {1, 2, 2.}, 22 {2, 1, 3.}, 23 {2, 4, 4.}, 24 {3, 0, 5.}, 25 {3, 3, 6.}, 26 {4, 1, 7.}, 27 {4, 4, 8.} 28 }; 29 const PetscInt ixrow[5] = {4, 2, 1, 3, 0}, ixcol[5] = {3, 2, 1, 4, 0}; 30 Mat A, B; 31 PetscInt i, rstart, rend, cstart, cend; 32 IS isrow, iscol; 33 PetscViewer viewer, sviewer; 34 PetscBool view_sparse; 35 36 PetscFunctionBeginUser; 37 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 38 /* ------- Assemble matrix, --------- */ 39 PetscCall(MatCreate(PETSC_COMM_WORLD, &A)); 40 PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, 5, 5)); 41 PetscCall(MatSetFromOptions(A)); 42 PetscCall(MatSetUp(A)); 43 PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 44 PetscCall(MatGetOwnershipRangeColumn(A, &cstart, &cend)); 45 46 for (i = 0; i < (PetscInt)PETSC_STATIC_ARRAY_LENGTH(entries); i++) PetscCall(MatSetValue(A, entries[i].i, entries[i].j, entries[i].v, INSERT_VALUES)); 47 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 48 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 49 50 /* ------ Prepare index sets ------ */ 51 PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, rend - rstart, ixrow + rstart, PETSC_USE_POINTER, &isrow)); 52 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 5, ixcol, PETSC_USE_POINTER, &iscol)); 53 PetscCall(ISSetPermutation(isrow)); 54 PetscCall(ISSetPermutation(iscol)); 55 56 PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD, &viewer)); 57 view_sparse = PETSC_FALSE; 58 PetscCall(PetscOptionsGetBool(NULL, NULL, "-view_sparse", &view_sparse, NULL)); 59 if (!view_sparse) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_DENSE)); 60 PetscCall(PetscViewerASCIIPrintf(viewer, "Original matrix\n")); 61 PetscCall(MatView(A, viewer)); 62 63 PetscCall(MatPermute(A, isrow, iscol, &B)); 64 PetscCall(PetscViewerASCIIPrintf(viewer, "Permuted matrix\n")); 65 PetscCall(MatView(B, viewer)); 66 if (!view_sparse) PetscCall(PetscViewerPopFormat(viewer)); 67 68 PetscCall(PetscViewerASCIIPrintf(viewer, "Row permutation\n")); 69 PetscCall(ISView(isrow, viewer)); 70 PetscCall(PetscViewerASCIIPrintf(viewer, "Column permutation\n")); 71 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 72 PetscCall(ISView(iscol, sviewer)); 73 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 74 75 /* Free data structures */ 76 PetscCall(ISDestroy(&isrow)); 77 PetscCall(ISDestroy(&iscol)); 78 PetscCall(MatDestroy(&A)); 79 PetscCall(MatDestroy(&B)); 80 81 PetscCall(PetscFinalize()); 82 return 0; 83 } 84