1 static char help[] = "Test CPU/GPU memory leaks, MatMult and MatMultTransposeAdd during successive matrix assemblies\n\n"; 2 3 #include <petscmat.h> 4 5 int main(int argc, char **argv) { 6 PetscMPIInt rank, size; 7 Mat A; 8 PetscInt i, j, k, n = 3, vstart, rstart, rend, margin; 9 Vec x, y; 10 11 PetscFunctionBeginUser; 12 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 13 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 14 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 15 16 PetscCall(MatCreate(PETSC_COMM_WORLD, &A)); 17 PetscCall(MatSetSizes(A, n, n, PETSC_DECIDE, PETSC_DECIDE)); 18 PetscCall(MatSetFromOptions(A)); 19 20 PetscCall(MatMPIAIJSetPreallocation(A, n, NULL, 0, NULL)); 21 PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 22 PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 23 PetscCall(MatCreateVecs(A, &x, &y)); 24 PetscCall(VecSet(x, 1.0)); 25 26 /* 27 Matrix A only has nonzeros in the diagonal block, which is of size 3x3. 28 We do three successive assemblies on A. The first two have the same non-zero 29 pattern but different values, and the third breaks the non-zero pattern. The 30 first two assemblies have enough zero-rows that triggers compressed-row storage 31 in MATAIJ and MATAIJCUSPARSE. 32 33 These settings are used to test memory management and correctness in MatMult 34 and MatMultTransposeAdd. 35 */ 36 37 for (k = 0; k < 3; k++) { /* Three assemblies */ 38 vstart = (size * k + rank) * n * n + 1; 39 margin = (k == 2) ? 0 : 2; /* Create two zero-rows in the first two assemblies */ 40 for (i = rstart; i < rend - margin; i++) { 41 for (j = rstart; j < rend; j++) { 42 PetscCall(MatSetValue(A, i, j, (PetscScalar)vstart, INSERT_VALUES)); 43 vstart++; 44 } 45 } 46 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 47 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 48 PetscCall(MatMult(A, x, y)); 49 PetscCall(MatMultTransposeAdd(A, x, y, y)); /* y[i] = sum of row i and column i of A */ 50 PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD)); 51 } 52 53 PetscCall(MatDestroy(&A)); 54 PetscCall(VecDestroy(&x)); 55 PetscCall(VecDestroy(&y)); 56 PetscCall(PetscFinalize()); 57 58 /* Uncomment this line if you want to use "cuda-memcheck --leaf-check full" to check this program */ 59 /*cudaDeviceReset();*/ 60 return 0; 61 } 62 63 /*TEST 64 65 testset: 66 nsize: 2 67 output_file: output/ex236_1.out 68 filter: grep -v type 69 70 test: 71 args: -mat_type aij 72 73 test: 74 requires: cuda 75 suffix: cuda 76 args: -mat_type aijcusparse 77 TEST*/ 78