1 static char help[] = "Tests assembly of a matrix from another matrix's hash table.\n\n"; 2 3 #include <petscmat.h> 4 5 PetscErrorCode SetValues(Mat A, PetscBool zero, PetscBool insertvals) 6 { 7 PetscInt m, n, i, j; 8 PetscScalar v; 9 10 PetscFunctionBeginUser; 11 PetscCall(MatGetSize(A, &m, &n)); 12 for (i = 0; i < m; i++) { 13 for (j = 0; j < n; j++) { 14 v = zero ? 0.0 : 10.0 * i + j + 1; 15 PetscCall(MatSetValues(A, 1, &i, 1, &j, &v, insertvals ? INSERT_VALUES : ADD_VALUES)); 16 } 17 } 18 PetscFunctionReturn(PETSC_SUCCESS); 19 } 20 21 PetscErrorCode CreateAndViewB(Mat A) 22 { 23 Mat B; 24 25 PetscFunctionBeginUser; 26 PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &B)); 27 PetscCall(MatCopyHashToXAIJ(A, B)); 28 PetscCall(MatView(B, PETSC_VIEWER_STDOUT_WORLD)); 29 PetscCall(MatDestroy(&B)); 30 PetscFunctionReturn(PETSC_SUCCESS); 31 } 32 33 PetscErrorCode AssembleAndViewA(Mat A) 34 { 35 PetscFunctionBeginUser; 36 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 37 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 38 PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD)); 39 PetscFunctionReturn(PETSC_SUCCESS); 40 } 41 42 int main(int argc, char **argv) 43 { 44 Mat A, T; 45 PetscInt N, n, m; 46 PetscBool zero = PETSC_FALSE, ignorezero = PETSC_FALSE, insertvals = PETSC_FALSE; 47 48 PetscFunctionBeginUser; 49 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 50 PetscCall(PetscOptionsGetBool(NULL, NULL, "-zero", &zero, NULL)); 51 PetscCall(PetscOptionsGetBool(NULL, NULL, "-ignorezero", &ignorezero, NULL)); 52 PetscCall(PetscOptionsGetBool(NULL, NULL, "-insertvals", &insertvals, NULL)); 53 54 PetscCall(MatCreate(PETSC_COMM_WORLD, &T)); 55 PetscCall(MatSetSizes(T, 1, 1, PETSC_DETERMINE, PETSC_DETERMINE)); 56 PetscCall(MatSetFromOptions(T)); 57 PetscCall(MatGetSize(T, NULL, &N)); 58 PetscCall(MatGetLocalSize(T, &m, &n)); 59 PetscCall(MatSeqAIJSetPreallocation(T, N, NULL)); 60 PetscCall(MatMPIAIJSetPreallocation(T, n, NULL, N - n, NULL)); 61 PetscCall(MatSetOption(T, MAT_IGNORE_ZERO_ENTRIES, ignorezero)); 62 PetscCall(MatSetUp(T)); 63 PetscCall(SetValues(T, zero, insertvals)); 64 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "DEBUG T\n")); 65 PetscCall(AssembleAndViewA(T)); 66 67 PetscCall(MatCreate(PETSC_COMM_WORLD, &A)); 68 PetscCall(MatSetSizes(A, 1, 1, PETSC_DETERMINE, PETSC_DETERMINE)); 69 PetscCall(MatSetFromOptions(A)); 70 PetscCall(MatSetOption(A, MAT_IGNORE_ZERO_ENTRIES, ignorezero)); 71 PetscCall(MatSetUp(A)); 72 73 PetscCall(SetValues(A, zero, insertvals)); 74 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "DEBUG B\n")); 75 PetscCall(CreateAndViewB(A)); 76 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "DEBUG A\n")); 77 PetscCall(AssembleAndViewA(A)); 78 79 PetscCall(MatResetHash(A)); 80 /* need to reset the option for MPIAIJ */ 81 PetscCall(MatSetOption(A, MAT_IGNORE_ZERO_ENTRIES, ignorezero)); 82 83 PetscCall(SetValues(A, zero, insertvals)); 84 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "DEBUG B\n")); 85 PetscCall(CreateAndViewB(A)); 86 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "DEBUG A\n")); 87 PetscCall(AssembleAndViewA(A)); 88 89 PetscCall(MatDestroy(&A)); 90 PetscCall(MatDestroy(&T)); 91 PetscCall(PetscFinalize()); 92 return 0; 93 } 94 95 /*TEST 96 97 test: 98 suffix: seq 99 diff_args: -j 100 args: -mat_type seqaij 101 filter: grep -v "Mat Object" 102 103 test: 104 suffix: mpi 105 diff_args: -j 106 args: -mat_type mpiaij 107 nsize: 4 108 filter: grep -v "Mat Object" 109 110 test: 111 diff_args: -j 112 suffix: seq_ignore 113 args: -mat_type seqaij -zero {{0 1}separate output} -ignorezero {{0 1}separate output} -insertvals {{0 1}separate output} 114 filter: grep -v "Mat Object" 115 116 test: 117 diff_args: -j 118 suffix: mpi 119 args: -mat_type mpiaij -zero {{0 1}separate output} -ignorezero {{0 1}separate output} -insertvals {{0 1}separate output} 120 nsize: 4 121 filter: grep -v "Mat Object" 122 123 TEST*/ 124