1 static char help[] = "Test Mat products \n\n"; 2 3 #include <petscmat.h> 4 int main(int argc,char **args) 5 { 6 Mat A = NULL,B=NULL,C=NULL,D=NULL,E=NULL; 7 PetscInt k; 8 const PetscInt M = 18,N = 18; 9 PetscMPIInt rank; 10 11 /* A, B are 18 x 18 nonsymmetric matrices and have the same sparsity pattern but different values. 12 Big enough to have complex communication patterns but still small enough for debugging. 13 */ 14 PetscInt Ai[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17}; 15 PetscInt Aj[] = {0, 1, 2, 7, 3, 8, 4, 9, 5, 8, 2, 6, 11, 0, 7, 1, 6, 2, 4, 10, 16, 11, 15, 12, 17, 12, 13, 14, 15, 17, 11, 13, 3, 16, 9, 15, 11, 13}; 16 PetscInt Bi[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17}; 17 PetscInt Bj[] = {0, 1, 2, 7, 3, 8, 4, 9, 5, 8, 2, 6, 11, 0, 7, 1, 6, 2, 4, 10, 16, 11, 15, 12, 17, 12, 13, 14, 15, 17, 11, 13, 3, 16, 9, 15, 11, 13}; 18 19 PetscInt Annz = sizeof(Ai)/sizeof(PetscInt); 20 PetscInt Bnnz = sizeof(Bi)/sizeof(PetscInt); 21 22 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 23 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 24 25 PetscCall(MatCreate(PETSC_COMM_WORLD,&A)); 26 PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,M,N)); 27 PetscCall(MatSetFromOptions(A)); 28 PetscCall(MatSeqAIJSetPreallocation(A,2,NULL)); 29 PetscCall(MatMPIAIJSetPreallocation(A,2,NULL,2,NULL)); 30 PetscCall(MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 31 32 if (rank == 0) { 33 for (k=0; k<Annz; k++) PetscCall(MatSetValue(A,Ai[k],Aj[k],Ai[k]+Aj[k]+1.0,INSERT_VALUES)); 34 } 35 36 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 37 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 38 39 PetscCall(MatCreate(PETSC_COMM_WORLD,&B)); 40 PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N)); 41 PetscCall(MatSetFromOptions(B)); 42 PetscCall(MatSeqAIJSetPreallocation(B,2,NULL)); 43 PetscCall(MatMPIAIJSetPreallocation(B,2,NULL,2,NULL)); 44 PetscCall(MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 45 46 if (rank == 0) { 47 for (k=0; k<Bnnz; k++) PetscCall(MatSetValue(B,Bi[k],Bj[k],Bi[k]+Bj[k]+2.0,INSERT_VALUES)); 48 } 49 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 50 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 51 52 PetscCall(MatMatMult(A,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C)); 53 PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 54 55 /* B, A have the same nonzero pattern, so it is legitimate to do so */ 56 PetscCall(MatMatMult(B,A,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C)); 57 PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 58 59 PetscCall(MatTransposeMatMult(A,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&D)); 60 PetscCall(MatView(D, PETSC_VIEWER_STDOUT_WORLD)); 61 62 PetscCall(MatPtAP(A,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&E)); 63 PetscCall(MatView(E,PETSC_VIEWER_STDOUT_WORLD)); 64 65 PetscCall(MatDestroy(&A)); 66 PetscCall(MatDestroy(&B)); 67 PetscCall(MatDestroy(&C)); 68 PetscCall(MatDestroy(&D)); 69 PetscCall(MatDestroy(&E)); 70 71 PetscCall(PetscFinalize()); 72 return 0; 73 } 74 75 /*TEST 76 testset: 77 filter: grep -ve type -ve "Mat Object" 78 output_file: output/ex250_1.out 79 80 test: 81 suffix: 1 82 nsize: {{1 3}} 83 args: -mat_type aij 84 85 test: 86 suffix: 2 87 nsize: {{3 4}} 88 args: -mat_type aij -matmatmult_via backend -matptap_via backend -mattransposematmult_via backend 89 90 test: 91 suffix: cuda 92 requires: cuda 93 nsize: {{1 3 4}} 94 args: -mat_type aijcusparse 95 96 test: 97 suffix: kok 98 requires: !sycl kokkos_kernels 99 nsize: {{1 3 4}} 100 args: -mat_type aijkokkos 101 102 TEST*/ 103