1 /* 2 Defines basic operations for the MATSEQAIJSELL matrix class. 3 This class is derived from the MATAIJCLASS, but maintains a "shadow" copy 4 of the matrix stored in MATSEQSELL format, which is used as appropriate for 5 performing operations for which this format is more suitable. 6 */ 7 8 #include <../src/mat/impls/aij/seq/aij.h> 9 #include <../src/mat/impls/sell/seq/sell.h> 10 11 typedef struct { 12 Mat S; /* The SELL formatted "shadow" matrix. */ 13 PetscBool eager_shadow; 14 PetscObjectState state; /* State of the matrix when shadow matrix was last constructed. */ 15 } Mat_SeqAIJSELL; 16 17 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJSELL_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat) 18 { 19 /* This routine is only called to convert a MATAIJSELL to its base PETSc type, */ 20 /* so we will ignore 'MatType type'. */ 21 Mat B = *newmat; 22 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*) A->spptr; 23 24 PetscFunctionBegin; 25 if (reuse == MAT_INITIAL_MATRIX) { 26 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B)); 27 } 28 29 /* Reset the original function pointers. */ 30 B->ops->duplicate = MatDuplicate_SeqAIJ; 31 B->ops->assemblyend = MatAssemblyEnd_SeqAIJ; 32 B->ops->destroy = MatDestroy_SeqAIJ; 33 B->ops->mult = MatMult_SeqAIJ; 34 B->ops->multtranspose = MatMultTranspose_SeqAIJ; 35 B->ops->multadd = MatMultAdd_SeqAIJ; 36 B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ; 37 B->ops->sor = MatSOR_SeqAIJ; 38 39 PetscCall(PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijsell_seqaij_C",NULL)); 40 41 if (reuse == MAT_INITIAL_MATRIX) aijsell = (Mat_SeqAIJSELL*)B->spptr; 42 43 /* Clean up the Mat_SeqAIJSELL data structure. 44 * Note that MatDestroy() simply returns if passed a NULL value, so it's OK to call even if the shadow matrix was never constructed. */ 45 PetscCall(MatDestroy(&aijsell->S)); 46 PetscCall(PetscFree(B->spptr)); 47 48 /* Change the type of B to MATSEQAIJ. */ 49 PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ)); 50 51 *newmat = B; 52 PetscFunctionReturn(0); 53 } 54 55 PetscErrorCode MatDestroy_SeqAIJSELL(Mat A) 56 { 57 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*) A->spptr; 58 59 PetscFunctionBegin; 60 61 /* If MatHeaderMerge() was used, then this SeqAIJSELL matrix will not have an 62 * spptr pointer. */ 63 if (aijsell) { 64 /* Clean up everything in the Mat_SeqAIJSELL data structure, then free A->spptr. */ 65 PetscCall(MatDestroy(&aijsell->S)); 66 PetscCall(PetscFree(A->spptr)); 67 } 68 69 /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 70 * to destroy everything that remains. */ 71 PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ)); 72 /* Note that I don't call MatSetType(). I believe this is because that 73 * is only to be called when *building* a matrix. I could be wrong, but 74 * that is how things work for the SuperLU matrix class. */ 75 PetscCall(MatDestroy_SeqAIJ(A)); 76 PetscFunctionReturn(0); 77 } 78 79 /* Build or update the shadow matrix if and only if needed. 80 * We track the ObjectState to determine when this needs to be done. */ 81 PETSC_INTERN PetscErrorCode MatSeqAIJSELL_build_shadow(Mat A) 82 { 83 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*) A->spptr; 84 PetscObjectState state; 85 86 PetscFunctionBegin; 87 PetscCall(PetscObjectStateGet((PetscObject)A,&state)); 88 if (aijsell->S && aijsell->state == state) { 89 /* The existing shadow matrix is up-to-date, so simply exit. */ 90 PetscFunctionReturn(0); 91 } 92 93 PetscCall(PetscLogEventBegin(MAT_Convert,A,0,0,0)); 94 if (aijsell->S) { 95 PetscCall(MatConvert_SeqAIJ_SeqSELL(A,MATSEQSELL,MAT_REUSE_MATRIX,&aijsell->S)); 96 } else { 97 PetscCall(MatConvert_SeqAIJ_SeqSELL(A,MATSEQSELL,MAT_INITIAL_MATRIX,&aijsell->S)); 98 } 99 PetscCall(PetscLogEventEnd(MAT_Convert,A,0,0,0)); 100 101 /* Record the ObjectState so that we can tell when the shadow matrix needs updating */ 102 PetscCall(PetscObjectStateGet((PetscObject)A,&aijsell->state)); 103 104 PetscFunctionReturn(0); 105 } 106 107 PetscErrorCode MatDuplicate_SeqAIJSELL(Mat A, MatDuplicateOption op, Mat *M) 108 { 109 Mat_SeqAIJSELL *aijsell; 110 Mat_SeqAIJSELL *aijsell_dest; 111 112 PetscFunctionBegin; 113 PetscCall(MatDuplicate_SeqAIJ(A,op,M)); 114 aijsell = (Mat_SeqAIJSELL*) A->spptr; 115 aijsell_dest = (Mat_SeqAIJSELL*) (*M)->spptr; 116 PetscCall(PetscArraycpy(aijsell_dest,aijsell,1)); 117 /* We don't duplicate the shadow matrix -- that will be constructed as needed. */ 118 aijsell_dest->S = NULL; 119 if (aijsell->eager_shadow) { 120 PetscCall(MatSeqAIJSELL_build_shadow(A)); 121 } 122 PetscFunctionReturn(0); 123 } 124 125 PetscErrorCode MatAssemblyEnd_SeqAIJSELL(Mat A, MatAssemblyType mode) 126 { 127 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 128 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*)A->spptr; 129 130 PetscFunctionBegin; 131 if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 132 133 /* I disable the use of the inode routines so that the AIJSELL ones will be 134 * used instead, but I wonder if it might make sense (and is feasible) to 135 * use some of them. */ 136 a->inode.use = PETSC_FALSE; 137 138 /* Since a MATSEQAIJSELL matrix is really just a MATSEQAIJ with some 139 * extra information and some different methods, call the AssemblyEnd 140 * routine for a MATSEQAIJ. 141 * I'm not sure if this is the best way to do this, but it avoids 142 * a lot of code duplication. */ 143 144 PetscCall(MatAssemblyEnd_SeqAIJ(A, mode)); 145 146 /* If the user has requested "eager" shadowing, create the SELL shadow matrix (if needed; the function checks). 147 * (The default is to take a "lazy" approach, deferring this until something like MatMult() is called.) */ 148 if (aijsell->eager_shadow) { 149 PetscCall(MatSeqAIJSELL_build_shadow(A)); 150 } 151 152 PetscFunctionReturn(0); 153 } 154 155 PetscErrorCode MatMult_SeqAIJSELL(Mat A,Vec xx,Vec yy) 156 { 157 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*)A->spptr; 158 159 PetscFunctionBegin; 160 PetscCall(MatSeqAIJSELL_build_shadow(A)); 161 PetscCall(MatMult_SeqSELL(aijsell->S,xx,yy)); 162 PetscFunctionReturn(0); 163 } 164 165 PetscErrorCode MatMultTranspose_SeqAIJSELL(Mat A,Vec xx,Vec yy) 166 { 167 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 168 169 PetscFunctionBegin; 170 PetscCall(MatSeqAIJSELL_build_shadow(A)); 171 PetscCall(MatMultTranspose_SeqSELL(aijsell->S,xx,yy)); 172 PetscFunctionReturn(0); 173 } 174 175 PetscErrorCode MatMultAdd_SeqAIJSELL(Mat A,Vec xx,Vec yy,Vec zz) 176 { 177 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 178 179 PetscFunctionBegin; 180 PetscCall(MatSeqAIJSELL_build_shadow(A)); 181 PetscCall(MatMultAdd_SeqSELL(aijsell->S,xx,yy,zz)); 182 PetscFunctionReturn(0); 183 } 184 185 PetscErrorCode MatMultTransposeAdd_SeqAIJSELL(Mat A,Vec xx,Vec yy,Vec zz) 186 { 187 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 188 189 PetscFunctionBegin; 190 PetscCall(MatSeqAIJSELL_build_shadow(A)); 191 PetscCall(MatMultTransposeAdd_SeqSELL(aijsell->S,xx,yy,zz)); 192 PetscFunctionReturn(0); 193 } 194 195 PetscErrorCode MatSOR_SeqAIJSELL(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 196 { 197 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 198 199 PetscFunctionBegin; 200 PetscCall(MatSeqAIJSELL_build_shadow(A)); 201 PetscCall(MatSOR_SeqSELL(aijsell->S,bb,omega,flag,fshift,its,lits,xx)); 202 PetscFunctionReturn(0); 203 } 204 205 /* MatConvert_SeqAIJ_SeqAIJSELL converts a SeqAIJ matrix into a 206 * SeqAIJSELL matrix. This routine is called by the MatCreate_SeqAIJSELL() 207 * routine, but can also be used to convert an assembled SeqAIJ matrix 208 * into a SeqAIJSELL one. */ 209 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJSELL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 210 { 211 Mat B = *newmat; 212 Mat_SeqAIJ *b; 213 Mat_SeqAIJSELL *aijsell; 214 PetscBool set; 215 PetscBool sametype; 216 217 PetscFunctionBegin; 218 if (reuse == MAT_INITIAL_MATRIX) { 219 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B)); 220 } 221 222 PetscCall(PetscObjectTypeCompare((PetscObject)A,type,&sametype)); 223 if (sametype) PetscFunctionReturn(0); 224 225 PetscCall(PetscNewLog(B,&aijsell)); 226 b = (Mat_SeqAIJ*) B->data; 227 B->spptr = (void*) aijsell; 228 229 /* Disable use of the inode routines so that the AIJSELL ones will be used instead. 230 * This happens in MatAssemblyEnd_SeqAIJSELL as well, but the assembly end may not be called, so set it here, too. 231 * As noted elsewhere, I wonder if it might make sense and be feasible to use some of the inode routines. */ 232 b->inode.use = PETSC_FALSE; 233 234 /* Set function pointers for methods that we inherit from AIJ but override. 235 * We also parse some command line options below, since those determine some of the methods we point to. */ 236 B->ops->duplicate = MatDuplicate_SeqAIJSELL; 237 B->ops->assemblyend = MatAssemblyEnd_SeqAIJSELL; 238 B->ops->destroy = MatDestroy_SeqAIJSELL; 239 240 aijsell->S = NULL; 241 aijsell->eager_shadow = PETSC_FALSE; 242 243 /* Parse command line options. */ 244 PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJSELL Options","Mat"); 245 PetscCall(PetscOptionsBool("-mat_aijsell_eager_shadow","Eager Shadowing","None",(PetscBool)aijsell->eager_shadow,(PetscBool*)&aijsell->eager_shadow,&set)); 246 PetscOptionsEnd(); 247 248 /* If A has already been assembled and eager shadowing is specified, build the shadow matrix. */ 249 if (A->assembled && aijsell->eager_shadow) { 250 PetscCall(MatSeqAIJSELL_build_shadow(A)); 251 } 252 253 B->ops->mult = MatMult_SeqAIJSELL; 254 B->ops->multtranspose = MatMultTranspose_SeqAIJSELL; 255 B->ops->multadd = MatMultAdd_SeqAIJSELL; 256 B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJSELL; 257 B->ops->sor = MatSOR_SeqAIJSELL; 258 259 PetscCall(PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijsell_seqaij_C",MatConvert_SeqAIJSELL_SeqAIJ)); 260 261 PetscCall(PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJSELL)); 262 *newmat = B; 263 PetscFunctionReturn(0); 264 } 265 266 /*@C 267 MatCreateSeqAIJSELL - Creates a sparse matrix of type SEQAIJSELL. 268 This type inherits from AIJ and is largely identical, but keeps a "shadow" 269 copy of the matrix in SEQSELL format, which is used when this format 270 may be more suitable for a requested operation. Currently, SEQSELL format 271 is used for MatMult, MatMultTranspose, MatMultAdd, MatMultTransposeAdd, 272 and MatSOR operations. 273 Because SEQAIJSELL is a subtype of SEQAIJ, the option "-mat_seqaij_type seqaijsell" can be used to make 274 sequential AIJ matrices default to being instances of MATSEQAIJSELL. 275 276 Collective 277 278 Input Parameters: 279 + comm - MPI communicator, set to PETSC_COMM_SELF 280 . m - number of rows 281 . n - number of columns 282 . nz - number of nonzeros per row (same for all rows) 283 - nnz - array containing the number of nonzeros in the various rows 284 (possibly different for each row) or NULL 285 286 Output Parameter: 287 . A - the matrix 288 289 Options Database Keys: 290 . -mat_aijsell_eager_shadow - Construct shadow matrix upon matrix assembly; default is to take a "lazy" approach, performing this step the first time the matrix is applied 291 292 Notes: 293 If nnz is given then nz is ignored 294 295 Level: intermediate 296 297 .seealso: MatCreate(), MatCreateMPIAIJSELL(), MatSetValues() 298 @*/ 299 PetscErrorCode MatCreateSeqAIJSELL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 300 { 301 PetscFunctionBegin; 302 PetscCall(MatCreate(comm,A)); 303 PetscCall(MatSetSizes(*A,m,n,m,n)); 304 PetscCall(MatSetType(*A,MATSEQAIJSELL)); 305 PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz)); 306 PetscFunctionReturn(0); 307 } 308 309 PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJSELL(Mat A) 310 { 311 PetscFunctionBegin; 312 PetscCall(MatSetType(A,MATSEQAIJ)); 313 PetscCall(MatConvert_SeqAIJ_SeqAIJSELL(A,MATSEQAIJSELL,MAT_INPLACE_MATRIX,&A)); 314 PetscFunctionReturn(0); 315 } 316