1 static char help[] = "Tests MATHTOOL with a derived htool::IMatrix<PetscScalar> class\n\n"; 2 3 #include <petscmat.h> 4 #include <htool/misc/petsc.hpp> 5 6 static PetscErrorCode GenEntries(PetscInt sdim,PetscInt M,PetscInt N,const PetscInt *J,const PetscInt *K,PetscScalar *ptr,void *ctx) 7 { 8 PetscInt d,j,k; 9 PetscReal diff = 0.0,*coords = (PetscReal*)(ctx); 10 11 PetscFunctionBeginUser; 12 for (j = 0; j < M; j++) { 13 for (k = 0; k < N; k++) { 14 diff = 0.0; 15 for (d = 0; d < sdim; d++) diff += (coords[J[j]*sdim+d] - coords[K[k]*sdim+d]) * (coords[J[j]*sdim+d] - coords[K[k]*sdim+d]); 16 ptr[j+M*k] = 1.0/(1.0e-2 + PetscSqrtReal(diff)); 17 } 18 } 19 PetscFunctionReturn(0); 20 } 21 class MyIMatrix : public htool::VirtualGenerator<PetscScalar> { 22 private: 23 PetscReal *coords; 24 PetscInt sdim; 25 public: 26 MyIMatrix(PetscInt M,PetscInt N,PetscInt spacedim,PetscReal* gcoords) : htool::VirtualGenerator<PetscScalar>(M,N),coords(gcoords),sdim(spacedim) { } 27 28 void copy_submatrix(PetscInt M,PetscInt N,const PetscInt *J,const PetscInt *K,PetscScalar *ptr) const override 29 { 30 PetscReal diff = 0.0; 31 32 PetscFunctionBeginUser; 33 for (PetscInt j = 0; j < M; j++) /* could be optimized by the user how they see fit, e.g., vectorization */ 34 for (PetscInt k = 0; k < N; k++) { 35 diff = 0.0; 36 for (PetscInt d = 0; d < sdim; d++) diff += (coords[J[j]*sdim+d] - coords[K[k]*sdim+d]) * (coords[J[j]*sdim+d] - coords[K[k]*sdim+d]); 37 ptr[j+M*k] = 1.0/(1.0e-2 + PetscSqrtReal(diff)); 38 } 39 PetscFunctionReturnVoid(); 40 } 41 }; 42 43 int main(int argc,char **argv) 44 { 45 Mat A,B,P,R; 46 PetscInt m = 100,dim = 3,M,begin = 0; 47 PetscMPIInt size; 48 PetscReal *coords,*gcoords,norm,epsilon,relative; 49 PetscBool sym = PETSC_FALSE; 50 PetscRandom rdm; 51 MatHtoolKernel kernel = GenEntries; 52 MyIMatrix *imatrix; 53 54 PetscCall(PetscInitialize(&argc,&argv,(char*)NULL,help)); 55 PetscCall(PetscOptionsGetInt(NULL,NULL,"-m_local",&m,NULL)); 56 PetscCall(PetscOptionsGetInt(NULL,NULL,"-dim",&dim,NULL)); 57 PetscCall(PetscOptionsGetBool(NULL,NULL,"-symmetric",&sym,NULL)); 58 PetscCall(PetscOptionsGetReal(NULL,NULL,"-mat_htool_epsilon",&epsilon,NULL)); 59 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 60 M = size*m; 61 PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL)); 62 PetscCall(PetscMalloc1(m*dim,&coords)); 63 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rdm)); 64 PetscCall(PetscRandomGetValuesReal(rdm,m*dim,coords)); 65 PetscCall(PetscCalloc1(M*dim,&gcoords)); 66 PetscCallMPI(MPI_Exscan(&m,&begin,1,MPIU_INT,MPI_SUM,PETSC_COMM_WORLD)); 67 PetscCall(PetscArraycpy(gcoords+begin*dim,coords,m*dim)); 68 PetscCall(MPIU_Allreduce(MPI_IN_PLACE,gcoords,M*dim,MPIU_REAL,MPI_SUM,PETSC_COMM_WORLD)); 69 imatrix = new MyIMatrix(M,M,dim,gcoords); 70 PetscCall(MatCreateHtoolFromKernel(PETSC_COMM_WORLD,m,m,M,M,dim,coords,coords,NULL,imatrix,&A)); /* block-wise assembly using htool::IMatrix<PetscScalar>::copy_submatrix() */ 71 PetscCall(MatSetOption(A,MAT_SYMMETRIC,sym)); 72 PetscCall(MatSetFromOptions(A)); 73 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 74 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 75 PetscCall(MatViewFromOptions(A,NULL,"-A_view")); 76 PetscCall(MatCreateHtoolFromKernel(PETSC_COMM_WORLD,m,m,M,M,dim,coords,coords,kernel,gcoords,&B)); /* entry-wise assembly using GenEntries() */ 77 PetscCall(MatSetOption(B,MAT_SYMMETRIC,sym)); 78 PetscCall(MatSetFromOptions(B)); 79 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 80 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 81 PetscCall(MatViewFromOptions(B,NULL,"-B_view")); 82 PetscCall(MatConvert(A,MATDENSE,MAT_INITIAL_MATRIX,&P)); 83 PetscCall(MatNorm(P,NORM_FROBENIUS,&relative)); 84 PetscCall(MatConvert(B,MATDENSE,MAT_INITIAL_MATRIX,&R)); 85 PetscCall(MatAXPY(R,-1.0,P,SAME_NONZERO_PATTERN)); 86 PetscCall(MatNorm(R,NORM_INFINITY,&norm)); 87 PetscCheckFalse(PetscAbsReal(norm/relative) > epsilon,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"||A(!symmetric)-A(symmetric)|| = %g (> %g)",(double)PetscAbsReal(norm/relative),(double)epsilon); 88 PetscCall(MatDestroy(&B)); 89 PetscCall(MatDestroy(&R)); 90 PetscCall(MatDestroy(&P)); 91 PetscCall(PetscRandomDestroy(&rdm)); 92 PetscCall(MatDestroy(&A)); 93 PetscCall(PetscFree(gcoords)); 94 PetscCall(PetscFree(coords)); 95 delete imatrix; 96 PetscCall(PetscFinalize()); 97 return 0; 98 } 99 100 /*TEST 101 102 build: 103 requires: htool 104 test: 105 requires: htool 106 suffix: 2 107 nsize: 4 108 args: -m_local 120 -mat_htool_epsilon 1.0e-2 -symmetric {{false true}shared output} 109 output_file: output/ex101.out 110 111 TEST*/ 112