1 2 static char help[] = "Test interface of Elemental. \n\n"; 3 4 #include <petscmat.h> 5 6 int main(int argc,char **args) 7 { 8 Mat C,Caij; 9 PetscInt i,j,m = 5,n,nrows,ncols; 10 const PetscInt *rows,*cols; 11 IS isrows,iscols; 12 PetscBool flg,Test_MatMatMult=PETSC_FALSE,mats_view=PETSC_FALSE; 13 PetscScalar *v; 14 PetscMPIInt rank,size; 15 16 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 17 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 18 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 19 PetscCall(PetscOptionsHasName(NULL,NULL,"-mats_view",&mats_view)); 20 21 /* Get local block or element size*/ 22 PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL)); 23 n = m; 24 PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL)); 25 26 PetscCall(MatCreate(PETSC_COMM_WORLD,&C)); 27 PetscCall(MatSetSizes(C,m,n,PETSC_DECIDE,PETSC_DECIDE)); 28 PetscCall(MatSetType(C,MATELEMENTAL)); 29 PetscCall(MatSetFromOptions(C)); 30 PetscCall(MatSetUp(C)); 31 32 PetscCall(PetscOptionsHasName(NULL,NULL,"-row_oriented",&flg)); 33 if (flg) PetscCall(MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE)); 34 PetscCall(MatGetOwnershipIS(C,&isrows,&iscols)); 35 PetscCall(PetscOptionsHasName(NULL,NULL,"-Cexp_view_ownership",&flg)); 36 if (flg) { /* View ownership of explicit C */ 37 IS tmp; 38 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Ownership of explicit C:\n")); 39 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Row index set:\n")); 40 PetscCall(ISOnComm(isrows,PETSC_COMM_WORLD,PETSC_USE_POINTER,&tmp)); 41 PetscCall(ISView(tmp,PETSC_VIEWER_STDOUT_WORLD)); 42 PetscCall(ISDestroy(&tmp)); 43 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Column index set:\n")); 44 PetscCall(ISOnComm(iscols,PETSC_COMM_WORLD,PETSC_USE_POINTER,&tmp)); 45 PetscCall(ISView(tmp,PETSC_VIEWER_STDOUT_WORLD)); 46 PetscCall(ISDestroy(&tmp)); 47 } 48 49 /* Set local matrix entries */ 50 PetscCall(ISGetLocalSize(isrows,&nrows)); 51 PetscCall(ISGetIndices(isrows,&rows)); 52 PetscCall(ISGetLocalSize(iscols,&ncols)); 53 PetscCall(ISGetIndices(iscols,&cols)); 54 PetscCall(PetscMalloc1(nrows*ncols,&v)); 55 for (i=0; i<nrows; i++) { 56 for (j=0; j<ncols; j++) { 57 /*v[i*ncols+j] = (PetscReal)(rank);*/ 58 v[i*ncols+j] = (PetscReal)(rank*10000+100*rows[i]+cols[j]); 59 } 60 } 61 PetscCall(MatSetValues(C,nrows,rows,ncols,cols,v,INSERT_VALUES)); 62 PetscCall(ISRestoreIndices(isrows,&rows)); 63 PetscCall(ISRestoreIndices(iscols,&cols)); 64 PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 65 PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 66 67 /* Test MatView() */ 68 if (mats_view) { 69 PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 70 } 71 72 /* Test MatMissingDiagonal() */ 73 PetscCall(MatMissingDiagonal(C,&flg,NULL)); 74 PetscCheck(!flg,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"MatMissingDiagonal() did not return false!"); 75 76 /* Set unowned matrix entries - add subdiagonals and diagonals from proc[0] */ 77 if (rank == 0) { 78 PetscInt M,N,cols[2]; 79 PetscCall(MatGetSize(C,&M,&N)); 80 for (i=0; i<M; i++) { 81 cols[0] = i; v[0] = i + 0.5; 82 cols[1] = i-1; v[1] = 0.5; 83 if (i) { 84 PetscCall(MatSetValues(C,1,&i,2,cols,v,ADD_VALUES)); 85 } else { 86 PetscCall(MatSetValues(C,1,&i,1,&i,v,ADD_VALUES)); 87 } 88 } 89 } 90 PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 91 PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 92 93 /* Test MatMult() */ 94 PetscCall(MatComputeOperator(C,MATAIJ,&Caij)); 95 PetscCall(MatMultEqual(C,Caij,5,&flg)); 96 PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultEqual() fails"); 97 PetscCall(MatMultTransposeEqual(C,Caij,5,&flg)); 98 PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultTransposeEqual() fails"); 99 100 /* Test MatMultAdd() and MatMultTransposeAddEqual() */ 101 PetscCall(MatMultAddEqual(C,Caij,5,&flg)); 102 PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultAddEqual() fails"); 103 PetscCall(MatMultTransposeAddEqual(C,Caij,5,&flg)); 104 PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"C != Caij. MatMultTransposeAddEqual() fails"); 105 106 /* Test MatMatMult() */ 107 PetscCall(PetscOptionsHasName(NULL,NULL,"-test_matmatmult",&Test_MatMatMult)); 108 if (Test_MatMatMult) { 109 Mat CCelem,CCaij; 110 PetscCall(MatMatMult(C,C,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&CCelem)); 111 PetscCall(MatMatMult(Caij,Caij,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&CCaij)); 112 PetscCall(MatMultEqual(CCelem,CCaij,5,&flg)); 113 PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"CCelem != CCaij. MatMatMult() fails"); 114 PetscCall(MatDestroy(&CCaij)); 115 PetscCall(MatDestroy(&CCelem)); 116 } 117 118 PetscCall(PetscFree(v)); 119 PetscCall(ISDestroy(&isrows)); 120 PetscCall(ISDestroy(&iscols)); 121 PetscCall(MatDestroy(&C)); 122 PetscCall(MatDestroy(&Caij)); 123 PetscCall(PetscFinalize()); 124 return 0; 125 } 126 127 /*TEST 128 129 test: 130 nsize: 2 131 requires: elemental 132 args: -mat_type elemental -m 2 -n 3 133 134 test: 135 suffix: 2 136 nsize: 6 137 requires: elemental 138 args: -mat_type elemental -m 2 -n 2 139 140 test: 141 suffix: 3 142 nsize: 6 143 requires: elemental 144 args: -mat_type elemental -m 2 -n 2 -test_matmatmult 145 output_file: output/ex38_2.out 146 147 TEST*/ 148