1 static char help[] = "Tests MATHTOOL\n\n"; 2 3 #include <petscmat.h> 4 5 static PetscErrorCode GenEntries(PetscInt sdim,PetscInt M,PetscInt N,const PetscInt *J,const PetscInt *K,PetscScalar *ptr,void *ctx) 6 { 7 PetscInt d,j,k; 8 PetscReal diff = 0.0,*coords = (PetscReal*)(ctx); 9 10 PetscFunctionBeginUser; 11 for (j = 0; j < M; j++) { 12 for (k = 0; k < N; k++) { 13 diff = 0.0; 14 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]); 15 ptr[j+M*k] = 1.0/(1.0e-2 + PetscSqrtReal(diff)); 16 } 17 } 18 PetscFunctionReturn(0); 19 } 20 21 static PetscErrorCode GenEntriesRectangular(PetscInt sdim,PetscInt M,PetscInt N,const PetscInt *J,const PetscInt *K,PetscScalar *ptr,void *ctx) 22 { 23 PetscInt d,j,k; 24 PetscReal diff = 0.0,**coords = (PetscReal**)(ctx); 25 26 PetscFunctionBeginUser; 27 for (j = 0; j < M; j++) { 28 for (k = 0; k < N; k++) { 29 diff = 0.0; 30 for (d = 0; d < sdim; d++) diff += (coords[0][J[j]*sdim+d] - coords[1][K[k]*sdim+d]) * (coords[0][J[j]*sdim+d] - coords[1][K[k]*sdim+d]); 31 ptr[j+M*k] = 1.0/(1.0e-2 + PetscSqrtReal(diff)); 32 } 33 } 34 PetscFunctionReturn(0); 35 } 36 37 int main(int argc,char **argv) 38 { 39 Mat A,AT,D,B,P,R,RT; 40 PetscInt m = 100,dim = 3,M,K = 10,begin,n = 0,N,bs; 41 PetscMPIInt size; 42 PetscScalar *ptr; 43 PetscReal *coords,*gcoords,*scoords,*gscoords,*(ctx[2]),norm,epsilon; 44 MatHtoolKernel kernel = GenEntries; 45 PetscBool flg,sym = PETSC_FALSE; 46 PetscRandom rdm; 47 IS iss,ist,is[2]; 48 Vec right,left,perm; 49 50 PetscCall(PetscInitialize(&argc,&argv,(char*)NULL,help)); 51 PetscCall(PetscOptionsGetInt(NULL,NULL,"-m_local",&m,NULL)); 52 PetscCall(PetscOptionsGetInt(NULL,NULL,"-n_local",&n,NULL)); 53 PetscCall(PetscOptionsGetInt(NULL,NULL,"-dim",&dim,NULL)); 54 PetscCall(PetscOptionsGetInt(NULL,NULL,"-K",&K,NULL)); 55 PetscCall(PetscOptionsGetBool(NULL,NULL,"-symmetric",&sym,NULL)); 56 PetscCall(PetscOptionsGetReal(NULL,NULL,"-mat_htool_epsilon",&epsilon,NULL)); 57 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 58 M = size*m; 59 PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL)); 60 PetscCall(PetscMalloc1(m*dim,&coords)); 61 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rdm)); 62 PetscCall(PetscRandomGetValuesReal(rdm,m*dim,coords)); 63 PetscCall(PetscCalloc1(M*dim,&gcoords)); 64 PetscCall(MatCreateDense(PETSC_COMM_WORLD,m,PETSC_DECIDE,M,K,NULL,&B)); 65 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 66 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 67 PetscCall(MatSetRandom(B,rdm)); 68 PetscCall(MatGetOwnershipRange(B,&begin,NULL)); 69 PetscCall(PetscArraycpy(gcoords+begin*dim,coords,m*dim)); 70 PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE,gcoords,M*dim,MPIU_REAL,MPI_SUM,PETSC_COMM_WORLD)); 71 PetscCall(MatCreateHtoolFromKernel(PETSC_COMM_WORLD,m,m,M,M,dim,coords,coords,kernel,gcoords,&A)); 72 PetscCall(MatSetOption(A,MAT_SYMMETRIC,sym)); 73 PetscCall(MatSetFromOptions(A)); 74 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 75 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 76 PetscCall(MatViewFromOptions(A,NULL,"-A_view")); 77 PetscCall(MatGetOwnershipIS(A,is,NULL)); 78 PetscCall(ISDuplicate(is[0],is+1)); 79 PetscCall(MatIncreaseOverlap(A,1,is,2)); 80 PetscCall(MatSetBlockSize(A,2)); 81 PetscCall(MatIncreaseOverlap(A,1,is+1,1)); 82 PetscCall(ISGetBlockSize(is[1],&bs)); 83 PetscCheckFalse(bs != 2,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect block size %" PetscInt_FMT " != 2",bs); 84 PetscCall(MatSetBlockSize(A,1)); 85 PetscCall(ISEqual(is[0],is[1],&flg)); 86 PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unequal index sets"); 87 PetscCall(ISDestroy(is)); 88 PetscCall(ISDestroy(is+1)); 89 PetscCall(MatCreateVecs(A,&right,&left)); 90 PetscCall(VecSetRandom(right,rdm)); 91 PetscCall(MatMult(A,right,left)); 92 PetscCall(MatHtoolGetPermutationSource(A,&iss)); 93 PetscCall(MatHtoolGetPermutationTarget(A,&ist)); 94 PetscCall(VecDuplicate(left,&perm)); 95 PetscCall(VecCopy(left,perm)); 96 PetscCall(VecPermute(perm,ist,PETSC_FALSE)); 97 PetscCall(VecPermute(right,iss,PETSC_FALSE)); 98 PetscCall(MatHtoolUsePermutation(A,PETSC_FALSE)); 99 PetscCall(MatMult(A,right,left)); 100 PetscCall(VecAXPY(left,-1.0,perm)); 101 PetscCall(VecNorm(left,NORM_INFINITY,&norm)); 102 PetscCheckFalse(PetscAbsReal(norm) > PETSC_SMALL,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"||y(with permutation)-y(without permutation)|| = %g (> %g)",(double)PetscAbsReal(norm),(double)PETSC_SMALL); 103 PetscCall(MatHtoolUsePermutation(A,PETSC_TRUE)); 104 PetscCall(VecDestroy(&perm)); 105 PetscCall(VecDestroy(&left)); 106 PetscCall(VecDestroy(&right)); 107 PetscCall(ISDestroy(&ist)); 108 PetscCall(ISDestroy(&iss)); 109 if (PetscAbsReal(epsilon) >= PETSC_SMALL) { /* when there is compression, it is more difficult to check against MATDENSE, so just compare symmetric and nonsymmetric assemblies */ 110 PetscReal relative; 111 PetscCall(MatDestroy(&B)); 112 PetscCall(MatCreateHtoolFromKernel(PETSC_COMM_WORLD,m,m,M,M,dim,coords,coords,kernel,gcoords,&B)); 113 PetscCall(MatSetOption(B,MAT_SYMMETRIC,(PetscBool)!sym)); 114 PetscCall(MatSetFromOptions(B)); 115 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 116 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 117 PetscCall(MatViewFromOptions(B,NULL,"-B_view")); 118 PetscCall(MatConvert(A,MATDENSE,MAT_INITIAL_MATRIX,&P)); 119 PetscCall(MatNorm(P,NORM_FROBENIUS,&relative)); 120 PetscCall(MatConvert(B,MATDENSE,MAT_INITIAL_MATRIX,&R)); 121 PetscCall(MatAXPY(R,-1.0,P,SAME_NONZERO_PATTERN)); 122 PetscCall(MatNorm(R,NORM_INFINITY,&norm)); 123 PetscCheckFalse(PetscAbsReal(norm/relative) > epsilon,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"||A(!symmetric)-A(symmetric)|| = %g (> %g)",(double)PetscAbsReal(norm/relative),(double)epsilon); 124 PetscCall(MatDestroy(&B)); 125 PetscCall(MatDestroy(&R)); 126 PetscCall(MatDestroy(&P)); 127 } else { 128 PetscCall(MatConvert(A,MATDENSE,MAT_INITIAL_MATRIX,&D)); 129 PetscCall(MatViewFromOptions(D,NULL,"-D_view")); 130 PetscCall(MatMultEqual(A,D,10,&flg)); 131 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Ax != Dx"); 132 PetscCall(MatMultTransposeEqual(A,D,10,&flg)); 133 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"A^Tx != D^Tx"); 134 PetscCall(MatMultAddEqual(A,D,10,&flg)); 135 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"y+Ax != y+Dx"); 136 PetscCall(MatGetOwnershipRange(B,&begin,NULL)); 137 PetscCall(MatDenseGetArrayWrite(D,&ptr)); 138 for (PetscInt i = begin; i < m+begin; ++i) 139 for (PetscInt j = 0; j < M; ++j) GenEntries(dim,1,1,&i,&j,ptr+i-begin+j*m,gcoords); 140 PetscCall(MatDenseRestoreArrayWrite(D,&ptr)); 141 PetscCall(MatMultEqual(A,D,10,&flg)); 142 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Ax != Dx"); 143 PetscCall(MatTranspose(D,MAT_INPLACE_MATRIX,&D)); 144 PetscCall(MatTranspose(A,MAT_INITIAL_MATRIX,&AT)); 145 PetscCall(MatMultEqual(AT,D,10,&flg)); 146 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"A^Tx != D^Tx"); 147 PetscCall(MatTranspose(A,MAT_REUSE_MATRIX,&AT)); 148 PetscCall(MatMultEqual(AT,D,10,&flg)); 149 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"A^Tx != D^Tx"); 150 PetscCall(MatAXPY(D,-1.0,AT,SAME_NONZERO_PATTERN)); 151 PetscCall(MatNorm(D,NORM_INFINITY,&norm)); 152 PetscCheckFalse(PetscAbsReal(norm) > PETSC_SMALL,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"||A-D|| = %g (> %g)",(double)norm,(double)PETSC_SMALL); 153 PetscCall(MatDestroy(&AT)); 154 PetscCall(MatDestroy(&D)); 155 PetscCall(MatMatMult(A,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&P)); 156 PetscCall(MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY)); 157 PetscCall(MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY)); 158 PetscCall(MatMatMultEqual(A,B,P,10,&flg)); 159 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"ABx != Px"); 160 PetscCall(MatTransposeMatMultEqual(A,B,P,10,&flg)); 161 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"A^TBx != P^Tx"); 162 PetscCall(MatDestroy(&B)); 163 PetscCall(MatDestroy(&P)); 164 if (n) { 165 PetscCall(PetscMalloc1(n*dim,&scoords)); 166 PetscCall(PetscRandomGetValuesReal(rdm,n*dim,scoords)); 167 N = n; 168 PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE,&N,1,MPIU_INT,MPI_SUM,PETSC_COMM_WORLD)); 169 PetscCall(PetscCalloc1(N*dim,&gscoords)); 170 PetscCallMPI(MPI_Exscan(&n,&begin,1,MPIU_INT,MPI_SUM,PETSC_COMM_WORLD)); 171 PetscCall(PetscArraycpy(gscoords+begin*dim,scoords,n*dim)); 172 PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE,gscoords,N*dim,MPIU_REAL,MPI_SUM,PETSC_COMM_WORLD)); 173 kernel = GenEntriesRectangular; 174 ctx[0] = gcoords; 175 ctx[1] = gscoords; 176 PetscCall(MatCreateHtoolFromKernel(PETSC_COMM_WORLD,m,n,M,N,dim,coords,scoords,kernel,ctx,&R)); 177 PetscCall(MatSetFromOptions(R)); 178 PetscCall(MatAssemblyBegin(R,MAT_FINAL_ASSEMBLY)); 179 PetscCall(MatAssemblyEnd(R,MAT_FINAL_ASSEMBLY)); 180 PetscCall(MatViewFromOptions(R,NULL,"-R_view")); 181 PetscCall(MatConvert(R,MATDENSE,MAT_INITIAL_MATRIX,&D)); 182 PetscCall(MatViewFromOptions(D,NULL,"-D_view")); 183 PetscCall(MatMultEqual(R,D,10,&flg)); 184 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Rx != Dx"); 185 PetscCall(MatTranspose(D,MAT_INPLACE_MATRIX,&D)); 186 PetscCall(MatTranspose(R,MAT_INITIAL_MATRIX,&RT)); 187 PetscCall(MatMultEqual(RT,D,10,&flg)); 188 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"R^Tx != D^Tx"); 189 PetscCall(MatTranspose(R,MAT_REUSE_MATRIX,&RT)); 190 PetscCall(MatMultEqual(RT,D,10,&flg)); 191 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"R^Tx != D^Tx"); 192 PetscCall(MatDestroy(&RT)); 193 PetscCall(MatDestroy(&D)); 194 PetscCall(MatCreateDense(PETSC_COMM_WORLD,n,PETSC_DECIDE,PETSC_DETERMINE,K,NULL,&B)); 195 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 196 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 197 PetscCall(MatSetRandom(B,rdm)); 198 PetscCall(MatMatMult(R,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&P)); 199 PetscCall(MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY)); 200 PetscCall(MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY)); 201 PetscCall(MatMatMultEqual(R,B,P,10,&flg)); 202 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"RBx != Px"); 203 PetscCall(MatDestroy(&B)); 204 PetscCall(MatDestroy(&P)); 205 PetscCall(MatCreateVecs(R,&right,&left)); 206 PetscCall(VecSetRandom(right,rdm)); 207 PetscCall(MatMult(R,right,left)); 208 PetscCall(MatHtoolGetPermutationSource(R,&iss)); 209 PetscCall(MatHtoolGetPermutationTarget(R,&ist)); 210 PetscCall(VecDuplicate(left,&perm)); 211 PetscCall(VecCopy(left,perm)); 212 PetscCall(VecPermute(perm,ist,PETSC_FALSE)); 213 PetscCall(VecPermute(right,iss,PETSC_FALSE)); 214 PetscCall(MatHtoolUsePermutation(R,PETSC_FALSE)); 215 PetscCall(MatMult(R,right,left)); 216 PetscCall(VecAXPY(left,-1.0,perm)); 217 PetscCall(VecNorm(left,NORM_INFINITY,&norm)); 218 PetscCheckFalse(PetscAbsReal(norm) > PETSC_SMALL,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"||y(with permutation)-y(without permutation)|| = %g (> %g)",(double)PetscAbsReal(norm),(double)PETSC_SMALL); 219 PetscCall(MatHtoolUsePermutation(R,PETSC_TRUE)); 220 PetscCall(VecDestroy(&perm)); 221 PetscCall(VecDestroy(&left)); 222 PetscCall(VecDestroy(&right)); 223 PetscCall(ISDestroy(&ist)); 224 PetscCall(ISDestroy(&iss)); 225 PetscCall(MatDestroy(&R)); 226 PetscCall(PetscFree(gscoords)); 227 PetscCall(PetscFree(scoords)); 228 } 229 } 230 PetscCall(PetscRandomDestroy(&rdm)); 231 PetscCall(MatDestroy(&A)); 232 PetscCall(PetscFree(gcoords)); 233 PetscCall(PetscFree(coords)); 234 PetscCall(PetscFinalize()); 235 return 0; 236 } 237 238 /*TEST 239 240 build: 241 requires: htool 242 243 test: 244 requires: htool 245 suffix: 1 246 nsize: 4 247 args: -m_local 80 -n_local 25 -mat_htool_epsilon 1.0e-11 -symmetric {{false true}shared output} 248 output_file: output/ex101.out 249 250 test: 251 requires: htool 252 suffix: 2 253 nsize: 4 254 args: -m_local 120 -mat_htool_epsilon 1.0e-2 -mat_htool_compressor {{sympartialACA fullACA SVD}shared output} -mat_htool_clustering {{PCARegular PCAGeometric BoundingBox1Regular BoundingBox1Geometric}shared output} 255 output_file: output/ex101.out 256 257 TEST*/ 258