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 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); 15 ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr); 16 17 ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); 18 ierr = MatSetSizes(A,n,n,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 19 ierr = MatSetFromOptions(A);CHKERRQ(ierr); 20 21 ierr = MatMPIAIJSetPreallocation(A,n,NULL,0,NULL);CHKERRQ(ierr); 22 ierr = MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 23 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 24 ierr = MatCreateVecs(A,&x,&y);CHKERRQ(ierr); 25 ierr = VecSet(x,1.0);CHKERRQ(ierr); 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 ierr = MatSetValue(A,i,j,(PetscScalar)vstart,INSERT_VALUES);CHKERRQ(ierr); 44 vstart++; 45 } 46 } 47 ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 48 ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 49 ierr = MatMult(A,x,y);CHKERRQ(ierr); 50 ierr = MatMultTransposeAdd(A,x,y,y);CHKERRQ(ierr); /* y[i] = sum of row i and column i of A */ 51 ierr = VecView(y,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 52 } 53 54 ierr = MatDestroy(&A);CHKERRQ(ierr); 55 ierr = VecDestroy(&x);CHKERRQ(ierr); 56 ierr = VecDestroy(&y);CHKERRQ(ierr); 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