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 PetscMPIInt rank,size; 8 Mat A; 9 PetscInt i,j,k,n = 3,vstart,rstart,rend,margin; 10 Vec x,y; 11 12 CHKERRQ(PetscInitialize(&argc,&argv,(char*)0,help)); 13 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 14 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 15 16 CHKERRQ(MatCreate(PETSC_COMM_WORLD,&A)); 17 CHKERRQ(MatSetSizes(A,n,n,PETSC_DECIDE,PETSC_DECIDE)); 18 CHKERRQ(MatSetFromOptions(A)); 19 20 CHKERRQ(MatMPIAIJSetPreallocation(A,n,NULL,0,NULL)); 21 CHKERRQ(MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 22 CHKERRQ(MatGetOwnershipRange(A,&rstart,&rend)); 23 CHKERRQ(MatCreateVecs(A,&x,&y)); 24 CHKERRQ(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 CHKERRQ(MatSetValue(A,i,j,(PetscScalar)vstart,INSERT_VALUES)); 43 vstart++; 44 } 45 } 46 CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 47 CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 48 CHKERRQ(MatMult(A,x,y)); 49 CHKERRQ(MatMultTransposeAdd(A,x,y,y)); /* y[i] = sum of row i and column i of A */ 50 CHKERRQ(VecView(y,PETSC_VIEWER_STDOUT_WORLD)); 51 } 52 53 CHKERRQ(MatDestroy(&A)); 54 CHKERRQ(VecDestroy(&x)); 55 CHKERRQ(VecDestroy(&y)); 56 CHKERRQ(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