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